Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ExitLong order triggering twice from OnMarketData

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

    ExitLong order triggering twice from OnMarketData

    Hi,
    I have a simple strategy that has ExitLong and ExitShort orders with connection to real account in interactive brockers.
    Sometimes (approximately once per every 5th trade), it exits twice and also opens an additional order in IB.
    Orders are triggered from OnMarketData() method and calculated by ticks, but it works with OnCandleClose.
    Example: Opening Long position with 5 contracts and exiting with 5 contracts, and opening an additional Sell 5 contracts order showing on chart 2 ExitLong orders.
    No messages in the Log.
    Is that known issue and how I can ensure that no additional positions opened after exiting positions?
    Last edited by romanfar; 10-01-2024, 02:22 PM.

    #2
    Hello romanfar,

    It is possible to see that if your exit condition remains true for multiple ticks. The position will not be updated that fast so that could allow for multiple exits. The easiest way to fix that would be to use a bool variable. Create a bool variable with a default of false. In the set with the exit check if the bool variable is true as part of the exit condition and set it back to false as part of the action. In your entry condition you can set it to true to enable the exit condition. The purpose of doing this to have the exit condition become true only 1 time total.

    Comment


      #3
      Thank you Jesse,

      So as I undestood, added flag that checks if position exists:
      Code:
      bool isOrderOpened=false;
      ​
      if (Position.MarketPosition == MarketPosition.Long&& isOrderOpened)
      
      {
      ​ ExitLong("TakeProfit", "LongEntry");
      isOrderOpened = false;
      }
      
      and when opening order:
      EnterLong(NumContracts, "LongEntry");
      isOrderOpened = true;
      ​
      ​

      Correct?

      Comment


        #4
        Hello romanfar,

        That may still allow that situation, the position wont get updated that quickly. Using a regular bool variable could be used to make sure it happens only one time.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        56 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        132 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        73 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        49 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X