Back

How do I close specific Oanda position?

Suppose I opened two EUR/USD long positions in Oanda. One with 10 lots and another 5 lots. How do I close the 5 lots position specifically? If I sell 5 lots market order, it might partially close the 10 lots instead.

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.


Hi Kun,

Every order that you create is receives an orderTicket. e.g.

var orderTicket = MarketOrder("IBM", 100);
var orderTicket2 = LimitOrder("IBM", 100, lastClose * .999m);
var orderTicket3 = StopMarketOrder("IBM", 100, stopPrice);

Each orderTicket has the properties of the order and an OrderId. e.g 

orderTicket.OrderId

Also whenever an order occurs, the following C# callback method is executed.

public override void OnOrderEvent(OrderEvent orderEvent) {
var order = Transactions.GetOrderById(orderEvent.OrderId);
Console.WriteLine("{0}: {1}: {2}", Time, order.Type, orderEvent);
}

In this method, you can see if your order was partially or fully filled by checking the status of the orderEvent by its OrderId.

Some additional methods to find open orders and close orders can be found below

// Get open orders
List openOrders = Transactions.GetOpenOrders();
// Cancel all open orders from SPY
List cancelledOrders = Transactions.CancelOpenOrders("SPY")
// Cancel order #10
OrderTicket cancelledOrder = Transactions.CancelOrder(10);

Additional documentation on creating and managing orders can be found here: https://www.quantconnect.com/docs/algorithm-reference/trading-and-orders. I hope this helps.

0

Jason,

Thanks for your response. I am not talking about open orders here. In my case, the 2 orders are fully filled. In Oanda, there will be 2 trades with a different ticket number. Is there the away to close out a specific trades with the ticket number?

0

If you know that they were filled you could do something like this: 

# Open position
orderTicket = self.MarketOrder(someSymbol, someQuantity)
...
# Close position
self.MarketOrder(orderTicket.Symbol, -orderTicket.Quantity)

 

0

Can't really answer your question in an unambiguous manner, but talking around it:

A) Not sure if relevant here, but there can be a FIFO requirement for osme customers: https://oanda.secure.force.com/AnswersSupport?urlName=OANDA-s-FIFO-policy&language=en_US

B) QC Lean API doesn't match Oanda, rather it is modelled after IB accounts. I am unsure whether there is support to doing anything to particular trades beyond cancellation. You can scale them down by issuing new orders in opposite direction.

In my case, I simply pretend Oanda is an IB account inside the algo, and if I would get an obscene number of open trades (didn't happen yet due to restarting my algos every few months) I would consolidate them manually at the next restart.

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