Katerina

Why I enabled Position Netting but PL still floating?

Programming


I have a question regarding position netting. From the Tech Doc description here, it mentioned that PL will be realized when the net position becomes zero.


In backtest setting, I already enabled Position Netting.

Then I opened 2 opposite trades with the same trading size, but the PL is still floating.

Here my simple script testing on XAUUSD on 2024.01


  1. from AlgoAPI import AlgoAPIUtil, AlgoAPI_Backtest
  2. from datetime import datetime, timedelta
  3. class AlgoEvent:
  4. def __init__(self):
  5. self.cnt = 0
  6. def start(self, mEvt):
  7. self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self, mEvt)
  8. self.evt.start()
  9. def on_marketdatafeed(self, md, ab):
  10. self.cnt+=1
  11. if self.cnt==1:
  12. order = AlgoAPIUtil.OrderObject(
  13. instrument=md.instrument,
  14. volume=1,
  15. openclose='open',
  16. buysell=1, #buy order
  17. ordertype=0,
  18. takeProfitLevel=md.askPrice*1.2,
  19. stopLossLevel=md.bidPrice*0.9
  20. )
  21. self.evt.sendOrder(order)
  22. elif self.cnt==2:
  23. order = AlgoAPIUtil.OrderObject(
  24. instrument=md.instrument,
  25. volume=1,
  26. openclose='open',
  27. buysell=-1, # opposite sell order
  28. ordertype=0
  29. )
  30. self.evt.sendOrder(order)

Anything I did wrong? Or do I misunderstand the description?

 
tony lam
From your script, the first open order has take TP/SL conditions. It refers to conditional orders. The system will separately monitor the close order conditions rather than just offsetting with another opposite order. 

To summarize, the system behavior under position netting setup: 
  • For non-conditional orders (i.e. order without specifying take profit, stop loss, holding period, trailing stop, etc), the position will offset based on FIFO approach. 
  • For conditional orders, no offsetting will be applied. Instead, the system will monitor your specified conditions to close that order.