Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop Loss based on Low / High

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

    Stop Loss based on Low / High

    Hi to all,

    Presently I have a system with the Stop Loss set up on the Strategy Wizard and as a percentage loss...therefore I have in:
    • #region Variables
    private double stop_Loss = 0.005; // Default setting for Stop_Loss
    • protected override void Initialize()
    SetStopLoss("", CalculationMode.Percent, Stop_Loss, true);
    • #region Properties
    [Description("Stop Loss")]
    [GridCategory("Parameters")]
    public double Stop_Loss
    {
    get { return stop_Loss; }
    set { stop_Loss = Math.Max(0.0025, value); }
    }

    Because I am back-testing this strategy on several currencies, with different volatilities, I need to have the stop loss with a bigger distance from the market in more volatile currencies.

    To achieve that would like to change the Stop Loss so that, when a position is Open it:
    1. Takes the Low/High of the last 5 bars
    2. Subtracts or adds the ATR(50)

    Therefore, for example in a long position, the Stop Loss would be set as:
    MIN(Low, 5)[0] - ATR(50)[0]

    and stay at that level until the position is closed.

    I have little programming knowledge and don't how how to change the code properly.

    If you could provide the changes I need to do and indicate were on the code I need to paste the new code would be very appreciated.


    many thanks in advance
    Paulo Ribeiro

    #2
    Hi Paulo, I will have our NinjaScript trainee take a look at your request and then we'll be back in touch.

    Comment


      #3
      Hi Paulo,

      I was able to go through and get a sample of the coding to work, attached in a zip file.

      to import NinjaScript file -

      1.From the Control Center window select the menu File > Utilities > Import NinjaScript to open the Import NinjaScript dialog window
      2.Select the file you want to import
      3.Press the "Open" button

      When calling an Indicator and checking past bars you need a variable to have the ATR(50), in this case, set in a Round2TickSize. This will ensure that when doing the calculations for the ATR, it will round the number based on the Instruments Tick size so we don’t have conflicts with the price in the code.

      The first step is creating a user defined variable, from the wizard
      • Name – ATRRounded
      • Type – Double
      • Value – 0

      After you have entered in the variable information you will need to Unlock the code. Note that when you unlock the code you cannot go back to the Wizard.

      In the code, under ‘protected override void OnBarUpdate()’you will need three main Conditions and want them to check for if the position is long or short or flat.

      The first one is checking if your position is flat and resets the StopLoss.

      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss("", CalculationMode.Percent, StopLoss, false);
      }

      The next one sets the condition of what position you are currently in, this case a long, and changes the stop loss according to those parameters of the ATR.

      else if (Position.MarketPosition == MarketPosition.Long)
      {
      aTRRounded = Instrument.MasterInstrument.Round2TickSize(ATR(50)[0]);
      SetStopLoss(MIN(Low, 5)[0] - aTRRounded);
      }

      The short position condition will check if you are in a short position and instead of MIN(Low,5)[0] – aTRRounded, you would change that to a MAX and a ‘+’.

      There is also a statement,

      if(historical)
      return;

      this will allow for real time trading. To enable to do Back Testing, you can delete the statement or comment it out using // in front of the two lines.

      Please let me know if I can be of further assistance
      Attached Files
      Last edited by NinjaTrader_CalH; 03-20-2013, 10:18 AM.
      Cal H.NinjaTrader Customer Service

      Comment


        #4
        Hi Cal,

        many thanks for your reply

        Just 2 additional questions:

        • In your code attached you have:
        if (Position.MarketPosition == MarketPosition.Flat)

        {

        SetStopLoss("", CalculationMode.Percent, StopLoss, false);
        EnterLong();



        The EnterLong(); is needed here? If yes, will not open positions without respecting my other rules to go Long?


        • Will the Stop Level stay the same in the following bars update?
        While the position is open and MAX / MIN and ATR formulas change value the Stop Level will stay with the initial price (as desired) or will update and change at every new Bar?

        Kind Regards
        Paulo

        Comment


          #5
          HI Paulo,

          The EnterLong() is used to let you see the strategy is actually working. You can take it out and put a different parameter here if you want. The EnterLong() can be removed if you wish.

          The StopLoss will change on Bar Update. Each time a new bar is processed the Stop Loss will adjust based on the 50 period ATR.

          Keep in mind that what I sent you was just meant to be an example of the code and how it works.

          I will attach a link to the online Help Guide that gives some tutorials on NinjaScript and how to develop Strategies.

          http://www.ninjatrader.com/support/h...strategies.htm

          If you are also interested in learning NinjaScript we do offer some Premium Education classes that run about 6 hours on the weekends and will teach you the fundamentals and Beginner level Scripting.

          http://www.ninjatrader.com/PremiumEducation.php
          Cal H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          648 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