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.
QUANTCONNECT COMMUNITY
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.
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.
Alexandre Catarino
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)
Adetokunbo Ojo
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)
Sdoof
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)
Adetokunbo Ojo
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.
Sdoof
Thanks for the clarification Ade, that helps
Kiran Reddy
Hi @Ade i have these questions about this code:
1. What happens if the limit or stop market order doesn't execute, As the OnData event is triggered for every new data point will this try to create new limit or market order?Â
2. Did you get insufficient balance issue with limit or stop market order? how did you solve this?
Kiran Reddy
In my case the Trade is set in:Â
def OpeningBar(self):
for symbol in symbolsToLong:
      if len(symbolsToLong) > 0:
        self.SetHoldings(symbol, 1/len(symbolsToLong))
Jack Simonson
Hi Kiran,
In the case of Ade's code, if the order doesn't execute, then the algorithm will place another stop market order when OnData is triggered again since self.Portfolio.Invested will be false. If you attach your code or a backseat, this will help us provide you with a more comprehensive solution to the insufficient balance issue as well.
Adetokunbo Ojo
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!