Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Changing an ATM Strategy Entry Order

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

    Changing an ATM Strategy Entry Order

    I'm implementing a strategy which enters the market using an ATM Strategy. I am entering a limit order which sometimes becomes an orphan. I would like to move that order to the current bid or ask, if it doesn't fill within X bars. It is an ATM order however so it seems a little more complicated to change it. Here is the code I'm using right now:

    Code:
    {
    atmStrategyId = GetAtmStrategyUniqueId();
    orderId = GetAtmStrategyUniqueId();
    AtmStrategyCreate(Action.Buy, OrderType.Limit, MAX(High, LB)[1] +2 * TickSize, 0, TimeInForce.Day, orderId, "KPSCAtm", atmStrategyId);
    }
    The User Guide has an AtmStrategyChangeEntryOrder
    Code:
    {   
        AtmStrategyChangeEntryOrder(GetCurrentBid(), 0, "orderIdValue");     
    }
    But it isn't clear to me, how to use it. Do you have any reference code that shows in more detail how to implement this AtmStrategyChangeEntry Order and insert a new limit order?
    Thanks
    DaveN

    #2
    When you first submitted your order you have an orderId. You tie that orderId to the AtmStrategyChangeEntryOrder() and it will modify it. Unfortunately there are no additional references besides what is available in the Help Guide and the pre-installed SampleAtmStrategy.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      AtmStrategyChangeOrder() method error: Missing orderId parameter

      Hey Everybody,
      I have a glitch with a strat that calls an ATM. When running live SIM or Replay, no problems. But when running LIVE, I get an error when trying to to change the entry order. Here's the error from the log:

      AtmStrategyChangeOrder() method error: Missing orderId parameter

      The example from Help says
      {
      AtmStrategyChangeEntryOrder(GetCurrentBid(), 0, "orderIdValue");
      }

      Do I need to put the quotes around orderID ?

      I really don't want to experiment with it Live.

      The Sample ATM included with Ninja doesn't use the Change Order code.

      Here's the code (Regression Channel used as example only)

      if (ToTime(Time[0]) >= ToTime(starttimehr, starttimemin, 0)
      && ToTime(Time[0]) <= ToTime(stoptimehr, stoptimemin, 0)
      && Close[0] < RegressionChannel(35, 2).Upper[0]
      && Close[0] > RegressionChannel(35, 2).Middle[0]
      && orderId.Length == 0
      && atmStrategyId.Length == 0)
      {
      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();
      AtmStrategyCreate(Action.Sell, OrderType.Limit, RegressionChannel(35, 2).Upper[0], 0,
      TimeInForce.Day,orderId , "JTM",
      atmStrategyId);
      }

      // Change entry order if not filled
      if (ToTime(Time[0]) >= ToTime(starttimehr, starttimemin, 0)
      && ToTime(Time[0]) <= ToTime(stoptimehr, stoptimemin, 0)
      && Close[0] < RegressionChannel(35, 2).Upper[0]
      && Close[0] > RegressionChannel(35, 2).Middle[0]
      && orderId.Length >= 0
      && atmStrategyId.Length >= 0)

      {
      AtmStrategyChangeEntryOrder(RegressionChannel(35, 2).Upper[0], 0, orderId);
      }

      It SHOULD have an order ID defined since it called it when it created the ATM, but simply typing in orderId doesn't work.
      I've read everything I could find in the forum about this. Like Daven, I'm not clear on what should go in the "orderIdValue" spot. If we aren't supposed to use the quotes (I'm not), shouldn't the orderId called when the strategy call was created still be valid? Or could it be that the Limit Price isn't valid since I'm using a moving entry point? Perhaps I need to set it 1 bar back? Sorry to be a bother, but I don't want to experiment Live, and it works fine in Replay or live SIM.
      Thanks for any help you might send my way!!
      Joe

      Comment


        #4
        You put whatever you want in there. If you want to change an order you need to place in an orderId of an order that already exists and is active. You place an ATM order with an orderId. If you later want to change that order you need to call that exact same orderId.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Josh,
          I believe that's what I did, or tried to do. As you can see in my code, I put:
          orderId = GetAtmStrategyUniqueId();

          Then I put orderId in where the quotes are:
          AtmStrategyChangeEntryOrder(RegressionChannel(35, 2).Upper[0], 0, orderId);

          The orderId is active. (In replay, it moves the order fine. In Live, real time, I get the ERROR:
          AtmStrategyChangeOrder() method error: Missing orderId parameter

          So, what would the missing orderId parameter be that I need to make this work in Live, real time mode??
          Thanks,
          Joe

          Comment


            #6
            Joe,

            You cannot do that. You can't just assign a new orderId and then try to change on the new ID. You can only change orders that exist.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Josh,
              Thanks for your response. I understood what you said the last two times you replied. I'm under the impression (apparently wrong) that when I put
              orderId = GetAtmStrategyUniqueId();
              when placing the first entry order for the ATM that it assigns an orderId to that order. Is that not correct? It seems like it MUST exist since I use orderId in the ATMStrategyCreate line and it calls the ATM just fine when the order is hit. If the ATMStrategyCreate line isn't using orderId that it got earlier, then what's it using?


              If orderId ISN'T the EXISTING OrderId assigned when it created the Entry Condition and called the ATM to execute if the order was hit, then what command do I use to get it? I searched Help and there is no such command as "GetExistingOrderId", or something close to that.

              All I'm trying to find out is where do I get the orderId to use in

              AtmStrategyChangeEntryOrder(RegressionChannel(35, 2).Upper[0], 0, orderId);

              Thanks,
              Joe

              Comment


                #8
                Joe,

                I think it would be easier if I saw the complete code. Thank you.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  ATM Strategy Sample Code

                  I don't know if this will help or not but I am providing the code I use to enter a trade then track it. It compiles (with some code not provided) and runs and calls an ATM strategy Called "HLBCrossATM".

                  Don't know if this will help but I do know this approach works though the strategy isn't very good I was just trying to get atm execution working. As I recall most of it came from the sample atm code that is provided in the forum:
                  DaveN

                  Code:
                         protected override void OnBarUpdate()
                          {
                  			// Make sure this strategy does not execute against historical data
                  
                  			if (Historical)
                  				return;
                         		{
                             // Add a loop to number print statements
                  				counter = counter + 1;
                  				
                  			// Wait until moving avg period is working with current active bars
                  			if (period < counter)
                  				return;
                  			// Check for no active order and cross up conditions
                  				
                  				Print(counter);
                  				Print("Code is entering Cross-Up test logic.");            
                  			
                  			// SMA Highs Cross Up and ATM Order Entry Long
                              if (Slope(SMA(Median, period), slopeLB, 0) > 0
                                  && CrossAbove(Close, SMA(High, period), 3))
                              		{
                                  	atmStrategyId = GetAtmStrategyUniqueId();
                  					orderId = GetAtmStrategyUniqueId();
                  					AtmStrategyCreate(Action.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "HLBCrossATM", atmStrategyId);
                             			}
                  				Print(counter);
                  				Print ("Code is entering the Cross-DN test logic.");
                  
                              // SMA Low Cross and ATM Order Entry Short
                              if (orderId.Length == 0 && atmStrategyId.Length == 0
                  				&& Slope(SMA(Median, period), slopeLB, 0) < 0
                                  && CrossBelow(Close, SMA(Low, period), 3))
                             		 	{		
                                  	atmStrategyId = GetAtmStrategyUniqueId();
                  					orderId = GetAtmStrategyUniqueId();
                  					AtmStrategyCreate(Action.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "HLBCrossATM", atmStrategyId);
                             			}
                  	        // Get Strategy identification information in order to print it out    	
                  					
                         		// Check for a pending entry order
                  			// Print where we are in the program
                  			{
                  				Print(counter);
                  				Print("Code has entered the status print out.");
                  			}
                  			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));
                  					}

                  Comment


                    #10
                    Thanks Josh/Daven,
                    I have the strat calling the ATM and everything working (except a bug causing order reject if the order is on the wrong side of the price, but that's an easy fix.)
                    My problem is moving the entry order. It seems to work great in Replay. Oddly enough, it still still took the trade but stopped when it got the order reject from an incorrect order on a later execution.
                    I'm going through the Log line by line so see if I can see exactly when it produces the error.
                    I"ll keep playing with it, but if you guys want to see this, I've attached them. I'll let you know if I find the problem and get it fixed.
                    Joe
                    Attached Files

                    Comment


                      #11
                      Since you have more than one order I recommend you not use the same variable for orderId and atmStrategyId.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        OK, I'll give that a try. In theory, there should never be more than 1 order at a time since it cancels the order if conditions are out of bounds. Then it won't generate another order unless no orders exist and the conditions are right.
                        I'll look closer at it and give the Buy and Sell variables for orderId and StrategyId different names anyway.
                        Thanks,
                        Joe

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by cmtjoancolmenero, Yesterday, 03:58 PM
                        2 responses
                        19 views
                        0 likes
                        Last Post cmtjoancolmenero  
                        Started by Stanfillirenfro, Today, 07:23 AM
                        0 responses
                        2 views
                        0 likes
                        Last Post Stanfillirenfro  
                        Started by olisav57, Yesterday, 07:39 PM
                        1 response
                        9 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by cocoescala, 10-12-2018, 11:02 PM
                        7 responses
                        942 views
                        0 likes
                        Last Post Jquiroz1975  
                        Started by oviejo, Today, 12:28 AM
                        1 response
                        11 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Working...
                        X