Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Too many excecutions

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

    Too many excecutions

    Hi guys, I been trying to figure out how to prevent my strategy from over executing since the last 4 days, i stripped my code to almost nothing and i still can't see what i am doing wrong, please help.

    thanks for your time in advance

    note: this strategy is set to calculate on bar close to false - could that be causing this? the executions could be happening before the atmstrategyid & orderid are assigned a value.

    i do have a template named TEST STRATEGY

    attached code is that one that i am trying to debug


    Code:
    if(Close[0]>Open[0]
                    &&     orderId.Length == 0 && atmStrategyId.Length == 0)
                {
                        atmStrategyId = GetAtmStrategyUniqueId();
                        orderId = GetAtmStrategyUniqueId();
                        AtmStrategyCreate(OrderAction.Buy, OrderType.Limit, Close[0], 0, TimeInForce.Gtc, orderId, "TEST STRATEGY",atmStrategyId);
                        
                }
                if(Close[0]<Open[0]
                    && orderId.Length == 0 && atmStrategyId.Length == 0    )
                {
                        
                        atmStrategyId = GetAtmStrategyUniqueId();
                        orderId = GetAtmStrategyUniqueId();                        
                        AtmStrategyCreate(OrderAction.Sell, OrderType.Limit, Close[0], 0, TimeInForce.Gtc, orderId, "TEST STRATEGY",atmStrategyId);
                }
            
                    //ENDING ENTRY CONDITIONS
    Code:
     [SIZE=3]                 
                    // Check for a pending entry order
                if (orderId.Length > 0)
                {
                    string[] status = GetAtmStrategyEntryOrderStatus(orderId);
                    //only change order once an order is submitted and reset it when done
                    AtmStrategyChangeEntryOrder(Close[0], 0, orderId);                    
                    // 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 (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
                            orderId = string.Empty;
                            
                    }
                } 
                // If the strategy has terminated reset the strategy id
                //Don't change the else if - its needed!
                else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat
                    &&    orderId.Length == 0)
                    /*//before you empty the atmstrategyid, make sure the previous order is filled [empty order id from above] (gauranteed to fill eventually b/c of change entry order. Else, you will get multiple positions*/                
                {
                    atmStrategyId = string.Empty;
                
                }
                    //____________________________________________Trade Management
                if (atmStrategyId.Length > 0)
                    {            
                    // You can change the stop price
                        if (GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Long)
                        {
                    
                                AtmStrategyChangeStopTarget(0, GetAtmStrategyPositionAveragePrice(atmStrategyId) - ATR(100)[0]*0.333, "STOP1", atmStrategyId);
                                AtmStrategyChangeStopTarget(GetAtmStrategyPositionAveragePrice(atmStrategyId) + ATR(100)[0]*1, 0, "TARGET1", atmStrategyId);
            
                        }
            
                        if (GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Short)
                        {
                        
                            AtmStrategyChangeStopTarget(0, GetAtmStrategyPositionAveragePrice(atmStrategyId) + ATR(100)[0]*0.333, "STOP1", atmStrategyId);                    
                            AtmStrategyChangeStopTarget(GetAtmStrategyPositionAveragePrice(atmStrategyId) - ATR(100)[0]*1, 0, "TARGET1", atmStrategyId);
                            
                        }
                    }[/SIZE]
    Attached Files
    Last edited by dawdler; 06-08-2011, 11:13 AM.

    #2
    Hi dawdler,

    You'll want to use Print() statements in most of these blocks to follow code flow. A simple Print("such and such triggered " + DateTime.Now); should be enough for you to track what is happening and when. The executions you list come from both entries and stop loss orders.

    For debugging NinjaScript connected to an ATM strategy, the simulated data feed works great. With the trend slider you don't have to wait for the market to turn in order to test functionality.
    Last edited by NinjaTrader_RyanM1; 06-08-2011, 11:22 AM.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Ryan,
      i tried that yesterday and this is what i found out

      the atmstrategy + orderid are being assigned after another position(s) has already been placed so this "&& orderId.Length == 0 && atmStrategyId.Length == 0" filtering isn't preventing from multiple executions taking place. But once they are assigned (although late) - the strategy isn't executions anymore.

      i am going to try again what you said in your post, but if what i typed above points out something obviously wrong with the code to you, please let me know, thanks again
      Last edited by dawdler; 06-08-2011, 11:41 AM.

      Comment


        #4
        I took a closer look at your code snippet and looks like you are using the same variables names for the long and short side of this. Instead of doing this, it may work better to duplicate the same structure with unique names for long and short.

        Also, be sure to print the value of your target and stop order when changing it. They may be moving to levels you're not expecting.

        #region Variables
        private string atmStrategyId = string.Empty;
        private string orderId = string.Empty;
        private string atmStrategyIdShort = string.Empty;
        private string orderIdShort = string.Empty;
        #endregion
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          i changed the names of the variables like you said and when i saw the code run live, it places orders on top of each other - its executing orders rapidly within milliseconds (NT 7 is very fast) BEFORE the orderid & atmstrategyid values are assigned a uniqueid!

          please if possible: can you run the code on your pc on any 1 minute chart (attached) - you will see that problem within a couple minutes.

          the solution would probably be to stop NT from executing rapidly before the code assigns a unique id for atmstrategy and order id. But how?
          Attached Files
          Last edited by dawdler; 06-08-2011, 12:56 PM.

          Comment


            #6
            I ran the strategy but did not notice anything unexpected. The only time I see orders in quick succession is toggling long > short side on the same bar. To this end, you may want to take out the short condition until you verify one side works as you expect.

            There is no built in position management when using ATM, so you have to custom code for proper transitions.

            You also move targets and stop losses close to the market. If they fill then everything is reset again and will place a new opening order.
            Last edited by NinjaTrader_RyanM1; 06-08-2011, 01:45 PM.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              did you set the calculate on bar close to false. if yes, then i am not sure why i am having this issue and u didn't.

              "You also move targets and stop losses close to the market. If they fill then everything is reset again and will place a new opening order." - not sure what u mean by that - my stops & targets are within 1 ATR of the market.
              Last edited by dawdler; 06-08-2011, 02:05 PM.

              Comment


                #8
                Yes, I ran it with COBC = false. I see lots of executions, but they were all expected. I never saw it execute the same entry block in quick succession. The length check on orderId and AtmStrategyId should prevent this.

                It looks like you are following with the sample but may want to create again in case there was something small overlooked. Simplify and let us know if you're still seeing it submit orders when you're not expecting.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Ryan, i think there is flaw with ATM strategy method, please take this issue more seriously, I've tried everything to fix this and now i tried with NT sample atm strategy and got the same multiple executions.

                  this maybe the flaw:
                  this following condition is being triggered when there is a pending order being filled (when its supposed to be triggering after the close of a position), there seems be a lag between order being filled and atmstrategy id being assigned a value.

                  Code:
                  else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
                  please run the attached strategy on a faster time frame with CalcOnBarClose set to false and on actual data feed (running this strategy on simulated data feed did not give me any problems)
                  Attached Files

                  Comment


                    #10
                    problem: the else if condition to empty the atmstrategyid was being executed when the position was just filled and the market position was flat due to some lag, when its supposed to be executed after it was closed.

                    solution: to make it execute only when i had a position, so i added another condition to make sure it only executes when there was a position. that fixed it.

                    that was a simple fix, i wish i had seen it earlier. Thanks again Ryan for your time & suggestions.

                    Comment


                      #11
                      Thanks for the reply. I was able to see the issue. It did require an actual data connection and high volume conditions.

                      The main thing contributing here is this note from GetAtmStrategyMarketPosition

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

                      Glad to hear you found the block of code that was contributing and were able to find a coding solution. Putting more control there or on your entry conditions seems like a good approach. Thanks for the follow up.
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12
                        C# expert

                        Hi Ryan,

                        I am also having a problem with GetAtmStrategyMarketPosition (). It seems that it can indicate flat for more than 5 OnBarUpdates in a row after a 3 target short ATM strategy indicates the first order is Filled.

                        Is there a way to get the status of the ATM strategy directly? I need to wait for the ATM strategy to be done before making any more orders. It seems the market position will go flat even after the strategy is running.

                        James Donald

                        Comment


                          #13
                          Hi James,

                          Welcome to the NinjaTrader forums. We're not aware of issues with GetAtmStrategyMarketPosition () and works as expected in all our tests here. It should typically update the position on the next bar update after a fill.

                          Are you following closely the sample built into all installations? Click Tools > Edit NinjaScript > Strategy > Open SampleAtmStratategy. You can save a copy of this by right clicking in the editor > Save As > Provide a new name.
                          Last edited by NinjaTrader_RyanM1; 11-09-2011, 09:41 AM.
                          Ryan M.NinjaTrader Customer Service

                          Comment


                            #14
                            I have used the SampleAtmStratategy as an example and wait one barUpdate after seeing the order Filled to check GetAtmStrategyMarketPosition () . I have changed it to require GetAtmStrategyMarketPosition () for 20 barUpdates (MinFlatTime = 20 below) and it still fails every once every 10-15 trades.

                            Note that I never have a problem running simulation, only when using a real brokerage. I even set the simulated exchange delay to 5 seconds, and still no problem. Also note that this is a stop order with a 3 Target strategy with a custom stop.

                            Below is the code trimmed with only the important parts:

                            Code:
                            if (!mOrderPlacedInhibit) {
                            		atmStrategyId = GetAtmStrategyUniqueId();
                            		orderId = GetAtmStrategyUniqueId();
                            		bool result = AtmStrategyCreate (Cbi.OrderAction.SellShort, OrderType.Stop, 0 , orderPrice, TimeInForce.Day, orderId, strategyTemplName, atmStrategyId);
                            
                            		mOrderPlacedInhibit = true;
                            }
                            
                            if (orderId.Length > 0) {
                            	string[] status = GetAtmStrategyEntryOrderStatus(orderId);
                            	
                            	if (status.GetLength(0) > 0) {
                            		if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected") { // Order Done
                            			orderId = string.Empty;
                            			mFlatTimer = 0;
                            		}
                            
                            	}
                            
                            } else if (atmStrategyId.Length > 0) {  // Order length 0 - Order already Filled, Rejected or Canceled
                            	if (GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat) {
                            		if (++mFlatTimer > MinFlatTime) {
                            			atmStrategyId = string.Empty;
                            		}
                            	} else mFlatTimer = 0;
                            } else mOrderPlacedInhibit = false;
                            James

                            Comment


                              #15
                              To confirm: it only happens 1 in 15 times and only live trading? We could review your log /trace files for an incident and see what is reported from your broker at this time To get a complete picture here, can you please assist answering the following questions:

                              1) What is the nature of the reporting issue? Is it that you are flat but it reports you are in a position or the other way around?
                              2) What exact date / time was the entry order placed?
                              3) What exact date / time were closing orders placed? \
                              4) How long exactly in time is the position not correctly reported?
                              4) What point in this sequence does the reporting does not match?
                              a) Before entry
                              b) During trade
                              c) After position is closed.

                              Please answer these questions and send log / trace files to support 'at' ninjatrader 'dot' com. Include a link to this thread and put Attn: RyanM. Send files for the most recent incident. If occured today, files will be dated 20111109.


                              You will find the log file in the Documents > NinjaTrader 7 > Log folder.

                              * The log file will be named "log.YYYYMMDD.txt"

                              You will find the trace file in the Documents > NinjaTrader 7 > Trace folder.

                              * The trace file will be named "trace.YYYYMMDD.txt"
                              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
                              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