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

keltner channel with additional properties

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

    keltner channel with additional properties

    I'm programming some additional properties on a keltner channel, which I'd like to see. I have managed to place in the lower right corner a box with the value of the width of the channel. the outermost channel has an offset of 3.5 and the inner one an offset of 1.75. I have tried to make the internal channel only show according to a condition, but from what I have read it is not possible to condition an AddPlot.

    using crossabove and crossbelow within if and else statements, when the price reaches a certain level of the external channel, is it possible?

    what I have achieved so far:

    Code:
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionKeltnerChannel;
    Name = "KeltnerChannelEMAwColorAndHalfline";
    Period = 52;
    IsOverlay = true;
    IsSuspendedWhileInactive = true;
    OffsetMultiplier = 3.5;
    RegionBrush = Brushes.WhiteSmoke;
    Opacity = 20;
    
    AddPlot(Brushes.White, NinjaTrader.Custom.Resource.KeltnerChannelMidline) ;
    AddPlot(Brushes.White, NinjaTrader.Custom.Resource.NinjaScriptIndicatorUp per);
    AddPlot(Brushes.White, NinjaTrader.Custom.Resource.NinjaScriptIndicatorLo wer);
    AddPlot(new Stroke(Brushes.Black, DashStyleHelper.Dash, 1, 70), PlotStyle.Hash, "UpperHalfline");
    AddPlot(new Stroke(Brushes.Black, DashStyleHelper.Dash, 1, 70), PlotStyle.Hash, "LowerHalfline");
    }
    else if (State == State.DataLoaded)
    {
    diff = new Series<double>(this);
    EMADiff = EMA(diff, Period);
    EMATypical = EMA(Typical, Period);
    }
    }
    
    protected override void OnBarUpdate()
    {
    diff[0] = High[0] - Low[0];
    
    double middle = EMATypical[0];
    double offset = EMADiff[0] * OffsetMultiplier;
    double offset1 = EMADiff[0] * OffsetMultiplier*0.5;
    
    double upper = middle + offset;
    double lower = middle - offset;
    double upperhalfline = middle + offset1;
    double lowerhalfline = middle - offset1;
    double bspread = upper - lower;
    
    Midline[0] = middle;
    Upper[0] = upper;
    Lower[0] = lower;
    Values[3][0] = upperhalfline;
    Values[4][0] = lowerhalfline;
    
    Draw.Region(this, "KeltnerChannelwColor", CurrentBar, 0, Upper, Lower, null, RegionBrush, Opacity, 0);
    // Draws text Band Spread in the lower right corner of panel 1
    Draw.TextFixed(this, "tag1", "B. Sp "+bspread.ToString("N2"), TextPosition.BottomRight, Brushes.Black, new NinjaTrader.Gui.Tools.SimpleFont ("Arial", 11), Brushes.Black, Brushes.Silver, 10);
    }
    one of the attached images shows the progress of my work and the next image my goal
    Last edited by barriosmex; 05-22-2022, 05:29 AM. Reason: Wrap [CODE] tags around code

    #2

    hi barriosmex

    maybe taking a look at the Plots objects page is what you where missing ?
    https://ninjatrader.com/fr/support/h...nt8/?plots.htm

    in your case maybe something like :
    Code:
    Midline[0] = middle;
    Upper[0] = upper;
    Lower[0] = lower;
    Values[3][0] = upperhalfline;
    Values[4][0] = lowerhalfline;
    
    
    if (condition of your liking)
    {
    Plots[3].Brush = Brushes.myColora;
    Plots[4].Brush = Brushes.myColorb;
    }
    else
    {
    Plots[3].Brush = Brushes.myColorc;
    Plots[4].Brush = Brushes.myColord;
    }
    edit :
    maybe have a try with PlotBrushes too see if it is more appropriate..
    Last edited by Amedeus; 05-21-2022, 01:26 PM.

    Comment


      #3
      Originally posted by Amedeus View Post
      hi barriosmex

      maybe taking a look at the Plots objects page is what you where missing ?
      https://ninjatrader.com/fr/support/h...nt8/?plots.htm

      in your case maybe something like :
      Code:
      Midline[0] = middle;
      Upper[0] = upper;
      Lower[0] = lower;
      Values[3][0] = upperhalfline;
      Values[4][0] = lowerhalfline;
      
      
      if (condition of your liking)
      {
      Plots[3].Brush = Brushes.myColora;
      Plots[4].Brush = Brushes.myColorb;
      }
      else
      {
      Plots[3].Brush = Brushes.myColorc;
      Plots[4].Brush = Brushes.myColord;
      }
      edit :
      maybe have a try with PlotBrushes too see if it is more appropriate..
      https://ninjatrader.com/fr/support/h...lotbrushes.htm
      Thank you!. Plot Brushes worked

      I place the following conditions with flags to alternate the use of dotted bands

      Code:
       if(!upperflag && High[0] >= Upper[0])
      {
      upperflag = true;
      lowerflag = false;
      }
      
      if(upperflag && !lowerflag && Low[0] <= Lower[0])
      {
      lowerflag = true;
      upperflag = false;
      }
      
      if(lowerflag)
      PlotBrushes[4][0] = Brushes.Transparent;
      if(upperflag)
      PlotBrushes[3][0] = Brushes.Transparent;
      I wonder if the implementation seems right to you? my knowledge in programming is very basic

      Click image for larger version

Name:	_  2022_05_19 (07_16_38).png
Views:	128
Size:	198.2 KB
ID:	1202371
      Last edited by barriosmex; 05-22-2022, 05:27 AM. Reason: add image

      Comment


        #4
        hi barriosmex,

        I wonder if the implementation seems right to you? my knowledge in programming is very basic
        as long as it works for you... it looks ok to me, but i am just a beginner as well.

        I've found PlotBrushes to be kinda demanding performance wise in the past.
        maybe have a look at the utilization monitor when your instrument is awake.. (output > right ckck)

        if you have one transparent plots anyway, maybe having one plot only ? beeing given different values depending on your flags could avoid the use of PlotBrushes ?
        I would test something like, or similar to :
        Code:
        if(lowerflag)
        Values[3][0] = upperhalfline;
        if(upperflag)
        Values[3][0] = lowerhalfline;

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, 04-16-2024, 06:09 PM
        4 responses
        12 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by terofs, Today, 04:18 PM
        0 responses
        5 views
        0 likes
        Last Post terofs
        by terofs
         
        Started by nandhumca, Today, 03:41 PM
        0 responses
        5 views
        0 likes
        Last Post nandhumca  
        Started by The_Sec, Today, 03:37 PM
        0 responses
        3 views
        0 likes
        Last Post The_Sec
        by The_Sec
         
        Started by GwFutures1988, Today, 02:48 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Working...
        X