• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using ATR in determining Take Profit and Stop values

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

    Using ATR in determining Take Profit and Stop values

    Hi -
    When entering trades for the Russell eminis, I would like to use the ATR at the time of execution as a factor in determining the take profit and stop loss values (in ticks). For instance, if the ATR is 2.0 (or 2 points/20 ticks), I would like my take profit to be 20 ticks times 2, and the Stop loss at 20 ticks (20 times 1). (I'm assuming this can't be done in the Strategy Wizard).

    Many thanks!

    #2
    Hello bonwitt,

    Thanks for your post and welcome to the forums!

    Correct, regrettably you would need to set this up in Ninjascript.

    Here is an example where the stop and profit target are calculated from 2 * ATR(14)[0] and converted to an integer value of ticks:

    int profitTarget = Convert.ToInt32(Math.Round( ATR(14)[0] * 2 / TickSize));

    int stopTarget = Convert.ToInt32(Math.Round( ATR(14)[0] * 2 / TickSize));

    The (14) indicates the example is using a 14 period ATR. The index value of [0] means use the current ATR value. You could always change (or eliminate) the 2 * multiplier to some other value or even a variable that you could set externally as needed.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Paul View Post
      Hello bonwitt,

      Thanks for your post and welcome to the forums!

      Correct, regrettably you would need to set this up in Ninjascript.

      Here is an example where the stop and profit target are calculated from 2 * ATR(14)[0] and converted to an integer value of ticks:

      int profitTarget = Convert.ToInt32(Math.Round( ATR(14)[0] * 2 / TickSize));

      int stopTarget = Convert.ToInt32(Math.Round( ATR(14)[0] * 2 / TickSize));

      The (14) indicates the example is using a 14 period ATR. The index value of [0] means use the current ATR value. You could always change (or eliminate) the 2 * multiplier to some other value or even a variable that you could set externally as needed.
      Does this go in Variables area? private int....?
      Thanks

      Comment


        #4
        Hello TOOLMachine462,

        The example shows a complete statement of declaration and assignment and can be used in the OnBarUpdate() method. The int variables profitTarget and stopTarget are later used in SetProfitTarget() and SetStopLoss() methods.

        You can certainly declare the ints in the variable section if you prefer, for example:
        private int profitTarget = 0;
        private int stopLoss = 0;
        but you would not be able to assign the ATR value until the OnBarUpdate()

        profitTarget = Convert.ToInt32(Math.Round( ATR(14)[0] * 2 / TickSize));
        stopLoss = Convert.ToInt32(Math.Round( ATR(14)[0] * 2 / TickSize));

        SetStopLoss (CalculationMode.Ticks, stopLoss);
        SetProfitTarget (CalculationMode.Ticks, profitTarget);

        // enter order
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thanks but I'm still not getting it right. Please see code below. I am trying to set initial stop via current ATR value. Then I want it to move to break-even +1 tick after so much profit. Do I need to reset stop? Do I need to add names of executions? Is my +1 done correctly? Thanks!

          Code:
          #region Stoploss reset & breakeven
          			// Resets the stop loss to the original value when all positions are closed
          			if (Position.MarketPosition == MarketPosition.Flat)
          				{
          				SetStopLoss("SWING LONG",CalculationMode.Ticks, StopLoss);
          				SetStopLoss("SWING SHORT",CalculationMode.Ticks, StopLoss);
          				}
          			
          			//Set ATR Stop in ticks
          				
          				int stopLoss = Convert.ToInt32(Math.Round( ATR(14)[0] * 2 / TickSize));
          				
          			// If a long position is open, allow for stop loss modification to breakeven
          			else if (Position.MarketPosition == MarketPosition.Long)
          				{
          				// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
          				if (Close[0] > Position.AvgPrice + BETrigger * TickSize)
          					{
          					SetStopLoss(CalculationMode.Price, Position.AvgPrice + 1*TickSize);
          					}
          				}
          				#endregion
          Last edited by TOOLMachine462; 05-06-2016, 09:57 PM.

          Comment


            #6
            Hello TOOLMachine462,

            Thanks for your post.

            If you use signal names in your SetStopLoss() then that stop loss will only apply to an entry with that same signal name. When using a signal name in theSetStopLoss() you will need to use the correct overload for SetStopLoss which is: SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool simulated) (see: http://ninjatrader.com/support/helpGuides/nt7/?setstoploss.htm

            You have the right idea to set the stop loss when flat but I would suggest that you calculate the stoploss variable prior to setting the stop loss:

            Code:
            if (Position.MarketPosition == MarketPosition.Flat)
            				{
                                            int stopLoss = Convert.ToInt32(Math.Round( ATR(14)[0] * 2 / TickSize));
            				SetStopLoss("SWING LONG",CalculationMode.Ticks, StopLoss, false);
            				SetStopLoss("SWING SHORT",CalculationMode.Ticks, StopLoss, false);
            				}
            Note that by declaring the variable int stopLoss inside the if condition, the variable will only be valid inside the {} of the if condition. If you will be using the stopLoss variable elsewhere in your strategy it would be better to declare it in the region variables so that it is accessible in all the methods of your strategy. Declaring only means that you are identifying it as an integer. the use of the "=" assigns a specific value.

            The adjustment to breakeven looks correct but would apply to an entry that is not already tied to the other signal named SetStopLoss() , "SWING LONG" "SWING SHORT".
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Ok thanks! I noticed that about the names while I was reviewing and waiting for your reply. Does the following look correct now?

              Code:
              protected override void OnBarUpdate()
                      {
              			#region Stoploss reset, ATR Stop set, & Breakeven (BE)
              			// Resets the stop loss to the original value when all positions are closed
              			if (Position.MarketPosition == MarketPosition.Flat)
              			{
              				//Set initial stop - ATR Stop in ticks
              				int StopLoss = Convert.ToInt32(Math.Round( ATR(ATRStopPeriod)[0] * ATRStopMulti / TickSize));
              				SetStopLoss("SWING LONG",CalculationMode.Ticks, StopLoss, false);
              				SetStopLoss("SWING SHORT",CalculationMode.Ticks, StopLoss, false);
              				
              			}
              			
              			// If a long position is open, allow for stop loss modification to breakeven
              			else if (Position.MarketPosition == MarketPosition.Long)
              			{
              				// Once the price is greater than entry price +BETrigger ticks, set stop loss to breakeven + 1
              				if (Close[0] > Position.AvgPrice + BETrigger * TickSize)
              					{
              					SetStopLoss("Swing Long", CalculationMode.Price, Position.AvgPrice + 1 * TickSize, false);
              					}
              			}
              				
              			// If a short position is open, allow for stop loss modification to breakeven
              			else if (Position.MarketPosition == MarketPosition.Short)
              			{
              				// Once the price is greater than entry price +BETrigger ticks, set stop loss to breakeven + 1
              				if (Close[0] < Position.AvgPrice - BETrigger * TickSize)
              					{
              					SetStopLoss("Swing Short", CalculationMode.Price, Position.AvgPrice - 1 * TickSize, false);
              					}
              			}
              			#endregion

              Comment


                #8
                Hello TOOLMachine462,

                Thanks for your reply.

                Yes that looks good.
                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by shmarola, Today, 01:01 AM
                0 responses
                1 view
                0 likes
                Last Post shmarola  
                Started by CriticalMind, 11-29-2015, 12:39 PM
                3 responses
                1,321 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by novel168, 06-14-2019, 09:20 PM
                1 response
                7 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by johnnybegoode, Yesterday, 08:00 PM
                3 responses
                9 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by digibob, Yesterday, 03:00 PM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Working...
                X