Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to close active ATM strategy and pending orders?

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

    How to close active ATM strategy and pending orders?

    I want to test a strategy which calls ATM strategy and reverses it on opposite rules. However, the ATM strategy and pending orders once opened does not close on opposite signal.
    I have a code as follows.
    Code:
     
    [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]protected[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]override[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] OnBarUpdate()[/FONT]
    [FONT=Microsoft Sans Serif]{[/FONT]
    [FONT=Microsoft Sans Serif][COLOR=#008000][FONT=Microsoft Sans Serif][COLOR=#008000]// Make sure this strategy does not execute against historical data[/COLOR][/FONT]
    [/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (Historical)[/FONT]
    [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]return[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif];[/FONT]
     
    [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (orderId.Length == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT]
    [FONT=Microsoft Sans Serif]&& atmStrategyId.Length == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT]
    [/COLOR][/FONT][FONT=Microsoft Sans Serif]&& FirstTickOfBar[/FONT]
    [FONT=Microsoft Sans Serif]&& CrossAbove(MAMA cross[/FONT][FONT=Microsoft Sans Serif])[/FONT]
    [FONT=Microsoft Sans Serif])[/FONT]
    [FONT=Microsoft Sans Serif]{[/FONT]
    [FONT=Microsoft Sans Serif]atmStrategyId = GetAtmStrategyUniqueId();[/FONT]
    [FONT=Microsoft Sans Serif]orderId = GetAtmStrategyUniqueId();[/FONT]
    [FONT=Microsoft Sans Serif]AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], TimeInForce.Day, orderId, [/FONT][FONT=Microsoft Sans Serif][COLOR=#800000][FONT=Microsoft Sans Serif][COLOR=#800000]"AtmStrategyTemplate"[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], atmStrategyId);[/FONT] 
    [FONT=Microsoft Sans Serif]}[/FONT]
     
    [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (orderId.Length == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT]
    [FONT=Microsoft Sans Serif]&& atmStrategyId.Length == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT]
    [FONT=Microsoft Sans Serif]&& FirstTickOfBar[/FONT]
    [FONT=Microsoft Sans Serif]&& CrossBelow(MAMA cross[/FONT][FONT=Microsoft Sans Serif]) )[/FONT]
    [FONT=Microsoft Sans Serif]{[/FONT]
    [FONT=Microsoft Sans Serif]atmStrategyId = GetAtmStrategyUniqueId();[/FONT]
    [FONT=Microsoft Sans Serif]orderId = GetAtmStrategyUniqueId();[/FONT]
    [FONT=Microsoft Sans Serif]AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], TimeInForce.Day, orderId, [/FONT][FONT=Microsoft Sans Serif][COLOR=#800000][FONT=Microsoft Sans Serif][COLOR=#800000]"AtmStrategyTemplate"[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], atmStrategyId);[/FONT]
    [FONT=Microsoft Sans Serif]}[/FONT]
     
    [FONT=Microsoft Sans Serif][COLOR=#008000][FONT=Microsoft Sans Serif][COLOR=#008000]// Check for a pending entry order[/COLOR][/FONT]
    [/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (orderId.Length > [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif])[/FONT]
    [FONT=Microsoft Sans Serif]{[/FONT]
     
    [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]string[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif][] status = GetAtmStrategyEntryOrderStatus(orderId);[/FONT]
     
    [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (status.GetLength([/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]) > [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]) AtmStrategyCancelEntryOrder(atmStrategyId);[/FONT]
     
    [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (status[[/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]2[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]] == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800000][FONT=Microsoft Sans Serif][COLOR=#800000]"Filled"[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] || status[[/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]2[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]] == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800000][FONT=Microsoft Sans Serif][COLOR=#800000]"Cancelled"[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] || status[[/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]2[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]] == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800000][FONT=Microsoft Sans Serif][COLOR=#800000]"Rejected"[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif])[/FONT]
    [FONT=Microsoft Sans Serif]{[/FONT]
    [FONT=Microsoft Sans Serif]orderId = [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]string[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif].Empty;[/FONT]
    [FONT=Microsoft Sans Serif]}[/FONT]
    [FONT=Microsoft Sans Serif]}[/FONT]
    [FONT=Microsoft Sans Serif][COLOR=#008000][FONT=Microsoft Sans Serif][COLOR=#008000]// If the strategy has terminated reset the strategy id[/COLOR][/FONT]
    [/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT]
    [FONT=Microsoft Sans Serif](atmStrategyId.Length > [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] &&[/FONT]
    [FONT=Microsoft Sans Serif]GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat[/FONT]
    [FONT=Microsoft Sans Serif])[/FONT]
    [FONT=Microsoft Sans Serif]{[/FONT][FONT=Microsoft Sans Serif]atmStrategyId = [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]string[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif].Empty;[/FONT][FONT=Microsoft Sans Serif]}[/FONT]
    [FONT=Microsoft Sans Serif]}[/FONT]
    [FONT=Microsoft Sans Serif][COLOR=#008000][FONT=Microsoft Sans Serif][COLOR=#008000]// Close ATM strategy and pending orders[/COLOR][/FONT]
    [/COLOR][/FONT]
    [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] ( atmStrategyId.Length > [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] &&[/FONT]
    [FONT=Microsoft Sans Serif]GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Long &&[/FONT]
    [FONT=Microsoft Sans Serif]CrossBelow(MAMA cross[/FONT][FONT=Microsoft Sans Serif])[/FONT]
    [FONT=Microsoft Sans Serif]{[/FONT]
    [FONT=Microsoft Sans Serif]AtmStrategyClose(atmStrategyId);[/FONT]
    [FONT=Microsoft Sans Serif]atmStrategyId = [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]string[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif].Empty;[/FONT]
    [FONT=Microsoft Sans Serif]}[/FONT]
    [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] ( atmStrategyId.Length > [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] &&[/FONT]
    [FONT=Microsoft Sans Serif]GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Short&&[/FONT]
    [FONT=Microsoft Sans Serif]CrossAbove(MAMA cross)[/FONT]
    [FONT=Microsoft Sans Serif]{[/FONT]
    [FONT=Microsoft Sans Serif]AtmStrategyClose(atmStrategyId);[/FONT]
    [FONT=Microsoft Sans Serif]atmStrategyId = [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]string[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif].Empty;[/FONT]
    [FONT=Microsoft Sans Serif]}[/FONT]
    [FONT=Microsoft Sans Serif]}[/FONT]

    #2
    woodies,

    You would need to close the ATM strategy before entering into the opposite direction here. It looks like you are closing the strategies below, however I would suggest doing it before you enter into a reverse order.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for suggestion.

      Do you mean something like this?

      if( Market Position==MarketPosition.Long && CrossBelow(MAcross)&& atmStrategyId.Length >0)
      {
      AtmStrategyClose(atmStrategyId);
      atmStrategyId= string.Empty;
      orderId=string.Empty
      if (orderId.Length == 0
      && atmStrategyId.Length == 0
      )
      {
      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();
      AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
      }


      }

      Followed by cancelling pending entry order if it is filles, cancelled or rejected and resetting Id?
      I will try this and see if it works.

      Comment


        #4
        Yes, this should be what you seek here to reverse with the ATM's.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          The code does not cancel out the pending stop and target orders on reversal and the ATM opens a new order in opposite direction. Can you suggest an alternate way?
          Thanks.

          Comment


            #6
            Hello woodies,
            You should close the existing position and the enter a new position at the same time. A sample code will be

            Code:
            if( Market Position==MarketPosition.Long && CrossBelow(MAcross)&& atmStrategyId.Length >0)
            {
            
            //exit current position
            AtmStrategyClose(atmStrategyId);
            
            //sell at market
            atmStrategyId = GetAtmStrategyUniqueId();
            orderId = GetAtmStrategyUniqueId();
            AtmStrategyCreate(Cbi.OrderAction.[B]Sell[/B], OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId); 
            }
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Thanks a lot for the support.

              However, the strategy still does not take a trade.
              Can you please check the error again?
              Attached Files

              Comment


                #8
                Hello woodies,
                Thanks for the code.

                In you code you never made the initial entry (i.e. when market position is flat).
                Please append the following code and see if you can get the correct entries.


                Code:
                if (atmStrategyId.Length == 0 && CrossAbove(SMA(Close,20),SMA(Close,100),1))
                {
                	atmStrategyId = GetAtmStrategyUniqueId();
                	 orderId = GetAtmStrategyUniqueId(); 
                	 AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);	
                }
                else if (atmStrategyId.Length == 0 && CrossBelow(SMA(Close, 20), SMA(Close, 100), 1))
                {
                	atmStrategyId = GetAtmStrategyUniqueId();
                	 orderId = GetAtmStrategyUniqueId();
                     AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
                }
                Please let me know if I can assist you any further.
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Hello Joydeep,

                  I have modified the code as per your advise and it takes the trades on both sides.
                  However, when it reverses, it does not close the existing stop and profit target orders.
                  So on reversal, there are two orders in opposite direction.
                  Can you please check it? Do we need to use AtmStrategy ChangeStopTarget() method to close the existing orders?
                  Thanks for help.
                  Attached Files

                  Comment


                    #10
                    Hello woodies,
                    Can you please try this code.

                    In this context let me say that the positions of the Atm straegy are updated atleast a tick after the order gets filled.

                    NOTE: Changes to positions will not be reflected till at least the next OnBarUpdate() event after an order fill.


                    This is conflicting with your code
                    Code:
                    else  if 
                    (atmStrategyId.Length > 0 &&
                    GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat
                    )
                    {
                    	atmStrategyId = string.Empty;
                    	orderId= string.Empty;
                    }
                    If I remove it I can get the strategy working.
                    Attached Files
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      Great!!! That finally works as expected.
                      This could be the only example on the forum of ATM strategy use in a main strategy.

                      I have seen problems with overfill when dealing with managed approach and unmanaged approach requires a lot of order handling. And ATM strategy would offer a simpler solution to his.

                      Thanks a lot for all the help and support to everybody.

                      Comment


                        #12
                        Hello woodies,
                        Thanks for your note.

                        Please let me know if I can assist you any further.
                        JoydeepNinjaTrader Customer Service

                        Comment


                          #13
                          Problem with realized PL

                          Hello,

                          I am trying to get relalized PL from ATM strat example shown here but it does not update properly.
                          Can you check the code and show me the error?

                          Code:
                           // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
                                                if (status.GetLength(0) > 0)
                                                      {
                                                           
                                                            // If the order state is terminal, reset the order id value
                                                            if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
                                                            {
                                                              orderId = string.Empty;
                                                            }
                                          
                                                      }
                                              }
                                           
                                              else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId)==MarketPosition.Flat)
                                                  {
                                                      if(GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
                                                         { realizedProfitLoss += Instrument.MasterInstrument.Round2TickSize(GetAtmStrategyRealizedProfitLoss(atmStrategyId));
                                                            atmStrategyId = string.Empty;
                                                            unrealizedProfitLoss = 0;
                                                          }
                                                  }
                                                  if(atmStrategyId.Length > 0)
                                                  { 
                                                      unrealizedProfitLoss=Instrument.MasterInstrument.Round2TickSize(GetAtmStrategyUnrealizedProfitLoss(atmStrategyId));
                                                  }
                          I am attaching the .cs file here for reference.
                          Thanks.
                          Attached Files

                          Comment


                            #14
                            Hello woodies,
                            You are always in a position as the first 2 block of codes will never let you in a flat position. Thus the last block of code (where you calculate the PnL) never gets updated.
                            JoydeepNinjaTrader Customer Service

                            Comment


                              #15
                              Thanks for reply.
                              Is there any workaround this problem so that we can get the ATM PL while in a long/shot psotion?
                              Can we use PL methods we use for managed approach like Performance.AllTrades.TradesPerformance.Currency.C umProfit
                              for ATM based strategy?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by techgetgame, Yesterday, 11:42 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post techgetgame  
                              Started by sephichapdson, Yesterday, 11:36 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post sephichapdson  
                              Started by bortz, 11-06-2023, 08:04 AM
                              47 responses
                              1,613 views
                              0 likes
                              Last Post aligator  
                              Started by jaybedreamin, Yesterday, 05:56 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              20 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Working...
                              X