• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stochastics issue on mutiple time frame

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

    Stochastics issue on mutiple time frame

    Hi Nanja expert,

    I am trying to set up a new indicator based on Stochastics values on 5 mins and 10 mins data.

    I am setting 5 minutes as primary and adding 10 minutes data by using:

    AddDataSeries(Data.BarsPeriodType.Minute, 10);

    The logic is as following:
    1. If on 5 minutes, Stochastics K and D both >= 80, then kd = kd + 5;
    1. If on 5 minutes, Stochastics K and D both <= 20, then kd = kd - 5;
    1. If on 10 minutes, Stochastics K and D both >= 80, then kd = kd + 10;
    1. If on 10 minutes, Stochastics K and D both >= 80, then kd = kd - 10;

    To use the primary array, am I supposed to use BarsArray[0]? For the secondary data, it should be BarsArray[1]??


    The code I have does not plot anything.

    public class KDJ : Indicator
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Mark K + D";
    Name = "KDJ";
    Calculate = Calculate.OnEachTick;
    IsOverlay = false;
    DisplayInDataBox = false;
    DrawOnPricePanel = false;
    DrawHorizontalGridLines = false;
    DrawVerticalGridLines = false;
    PaintPriceMarkers = false;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    AddLine(Brushes.Orange, 0.5, "Zero");
    AddPlot(new Stroke(Brushes.Orange, 2), PlotStyle.Bar, "KD");
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 10);


    }
    }

    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.
    double kd = 0;

    //For the 5 minutes data
    if (Stochastics(BarsArray[0],3, 9, 3).D[0]>=80 & Stochastics(BarsArray[0],3, 9, 3).K[0]>=80)
    {
    kd = kd + 5;
    }
    else if (Stochastics(BarsArray[0],3, 9, 3).D[0]>=80 & Stochastics(BarsArray[0],3, 9, 3).K[0]>=80)
    {
    kd= kd - 5;
    }
    else{
    kd = 0;
    }

    //For the 10 minutes data
    if (Stochastics(BarsArray[1],3, 9, 3).D[0]>=80 & Stochastics(BarsArray[1],3, 9, 3).K[0]>=80)
    {
    kd = kd + 10;
    }
    else if (Stochastics(BarsArray[1],3, 9, 3).D[0]>=80 & Stochastics(BarsArray[1],3, 9, 3).K[0]>=80)
    {
    kd= kd - 10;
    }
    else{
    kd = 0;
    }


    KD[0] = kd;

    }

    #2
    Hello jnsong,

    Thanks for your post and welcome to the forums!

    When you are working with a script and it does not seem to perform, a general guideline is to check the "log" tab of the control center for any error messages related to the script.

    In looking at your script, I would expect the indicator to error out on the first bar of data loaded because you are not checking to ensure enough bars are loaded before trying to access them. For your understanding, when you apply the script it will start processing the very first bar of data you have so if on the first bar you are referring to any type of previous bar (that does not exist) you would get an error. While you are not refering to a previous bar you do have an added data series which may not have loaded its first bar.

    A common way to prevent these type errors is to check if the CurrentBars[n] is greater than some value where [n] is the bars array (you have two, 0 = chart bars and 1 = added data series), for example:

    if (CurrentBars[0] < 1 || CurrentBars[1] < 1) return;// do not process until 1 bar of each series available.

    Another point of this is to consider the accuracy of the calculations if only 1 bar of data is loaded how accurate will a stochastics(3,9,3) be? If you need immediate accuracy then you should prevent checking until perhaps 10 bars have loaded.

    For reference, please review: https://ninjatrader.com/support/help...urrentbars.htm

    I would also recommend a review of this section as you may want to segment your code to work only in BarsInProgress = 0: https://ninjatrader.com/support/help...nstruments.htm

    At a later time, once the indicator is working as you want, please review the "Performance practices" in https://ninjatrader.com/support/help..._practices.htm to better write your scripts when referencing indicators. What you have will work but uses more resources than need be.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul,

      Thanks a lot for your reply. With your hint, I was able to read the log file; I did not realize there was one. I was able to get the indicator plotted. Still reading the two links you shared with me. I really appreciate your help.

      Regards,
      Jason

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by tonynt, Today, 01:20 AM
      0 responses
      5 views
      0 likes
      Last Post tonynt
      by tonynt
       
      Started by hemlaguza, Today, 12:19 AM
      0 responses
      2 views
      0 likes
      Last Post hemlaguza  
      Started by sdauteuil, Yesterday, 11:02 PM
      1 response
      3 views
      0 likes
      Last Post sdauteuil  
      Started by Jschmelz, Yesterday, 04:45 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Marco  
      Started by sisenberg, Yesterday, 08:06 PM
      1 response
      11 views
      0 likes
      Last Post sisenberg  
      Working...
      X