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

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.
    JesseNinjaTrader Customer Service

    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 cupir2, Yesterday, 07:44 PM
      3 responses
      21 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by reynoldsn, Yesterday, 07:26 PM
      2 responses
      14 views
      0 likes
      Last Post reynoldsn  
      Started by MartinT, 05-17-2023, 06:00 AM
      18 responses
      173 views
      0 likes
      Last Post flybuzz
      by flybuzz
       
      Started by sgordet, Today, 05:24 AM
      2 responses
      20 views
      0 likes
      Last Post sgordet
      by sgordet
       
      Started by Board game geek, Today, 02:20 AM
      1 response
      15 views
      0 likes
      Last Post NinjaTrader_Erick  
      Working...
      X