Back

Sample trade to place Buy/Sell Order on Forex With TakeProfit and StopLoss

Can someone please show me  how to place a trade to Buy/Sell  using Forex instrument With TakeProfit and StopLoss?

Example will be so much apprecated.

Update Backtest








I attached a simple example.
Basically that is how it works: you place a market order, a stop market order and a limit order together:

if not self.Portfolio.Invested:
price = data[self.pair].Close
onePercent = d.Decimal(1.01)
self.Buy(self.pair, 1000)
self.LimitOrder(self.pair, -1000, price * onePercent)
self.StopMarketOrder(self.pair, -1000, price / onePercent)

The market order will be filled immediately and the two other other will wait for their fills.
Once the stop loss order is filled, we cancel the take profit order and vice-versa:

if order.Status == OrderStatus.Filled:
if order.Type == OrderType.Limit or order.Type == OrderType.Limit:
self.Transactions.CancelOpenOrders(order.Symbol)
1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Thnak you so much for comming to my rescue.

Here is my modication of OnOrderEvent:

if order.Status == OrderStatus.Filled:
if order.Type == OrderType.Limit or order.Type == OrderType.StopMarket:
self.Transactions.CancelOpenOrders(order.Symbol)

Following is my Sell order:

if not self.Portfolio.Invested:
price = data[self.pair].Close
onePercent = d.Decimal(1.01)
self.Sell(self.pair, 1000)
self.LimitOrder(self.pair, 1000, price / onePercent)
self.StopMarketOrder(self.pair, 1000, price * onePercent)
3


Hey Ade

in the second part of your code, should you not have a"minus" sign in your order volume?

self.LimitOrder(self.pair, -1000, price / onePercent)

self.StopMarketOrder(self.pair, -1000, price * onePercent)

1

Hello Olivier

>> in the second part of your code, should you not have a"minus" sign in your order volume?

It has no significance, whether a "minus" or a "plus" sign.

If plus, an order will be opened or filled to go long(Buy) and if minus, an order will be opened or filled to short(Sell).

But, take a look at OnOrderEvent Code

if order.Status == OrderStatus.Filled:
if order.Type == OrderType.Limit or order.Type == OrderType.StopMarket:
self.Transactions.CancelOpenOrders(order.Symbol)

Note: Immediately, LimitOrder or StopMarketOrder is filled or opened, the code above attempt to cancel all opened orders including LimitOrder or StopMarketOder recently filled or opened.

Please, feel free to correct me if you have contrary opinion and please ask if you dont understand my explanation.

1

Thanks for the clarification Ade, that helps

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed