Hi,
I have placed a Buy/Sell market order on forex backtest, now I'm tring to close it. how can i do it ? is there a solution for this?
how can we close a market Buy/Sell order placed in forex?
QUANTCONNECT COMMUNITY
Hi,
I have placed a Buy/Sell market order on forex backtest, now I'm tring to close it. how can i do it ? is there a solution for this?
how can we close a market Buy/Sell order placed in forex?
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.
Derek Melchin
Hi Amir,
We can close the position by using either the MarketOrder or Liquidate methods. See the attached backtest for reference. If these solutions don't solve the issue, please attach a backtest so we can further assist.
Best,
Derek Melchin
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.
Amir Atashin
Hi Derek,
Thank you for your response.
Your solution for closing with the MarketOrder function is interesting (It works like a charm in the attached project). But, IMHO, it has some deficiency when trading in both live and backtesting with any Forex broker that I test (FXCM and Oando):
However, In comparison to some other Trading platforms that use specific functions for closing an order, Lean does this job differently. (In the above example, it sends a sell market order to close the first buy.)
Yet, I am looking for some other command like a close position, which gets an id of a market order and uses it to cancel or close the corresponding order. As far as I know, brokers and trading platforms support such a command, like NinjaTrader. Unfortunately, I did not find an equivalent function in the Lean for this purpose.
Â
As a simple example, consider another scenario in which I have two open buy orders with an equal amount but opened in the different time stamps. Now, I want to close the second one with the MarketOrder function. Is that possible in lean? Since if I call MarketOrder with the negative amount, it closes the first one, not the second.
I try to solve these matters by searching for something like a specific ID for each order (whether pending or not). Although I find a unique ID in the Lean API source code for each of them, these IDs were not equal to the broker’s ticket ID, which needed to close an order.
Thank you for your time.
Kind Regards,
Amir
Â
Â
Derek Melchin
Hi Amir,
If we want to close out specific market orders, one solution is to keep track of each order ticket. Since each order ticket saves the quantity, we just negative this quantity value to close the position.
See the attached backtest for reference.
Best,
Derek Melchin
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.
Amir Atashin
Hi Derek,
Thank you for your help and valuable suggestion so far.
After reading your solution, we figure out that currently, there is no way available in Lean API to do our need. The answer you suggest to us is attractive. However, we found out there are some problems with it:
Â
So, me and my friend decided to implement close_order function for the order_ticket inspiring the idea behind your response. Something like this:
I think close_order for each ticket is indispensable for the forex.self.order_ticket = self.MarketOrder(self.symbol, some_value) if some condition satisfied: self.order_ticket.close_order()
Â
Thank you again Derek,
Best regards,
Amir
Â
Amir Atashin
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!