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

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.

    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by JoMoon2024, Today, 06:56 AM
    0 responses
    6 views
    0 likes
    Last Post JoMoon2024  
    Started by Haiasi, 04-25-2024, 06:53 PM
    2 responses
    19 views
    0 likes
    Last Post Massinisa  
    Started by Creamers, Today, 05:32 AM
    0 responses
    6 views
    0 likes
    Last Post Creamers  
    Started by Segwin, 05-07-2018, 02:15 PM
    12 responses
    1,786 views
    0 likes
    Last Post Leafcutter  
    Started by poplagelu, Today, 05:00 AM
    0 responses
    3 views
    0 likes
    Last Post poplagelu  
    Working...
    X