Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy & Account Position Not in Sync

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Strategy & Account Position Not in Sync

    I am trying to have a strategy close any open account positions if they exist so only positions initiated and managed by the strategy are executed.

    I am using the following code:

    bool _closePosition = false;

    // if there are active positions they must be part of current trade object

    if (PositionAccount.MarketPosition != MarketPosition.Flat)
    {

    // check to see if current trade object has active trade
    _closePosition = CurrentTrade.TradeStatus == tradeStatusState.None;

    // close all positions if necessary
    if (_closePosition)
    {
    int _qty = PositionAccount.Quantity;

    if (PositionAccount.MarketPosition == MarketPosition.Long)
    {

    // execute opposite order
    EnterShort(_qty); // <--- this is closing the unwanted position


    ExitShort(); // <---- i was hoping that this would set the strategy marketposition to flat

    }
    }
    }

    When testing I added a Buy Market using the chart trader and the strategy quickly closed the order by placing a Sell Market via the strategy (using code above). The account market position becomes flat (like it should) but the strategy market position is now short.

    I tried adding an ExitShort() command thinking that it would reset the strategy so the account and strategy market positions were both in sync.

    I can't use the flatten command because it shuts down the strategy. Is there another way to do this without using an unmanaged approach?



    #2
    Hello kenconcannon,

    In the described use case the strategy is not aware of the account position as part of its virtual position so the described scenario is accurate. The strategy is short based on its knowledge, calling EnterShort will enter a position and immediately calling ExitShort will not do anything because the position wont be updated. The strategy would need to wait for that position to be updated and then you could call ExitShort.

    To have the Sync column show true in the control center you would need to start the strategy in the same position as the account or close the position on the account before starting it. For the account and strategy position to be in sync you would need to either close the position first before starting the strategy or have strategy logic which equates that position so submits an identical order to enter into that position. The strategy start behavior would be used in that case to resume the account position or wait depending on the selected behavior.




    Comment


      #3
      I understand it is working as designed.

      What I am trying to do is that if the strategy detects an account position (one that was not initially placed by the strategy but placed manually by some other mechanism) it will close it the open account position and continue that strategy.

      Is there a way to do this and set the strategy market position to flat?

      Comment


        #4
        If I wanted to equate the position by submitting an identical order do you have a code example and where you would add this logic?

        Comment


          #5
          Hello kenconcannon,

          There is not a specific use case for that, you could potentially try using the addon framework account class to submit a order to close the position so that the strategy is not using its own orders/position.




          Using a strategy based order will still work to close that position however you would need to then keep track of the account position directly and avoid using the strategies position for anything. PositionAccount could be used in the strategies logic and then you would have to watch the account position in the control center for an accurate result. That could also affect the performance report order pairing.

          The easiest solution here would be to have the strategy pause and notify the user that there is an order open and to close the order then re enable the strategy.



          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          71 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          143 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          76 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          47 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          51 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X