Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Automated ATM Strategy Check if Flat

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

    Automated ATM Strategy Check if Flat

    I am trying to implement a strategy which launches an ATM strategy then returns control to the strategy once the ATM is closed. However, I can't get the test if strategy is flat to work so I get a strategy entry every time my entry signal occurs despite the fact that it isn't supposed to launch the ATM if there is already a trade working. How do I need to change the code below to get it to work? there are two ATM's being used, one for a long entry, one for a short entry. There used to be only one but I thought having one for each direction would work better, but so far that hasn't proven to be true,
    Thanks
    DaveN

    Code:
    			if ((ToTime(Time[0]) > TT1_Start && ToTime(Time[0]) < TT1_End ) || (ToTime(Time[0]) > TT2_Start && ToTime(Time[0]) < TT2_End)
    				&& orderId.Length == 0 && atmStrategyId.Length == 0 && GetAtmStrategyMarketPosition("CB_ATM_L") == Cbi.MarketPosition.Flat 
    				&& GetAtmStrategyMarketPosition("CB_ATM_S") == Cbi.MarketPosition.Flat)

    #2
    Hello Daven,

    You need to check the Strategy ID not the name of the strategy.

    Use atmStrategyId for the check in GetAtmStrategyMarketPosition()

    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      I'm confused, in that same statement I am using "atmStrategyID.Length == 0". for that to be true doesn't the AtmStrategyID already have to be null, or non-existent? At any rate, how do I actually get the AtmStrategyID ? Do I have to equate it to a variable at the time it is launched and then later recall that variable?
      Thanks
      DaveN

      Comment


        #4
        Daven,

        GetAtmStrategyMarketPosition() uses the strategy id that you set when you create the ATM order to check the actual position.

        let's go a step further. You are checking if the atmStrategyId is 0 and then checking the MarketPosition. If the id is already at 0 then I wouldn't expect to see you get any returns as then the strategy ID would be non-existent.

        What is the end goal for the IF statement that you have provided?
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          I don't want to enter a new position until the old position is resolved, in other words the ATM has run it's course, finished at the profit target or stopped out. The way it works now, if a new entry condition occurs, either long or short, it enters the position without closing out the old, so I either get a position with two contracts (enter in the same direction) or no position at all, and in both cases it leaves behind both stop and profit targets for both positions.
          One other question, It would be nice to reverse my position if I get a new entry signal, so that would mean closing the existing Atm, and launching a new one. Is there a way to do that?
          Thanks again.
          Dave

          Comment


            #6
            I tried this and it doesn't work either.

            && GetAtmStrategyMarketPosition("orderId") == Cbi.MarketPosition.Flat

            Am I using "orderId" correctly? When the trade is entered I set orderId equal to GetAtmStrategyUniqueId equal to the variable orderId.

            {
            atmStrategyId = GetAtmStrategyUniqueId();
            orderId = GetAtmStrategyUniqueId();
            AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "CB_ATM_L", atmStrategyId);
            Print(" OrderPlaced " + GetAtmStrategyPositionQuantity("CB_ATM_L").ToStrin g());
            }

            Comment


              #7
              Daven,

              Can you please attach your strategy so that I can further assist with what you are trying to do and see what you have written.
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                Test File

                I am attaching the strategy. What I am trying to accomplish is to prevent the initiation of a new order, while there is still an existing ATM position that has not yet closed. It appears that the marketposition == flat, portion of my code just isn't working. I am a bit confused about how to call an exisitng ATM ID , since from the sample provided in the forum both the orderId variable and the ATM Id, pull the same data. Not sure why or how that works to assist in identifying an existing position.
                Hope you can help Cal, and thanks.
                DaveN
                Attached Files

                Comment


                  #9
                  Originally posted by daven View Post
                  I am attaching the strategy. What I am trying to accomplish is to prevent the initiation of a new order, while there is still an existing ATM position that has not yet closed. It appears that the marketposition == flat, portion of my code just isn't working. I am a bit confused about how to call an exisitng ATM ID , since from the sample provided in the forum both the orderId variable and the ATM Id, pull the same data. Not sure why or how that works to assist in identifying an existing position.
                  Hope you can help Cal, and thanks.
                  DaveN
                  With this code, you prematurely reset the orderId, because you reset it when the order was filled.
                  Code:
                  					// If the order state is terminal, reset the order id value
                  					if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
                  						orderId = string.Empty;
                  Therefore, your entry conditions will now allow a new entry. Reset the order only when you want to allow new entries.
                  Last edited by koganam; 09-21-2014, 01:20 PM. Reason: Corrected misspelling.

                  Comment


                    #10
                    That helps, if I could inquire further, If I want to reset the orderId after the position is completely closed what query would I use for that. In essence, both the profit order and the stop order would have to be closed and the position would have to be flat?
                    Thanks for your help.
                    DaveN

                    Comment


                      #11
                      Originally posted by daven View Post
                      That helps, if I could inquire further, If I want to reset the orderId after the position is completely closed what query would I use for that. In essence, both the profit order and the stop order would have to be closed and the position would have to be flat?
                      Thanks for your help.
                      DaveN
                      In your Strategy, you have this:
                      Code:
                      				// If the strategy has terminated reset the strategy id 
                      ...
                      Reset the orderId in the same place.
                      Last edited by koganam; 09-21-2014, 01:32 PM.

                      Comment


                        #12
                        I commented out the sections that reset the strategy, compiled and rerun the strategy on market replay. No change in behavior at all. The strategy continues to add positions to trades already open, so the check for market position flat does not work at all. Is there an example anywhere of checking a strategy-launched Atm, to see if the market positions is flat, then re-enabling the strategy. I can find nothing in the forum that does this.
                        Thanks
                        DaveN

                        Comment


                          #13
                          Originally posted by daven View Post
                          I commented out the sections that reset the strategy, compiled and rerun the strategy on market replay. No change in behavior at all. The strategy continues to add positions to trades already open, so the check for market position flat does not work at all. Is there an example anywhere of checking a strategy-launched Atm, to see if the market positions is flat, then re-enabling the strategy. I can find nothing in the forum that does this.
                          Thanks
                          DaveN
                          Just use some Print()s to check if the position ever goes flat.
                          Code:
                          if (GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat) Print("Position is flat");
                          else if (GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Long) Print("Position is long");
                          else if (GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Short) Print("Position is short");
                          else Print("Position is unknown");
                          If it ever comes back "unknown", we know that we have a serious problem. Otherwise, we shall still know what gives anyway.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by mjairg, 07-20-2023, 11:57 PM
                          3 responses
                          213 views
                          1 like
                          Last Post PaulMohn  
                          Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                          4 responses
                          544 views
                          0 likes
                          Last Post PaulMohn  
                          Started by GLFX005, Today, 03:23 AM
                          0 responses
                          3 views
                          0 likes
                          Last Post GLFX005
                          by GLFX005
                           
                          Started by XXtrader, Yesterday, 11:30 PM
                          2 responses
                          12 views
                          0 likes
                          Last Post XXtrader  
                          Started by Waxavi, Today, 02:10 AM
                          0 responses
                          7 views
                          0 likes
                          Last Post Waxavi
                          by Waxavi
                           
                          Working...
                          X