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

Plotting a chart or alert

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

    Plotting a chart or alert

    Hi all - I have been researching over the last few days and I have not been able to find some examples of how to set a variable to a specific number and if that is true set another variable to 0 or 1. After I have that, then I would like to create a 0 or 1 plot as an indicator. Below is the code I have so far. I have been using other languages and platforms to code, but this is my first time in NinjaScript, help will be appreciated. Or even if you can point our any mistakes. So, far my NinjaScripts compiles but when I add my indicator to the chart it doesn't plot anything.

    {
    public class AlertValueLong : Indicator
    {
    private EMA ValueFast;
    private EMA ValueSlow;
    private EMA Avg;
    private Stochastics StoValues;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Provides an alert on when we have reached the bottom of a bearish run.";
    Name = "AlertValueLong";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    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;
    FastLength = 12;
    SlowLength = 26;
    MACDLength = 9;
    KPeriod = 10;
    DPeriod = 10;
    RSILength = 50;

    AddPlot(Brushes.DarkCyan, "EntryLX");

    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    ValueFast = EMA(Close, Convert.ToInt32(FastLength));
    ValueSlow = EMA(Close, Convert.ToInt32(SlowLength));
    Avg = EMA(Value, Convert.ToInt32(MACDLength));
    StoValues = Stochastics(DPeriod, KPeriod, 3);

    }
    }

    protected override void OnBarUpdate()
    {
    if (
    (StoValues.K[1] < StoValues.D[1]) &&
    (StoValues.K[0] > StoValues.D[0]) &&
    (StoValues.K[1] < 70 || StoValues.K[2] < 60|| StoValues.K[3] < 30) &&
    ((ValueFast[1] - ValueSlow[1] - Avg[1]) > (ValueFast[2] - ValueSlow[2] - Avg[2])) &&
    ((ValueFast[0] - ValueSlow[0]) < Avg[0] || (ValueFast[1] - ValueSlow[1]) < Avg[1] || (ValueFast[2] - ValueSlow[2]) < Avg[2]) &&
    ((ValueFast[0] - ValueSlow[0]) < 0 || (ValueFast[1] - ValueSlow[1]) < 0 || (ValueFast[2] - ValueSlow[2]) < 0))
    {
    Data[0] = 1;
    }

    else
    {
    Data[0] = 0;
    }



    if (RSI(RSILength, 3)[0] < 20)
    {
    RSIEntry[0] = 1;
    }
    else
    {
    RSIEntry[0] = 0;
    }

    if (Data == 1 && RSIEntry == 1)
    {
    EntryLX[0] = 1;
    }
    else
    {
    EntryLX[0] = 0;
    }
    }

    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="FastLength", Order=1, GroupName="Parameters")]
    public int FastLength
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="SlowLength", Order=3, GroupName="Parameters")]
    public int SlowLength
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="MACDLength", Order=4, GroupName="Parameters")]
    public int MACDLength
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="OverBought", Order=5, GroupName="Parameters")]
    public int OverBought
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="OverSold", Order=6, GroupName="Parameters")]
    public int OverSold
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="KPeriod", Order=7, GroupName="Parameters")]
    public int KPeriod
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="DPeriod", Order=8, GroupName="Parameters")]
    public int DPeriod
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="RSILength", Order=9, GroupName="Parameters")]
    public int RSILength
    { get; set; }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> RSIEntry
    {
    get { return Values[0]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Data
    {
    get { return Values[0]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> EntryLX
    {
    get { return Values[0]; }
    }

    #endregion
    }
    }

    Please let me know if other information from the coding is needed. Again I am definitely a newbie with NinjaScript.

    #2
    Hello felixolmo,

    Thanks for your post and welcome to the NinjaTrader forums!

    It looks like you are using Values[0] in three different outputs of RSIEntry, Data, and EntryLX. As you have added a plot called EntryLX, that is the one that should be associated with Values[0] and is the actual plot output.

    I would suggest that instead of trying to use a series for Data and RSIentry that you change those to be simple bool variables that are either true or false.

    private bool RSIEntry, Data; // place this with your other variable declarations
    .
    .
    .
    {
    Data = true; // set bool true
    }

    else
    {
    Data = false;
    }



    if (RSI(RSILength, 3)[0] < 20)
    {
    RSIEntry = true;
    }
    else
    {
    RSIEntry = false;
    }

    if (Data == true && RSIEntry == true) // you can also write this like: if (Data && RSIEntry) which also requires them to both be true
    {
    EntryLX[0] = 1; // set the plot to a value of 1
    }
    else
    {
    EntryLX[0] = 0;
    // set the plot to a value of 0
    }
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thank you Paul. I updated some things in teh code. Im still not succesful in plotting the 0 or 1 line. See below updates I made. Let me know if you can help further...

      {
      public class AlertValueLong : Indicator
      {
      private EMA ValueFast;
      private EMA ValueSlow;
      private EMA Avg;
      private Stochastics StoValues;
      private bool RSIEntry, Data;
      private MACD MACDVAD;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Provides an alert on when we have reached the bottom of a bearish run.";
      Name = "AlertValueLong";
      Calculate = Calculate.OnBarClose;
      IsOverlay = false;
      DisplayInDataBox = true;
      DrawOnPricePanel = true;
      DrawHorizontalGridLines = true;
      DrawVerticalGridLines = true;
      PaintPriceMarkers = true;
      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;
      FastLength = 12;
      SlowLength = 26;
      MACDLength = 9;
      KPeriod = 10;
      DPeriod = 10;
      RSILength = 50;
      AddPlot(Brushes.DarkCyan, "EntryLX");

      }
      else if (State == State.Configure)
      {
      }
      else if (State == State.DataLoaded)
      {

      MACDVAD = MACD(FastLength, SlowLength, MACDLength);
      StoValues = Stochastics(DPeriod, KPeriod, 3);

      }
      }

      protected override void OnBarUpdate()
      {
      if (
      (StoValues.K[1] < StoValues.D[1]) &&
      (StoValues.K[0] > StoValues.D[0]) &&
      (StoValues.K[1] < 70 || StoValues.K[2] < 60 || StoValues.K[3] < 30) &&
      (MACDVAD.Diff[1] > MACDVAD.Diff[2]) &&
      (MACDVAD[0] < MACDVAD.Avg[0] || MACDVAD[1] < MACDVAD.Avg[1] || MACDVAD[2] < MACDVAD.Avg[2]) &&
      (MACDVAD[0] < 0 || MACDVAD[1] < 0 || MACDVAD[2] < 0))
      {
      Data = true;
      }

      else
      {
      Data = false;
      }



      if (RSI(RSILength, 3)[0] < 20)
      {
      RSIEntry = true;
      }
      else
      {
      RSIEntry = false;
      }

      if (Data && RSIEntry)
      {
      EntryLX[0] = 1;
      }
      else
      {
      EntryLX[0] = 0;
      }
      }

      #region Properties
      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="FastLength", Order=1, GroupName="Parameters")]
      public int FastLength
      { get; set; }

      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="SlowLength", Order=3, GroupName="Parameters")]
      public int SlowLength
      { get; set; }

      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="MACDLength", Order=4, GroupName="Parameters")]
      public int MACDLength
      { get; set; }

      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="KPeriod", Order=7, GroupName="Parameters")]
      public int KPeriod
      { get; set; }

      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="DPeriod", Order=8, GroupName="Parameters")]
      public int DPeriod
      { get; set; }

      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="RSILength", Order=9, GroupName="Parameters")]
      public int RSILength
      { get; set; }

      [Browsable(false)]
      [XmlIgnore]
      public Series<double> EntryLX
      {
      get { return Values[0]; }
      }

      #endregion
      }
      }

      Comment


        #4
        Hello felixolmo,

        Thanks for your reply.

        What you have appears correct.

        I suggest that you use Print statements to debug your strategy to find out why it is not plotting as expected.

        Start by printing the values used in the conditions that will set the bools and print the state of the bools themselves for all your conditions that affect the ultimate plot value.

        For example: Print (Time[0]+" RSI value: "+RSI(RSILength, 3)[0]+" RSIEntry bool is: "+RSIEntry);

        Prior to applying the script to the chart, open the New>Ninjascript output window to view the print output.

        Here is a link to further debugging tips: https://ninjatrader.com/support/help...script_cod.htm

        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by cmv727, Today, 05:38 AM
        0 responses
        12 views
        0 likes
        Last Post cmv727
        by cmv727
         
        Started by AlessioCianini12, Today, 01:38 AM
        0 responses
        33 views
        0 likes
        Last Post AlessioCianini12  
        Started by nandhumca, Yesterday, 11:54 PM
        0 responses
        29 views
        0 likes
        Last Post nandhumca  
        Started by nandhumca, Yesterday, 11:51 PM
        0 responses
        28 views
        0 likes
        Last Post nandhumca  
        Started by ezrollin, Yesterday, 11:48 PM
        2 responses
        31 views
        0 likes
        Last Post ezrollin  
        Working...
        X