Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can't make the AddPlot function to work

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

    Can't make the AddPlot function to work

    Hello There, I am trying to create a strategy that has 3 indicators and output of Indicator 1 is input to indicators 2 & 3. I also want to plot these indicators on the chart in a panel below the main chart panel. I've read the documentation and other similar questions on the forum, but I can't make this to work. Following is the code that I've come up with so far :-



    Code:
    using NinjaTrader.Cbi;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.NinjaScript.Strategies;
    using System;
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using NinjaTrader.NinjaScript.Indicators;
    using System.Threading.Tasks;
    using System.Windows.Media;
    using System.ComponentModel;
    using System.Xml.Serialization;
    using NinjaTrader.Gui;
    
    namespace NinjaTrader.Custom.Strategies.Udemy
    {
    internal class SampleStrat : Strategy
    {
    private const int rsi_period = 9;
    private const int vwma_period = 21;
    private const int ema_period = 3;
    
    private Indicator _rsi;
    private Indicator _vwma;
    private Indicator _ema;
    
    [NinjaScriptProperty]
    [Display(Name = "Trace My Orders", GroupName = "Parameters", Order = 3)]
    public bool TraceMyOrders { get; set; }
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    SetDefaults();
    SetParameters();
    }
    else if (State == State.Configure)
    {
    ClearOutputWindow();
    TraceOrders = TraceMyOrders;
    CreateIndicators();
    }
    else if (State == State.Terminated)
    {
    
    }
    }
    
    private void CreateIndicators()
    {
    _rsi = RSI(Close, rsi_period, 3);
    _vwma = VWMA(_rsi, vwma_period);
    _ema = EMA(_rsi, ema_period);
    }
    
    private void SetParameters()
    {
    TraceMyOrders = false;
    }
    
    private void SetDefaults()
    {
    Name = "SampleStrat";
    Calculate = Calculate.OnBarClose;
    TraceOrders = false;
    StartBehavior = StartBehavior.WaitUntilFlat;
    IsExitOnSessionCloseStrategy = false;
    BarsRequiredToTrade = 65;
    BarsRequiredToPlot = 65;
    IsOverlay = false;
    
    AddPlot(new Stroke(Brushes.White), PlotStyle.Line, "rsiPlot");
    AddPlot(new Stroke(Brushes.Red), PlotStyle.Line, "vwmaPlot");
    //AddPlot(new Stroke(Brushes.Blue, DashStyleHelper.Solid, 25, 150), PlotStyle.Line, "ema");
    }
    
    protected override void OnBarUpdate()
    {
    rsiPlot[0] = RSI(Close, rsi_period, 3)[0];
    vwmaPlot[0] = VWMA((RSI(Close, rsi_period, 3)), vwma_period)[0];
    //ema[0] = 3;
    }
    
    [HASHTAG="t3322"]region[/HASHTAG] Properties
    
    //Properties
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> rsiPlot
    {
    get { return Values[0]; }
    }
    
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> vwmaPlot
    {
    get { return Values[0]; }
    }
    
    #endregion
    }
    }​

    #2
    Hello xs2abhishek,

    One problem is that both of your public properties reference Values[0], each public property needs to use the index for the plot you want it to target. [0] is the first plot and [1] would be the second plot.

    If you are not seeing any value plotted you may need to add a Print into OnBarUpdate to output the values you are trying to plot to see what they are.

    Additionally if you have added the AddPlot statements you will also need to re apply the strategy after doing that. remove it from the chart and then re apply it.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    54 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    72 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X