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

Can't figure out right variable for GetAtmStrategyMarketPosition("atmStrategyId")

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

    Can't figure out right variable for GetAtmStrategyMarketPosition("atmStrategyId")

    I have a script that I am incorporating an atm strategy into. I have the following strings associated with the ATM

    string atmStrategyId = GetAtmStrategyUniqueId();
    string orderId = GetAtmStrategyUniqueId();

    It's my understanding that the GetAtmStrategyUniqueId(); command generates a random atmStrategyId and orderId upon execution.

    After an order is entered into I want to check once its flat. Accordingly, I have added the following:

    if (GetAtmStrategyMarketPosition("Id") == MarketPosition.Flat)

    The problem is I cannot figure you what to use as the Id. I have tried atmStrategyId (both with and without quotes). This gives me an error in the log saying 'atmStrategyId" does not exit.

    I have tried putting in GetAtmStrategyUniqueId(). This generates a random number on each update which the log than says does not exist

    If the atmStrategyId is randomly generated, how can you Identify it for purposes of the GetAtmStrategyMarketPosition("Id") . command. Any help would be appreciated

    #2
    Hello mdb4403,
    Thank you for your post.
    For GetATMStrategyMarketPostion() the "ID" will be your string you created for the ATM strategy unique id. For example:
    Code:
    private string	atmStrategyId		= string.Empty;
    
    protected override void OnBarUpdate()
    {
    	atmStrategyId = GetAtmStrategyUniqueId();
            
            if(GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
            {
                //Do Something
             }
    }
    If we can be of any other assistance please let us know.
    Cody B.NinjaTrader Customer Service

    Comment


      #3
      So I have the exact same script as your example. I have the term "atmStrategyId" in the paranthesis but without the quotes. When I start the strategy before it enters any position the log has a yellow error message that says "GetATMStrategyMarketPosition() method error "Missing atmStrategy Parameter".

      Then if it enters an order, I get an error message that says it can't find what appears to be a randomly generated number. or that number does not exist.

      What is happening is that when the strategy enters an order it enters multiple times (like 80 times) because I am running my strategy with Onbarclose =false and on each tick if the conditions of the trade are met it enters an order. I am trying to make it be flat before it enters another trade but the GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat) does not seem to be working. Any suggestions. Ive been working with Paul through emails but still can't get it to work.

      Comment


        #4
        Hello,
        Can you provide a simplified example of your entry code?
        Also I would recommend to review how the SampleATMStrategy handles the ATM Strategy ID and how it checks for when the ATM Market Position is flat. To review this you can go to Tools> Edit NinjaScript> SampleATMStrategy.
        Cody B.NinjaTrader Customer Service

        Comment


          #5
          protected override void OnBarUpdate()
          {


          if (GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
          {
          atmStrategyId=string.Empty;
          orderId = string.Empty;
          }


          if (ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 170000)
          if(BarsInProgress ==0)



          // Condition set 2
          if (EMA(10)[0]>EMA(10)[1]
          && EMA(10)[0]-SMA(20)[0]>EMA(10)[1]-SMA(20)[1]
          && Closes[1][0]>Opens[1][0]+100.0
          && Closes[5][0]>Opens[5][0]+100.0
          "
          "
          "

          && orderId.Length == 0
          && atmStrategyId.Length == 0

          )



          {
          atmStrategyId = GetAtmStrategyUniqueId();
          orderId = GetAtmStrategyUniqueId();
          AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0,
          TimeInForce.Day, orderId , "FuturesFinal", atmStrategyId);
          DrawArrowUp("My arrowup" + CurrentBar, false, 0, Low[0] - 12 * TickSize, Color.Yellow);
          Alert("MyAlert1", Priority.Medium, "Long 3MinLaxRevised", "", 59, Color.White, Color.Black);
          DrawText("My text" + CurrentBar, "KU1 ", 1, Low[0] - 10 * TickSize, Color.Blue);

          }


          Now when I enable this I immediately get an error message GetATMStrategyMarketPosition method error "Missing atmStrategyId parameter.

          If I add the line atmStrategyId = GetAtmStrategyUniqueId(); as the first line of my code like you have in your example, I get this message "GetAtmStrategyMarketPosition method error: AtmStrategyId (random number) does not exist.

          I went back and ran the Sample Atm strategy on my system and did not get these errors

          Comment


            #6
            Hello,
            When checking if the atmStrategy Market Position is flat you will need to check that the atmStrategy has a value. You could check this by doing the following:
            Code:
            if (atmStrategyID.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
            {
            	atmStrategyId=string.Empty;
            	orderId = string.Empty;	
            }
            Without checking that the length is greater than 0 this checks that there is an ATM strategy position to check.
            Cody B.NinjaTrader Customer Service

            Comment


              #7
              Im pretty sure I have tried that but Ill give it another go and let you know. I saw this was in the SampleATM strategy

              Comment


                #8
                Cody,that seems to have done the trick adding atmStrategyID.Length > 0. It works beautfully in repay mode. I will give it a try tomorrow with live data feed. Thank you so much. All you guys there are great. Incredibly helpful

                Comment


                  #9
                  Well I spoke too soon. It works fine on Market Replay but using a live feed I have the same problem. Since Im running onbarclose=false it enters orders every tick one of my condition sets is true even though it is suppose to check to be sure its flat before entering more orders. Any other suggestions. It just entered the first trade of the day to the tune of 250 NQ contracts. And while I made 27,000 in the simulator my real account can't handle that.

                  Comment


                    #10
                    Hello,
                    I have tested the code on my end and have gotten it to work. The reason it is submitting so many orders is due to On each tick update the entry order has not gotten filled yet so you are still in a flat position. Thus we have and orderId.length that is greater than 0, and atmStrategyId.length that is greater than 0 and a position that is flat thus, the atmStrategyId and orderId are reset and begins the process of submitting the order again and the cycle repeats.
                    So what you need to add in is the check the order status of the entry order first and if it has been filled you could then reset the atmStrategyId and orderId. The SampleATMStrategy provides a great example of how you could do this. I have provided the example below:

                    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;
                    For more information on GetAtmStrategyEntryOrderStatus() please see the following link: http://ninjatrader.com/support/helpG...tegycreate.htm
                    Cody B.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Cody but Im not sure how that can be. I can see it adding orders even after other orders have clearly filled. It goes from 4 orders to 8 then 12 then.......... After the initial fill of 4 how can it add any more. Certainly I could understand if maybe two orders are submitted but even in its current state why does it keep submitting orders even though it is clearly not flat

                      Comment


                        #12
                        Im also running the identical strategy today with COBC=True and I get the same problem. So if I get an alert on one bar and it enters and I get an alert on the next bar (1 minute later) it will still enter another trade even if the earlier trade has not completed and I am not flat

                        Comment


                          #13
                          Hello,
                          Can you provide a screenshot of your entry orders filling and then the orders continue to submit?
                          When I have ran the code I do not get fills. I get roughly 200 orders all stuck in a pending state waiting to be filled but are not.
                          If you implement my logic do you see it not working?
                          Cody B.NinjaTrader Customer Service

                          Comment


                            #14
                            cody thanks for sticking with me. I sure well. I'll have to do it tomorrow but Ill sure do it

                            Comment


                              #15
                              Cody I am going to get that screen shot for you tomorrow. As I watched the script trade today there was another anomaly that I found maybe you can comment on. The script trades on an NQ chart as the primary. I've added the following object in the Initialize section:

                              Add("^TICKQ", PeriodType.Minute, 1).

                              It is the first object added. Other objects have been added but they are just different time periods of the NQ.

                              In my strategy a condition to entering a trade long or short is that the ^TICKQ must be at a certain level to enter the trade. For example, to enter short the ^TICKQ must be below -275. This is represented in the Code by the following line

                              && Closes[1][0]<-275

                              Today I ran the strategy with COBC=True. Accordingly, I would assume that it would only enter a trade if the one minute Tick closed at <-275. Attached you will see a picture of the NQ chart with the ^Tickq added as a data series.You can see that an alert and trade was entered into at 15:59. However, on that period, the ^TICKQ closed at +91 well above the -275 threshold. There were actually several trades like that today where the close of the ^TICKQ did not meet the threshold of the trade entry and yet the trade still executed. Here is the other interesting component. If I shut the strategy down and restart it. Those anamolous trade signals don't show up and the only trade signals on the chart are where the ^TICKQ did meet the threshold for entry.

                              Any ideas on this issue (actually not sure how to upload an image-will do it tomorrow)

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by algospoke, 04-17-2024, 06:40 PM
                              3 responses
                              26 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by bmartz, 03-12-2024, 06:12 AM
                              3 responses
                              27 views
                              0 likes
                              Last Post NinjaTrader_Zachary  
                              Started by Aviram Y, Today, 05:29 AM
                              2 responses
                              8 views
                              0 likes
                              Last Post Aviram Y  
                              Started by gentlebenthebear, Today, 01:30 AM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by cls71, Today, 04:45 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X