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.
Don't have an account? Join QuantConnect Today
QuantConnect Community Discussions
QUANTCONNECT COMMUNITY
LEAN is the open-source algorithmic trading engine powering QuantConnect. Founded in 2012 LEAN has been built by a global community of 180+ engineers and powers more than 300+ hedge funds today.
Join QuantConnect's Discord server for real-time support, where a vibrant community of traders and developers awaits to help you with any of your QuantConnect needs.
The Open-Quant League is a quarterly competition between universities and investment clubs for the best-performing strategy. The previous quarter's code is open-sourced, and competitors must adapt to survive.
Request for code example to place Forex trade with TakeProfit and StopLoss.
Continue ReadingRefer to our Research Guidelines for high quality research posts.
Create an account on QuantConnect for the latest community delivered to your inbox.
Sign Up Today
|
|
|||||||
|
|
||||||||
|
Sample trade to place Buy/Sell Order on Forex With TakeProfit and StopLoss
Adetokunbo Ojo | October 2017
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â„¢ 2025. All Rights Reserved
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)
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.
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)
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.
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)
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.
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.
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.
Sdoof
Thanks for the clarification Ade, that helps
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.
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?
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.
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))
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.
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.
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!