Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Impossible exits from strategy

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

    Impossible exits from strategy

    when I show the chart for my strategy I'm seeing strange exit positions from my trades. i've included a picture for reference and my code is as follows:
    Notice from the image that the exits occur below the market. how is this possible?


    protected override void Initialize()
    {
    Add(PeriodType.Minute, timeframe2);
    Add(PeriodType.Minute, timeframe3);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (Rising(SMA(BarsArray[1],50)) == true
    && MACD(BarsArray[2],12, 26, 9).Avg[0] < MddLowVal
    && Rising(MACD(BarsArray[2],12, 26, 9).Diff) == true
    && CUMRSI(2,3)[0] < rsiLow)
    {
    EnterLong(DefaultQuantity, "Buy");
    }

    // Condition set 2
    if (Falling(SMA(BarsArray[2],50)) == true)
    {
    ExitLong("Sell", "Buy");
    }
    }

    #region Properties
    [Description("RSI High threshold")]
    [GridCategory("Parameters")]
    public int RsiHigh
    {
    get { return rsiHigh; }
    set { rsiHigh = Math.Max(1, value); }
    }

    [Description("RSI Low Threshold")]
    [GridCategory("Parameters")]
    public int RsiLow
    {
    get { return rsiLow; }
    set { rsiLow = Math.Max(1, value); }
    }

    [Description("Second timeframe")]
    [GridCategory("Parameters")]
    public int Timeframe2
    {
    get { return timeframe2; }
    set { timeframe2 = Math.Max(10, value); }
    }

    [Description("Third timeframe")]
    [GridCategory("Parameters")]
    public int Timeframe3
    {
    get { return timeframe3; }
    set { timeframe3 = Math.Max(60, value); }
    }

    [Description("MACD High Value limit")]
    [GridCategory("Parameters")]
    public int MddHighVal
    {
    get { return mddHighVal; }
    set { mddHighVal = Math.Max(1, value); }
    }

    [Description("MACD Low Value limit")]
    [GridCategory("Parameters")]
    public int MddLowVal
    {
    get { return mddLowVal; }
    set { mddLowVal = Math.Max(1, value); }
    }

    [Description("Swing indicator default parameter")]
    [GridCategory("Parameters")]
    public int SwingStrength
    {
    get { return swingStrength; }
    set { swingStrength = Math.Max(1, value); }
    }
    #endregion
    }
    }
    Attached Files

    #2
    Hello,

    Thank you for providing the code on this.

    This could potentially be that you are executing your logic in all of the BarsInProcess indexes instead of one.

    I would recommend looking over this document : http://www.ninjatrader.com/support/h...tsub=barsInpro

    The OnBarUpdate method is called for all BarsInProcess indexes so in your current script, the 5 minute chart will call OnBarUpdate every 5 minutes.
    The two series you have added will also call OnBarUpdate every X minutes or what they are set to.
    There is more information on this here: http://www.ninjatrader.com/support/h...nstruments.htm

    To correct this you will need to determine which BarsInProgress index you want to use for the logic here.

    If the script is only supposed to submit the order every 5 minutes or your base chart time, you would need to surround the current Entry statement with the following check:

    if (BarsInProgress == 0)
    {

    }

    Otherwise if this was only to be executed in the second added series it would be index 1, for the third series index 2 etc..

    Because you do not currently know when the order will take place this can cause what you are seeing.


    I look forward to being of further assistance.

    Comment


      #3
      Ok I see. i've seen that code before where I could say:

      if (BarsinProgress != 0)
      return;

      this way it ignores the other timeframes and only processes code in the default chart timeframe. or...as you have in your example

      if (BarsinProgress == 0)
      {
      run some code;
      }

      Either could work I suppose. thanks for the reference also. it must get very old covering the same old BarsinProgress issue that people have. I'm a little embarrased that i couldn't have figured it out after seeing this issue for myself in the past.

      thanks again!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      81 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      41 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      64 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      66 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      54 views
      0 likes
      Last Post CarlTrading  
      Working...
      X