Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need an example for time based stop

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

    Need an example for time based stop

    I am having difficulties to add a time based stop loss strategy, for example, I have codes below.
    what it does is that to exit the position if the price did not reach my profit target(6%) after 10 bars(bus days).
    The problem is that once this profit target get triggered, my 10 bar stop code would not know that I am no longer in a long position. so I want to know how can I get SetProfitTarget() to update variable hasPosition, or how to find out i am no longer in the postion...I tied iPosition and IOrder but i couldnt get them work...
    or if there are smarter way to do this. thank you.

    Code example:
    =======================================
    protected override void Initialize()
    {
    SetProfitTarget(CalculationMethod.Percent, 0.06);
    }

    protected override void OnBarUpdate()
    { private bool hasPosition=0;
    if(MyIndicator==true)
    { EnterLong(0, "myIndicatorLong", 1);
    hasPosition=1;
    }


    If(BarssinceEntry()>10&&hasPosition!=0)
    {
    ExitLong("myIndicatorLong", 1);
    hasPosition=0;
    }
    }
    ========================================

    #2
    Hello lang0477,

    Thank you for your post.

    The Position.MarketPosition would be used. For example:
    Code:
    If(BarssinceEntry()>10&&Position.MarketPosition == MarkterPosition.Long)
    For information on Position please visit the following link: http://www.ninjatrader.com/support/h...etposition.htm

    Comment


      #3
      Thanks for reply, I actually had Position.MarketPosition != MarketPosition.Flat but i couldnt get it working correctly. it will pass compiling but throw an error msg in the output window when back test the strategy. and the strategy holds the first long positin until the end.

      and if i remove "&& Position.MarketPosition != MarketPosition.Flat"
      it will correctly exit the long after 10 days but since it doesnt know about the positon, it prints thousand of text from DrawText().
      the codes are blow. Position is declaried in Variables as iPosition.



      protected override void Initialize()
      {
      CalculateOnBarClose = true;
      AccountSize = 10000;
      Add(PAMBullishEngulfing());
      SetProfitTarget("bullishEngulf",CalculationMode.Pe rcent,0.06);
      enterPrice=0;
      }
      protected override void OnBarUpdate()
      {
      //[CandleStick]Bullish Engulfing
      if (PAMBullishEngulfing().IsBullishingEngulfing[0])
      {
      EnterLong(0,1,"bullishEngulf");
      enterPrice=Open[0];
      }



      //time stop
      if (BarsSinceEntry(0,"bullishEngulf",0)>10 && Position.MarketPosition != MarketPosition.Flat)
      {
      ExitLong("bullishEngulf");
      DrawText("ExitLong"+CurrentBar,"10 Day SL", 0, High[0]+1,Color.Blue);

      }

      }

      Comment


        #4
        Hello lang0477,

        Thank you for your response.

        What is the error you are seeing?

        Can you provide the full code for the strategy?

        Comment


          #5
          The error in the output window is this

          **NT** Error on calling 'OnBarUpdate' method for strategy 'MasterStrategy/adb8a8fab5854840a103eb9c0f33dc3a': Object reference not set to an instance of an object.

          fulll code below, tks

          namespace NinjaTrader.Strategy
          {

          [Description("Enter the description of your strategy here")]
          public class MasterStrategy : Strategy
          {
          #region Variables
          private IPosition Position= null;
          private double enterPrice;

          #endregion

          protected override void Initialize()
          {
          CalculateOnBarClose = true;
          AccountSize = 10000;
          Add(PAMBullishEngulfing());
          SetProfitTarget("bullishEngulf",CalculationMode.Pe rcent,0.06);
          enterPrice=0;
          }

          protected override void OnBarUpdate()
          {


          if (PAMBullishEngulfing().IsBullishingEngulfing[0])
          {


          EnterLong(0,1,"bullishEngulf");
          enterPrice=Open[0];

          }


          else if ((BarsSinceEntry(0,"bullishEngulf",0)>10) && (Position.MarketPosition != MarketPosition.Flat))
          {
          ExitLong("bullishEngulf");
          DrawText("ExitLong"+CurrentBar,"10 Day SL", 0, High[0]+1,Color.Blue);

          }

          }

          #region Properties
          #endregion
          }
          }

          Comment


            #6
            Hello,

            This is because you have defined an object with a reserved word for NinjaScript -
            private IPosition Position= null;

            I would change the name of this object to a different name or use a lower case word instead.

            Let me know if I can be of further assistance.
            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