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

Enabling a Builder Strategy with wrapping ChartControl != null to pass values

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

    Enabling a Builder Strategy with wrapping ChartControl != null to pass values

    Click image for larger version

Name:	NinjaTraderTrend.png
Views:	43
Size:	259.4 KB
ID:	1200914

    I have to wrap this line of code: if (ChartControl != null)

    around OnBarUpdate() section of code to Enable checkbox to stay on and enabled for Strategy Builder. All the code except the vertical lines is a template that was prebuilt. I added an indicator for the trend strength which shows vertical lines and the Colored Label on the upper right side of the chart. I can't get the vertical lines to be read by StrategyBuilder using a value of TrendBreak that I named. How can I get TrendBreak to work to pass the value to Strategy Builder as I have used this way of passing values to Strategy Builder previously, but those aren't using a template, where as I'm adding an indicator that only strength and weakness vertical lines and a Trend label like this is?



    public class StackedEMAs : Indicator
    {

    private Series<double> trendBreak;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {

    AddPlot(new Stroke(Brushes.Transparent, 2), PlotStyle.Bar, "TrendBreak");
    }
    else if (State == State.DataLoaded)
    {
    trendBreak = new Series<double>(this);
    }
    }

    // OnBarUpdate code

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;


    if (ChartControl != null)
    {
    EMA8 = EMA(8)[0];
    EMA5 = EMA(5)[0];

    SNS = EMA5 < EMA8

    if (SNS) // Negative
    {
    trendBreak[0] = -1;
    }

    // Properties

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> TrendBreak
    {
    get { return trendBreak; }
    }


    #2
    Hello Tonofit,

    For indicators to be used as signals in the builder you have to have the indicator plot a value. if it is using drawing objects that won't be able to be detected in the builder.

    The code you added if (ChartControl != null) would not be needed for the strategy its self, if one of the indicators you are using requires a chart that may be what the reason is for needing that code. If thats the case then the indicator needs to be fixed and add that code in so the strategy does not require it.

    JesseNinjaTrader Customer Service

    Comment


      #3
      I'm using multiple EMA values and the realtionship's they have to each other to indicate strength and weakness of a trend and drawing a marker such as a vertical line or arrow for instance. I'm trying to use trendBreak as a plot. How should a plot be done with EMA's to show this relationship to use by Strategy Builder to signal a long or a short?

      public class StackedEMAs : Indicator
      {
      private Series<double> trendBreak;

      private double EMA13;
      private double EMA8;
      private double EMA5;

      private bool SPS;
      private bool SNS;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"";
      Name = "StackedEMAs";
      Calculate = Calculate.OnBarClose;
      IsOverlay = false;
      DisplayInDataBox = false;
      DrawOnPricePanel = true;
      DrawHorizontalGridLines = false;
      ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
      IsSuspendedWhileInactive = true;

      else if (State == State.DataLoaded)
      {
      trendBreak = new Series<double>(this);
      }


      AddPlot(new Stroke(Brushes.Transparent, 2), PlotStyle.Bar, "Lock");
      AddPlot(Brushes.Transparent, "TrendBreak");


      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 1)
      return;

      EMA13 = EMA(13)[0];
      EMA8 = EMA(8)[0];
      EMA5 = EMA(5)[0];

      SPS = EMA5 > EMA8 && EMA8 > EMA13;
      SNS = EMA5 < EMA8 && EMA8 < EMA13;


      if (SNS) // Negative Strong
      {
      if (Lock[1] != -1)
      {
      Draw.VerticalLine(this, "TrendDownStrong" + CurrentBar, 0, Brushes.Red);
      trendBreak[0] = -1;
      }
      //PlotBrushes[0][0] = TransparentBrush;
      Lock[0] = -1;
      }
      else if (SPS) // Positive Strong
      {
      if (Lock[1] != 1)
      {
      Draw.VerticalLine(this, "TrendUpStrong" + CurrentBar, 0, Brushes.Green);
      trendBreak[0] = 1;
      }
      //PlotBrushes[0][0] = TransparentBrush;
      Lock[0] = 1;

      }


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

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

      Comment


        #4
        Hello Tonofit,

        From what you have shown that appears to be correct for making a signal. The TrendBreak plot could be set to values of -1,0,1 to denote different states. The strategy could then read that value as a signal using a condition.

        You would need to use the capitalized TrendBreak for setting the plot.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse,

          Thanks for taking a look at this...

          I've attached a chart and the indicator StrategyEMAs export zip file. I'm not findiing the problem with enable Strategy Builder still occurs.

          If I enable the Builder Strategy without my StackedEMA's indicator code having If (ChartControl != null) wrapped around inside the function OnBarUpdate() and the indicator code still won't stay enabled for the strategy. I'm interested to see how this will work for Trend Strength, Weakness. I made a 1 Min MNQ chart and added my indicator and tried it without using the more complex template and the same results of momentary enable and then a disable of the checkbox for strategy still happens.

          Strategy using StackedEMAs indicator code

          protected override void OnBarUpdate()
          {
          if (BarsInProgress != 0)
          return;

          if (CurrentBars[0] < 1)
          return;

          // Set 1
          if ((StackedEMAs1.TrendBreak[0] == 1)
          || (StackedEMAs1.TrendBreak[0] == 2))
          {
          EnterLong(Convert.ToInt32(DefaultQuantity), "");
          }

          // Set 2
          if ((StackedEMAs1.TrendBreak[0] == -1)
          || (StackedEMAs1.TrendBreak[0] == -2))
          {
          EnterShort(Convert.ToInt32(DefaultQuantity), "");
          }

          }


          Attached Files

          Comment


            #6
            Hello Tonofit,

            I missed from your last message is that you are not setting a plot, you made a variable. You need a plot to use the Values collection:

            Code:
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> TrendBreak
            {
            //get { return Values[0]; }
            get { return trendBreak; }
            }
            needs to be

            Code:
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> TrendBreak
            {
            get { return Values[1]; }
            }
            The [1] is because the AddPlot with TrendBreak is the second AddPlot, that makes the count of the index for this plot 1 and the other addplot is 0.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hi Jesse,

              I revised the code to get it to work now. I used if (ChartControl != null) and wrapped the code for the label in the upper right corner of the chart as isn't part of the plot. The plot I used trendbreak within TrendBreak so Values[1] looked correct but it worked the way I did it.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by AdeptistJune, Today, 10:00 PM
              0 responses
              2 views
              0 likes
              Last Post AdeptistJune  
              Started by Genesis2021, Today, 04:13 PM
              1 response
              25 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by tonystarks, Today, 05:00 PM
              2 responses
              10 views
              0 likes
              Last Post tonystarks  
              Started by aleksov_dimitar, Today, 04:10 PM
              0 responses
              10 views
              0 likes
              Last Post aleksov_dimitar  
              Started by benjamin$, Today, 03:55 PM
              0 responses
              28 views
              0 likes
              Last Post benjamin$  
              Working...
              X