Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

AtmStrategyClose

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

    AtmStrategyClose

    Can anyone tell me what happens in the background when AtmStrategyClose method is triggered?

    I'm wanting to know if the variables orderId and atmStrategyId are automatically 'cleared' or set to empty strings.

    I'm having trouble getting my strategy to 'reset' when it gets stopped out or hits it's target. Essentially it stops taking new trades after getting stopped out or hitting targets as if the strategy thinks the ATM is still active.

    Do I need to empty those variables myself or will atmStrategyClose(atmStrategyId) do it for me?

    #2
    Hello ShruggedAtlas,

    Thank you for writing in. The AtmStrategyClose() method does not clear the orderId or atmStrategyId variables. In the SampleAtmStrategy example, separate code is used to clear these values:
    Code:
    // Check for a pending entry order
    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;
    Note: Calling AtmStrategyClose() would cause the above conditions to be met thus resetting the orderId and the atmStrategyId.

    Please let us know if you have any further questions.
    Last edited by NinjaTrader_MichaelM; 06-17-2015, 11:20 AM.
    Michael M.NinjaTrader Quality Assurance

    Comment


      #3
      I couldn't find CloseAtmStrategy() in the NT7 help files. From what you're saying, CloseAtmStrategy() does reset orderId and atmStrategyId yet I can't find any reference to that function.

      AtmStrategyClose() on the other hand does not reset those variables. it only cancels working orders and closes the position however the variables set throughAtmStrategyCreate() are still open and need to be manually closed as shown in the sampleATMStrategy you presented.

      Am I correct here?

      Comment


        #4
        Hello ShruggedAtlas,

        That was a typo and I apologize for the confusion. The only method we are referring to here is called AtmStrategyClose() and it does not reset those variables. You have to add custom logic like in the SampleAtmStrategy to reset the variables.

        I was commenting in my note that if you called AtmStrategyClose(), it would cause the conditions in the example code to be true and allow the variables to be reset.

        Please let us know if we may assist further.
        Michael M.NinjaTrader Quality Assurance

        Comment


          #5
          Ok I understand now...so it's a necessary two part process....first close the ATMStrategy and then clear the variables 'manually'

          Sure would be nice if was done automatically via AtmStrategyClose() method

          Comment


            #6
            Getting multiple entries in the same bar

            The following test code results in multiple entries in a single bar and I don't know why.

            I am checking for instances where the orderId and atmStrategyId have length yet the position is flat which should occur only when the ATM has been exited.

            Note: when I remove the reset code for the ID variables that problem goes away but then it will only trade once and will never enter a new trade from there because orderId and atmStrategyId have length. Yet when I check for positive length and flat position it produces a continuous stream of orders within the same bar.
            Code:
                              if (orderId.Length == 0 && atmStrategyId.Length == 0 &&
                                Close[0] > Close[1])
                                {  
                                    atmStrategyId = GetAtmStrategyUniqueId();
                                    orderId = GetAtmStrategyUniqueId();
                                
                                    AtmStrategyCreate(OrderAction.SellShort, OrderType.Market, 0, 0,
                                    TimeInForce.Gtc, orderId, "QUICKTEST",atmStrategyId);
                                }
                            // Long Exit Condition
                            if (orderId.Length > 0 && atmStrategyId.Length > 0 &&
                                GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Long)
                            {
                                if(Close[0] > Close[2])
                                    {
                                        Print("CLOSING LONG POSITION");
                                        AtmStrategyClose(atmStrategyId);
                                    }
                            }                
                            Print(orderId);
                            Print(atmStrategyId);
                                            
                            if (orderId.Length > 0 && atmStrategyId.Length > 0 &&
                                GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Flat)
                            {
                                Print("Calling AtmStrategyClose fuction");
                                AtmStrategyClose(atmStrategyId);
                                Print("Clearing atmStrategyId and orderId");
                                atmStrategyId = string.Empty;
                                orderId = string.Empty;
                                Print(atmStrategyId);                    
                            }
            It's madness I tell you! grrrrr
            ----------------------------------------------------------------------------------
            Ok I think the reason it's giving multiple entries is that for some reason the code:
            GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Flat
            is not working in the current context.

            When i'm in a trade and the orderId and atmStrategyId have length it should only see the code in that if statement if atmStrategyId is flat...but it's not...so how can it be seeing it as flat wen it's clearly in the middle of an active ATM strategy (order)?
            Last edited by ShruggedAtlas; 06-17-2015, 12:44 PM.

            Comment


              #7
              Hello ShruggedAtlas,

              After the ATM strategy places an entry, you are still in a flat market position. You are not in a long or short market position until the order fills.

              You are creating a loop by entering and then immediately cancelling and resetting the strategy and orderId/atmStrategyId variables. I cannot think of any reason you would want to do this in a real strategy.

              Please adjust your code so that it checks if you are in a short market position (because you are entering short) before cancelling the strategy.

              Please let us know if we may be of further assistance.
              Michael M.NinjaTrader Quality Assurance

              Comment


                #8
                That is immensely confusing for me and perhaps a bit ironic because as soon as I check to see if i'm in a long or short position then I don't want to cancel the strategy. I need to stay in the ATM strategy until it is stopped out or hits a target. I only want to 'cancel' the strategy after it has gone flat. I'm caught in a weird logical loop where in order to cancel the strategy I need to be flat yet when I submit an order I'm flat and so the reset code is triggered even when i don't want it to.

                I don't know how to resolve this.
                Last edited by ShruggedAtlas; 06-17-2015, 01:40 PM.

                Comment


                  #9
                  Hello ShruggedAtlas,

                  The code in the SampleAtmStrategy does exactly what you want.

                  You do not need to use AtmStrategyClose() to have the ATM strategy exit upon filling the profit target or stop loss, it does that automatically. All you have to take care of once the stop loss or profit target is hit, is resetting the orderId and atmStrategyId so the ATM strategy can start again. See the example code I provided in my first post as an example of how to do this.

                  You would use AtmStrategyClose() to exit the strategy manually. So if you wanted it to exit somewhere besides at the stop loss or profit target if some other conditions were met.

                  Please let us know if you require further explanation.
                  Michael M.NinjaTrader Quality Assurance

                  Comment


                    #10
                    I'm afraid I have used that code however it generates multiple entries within the same bar. i only want one entry per direction. Taking the code out allows my strategy to perform great...but only once and then I have to manually reset the strategy in the control center.

                    I have also tried using a counter to force only one trade per direction with no success. The reset code defined in the ATMStrategy sample code always results in multiple entries per direction.

                    Adding the code back in creates the loop which allows hundreds of orders to occur within the same bar. No matter if my CalculateOnBarClose = true or false, I get the same results.

                    If only the ATMStrategy sample included an option with code to force one trade per direction then I can see how this is possible. As it stands now, with my limited coding experience, I see no way to accomplish this.

                    Comment


                      #11
                      Originally posted by NinjaTrader_MichaelM View Post
                      Please adjust your code so that it checks if you are in a short market position (because you are entering short) before cancelling the strategy.
                      .
                      If I am in a short position then that is because I want to be in a short position. I need to check after the trade closes out and I am flat. If I reset my ATM strategy when I'm in a short or long position then my strategy thinks I'm flat and will enter a new position on the next valid entry signal, which could be instantly

                      I'm afraid I just don't understand

                      Comment


                        #12
                        Hello ShruggedAtlas,

                        I understand another member of the NinjaScript team is currently working with you on this issue via e-mail.

                        Please let us know if you have any other questions.
                        Michael M.NinjaTrader Quality Assurance

                        Comment


                          #13
                          Yes Paul worked on this today and worked it out for me. In yet another thread I was advised to consider creating a counter variable that would allow me to limit the number of entries. I attempted that approach but failed. It's no surprise then that Paul fixed my problem by using the essential features of the atmsamplestrategy as you suggested and was able to properly apply a counter of sorts to track when I was in an entry and when I was out. I'm posting the results here because I'm sure this has to be a fairly common problem for traders wanting to automate using ATM as well as stay in only one trade/position at a time. Funny that I haven't seen any posts about this. Code is as follows:

                          Exit and entry conditions: (Note the bool variable called onceonly and exitonce - that is key)
                          Code:
                                          // Long Conditions
                                          if (orderId.Length == 0 && atmStrategyId.Length == 0 && onceonly )
                                          {
                                                 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);
                                                          
                                                          onceonly = false;    // set flag so that we only have the one order
                                                          
                                                          exitOnce= true;      // Reset flag for Long/short exits
                                                      }
                                                  }
                                              }
                                          }
                                          
                          
                                          // Long Exit Condition
                                          if (atmStrategyId.Length > 0 && 
                                              GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Long)                    
                                          {
                                              if(Falling(CUMRSI(BarsArray[1],periodRsi,3)) && exitOnce)
                                                  {
                                                      AtmStrategyClose(atmStrategyId);
                                                      exitOnce = false;  // Set flag to false so we execute close once
                                                  }
                                          }
                          NEXT IS THE RESET CODE: Again notice the bool - the only one needed here)
                          Code:
                          //                 Check for a pending entry order
                                          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)
                                              {
                                                  // 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;
                                          
                                              onceonly = true;    // Reset flag so we can once again enter an order
                                          }
                          I hope this is helpful to others trying to accomplish the same thing

                          Comment


                            #14
                            Hello ShruggedAtlas,

                            I am glad to hear you were able to get the issue resolved.

                            Thank you for posting your resolution so any users who may be experiencing similar problems might benefit from it.

                            Please have a great day!
                            Michael M.NinjaTrader Quality Assurance

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by GussJ, 03-04-2020, 03:11 PM
                            11 responses
                            3,227 views
                            0 likes
                            Last Post xiinteractive  
                            Started by andrewtrades, Today, 04:57 PM
                            1 response
                            13 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Started by chbruno, Today, 04:10 PM
                            0 responses
                            7 views
                            0 likes
                            Last Post chbruno
                            by chbruno
                             
                            Started by josh18955, 03-25-2023, 11:16 AM
                            6 responses
                            440 views
                            0 likes
                            Last Post Delerium  
                            Started by FAQtrader, Today, 03:35 PM
                            0 responses
                            11 views
                            0 likes
                            Last Post FAQtrader  
                            Working...
                            X