Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ATM Strategy Entry and Exit

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

    ATM Strategy Entry and Exit

    Here is my code for executing the ATM Strat (sell side). I have noticed that on an exit I was left with two limit orders 5 ticks above and below price (the atm strat has a hard 5 tick stop). I was hoping you guys could see if there were any discrepancy in the coding here that would make it act funny. Thanks again.

    // ATM Strategy create and close code
    AtmStrategyCreate(OrderAction.Sell, OrderType.Market, 0, 0,
    TimeInForce.Day, GetAtmStrategyUniqueId(), "AtmStrategyTemplate",
    GetAtmStrategyUniqueId());
    PlaySound(@"C:\Program Files (x86)\NinjaTrader 7\sounds\shortentry.wav");
    if (GetAtmStrategyUnrealizedProfitLoss("idValue") > 500)
    AtmStrategyClose("idValue");
    Here is one of the additional exit conditions I am working on:

    // Exit Short Condition set 4
    if (insert exit condition here)
    {
    ExitShort("", "S");
    }
    What should the "S" be? I used to have:
    EnterShort(DefaultQuantity, "S");
    but I had to take it off because it was putting one two contracts and I think because it was reading the short entry signal twice.
    Last edited by brucelevy; 02-16-2015, 09:23 PM.

    #2
    brucelevy, so the erroneous quantity is only seen as you mix / match the ATM strategy method with the Enter() methods? That would be generally not advisable.

    Just use the AtmStragtegyClose() command as replacement, it will close the ATM strategy and exit any positions - https://www.ninjatrader.com/support/...ategyclose.htm

    Comment


      #3
      So which way should it be added:

      AtmStrategyClose(string AtmStrategyId);
      or like this:
      AtmStrategyClose("idValue");

      Comment


        #4
        brucelevy, you generally want to pass in the string value of the atm you want to close down. This would need to be saved to a variable first so your script 'remembers' the value. An example of this would be seen in SampleATMStrategy shipped with NT.

        You just generate a unique id now everytime you call AtmStrategyCreate(), however you keep no reference of it.

        Comment


          #5
          The string would go in the variables area like so?
          HTML Code:
          #region Variables
          private string	atmStrategyId		= string.Empty;
          private string	orderId			= string.Empty;
          #endregion
          Then to initiate the order through the ATM strategy I would put the following in place of the standard buy signal?:
          HTML Code:
          // Checks if both order id and strategy id are in a reset state
          if (orderId.Length == 0 && atmStrategyId.Length == 0)
          atmStrategyId = GetAtmStrategyUniqueId();
          orderId = GetAtmStrategyUniqueId();
          AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
          Also, where do I put the following information so that it does not give me an error code:
          HTML Code:
          // Make sure this strategy does not execute against historical data
          		    if (Historical)
          			return;
          			// 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;
          
          
          			if (atmStrategyId.Length > 0)
          			{
          				
          				// Print some information about the strategy to the output window
          				Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyId));
          				Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyId));
          				Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyId));
          				Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyId));
          			}
          Lastly, I have multiple exit conditions in addition to the atm strategy itself; is "orderid" the correct term to close out the atm strategy/position?:
          HTML Code:
          // Exit Short Condition set 6
          if (INDICATOR CONDITION INFO GOES HERE*))
          {
           AtmStrategyClose("orderId");
          }
          Last edited by brucelevy; 02-22-2015, 11:17 AM.

          Comment


            #6
            Hello brucelevy,

            Thank you for your response.

            The atmStrategyId is passed to the AtmStrategyClose() method: http://www.ninjatrader.com/support/h...ategyclose.htm

            For the other questions please refer to the example for the proper placement under Tools > Edit NinjaScript > Strategy > SampleAtmStrategy.

            Comment


              #7
              I've read the help pages on closing an atm strategy and searched the forum for others who have had trouble closing atm strats but still havn't come up with an answer on how to close an atm strategy with a condition.


              I added the string to the region variables:
              private string atmStrategyId = string.Empty;
              private string orderId = string.Empty;

              and the exit condition uses the following to close the atm strat and doesn't work:

              {
              AtmStrategyClose(atmStrategyId);
              }


              I'm going to need a little more than a link to the help pages again, thanks.

              Comment


                #8
                Bruce, the reason would be that you've never assigned the actual string id of your current atm to the variable for tracking that would then pass to the AtmStrategyClose call, the empty assignment you have shown could just be though of the intial value for the variable (empty).

                Patrick pointed to the SampleATMStrategy script we ship with NT as good resource and you see the following practice in it's AtmStrategyCreate() call where the entry is generated.

                Code:
                if (orderId.Length == 0 && atmStrategyId.Length == 0 && yourCondition)
                	{
                		atmStrategyId = GetAtmStrategyUniqueId();				
                                orderId = GetAtmStrategyUniqueId();
                	        AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
                	}
                You can see we assign here the unique id to the atmStrategyId variable and use this variable in the create call later on. Unless we assign another value (for example by nullifying it if the atm was closed or position flat) we would keep the reference and thus a call to AtmStrategyClose(atmStrategyId); would succeed.

                Comment


                  #9
                  So instead of string.Empty we should use string.atmStrategyId in the variables region?

                  Comment


                    #10
                    Brucelevy,

                    Not exactly.

                    You would want to have unique id each time you want to create a new ATM to submit.

                    Each time your script submits new ATM order, you need to generate unique IDs strings for those variables. Using GetAtmStrategyUniqueId() will do this each time it is called and being assigned to your variables in OnBarUpdate.

                    After you have submitted these orders you would then want to reset the variables by setting them to use string.Empty as you had in the variables region.

                    If you haven't already, take a look at the SampleAtmStrategy in Tools -> Edit NinjaScript -> Strategy

                    This will show you how to set and reset your variables accordingly and when to do so.
                    Cal H.NinjaTrader Customer Service

                    Comment


                      #11
                      So this is what sample atm comes with below, and is similar to what I used.



                      if (orderId.Length == 0 && atmStrategyId.Length == 0 && Close[0] > Open[0])
                      {
                      atmStrategyId = GetAtmStrategyUniqueId();
                      orderId = GetAtmStrategyUniqueId();

                      AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
                      }

                      --

                      So are you saying that to close the atm with an exit condition it should include getatmstratuniqueid as well, like so?

                      {
                      GetAtmStrategyUniqueId();
                      }

                      {
                      AtmStrategyClose(atmStrategyId);
                      }

                      Comment


                        #12
                        Hello brucelevy,

                        No, you would just call the atmStrategyId string in the AtmStrategyClose() method.

                        Comment


                          #13
                          So this should call the atm to close

                          {
                          AtmStrategyClose(atmStrategyId);
                          }

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          668 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          377 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          110 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
                          580 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X