Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Scale in with iOrder objects using Advanced Order Handling

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

    Scale in with iOrder objects using Advanced Order Handling

    Greetings everybody,

    I am really struggling with the attached reversion-to-the-mean strategy/framework and I'm hoping that some of you can shed some light on the scenario...

    Note that the strategy (IorderFadeLegInTwice) is a simple and generic framework (not to mention buggy) so you will probably get killed if you trade it live as-is...

    The whole point of this exercise is actually to gain a better understanding of what commands to put where/how/when/why in the context of "Advanced Order Handling" rules using iOrder objects. Creating some sort of reference "master logic flow chart" for Advanced Order Handling rules would be a great idea as well.

    i.e. cancelling/resetting objects to null when/where to do these commands OnOrderUpdate(IOrder order) and OnExecution(IExecution execution) etc.

    As for the strategy, basically it fades price action as it spikes away from a user-defined SMA. Once the 1st limit order is filled, a second limit order is automatically placed at a user defined number # of pips away from the first entry as a "2nd leg" limit entry.

    If price moves to the other side of the SMA any unfilled/pending limit order is cancelled and the strategy waits for another price spike away from the mean (the user-defined SMA).

    The desired outcome of course is for price to retrace to the mean (the user-defined SMA), where our Target(s) (exit orders for one or both orders) reside. The target(s) updates each bar, following the SMA.

    I usually place the 1st and 2nd Stop orders 50-100 pips away as protection in a 'black swan' price surge, other than that the goal is to exit at the SMA every time (win or loss the SMA is the target/exit price level for 99% of all trades).

    - Try running a backtest on the Strategy (using the default settings) on a 1 or 5 minute ES contract with fill type set to liberal, entries per direction set to 2 and exit on close set to false. (Note that for this iteration of the program minBarsSinceLosingTradeClosed and violentSpi****TriggerMktOrder are not being used.)

    You will see that there are three issues:

    1. In trades where the 'second leg' gets filled, the second leg almost always exits BEFORE the first entry......(???) This makes no sense to me, especially since I have options/settings set to FIFO. The Target (Exit Limit) orders should both reside at the exact same price level and get filled pretty much simultaneously, especially if price action goes THRU the level where they exit orders are supposed to be sitting.

    2. The "Long/Short filters" module which is supposed to prevent sequential losing long/long/long or short/short/short orders, is not working. Supposed to work like this: If most recent trade was a Long and it was a "losing trade", then next trade MUST be a Short. (It worked perfectly in a previous iteration where there was only leg as opposed to the current two legged version).

    3. Other than the issues described above, the Strategy usually works "just fine" for a couple dozen trades and then I get the dreaded "Error on calling 'OnExecution' method for strategy" error. Presumably, the "null reset" is causing issues but I just can't figure out why or where else to put the Null Reset commands.

    As an aside, I have had a hard time understanding exactly how to implement "catch blocks" to debug this error using the sample at:
    (http://www.ninjatrader.com/support/f...ead.php?t=9825)

    However, even if I could get the catch blocks to work properly, it would not help me understand the flow of the logic when using Advanced Order Handling withing the context of a Managed strategy...

    I intend to try the Unmanaged Approach in the future but, for now i was hoping that there are other aspiring "Advanced Order Handling" level programmers who would be willing to chip in and resolve/explain/augment what I have posted here.

    I have put a LOT of work into this (my programming skills are actually quite weak - having gleaned what I could from the samples and reading the forum posts and watching videos)

    For the record, I think that this forum is actually quite amazing and I'm very excited to see what NT8 will bring to the table.

    Anyway, here's to all of us learning how to be better coders !
    Attached Files

    #2
    Hello Zigman,

    Thank you for your post.

    We will review your file further on our end and follow up when we have further details.

    Comment


      #3
      Hello Zigman,

      Thank you for your patience.

      You should not need to run Try Catch blocks in this strategy. Your logic is working without error.

      Your exits are occurring for the second entry first due to your conditions to exit "if (Position.MarketPosition == MarketPosition.Long && Position.Quantity == 2)" and then you assign the exit to the second signal name: targetOrderLong2 = ExitLongLimit(0, true, 1,SMA (SMALength)[0], "exit long limit entry 2", "long limit entry 2");

      This means if you are at 2 Long (or even 2 Short as that is just the reverse of the Long) you are going to exit the second entry first. This is due to your logic and not a matter of FIFO.

      For the third item, I would not expect this error base don the fact you check for null in OnOrderUpdate() and OnExecution(). I would recommend going through these lines of code and looking for any circumstance that would result in an incorrect call to a value that does not exist.

      Please let me know if you have any questions.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      633 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      364 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      567 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      568 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X