Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Martingail

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

    Martingail

    I am writing a Martingail entry system (NT 6.5).

    The first order is a market order, placed during OnBarUpdate().

    Next, within OnExecution(), I would like to do all of the following once I know my first entry order has been executed (and so know my weighted average entry price), but want to check with you first that I won’t be violating NT order handling rules by doing so:

    ·Submit Exit Orders (i.e. target and stop)

    ·Also submit additional Entry Orders (limit orders, with the price of each referenced to the weighted average price of the position already held) that will increase my position (and average down my weighted average price) if price moves away from the target after the first entry

    Can I do all this within the same OnExecution()? i.e. while I already hold a position, submit BOTH a) exit orders for the held position, AND b) further entry orders to average down if price moves against me?

    ... Or will I be violating NT order handling rules?

    Thanks.

    #2
    Hello Another Trader,

    Internal Order Handling rules can be found here.

    There shouldn't be a conflict as long as the enter orders are in the same direction that you are trading. You would run into the rules if they're used to open a position in the opposite direction.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Many thanks. I couldn't find that link.

      Comment


        #4
        Code:
        [SIZE=3][COLOR=black][FONT=Calibri]ifAs per my opening post, when the first order EnterShort() has been executed, within OnExecution() I first of all submit Exit Orders (i.e. target and stop), and then also submit additional Entry Orders to increase my position (and average down my weighted average price) should price move away from my target after the first entry. 
         
        Unfortunately, my additional entry orders are getting cancelled as soon as they are submitted (however, this does not happen for the LONG equivalent of the above entries!).
         
        I can’t figure out why ....
         
        Here is the TraceOrders info I get:
        
        1/25/2011 10:44:30 AM Entered internal PlaceOrder() method at 1/25/2011 10:44:30 AM: Action=SellShort OrderType=Market Quantity=100 LimitPrice=0 StopPrice=0 SignalName='Short Entry 0 instr0' FromEntrySignal='' 1/25/2011 10:44:30 AM Entered internal PlaceOrder() method at 1/25/2011 10:44:30 AM: Action=BuyToCover OrderType=Stop Quantity=100 LimitPrice=0 StopPrice=34.43 SignalName='Stop' FromEntrySignal='Short Entry 0 instr0' 1/25/2011 10:44:30 AM Entered internal PlaceOrder() method at 1/25/2011 10:44:30 AM: Action=BuyToCover OrderType=Limit Quantity=100 LimitPrice=34.03 StopPrice=0 SignalName='Limit' FromEntrySignal='Short Entry 0 instr0' 1/25/2011 10:44:30 AM Entered internal PlaceOrder() method at 1/25/2011 10:44:30 AM: Action=SellShort OrderType=Limit Quantity=100 LimitPrice=34.26 StopPrice=0 SignalName='Short Entry 1 instr0' FromEntrySignal='' 1/25/2011 10:44:30 AM Entered internal PlaceOrder() method at 1/25/2011 10:44:30 AM: Action=SellShort OrderType=Limit Quantity=100 LimitPrice=34.35 StopPrice=0 SignalName='Short Entry 2 instr0' FromEntrySignal='' Cancelled custom managed order at 1/25/2011 10:44:30 AM: Order='NT-00013/Back101' Name='Short Entry 1 instr0' State=PendingSubmit Instrument='ABC' Action=SellShort Limit price=34.2625 Stop price=0 Quantity=100 Strategy=‘MyStrategy’' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='30da7584cd964fcd87b5f3ae12f18e75' Gtd='12/1/2099 12:00:00 AM' Cancelled custom managed order at 1/25/2011 10:44:30 AM: Order='NT-00014/Back101' Name='Short Entry 2 instr0' State=PendingSubmit Instrument='ABC' Action=SellShort Limit price=34.345 Stop price=0 Quantity=100 Strategy=‘MyStrategy’' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='d7ec8e3dca054c51880de75c0717b5e7' Gtd='12/1/2099 12:00:00 AM'
        You can see that the five orders get submitted, but then the last two are cancelled immediately. Here is my code from within OnExecution():
        [/SIZE] [SIZE=3][COLOR=black][FONT=Calibri](entryOrderSHORT0 != null && entryOrderSHORT0.Token == execution.Order.Token) I have coded the strategy but have the following issue in backtest when shorting (but not when going long). [/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]if(execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)) [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]{[/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]if(Position.MarketPosition == MarketPosition.Short) [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]{[/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]// Submit target and stop orders [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]stopPrice = execution.Order.AvgFillPrice + (stop * 0.01); [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]targetPrice = execution.Order.AvgFillPrice - (target * 0.01); [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]stopOrderSHORT0 = ExitShortStop(instrument, true,execution.Order.Filled, StopPrice,"Stop","Short Entry 0 instr" + instrument); [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]targetOrderSHORT0 = ExitShortLimit(instrument, true,execution.Order.Filled, TargetPrice,"Limit","Short Entry 0 instr" + instrument); [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]// Submit further Martingail entry orders [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]entryOrderSHORT1 = EnterShortLimit(instrument, true, shares, myEntryPrice1,"Short Entry 1 instr" + instrument); [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]entryOrderSHORT2 = EnterShortLimit(instrument, true, shares, myEntryPrice2,"Short Entry 2 instr" + instrument); [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]}[/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]if(execution.Order.OrderState != OrderState.PartFilled) [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]entryOrderSHORT0 = [/FONT][/COLOR][/SIZE][SIZE=3][COLOR=black][FONT=Calibri]null; [/FONT][/COLOR][/SIZE] [SIZE=3][COLOR=black][FONT=Calibri]}[/FONT][/COLOR][/SIZE]
        Any idea why my orders get cancelled?

        BTW,
        A) the code for long is the same as the above (except it’s the “long version”, and I have checked this multiple times); strangely, the code for LONG works, while that for SHORT has the above issue.
        B) There is no additional info in Trace or Log relating to these cancelled orders
        [/COLOR][/FONT][/FONT][/COLOR]
        Last edited by AnotherTrader; 01-26-2011, 09:33 AM.

        Comment


          #5
          Thank you for the report. It should work the same way short or long. I would test this scenario using version 7, since version 6.5 will not receive further updates to the strategy engine. The only code change you should need to make is to compare the objects directly for equality and not the token.

          if (entryOrderSHORT0 != null && entryOrderSHORT0 == execution.Order)

          I will also give this scenario a run on 7 as well to see if i'm running into the same thing.
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_RyanM View Post
            ... I will also give this scenario a run on 7 as well to see if i'm running into the same thing...
            OK, please let me know how it goes with your NT 7 trial. Once I know you haven't had a problem, I'll try it out myself in NT 7.

            Comment


              #7
              I didn't run into any issues with this using NinjaTrader 7. All 4 orders remain active. I've attached the simple strategy used for testing this in real time.
              Attached Files
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_RyanM View Post
                ...The only code change you should need to make is to compare the objects directly for equality and not the token.

                if (entryOrderSHORT0 != null && entryOrderSHORT0 == execution.Order)

                ...
                Is the above one of the changes listed here ... http://www.ninjatrader.com/support/h...er_11_2010.pdf

                If so, can you point out which it is so that I can read around the issue a bit more? Thanks.

                Or is this related to the fact that IOrder objects are now unique outside of OnOrderUpdate()? i.e. it will still work as written previously, but alternatively can now be handled more elegantly?
                Last edited by AnotherTrader; 01-27-2011, 07:13 AM.

                Comment


                  #9
                  It's not documented in code breaking changes, because it's still likely to compile. This is documented in the IOrders section, but reference to the 6.5 technique is omitted for clarity.



                  Any example you find for version 7 will use this as well, such as this one:
                  The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    Just to close to matter...

                    Found the problem. I had an extra "}" in the original code that shouldn't have been there (and one that was was missing somewhere else).

                    All sorted now.

                    Thanks for the help!

                    Comment


                      #11
                      Thank you for following up here, AnotherTrader. Glad to hear you were able to identify cause of the issue.
                      Ryan M.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      647 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      369 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      108 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      572 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      573 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X