Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple Entries in ATM Strategy

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

    Multiple Entries in ATM Strategy

    Hi,
    Sorry to beat this dead horse but this ATM thing is killing me - LOL ! I keep getting multiple entries in the same direction and i cant figure out why. i'm using a variable for order ID and a variable for Strategy ID and using a test on the string lengths to make sure those are null strings with length 0 (this is from an example script on the forum). once there is an order filled the orderID string is not empty so I'm not exactly sure why i keep getting multiple entries here (btw, how do i insert code in the message)

    if ((orderIdL.Length == 0) && (atmStrategyIdL.Length == 0) && longSignal && GetAtmStrategyMarketPosition(atmStrategyIdS)<0)
    {
    // if short then exit and reverse
    if (atmStrategyIdS.Length>0)
    {
    AtmStrategyClose(atmStrategyIdS);
    orderIdS = string.Empty;
    atmStrategyIdS = string.Empty;
    }

    atmStrategyIdL = GetAtmStrategyUniqueId();
    orderIdL = GetAtmStrategyUniqueId();
    orderBarL = CurrentBar;
    AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market,0,0, TimeInForce.Day, orderIdL, "1BWTrades", atmStrategyIdL);
    } is the example of what I'm doing

    #2
    buylosellhi,

    Could you possible post more of your code here? I would help in seeing whats going on.

    I look forward to helping you resolve your issue.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your reply Adam. Could you pls show me how to post code 'inline' in the message to make it more readable...
      thanks in advance

      Comment


        #4
        buyloselhi,

        If you click "Go advanced" when you reply, there is an option to post a code section. It looks like the "#" key.

        Code:
        Example
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          awesome thanks. here is the trading part of the code

          Code:
          #region TradingPart
          			 // Make sure this strategy does not execute against historical data
                                  if (Historical) return;
          						
          						 
          										
          						 if ((orderIdL.Length == 0) && (atmStrategyIdL.Length == 0) && longSignal && GetAtmStrategyMarketPosition(atmStrategyIdS)<0)
                                      {
                                          // if short then exit and reverse
          								if (atmStrategyIdS.Length>0)
          								{
          									AtmStrategyClose(atmStrategyIdS);
          									orderIdS = string.Empty;
          									atmStrategyIdS = string.Empty;
          								}
          								
          								atmStrategyIdL = GetAtmStrategyUniqueId();
                                          orderIdL = GetAtmStrategyUniqueId();
                                          orderBarL = CurrentBar;
                                          AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market,0,0, TimeInForce.Day, orderIdL, "1BWTrades", atmStrategyIdL);	
              						}
                                  
          						if (orderIdS.Length == 0 && atmStrategyIdS.Length == 0 && shortSignal && GetAtmStrategyMarketPosition(atmStrategyIdL)>0)                               
                                  {
          							// if long then exit and reverse
          							if (atmStrategyIdL.Length>0)
          							{
          								AtmStrategyClose(atmStrategyIdL);
          								orderIdL = string.Empty;
          								atmStrategyIdL = string.Empty;
          							}
                                          atmStrategyIdS = GetAtmStrategyUniqueId();
                                          orderIdS = GetAtmStrategyUniqueId();
                                          orderBarS = CurrentBar;
                                          AtmStrategyCreate(Cbi.OrderAction.SellShort, OrderType.Market,0,0, TimeInForce.Day, orderIdS, "1BWTrades", atmStrategyIdS);                 
          						}
          						
          						// Information about strategy
          						if (atmStrategyIdL.Length > 0)
          						{
          							Print("Position = " + GetAtmStrategyMarketPosition(atmStrategyIdL) + " Quantity = " + Position.Quantity + " StrategyId = " + atmStrategyIdL + " + orderID = " + orderIdL);
          						}
          						
          						if (atmStrategyIdS.Length > 0)
          						{
          							Print("Position = " + GetAtmStrategyMarketPosition(atmStrategyIdS) + " Quantity = " + Position.Quantity + " StrategyId = " + atmStrategyIdS + " + orderID = " + orderIdS);
          						}
          						
          						if (atmStrategyIdL.Length == 0 && atmStrategyIdS.Length == 0)
          						{
          							Print("LongStrategyPosition = " + GetAtmStrategyMarketPosition(atmStrategyIdL) + "  ShortMarketPosition = " + GetAtmStrategyMarketPosition(atmStrategyIdS) + " and quantity = " + Position.Quantity);
          						}
          						
          						
          						#region if ATM Strategy is not null and Position is flat then reset everything
          						if (atmStrategyIdL.Length>0 && GetAtmStrategyMarketPosition(atmStrategyIdL)==Cbi.MarketPosition.Flat)
          						{
          							orderIdL = string.Empty;
          							atmStrategyIdL = string.Empty;
          							Print("Confirming successful close of Long strategyID = " + atmStrategyIdL + " and no order ID = " + orderIdL);
          						}
          						if (atmStrategyIdS.Length>0 && GetAtmStrategyMarketPosition(atmStrategyIdS)==Cbi.MarketPosition.Flat)
          						{
          							orderIdS = string.Empty;
          							atmStrategyIdS = string.Empty;
          							Print("Confirming successful close of Short strategyID = " + atmStrategyIdS + " and no order ID = " + orderIdS);
          						}
          						#endregion
          			#endregion

          Comment


            #6
            buylosellhi,

            Thanks for posting the code.

            How are you generating the longSignal?

            Could you describe to me how the multiple entries are occurring? Do you get a long and short ATM strategy at the same time, or are you getting multiple longEntries?
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Hi Adam, thanks for your reply. orders are generated in a multi-timeframe script and only long or short orders are generated for each bar of the base chart. and I'm getting multiple entries in the same direction. infact, when a signal in the other direction is generated then the entire (pyramided) postion from multiple entries is closed correctly and a new position in the other direction is entered, and then starts to pyramid in that (new) direction...

              which leads me to believe it might be an issue where I'm checking if Strategy string ID still exists but the postion is flat - not sure why that would happen but it always worries if I left out some checks etc so it was a failsafe. I'm checking for that using the following part of the code. looking forward to your thoughts.

              Code:
              [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] (atmStrategyIdL.Length>[/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] && GetAtmStrategyMarketPosition(atmStrategyIdL)==Cbi.MarketPosition.Flat)[/COLOR]
              {
              orderIdL = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].Empty;
              atmStrategyIdL = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].Empty;
              Print([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Confirming successful close of Long strategyID = "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + atmStrategyIdL + [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]" and no order ID = "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + orderIdL);
              }
              [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (atmStrategyIdS.Length>[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] && GetAtmStrategyMarketPosition(atmStrategyIdS)==Cbi.MarketPosition.Flat)
              {
              orderIdS = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].Empty;
              atmStrategyIdS = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].Empty;
              Print([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Confirming successful close of Short strategyID = "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + atmStrategyIdS + [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]" and no order ID = "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + orderIdS);
              }
              [/SIZE][/FONT][/SIZE][/FONT]

              Comment


                #8
                Hi Adam, so I ran the strategy again and saved the Output on first entry. Here is what I got

                Code:
                 
                 **NT** Submitting order with strategy 'TradeStrategy1BW/c520993d60a24a779d86fdd5cdd5b48d'
                Position = Flat Quantity = 0 StrategyId = c3c28d159ef64bc79687d15d52ed0185 + orderID = c5de585721fb4204984f0dcc9168ad72
                Confirming successful close of Short strategyID =  and no order ID =
                Couple of things - first of all, I realized I actually dont have the AtmStrategyClose(...) command in there
                and second, for some reason even when ATM strategy is created for short and a shortID is generated GetAtmStrategyMarketPosition(shortID) is giving a flat position, instead of a short position. Does it have to do with where exactly the order is when its submitted, pending, accepted, working, filled etc ? I'm thinking maybe the order is not filled fast enough and the code moves on to the next part where it still sees position as flat ?
                looking forward to your comments...

                Comment


                  #9
                  Hello buylosellhi,
                  Thanks for writing in and I am replying for Adam.

                  Is your strategy a multi-timeframe strategy? If so then it is possible that the orders are being submitted on both/all the bars array. Please filter your code with BarsInProgress to make sure that orders are submitted on your desired bar series only

                  Code:
                  If (BarsInProgress == 0)  //send order on primary bar only
                  {
                      //rest of the codes
                  }


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

                  Comment


                    #10
                    hi Joydeep, thanks for your reply. yes, I'm aware of peculiarities of BarsInProgress and I'm taking that into account before deciding to evaluate signals or entering orders in the strategy

                    Comment


                      #11
                      from everything i can see from the output its resetting the strategy id to null immediately after creating the strategy and entering the order. because somehow
                      Code:
                      	if (atmStrategyIdS.Length>0 && GetAtmStrategyMarketPosition(atmStrategyIdS)==Cbi.MarketPosition.Flat)
                      evaluates to true and because strategyID is not null so we know somehow the GetAtmStrategyMarketPosition(...) is not showing it as Short

                      Comment


                        #12
                        Joydeep,
                        just to make myself clear - i'm using 3 additional timeseries within the script and because and I'm entering trades on the last BarsInProgress because by that time whatever updates needed to happen on other series are all complete. and because i'm adding bigger timeseries successively to the base series I've set the last BarsArray to be the same size as the first one - to kinda cheat and work around the sequence of BarsArray calculations.
                        Unless you tell me otherwise I thought this was the best way to do it..

                        Comment


                          #13
                          Hello buylosellhi,
                          In an Atm Strategy the position will be updated atleast after one tick.

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


                          This may affect your strategy. As such you may consider to return after submitting the entry orders so that it does not nullify the strings.

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

                          Comment


                            #14
                            Joydeep, thanks for pointing this out. I guess I missed the obvious !!
                            I think this should work

                            Comment


                              #15
                              Hello buylosellhi,
                              Thanks for your note.
                              Please let me know if I can assist you any further.
                              JoydeepNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              670 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              379 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              111 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              582 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X