Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy stops working after last trade exit

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

    Strategy stops working after last trade exit

    I have noticed on at least 2 or 3 occassions when trading FXCM with NT that after an autotrade with ATM finishes a trade the strategy stops triggering any new trades. I have no way to troubleshoot this. I get no error messages in my log. it just stops working. It passes up perfectly good trades that are perfectly matched to the conditions in the code.

    Has anyone experienced this or have any suggestions for why a strategy would only intermittently stop working?

    Could it have something to do with FXCM data feed?
    Is there any known bugs in NT that might cause this?
    Attached Files
    Last edited by ShruggedAtlas; 06-15-2015, 07:51 AM.

    #2
    Hello Shruggedatlas,

    Thanks for your post.

    I recommend that you add print statements to verify that where your entry orders are (intended to be) placed is being reached in your code. Use print statements that include the time as well as any relevant values being evaluated. If you find that the print statements are not being reached in your code then move them just above the entry condition evaluation and again use them to output the time and the various conditions that are being evaluated. The print sends its output to the Tools>output window.

    Here is a good reference to the debugging process: http://www.ninjatrader.com/support/f...ead.php?t=3418
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Based on my print statements I can see that none of my strategy coded exit conditions are ever followed. Once a trade is begun, the ATM takes over and at the conclusion of the ATM either when stopped out or when targets are taken the strategy doesn't appear to "reset". The entry conditions seem to be in good shape but is there a better procedure for exit conditions when using ATM? Here are my exit conditions/code:
      Code:
                      if (Position.MarketPosition == MarketPosition.Long)
                      {
                          Print("Currently in a Long position!");
                          if(CUMRSI(BarsArray[2],rPeriod,3)[0] > upper)
                              {
                                  Print("CLOSING LONG POSITION");
                                  AtmStrategyClose("QUICKTEST");
                              }
                      }
      
                      // Short Exit Condition
                      if (Position.MarketPosition == MarketPosition.Short)
                      {
                          Print("Currently in a Short postion");
                          if(CUMRSI(BarsArray[2],rPeriod,3)[0] < lower)
                              {
                                  Print("CLOSING SHORT POSITION");
                                  AtmStrategyClose("QUICKTEST");
                              }
                      }
      ???
      Should I be using:
      if (GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Long)

      instead of:
      if (Position.MarketPosition == MarketPosition.Short)

      note: I get an error in my output window as follows:
      GetAtmStrategyMarkePosition() method error: missing atmStrategyId parameter when using the GetAtmStrategyMarketPosition method and not in a trade. I'm assuming that error will go away once an ATM is in play?
      Last edited by ShruggedAtlas; 06-15-2015, 12:12 PM.

      Comment


        #4
        Hello ShruggedAtlas,

        Thanks for your reply.

        It looks like you are using a signal name "QUICKTEST" instead of a strategy ID. Please review the code and comments in SampleATMStrategy on your NinjaTrader for examples of obtaining and using the uniqueID needed for coded ATM strategies. Here is another example you can reference: http://www.ninjatrader.com/support/f...ad.php?t=19148

        Here is the helpguide section that relates: http://www.ninjatrader.com/support/h...gy_methods.htm
        and
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Why can't I just get rid of:
          atmStrategyId = GetAtmStrategyUniqueId();

          QUICKTEST is my strategyId

          I am choosing to use a predefined ATM Strategy called "QUICKTEST" When the strategy kicks in it correctly applies QUICKTEST.

          The real issue is that while QUICKTEST was active, the exit conditions were not being seen. I think it was because i wasn't using the correct code to detect ATMStrategyPosition. as in:
          if (GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Long)

          My current error I'm getting is Missing atmStrategyId parameter. i think that is because it is now seeing the exit conditions now that I fixed the exit condition get atm position code. now the problem is that it's testing for exits even when it's not in a position. thus, the error.

          my revised code is as follows:

          Code:
                          // Long Conditions
                          if (orderId.Length == 0 && atmStrategyId.Length == 0)
                          {
                                 if(Rising(CUMRSI(BarsArray[1],periodRsi,3)))
                              {
                                  Print("Rising 172");
                                  Print("current CUMRSI(BarsArray[1] is: " + CUMRSI(BarsArray[1],periodRsi,3)[0]);
                                  
                                  if(CUMRSI(BarsArray[2],periodRsi,3)[1] < lower)
                                  {
                                      Print("10 Min RSI below 30");
                                      Print("CUMRSI(BarsArray[2]) is: " + CUMRSI(BarsArray[2],periodRsi,3)[0]);
                                      
                                      if(CUMRSI(BarsArray[0],rPeriod,3)[0] < lower)
                                      {
                                          Print("1 Min RSI below 30");
             
                                          atmStrategyId = "QUICKTEST";
                                          orderId = GetAtmStrategyUniqueId();
                              
                                          AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0,
                                          TimeInForce.Day, orderId, atmStrategyId, atmStrategyId);
                                      }
                                  }
                              }
                          }
          
                          // Long Exit Condition
                          Print("test");
                          if (GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Long)
                          {
                              Print("Currently in a Long position!");
                              if(CUMRSI(BarsArray[2],rPeriod,3)[0] > upper)
                                  {
                                      Print("CLOSING LONG POSITION");
                                      AtmStrategyClose(atmStrategyId);
                                  }
                          }
          Last edited by ShruggedAtlas; 06-15-2015, 01:29 PM.

          Comment


            #6
            Hello ShruggedAtlas,

            Thanks for your reply.

            You need to use atmStrategyId = GetAtmStrategyUniqueId(); so that you are getting a unique ATM strategyID each time you use the ATM template "QUICKTEST". The ID must be unique each time the ATM strategy it is used, if you use the same ID it will not work after the first time and this is what you were reporting.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              I'm sorry I'm not entirely convinced that I need to use the get unique Id method since I'm always using the same atm called QUICKTEST. I have already tested using quicktest as the uniqueid and it has been working. The only problem I have now is I get a Missing atmStrategyId parameter error.

              IMPORTANT NOTE: I followed your suggestion even still and it has apparently made no difference. however, I now have no idea how to specify that I want to use my QUICKTEST atm strategy AND the error regarding a missing atmStrategyId parameter is still showing up.

              The exact error in the output window is as follows:
              **NT** GetAtmStrategyMarketPosition() method error: Missing atmStrategyId parameter

              it appears to be occurring because of this statement:
              if (GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Long)

              revised code following your suggestion is as follows:

              Code:
                              if (orderId.Length == 0 && atmStrategyId.Length == 0)
                              {
                                     if(Rising(CUMRSI(BarsArray[1],periodRsi,3)))
                                  {
                                      Print("Rising 172");
                                      Print("current CUMRSI(BarsArray[1] is: " + CUMRSI(BarsArray[1],periodRsi,3)[0]);
                                      
                                      if(CUMRSI(BarsArray[2],periodRsi,3)[1] < lower)
                                      {
                                          Print("10 Min RSI below 30");
                                          Print("CUMRSI(BarsArray[2]) is: " + CUMRSI(BarsArray[2],periodRsi,3)[0]);
                                          
                                          if(CUMRSI(BarsArray[0],rPeriod,3)[0] < lower)
                                          {
                                              Print("1 Min RSI below 30");
                 
                                              atmStrategyId = GetAtmStrategyUniqueId();
                                              orderId = GetAtmStrategyUniqueId();
                                  
                                              AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0,
                                              TimeInForce.Day, orderId, atmStrategyId, atmStrategyId);
                                          }
                                      }
                                  }
                              }
              
                              // Long Exit Condition
                              if (GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Long)
                              {
                                  Print("Currently in a Long position!");
                                  if(CUMRSI(BarsArray[2],periodRsi,3)[0] > upper)
                                      {
                                          Print("CLOSING LONG POSITION");
                                          AtmStrategyClose(atmStrategyId);
                                      }
                              }
              Last edited by ShruggedAtlas; 06-16-2015, 07:31 AM.

              Comment


                #8
                Ok here is a slight revision. I see where I need to specify my QUIKTEST atm strategy in the entry condtions as follows:
                Code:
                                                AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0,
                                                TimeInForce.Day, orderId, "QUICKTEST", atmStrategyId);
                I assume this allows me to match a randomly generated atmStrategyId to my previously defined atm strategy called QUICKTEST.

                now i'm still left with the missing atmStrategyId parameter error. note I get two of them in my output window which I believe corresponds to the fact that I have two exit condtions looking for an atmStrategyId which doesn't exist when not in a trade.

                The error disappears when I am in a trade and an ATM Strategy is in effect

                Update: I just tested. Once the ATM closed out via stop or target, the strategy simply stops working. There are no prints to the output window even though strategy is technically running. I don't even get the missing parameter error anymore. In order to get the strategy running again I have to uncheck it in the strategies window in the control center and then reselect it again.
                Last edited by ShruggedAtlas; 06-16-2015, 08:26 AM.

                Comment


                  #9
                  Hello ShruggedAtlas,

                  Thanks for your reply and update.

                  I think I need to clarify the ATMstrategy process so once again if you would review the sampleATMStrategy in your Ninjatrader my comments will be directed to that code.

                  Two strings variables, named atmStrategyID and OrderID are initialized to string.empty in the variables declaration area.

                  In the OnBarUpdate() section the statement:
                  if (orderId.Length == 0 && atmStrategyId.Length == 0 && Close[0] > Open[0])
                  is checking to make sure that there is not an active atm strategy by verifying that the orderId and atmStrategyIDs are empty and if they are empty and if the Close of the currentbar is greater than the Open of the current bar to then place an order. So in this example this is the order entry criteria.

                  In these next lines:
                  atmStrategyId = GetAtmStrategyUniqueId();
                  orderId = GetAtmStrategyUniqueId();

                  The unique string IDs are generated before placing any order and creating the atm strategy.

                  This line:
                  AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
                  is placing the actual entry order using the pre existing ATM template "ATMStrategyTemplate". The ATM strategy then has a unique ID associated with it.

                  Later the code checks the status of the orders. What is key here is that once the ATM strategy has terminated (usually by stop or target filled) the code then resets the orderID and atmStrategyID strings back to empty so that when the next entry condition occurs the process can start over, first by generating the unique IDs needed.

                  So in summary then:

                  1) Create string variables for your orderID and atmStrategyID and set them to empty
                  2) When your entry conditions agree, generate unique order and strategy IDs with GetAtmStrategyUniqueId()
                  3) Place entry order using ATMStrategyCreate() with the generated unique Ids and name of your ATM strategy template
                  4) Continue using the IDs as needed to check status, change stops/targets until the ATM strategy finishes (stop or target filled)
                  5) Set the string IDs back to empty (as a good practice if nothing else)
                  6) Repeat steps 2 - 5 for each subsequent entry condition.
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    The error I was getting:
                    **NT** GetAtmStrategyMarketPosition() method error: Missing atmStrategyId parameter

                    ... was caused because i did not include:
                    Code:
                    orderId.Length > 0 && atmStrategyId.Length > 0
                    in my exit condition. As a result when not in a trade the orderId and atmStrategyId variables were empty and therefore caused the error. adding the above code filters out situations when those variables are empty ie when no ATM strategy is active.

                    The fact that my earlier code would not enter a new trade once exiting a previous trade seems to be due to the fact that I didn't include the following code:
                    Code:
                                            if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
                                                orderId = string.Empty;
                    and
                    Code:
                    if (atmStrategyId.Length > 0 && 
                       GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Flat)
                                        atmStrategyId = string.Empty;
                    Which are necessary as NinjaTrader_Paul suggested in his posts in order to reset the strategy after a trade closes. (Thanks NT_Paul!)

                    My strategy is running perfectly at the moment after making these changes. I hope this is helpful to others.
                    Last edited by ShruggedAtlas; 06-16-2015, 06:56 PM.

                    Comment


                      #11
                      Still not working!

                      Ok, my strategy took an order this evening and once the ATM kicked in the exit condition code was not working.

                      i have absolutely no idea at this point why my exit conditions are being ignored.

                      once again here is my most recent code:

                      Code:
                                      // Long Conditions
                                      if (orderId.Length == 0 && atmStrategyId.Length == 0)
                                      {
                                             if(Rising(CUMRSI(BarsArray[1],periodRsi,3)))
                                          {
                                              Print("Rising 172");
                                              Print("current CUMRSI(BarsArray[1] is: " + CUMRSI(BarsArray[1],periodRsi,3)[0]);
                                              
                                              if(CUMRSI(BarsArray[2],periodRsi,3)[1] < lower)
                                              {
                                                  Print("10 Min RSI below 30");
                                                  Print("CUMRSI(BarsArray[2]) is: " + CUMRSI(BarsArray[2],periodRsi,3)[0]);
                                                  
                                                  if(CUMRSI(BarsArray[0],rPeriod,3)[0] < lower)
                                                  {
                                                      Print("1 Min RSI below 30");
                         
                                                      atmStrategyId = GetAtmStrategyUniqueId();
                                                      orderId = GetAtmStrategyUniqueId();
                                          
                                                      AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0,
                                                      TimeInForce.Gtc, orderId, "EURUSD ATM", atmStrategyId);
                                                  }
                                              }
                                          }
                                      }
                      
                                      // Long Exit Condition
                                      if (orderId.Length > 0 && atmStrategyId.Length > 0 &&
                                          GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Long)
                                      {
                                          Print("Currently in a Long position!");
                                          if(CUMRSI(BarsArray[2],periodRsi,3)[0] > upper)
                                              {
                                                  Print("CLOSING LONG POSITION");
                                                  AtmStrategyClose(atmStrategyId);
                                              }
                                      }
                      next i have the following code basically taken straight from the atmsample strategy supplied by NT:

                      Code:
                                      if (orderId.Length > 0)
                                      {
                                          string[] status = GetAtmStrategyEntryOrderStatus(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)
                                          {
                                              // Print out some information about the order to the output window
                                              Print("The entry order average fill price is: " + status[0]);
                                              Print("The entry order filled amount is: " + status[1]);
                                              Print("The entry order order state is: " + status[2]);
                      
                                              // If the order state is terminal, reset the order id value
                                              if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
                                                  orderId = string.Empty;
                                          }
                                      } // If the strategy has terminated reset the strategy id
                                      else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
                                          atmStrategyId = string.Empty;
                                      }
                      What am I doing wrong?

                      Further update: This morning my strategy attempted to take a thousand trades. See image below. Something regarding the OrderId and atmStrategyId reset procedure is messed up. I have no idea how or why.
                      Attached Files
                      Last edited by ShruggedAtlas; 06-17-2015, 06:39 AM.

                      Comment


                        #12
                        Hello ShruggedAtlas,

                        Thanks for your post.

                        When looking at the chart it appears you have multiple time frames. Does your strategy use ADD statement to add in additional time frames?

                        Can you advise if you are running with CalculateOnbarClose = true or false?

                        You have incorporated a number of print statements, does the output of those show that the code got into your exit section? (Do you see "Currently in a Long position!").
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          I am using a multi timeframe stategy and I have used the code as follows:
                          Code:
                           Add(PeriodType.Minute, chartOne);
                                      Add(PeriodType.Minute, chartTwo);
                          The entries work great and I'm convinced that the multi-timeframe aspect of the strategy and code is not the problem.

                          My CalculateOnBarClose == false which is a necessity because I need to be able to see the longer timeframe in real time.

                          The problem from what I can see is in how the strategy resets. I have, for the moment, removed my exit code and reduced the reset code to simplify and test as follows:
                          Code:
                          if (orderId.Length == 0 && atmStrategyId.Length == 0)
                          {
                               if(Rising(CUMRSI(BarsArray[1],periodRsi,3)))
                               {                        
                                    if(CUMRSI(BarsArray[2],periodRsi,3)[1] < lower)
                                    {                        
                                         if(CUMRSI(BarsArray[0],rPeriod,3)[0] < lower)
                                         {  
                                              atmStrategyId = GetAtmStrategyUniqueId();
                                              orderId = GetAtmStrategyUniqueId();
                                              
                                              AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0,
                                              TimeInForce.Gtc, orderId, "EURUSD ATM", atmStrategyId);
                                          }
                                     }
                                }
                          }
                                          
                          if (atmStrategyId.Length > 0 && 
                              GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Flat)
                          {
                               Print("Setting atmStrategyId and orderId to empty");
                               atmStrategyId = string.Empty;
                                orderId = string.Empty;
                           }
                          The idea here is that I have my original entry, no exit outside of the ATM. I can also see if the above reset code will reset properly.

                          Basically what I see from this reset code is that if there is orderId and atmStrategyId active but position is flat...to go ahead and reset.

                          Also, note my strategy is set in the parameters (not in code) to only create one order per direction.

                          One final note. I tested the NT sample ATMSTrategy and discovered that the way it is coded, it allows mutliple entries in the same direction....even though the strategy parameters (not in code) are set to only 1 entry per direction. That presents a fundamental flaw in that sample code. Very Few people want their strategy to constently reenter positions while already in the position.

                          Update: The revised code shown in this post STILL generates multiple entries. Taking out the reset code results in only one direction per entry as desired. There is definately a flaw in the sample ATM Strategy that needs addressing.
                          Last edited by ShruggedAtlas; 06-17-2015, 09:19 AM.

                          Comment


                            #14
                            Hello ShruggedAtlas,

                            Thanks for your post and comments.

                            Please note that: When using ATM Strategies, strategy set up parameters such as EntriesPerDirection, EntryHandling, and ExitOnClose do not apply. You have started another thread on this topic (ATM) and that will also be replied to shortly if not already.

                            In order for us to assist you with the MTF ATM strategy, please send your complete code to Platformsupport[at]Ninjatrader[dot]Com with Atten Paul and a link to this thread in the subject line.

                            I appreciate your patience as we work to assist you.
                            Paul H.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by bc24fl, 08-30-2019, 01:58 PM
                            4 responses
                            259 views
                            0 likes
                            Last Post PaulMohn  
                            Started by sugalt, Today, 04:02 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post sugalt
                            by sugalt
                             
                            Started by tradingnasdaqprueba, 04-09-2024, 09:52 AM
                            6 responses
                            30 views
                            0 likes
                            Last Post tradingnasdaqprueba  
                            Started by PaulMohn, Today, 02:06 AM
                            1 response
                            8 views
                            0 likes
                            Last Post PaulMohn  
                            Started by Mindset, Today, 01:27 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Working...
                            X