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

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 bmartz, 03-12-2024, 06:12 AM
                4 responses
                32 views
                0 likes
                Last Post bmartz
                by bmartz
                 
                Started by Aviram Y, Today, 05:29 AM
                4 responses
                12 views
                0 likes
                Last Post Aviram Y  
                Started by algospoke, 04-17-2024, 06:40 PM
                3 responses
                28 views
                0 likes
                Last Post NinjaTrader_Jesse  
                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