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 Mindset, 04-21-2026, 06:46 AM
        0 responses
        87 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        128 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        65 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by cmoran13, 04-16-2026, 01:02 PM
        0 responses
        117 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        67 views
        0 likes
        Last Post PaulMohn  
        Working...
        X