Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with example of adding atm to a Strat. Looking for example.

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

    Help with example of adding atm to a Strat. Looking for example.

    Howdy.

    I'm looking to add a simple atm to a strategy.

    The strategy is simple(long example), upon some condition, enter a trade at market. At that time I would like an OCO with a profit target of a fixed tics. And an emergency stop a fixed tics that would always be there just in case a ways off.

    After my entry occurs...... and price moves a fixed distance in my favor, I'd like to adjust that stop to BE+1.

    At any point, if price Closes below an indicator, I'd like to exit the position immediately.

    I'm attaching a pic that should explain this, and some script.

    There is something missing, as my atm isn't executing the way it should(sometimes), mainly the either of the stops I have in place, my hard stop, or my stop below the indicator.

    "SpanB" is just the indicator I am using to exit and enter a trade btw. "inTrade" is the flag I'm setting.
    Code:
    protected override void OnBarUpdate()
            {
    			
    
    			
    			[COLOR="SeaGreen"]//Entry Condition[/COLOR]
    			if (CrossAbove(Close, spanB, 5) && Close[0] > spanB[0] && High[1] < spanB[1])
    			{
    				EnterLong(DefaultQuantity, "Long1");
    				//Print("Hello");
    				inTrade = true;  [COLOR="SeaGreen"]//we are in a trade[/COLOR]
    			}
    			
    			if(inTrade == true)  [COLOR="SeaGreen"]//checks if in a trade[/COLOR]
    			{       
                                   [COLOR="SeaGreen"] //Catastrophe stop[/COLOR]
    				SetStopLoss("Long1", CalculationMode.Ticks, Position.AvgPrice - 16* TickSize, false);
    				if(Close[0] < spanB[0])   [COLOR="SeaGreen"]//If at anytime after entry price closes below indicator, exit trade[/COLOR]
                                    {
    					ExitLong("", "Long1");
    					inTrade = false;   [COLOR="SeaGreen"]//just exited so set flag to false[/COLOR]
    				}
    			}
    
    			[COLOR="SeaGreen"]//If price moves in my favor a set # of tics, adjust initial stop to BE+2[/COLOR]
    			if (High[0] >= (Position.AvgPrice + 8* TickSize) && inTrade == true)
    			{
    				SetStopLoss("Long1", CalculationMode.Price, Position.AvgPrice + 2* TickSize, false);
    			}
    			
                            [COLOR="SeaGreen"]//checks to see if we are flat or target was hit.  If so, set flag back to false[/COLOR].
    			if (Position.MarketPosition == MarketPosition.Flat)
    				inTrade = false;
    			
            }
    Attached Files
    Last edited by forrestang; 07-31-2011, 09:25 PM.

    #2
    Forrestang, what is the exact error you're running into here?
    AustinNinjaTrader Customer Service

    Comment


      #3
      Howdy Austin. I'm not getting an error in terms of compiling or running it.

      The problem is that at times my stops aren't being executed. I must have my logic or my flag implemented in a bad way?

      So my Fixed Profit Target Exit, will always be a set number, so I put that in the 'Initialize' section.

      My STOP Exits, can occur on 3 possible conditions. So I put the 'setStopLoss' in the 'onBarUpdate()' section.

      The 3 possible stops are:
      1. Upon Entry, a fixed catastrophe stop a set number of tics from entry
      2. Price moves a fixed number of tics in my favor, then MOVES to BE+2 tics
      3. At any time after entry, if price CLOSES below my indicator, EXIT the trade

      Comment


        #4
        Hi forrestang, if you find your stops being not executed / ignored, please rerun your strategy with TraceOrders enabled, so you would get detailed order debug info shown in your output window.

        Comment


          #5
          Thanks for the help fellas.

          I think I got it now.

          I went with this, slight modification from my original by pulling the flag out of my entry loop:

          Code:
          #region Variables
          private bool buyCase = true;
           #endregion
          
          protected override void Initialize()
                  {
                      CalculateOnBarClose = true;
          	    TraceOrders      = true;
                      SetProfitTarget(CalculationMode.Ticks, 16);
                   }
          
          protected override void OnBarUpdate()
                  {
          
          		if (buyCase == true)
          		{
          			SetStopLoss("Long1", CalculationMode.Price, Position.AvgPrice - 16* TickSize, true);
          			buyCase = false;
          		}
          
          		if (Close[0] >= (Position.AvgPrice + 8* TickSize) && buyCase == false)
          		{
          			SetStopLoss("Long1", CalculationMode.Price, Position.AvgPrice + 2* TickSize, true);
          		}
          			
          		if (Close[0]< spanB[0] && buyCase ==  false)
          		{
          			ExitLong("ExitLong1", "Long1");
          		}
          						
          			
          		if (Position.MarketPosition == MarketPosition.Flat)
          				buyCase = true;  //clear flag
                  }
          Last edited by forrestang; 08-01-2011, 09:52 AM.

          Comment


            #6
            Ok... I have a slight problem. I'm using this to reset my flag:
            Code:
            			//Reset your bool flag when flat. 
            			if (Position.MarketPosition == MarketPosition.Flat)
            				sellCase = true;
            I duplicated the script, and flipped all of the values(for trades in the opposite direction), but the problem is above, as the strat may be in a short trade, and then trigger a long trade, so I would have a net 2 positions. Once it goes down to evaluate this to reset the flag, the strat thinks it may still be in a long position even due to the opposite direction being in a trade, and it resets my bool.

            Is there a way around this?
            Last edited by forrestang; 08-01-2011, 10:41 PM.

            Comment


              #7
              Since I tag my entry with a name upon entry, and so are the exits..... there must be some way to just check if that unique trade placed is still in the market?

              Comment


                #8
                forrestang, it would be advantageous working with the IOrder objects then for you, since you could check the detailed states / properties for each making up your full position - http://www.ninjatrader.com/support/h...nt7/iorder.htm

                This will give more granular control in this area.

                Comment


                  #9
                  I have yet to look into IOrder object.

                  But I think it might be executing the trades I want.

                  I have a different question about my stop loss. At one point I move my stop to BE+2. I would expect the trade to exit immediately once price touches that stop loss point. But it actually doesn't trigger till price moves through it one tic.

                  Is there a way to change it so that it actually executes like a typical sell stop? Or would I just be better served by actually setting my stoploss order to BE+3 if I want it to perform like a BE+2?

                  Code:
                  			//BUY LOGIC BELOW-------------------------------------------------------------
                  			if (Close[0]>spanB[0] && Close[1] > spanB[1] && Close[2] <= spanB[2])
                  			{
                  				EnterLong(DefaultQuantity, "Long1");
                  			}
                  						
                  			if (buyCase)
                  			{
                  				SetStopLoss("Long1", CalculationMode.Price, Position.AvgPrice - initialStop* TickSize, true);
                  				buyCase = false;
                  			}
                  
                  			if (High[0] >= (Position.AvgPrice + beMove* TickSize) && !buyCase)
                  			{
                  				SetStopLoss("Long1", CalculationMode.Price, [COLOR="DarkOrange"]Position.AvgPrice + 2* TickSize[/COLOR], true);
                  			}
                  			
                  			if (Close[0]< spanB[0] && !buyCase)
                  			{
                  				ExitLong("ExitLong1", "Long1");
                  			}
                  						
                  			//Reset your bool flag when flat. 
                  			if (Position.MarketPosition == MarketPosition.Flat )
                  				buyCase = true;

                  Comment


                    #10
                    forrestang, for that case try working with a non simulated stoploss, last parameter set to 'false' instead of 'true'.

                    Comment


                      #11
                      Would it be possible to just create two strategies? One for short, and one for long, so that it will execute the way that it should?

                      Comment


                        #12
                        You could go that route as well, but then they would not be able to cross communicate about the positions per default, I'm not sure if you would need or not, they would run independently then.

                        Comment


                          #13
                          Originally posted by NinjaTrader_Bertrand View Post
                          You could go that route as well, but then they would not be able to cross communicate about the positions per default, I'm not sure if you would need or not, they would run independently then.
                          Great! That probably would work. I'll give that a try until I can figure out how to get both the long and short side to work concurrently.

                          Comment


                            #14
                            Just an update, I got it working!

                            Previously, I was resetting my bool at the END of onBarUpdate(). I moved this and made it check at the beginning, and now it works fine.

                            So both long and short are working properly in one script.

                            Woot!

                            Comment


                              #15
                              This thing is driving me crazy!!!

                              Ok guys, this script is driving me nutz! I just can't seem to figure this out. I now have the long and short sides working fine.

                              Now the problem is, that when the strategy takes a short, IT WILL NOT move my stop to BE+2. And it ONLY does this for the short side. If I comment out the long side completely, and only attempt to execute the short side, it STILL does not work.

                              But EVERYTHING works for the long side, using the exact same code, but with different variables and operands of course.

                              This is just a simple strat, trying to get the stops to work. I'm using a moving average to try to figure out how to get the stops to work.

                              Here are the rules for the strategy (short example):
                              1. If price crosses below an ema, go short
                              2. At time of the short, enter a stop of 28 tics
                              3. At time of short, enter profit target of 16 tics
                              4. If price moves 8 tics in my favor, move stop to BE+2
                              5. If at anytime price closes back below ema, exit trade immidiately


                              Im attaching the script, and some examples. Any help would be greatly appreciated!
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              647 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              369 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              108 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              572 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              573 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X