Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

how to set ATR as stop loss?

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

    #31
    Hello Dolfan,

    Thank you for writing in.

    What steps have you already taken in debugging your code? http://ninjatrader.com/support/forum...58&postcount=1

    What are your expected results? What are you seeing when debugging? What is the particular code you are using that is producing the unexpected result?
    Zachary G.NinjaTrader Customer Service

    Comment


      #32
      I have managed to remove all of the errors in the compile process but I still have a strategy that enters no short positions and fails to hit any stops where I would like. The intention is for the ATR to be measured at every bar close and the stop to move ((ATR * 2) ticks) from that close. In a long position it would be (close price - (ATR*2)) = new stop. For a long position it would be (close price + (ATR*2)) = new stop. Why is it not reading my stops? The syntax is included below for you reference. Thanks for your assistance.

      Best regards,

      Dolfan

      protected override void Initialize()
      {
      Add(HMA(8));
      Add(SMA(30));
      Add(ATR(14));
      Add(HMA(8));
      Add(SMA(30));
      Add(ATR(14));
      Add(ATR(10));
      SetStopLoss("", CalculationMode.Price, Variable0, false);

      EntriesPerDirection = 5;
      EntryHandling = EntryHandling.AllEntries;

      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if ((CrossAbove(HMA(8), EMA(30), 1))
      && ATR(14)[0] > .112);
      {
      EnterLong(1, "");
      Variable0 = CurrentBar - 2* ATR(10)[0];
      }

      // Condition set 2
      if ((CrossBelow(HMA(8), EMA(30), 1))
      && ATR(14)[0] > .112);
      {
      EnterShort(1, "");
      Variable0 = CurrentBar + 2*ATR(10)[0];
      }

      // Condition set 3
      if (Position.MarketPosition == MarketPosition.Long
      && CrossAbove(Close, Position.AvgPrice + 25 * TickSize, 1))
      {
      EnterLong(1, "");
      Variable0 = CurrentBar - 2* ATR(10)[0];
      }

      // Condition set 4
      if (Position.MarketPosition == MarketPosition.Short
      && CrossBelow(Close, Position.AvgPrice - 25 * TickSize, 1))
      {
      EnterShort(1, "");
      Variable0 = CurrentBar + 2*ATR(10)[0];
      }

      Comment


        #33
        Hello Dolfan,

        If you wish to refer to the current close price, you will want to be using Close[0]. CurrentBar, which is what you are using in your code, is going to return the index value of the current bar.

        For more information about Close, please take a look at this help guide link: https://ninjatrader.com/support/help...nt7/?close.htm

        For more information about CurrentBar, please take a look at this help guide link: https://ninjatrader.com/support/help...currentbar.htm

        Additionally, the stop will not dynamically move if you have only set it in the Initialize() method. If you wish for the stop to move to a new value, you will need to call it in OnBarUpdate().

        Please take a look at the help guide link for SetStopLoss() for more information about using this method call: https://ninjatrader.com/support/help...etstoploss.htm

        Additionally, we have a reference sample on our support forum for modifying the price of stop loss and profit target orders: http://ninjatrader.com/support/forum...ead.php?t=3222
        Zachary G.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by pibrew, Today, 06:37 AM
        0 responses
        0 views
        0 likes
        Last Post pibrew
        by pibrew
         
        Started by rbeckmann05, Yesterday, 06:48 PM
        1 response
        12 views
        0 likes
        Last Post bltdavid  
        Started by llanqui, Today, 03:53 AM
        0 responses
        6 views
        0 likes
        Last Post llanqui
        by llanqui
         
        Started by burtoninlondon, Today, 12:38 AM
        0 responses
        10 views
        0 likes
        Last Post burtoninlondon  
        Started by AaronKoRn, Yesterday, 09:49 PM
        0 responses
        15 views
        0 likes
        Last Post AaronKoRn  
        Working...
        X