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

How to modify my indicator to calculate multiple Data Series

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

    How to modify my indicator to calculate multiple Data Series

    I have developed the following indicator, and it works just fine when applied to an instrument. I would like to adapt it to run the same calculations on 2-4 different instruments, add them together and divide by the number of instruments to give me an average value across the different instruments. I know how to AddDataSeries in State.Configure. I just cannot seem to wrap my mind around how to get the indicator to run the calculations on each instrument separately so I can then average them into a single value.

    Or is there a way to reference this indicator applied to a specific instrument within another indicator that can then average them? Thanks.


    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class NetVolDeltaPercent : Indicator
    {
    private double buys;
    private double sells;
    private int activeBar = 0;
    private Series<double> Buys;
    private Series<double> TotalVol;
    private Series<double> CumNet;
    private Series<double> CumVol;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Cumulative Net Vol Delta as a Percent of Total Vol.";
    Name = "Net Vol Delta Percent";
    Calculate = Calculate.OnEachTick;
    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 = false;
    Periods = 25;
    AddPlot(Brushes.Gainsboro, "Plot1");
    AddPlot(Brushes.DarkGray, "Plot2");
    }
    else if (State == State.Historical)
    {
    if (Calculate != Calculate.OnEachTick)
    {
    Draw.TextFixed(this, "NinjaScriptInfo", string.Format(NinjaTrader.Custom.Resource.NinjaScr iptOnBarCloseError, Name), TextPosition.BottomRight);
    Log(string.Format(NinjaTrader.Custom.Resource.Ninj aScriptOnBarCloseError, Name), LogLevel.Error);
    }
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    Buys = new Series<double>(this);
    TotalVol = new Series<double>(this);
    CumNet = new Series<double>(this);
    CumVol = new Series<double>(this);
    }
    }
    
    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if(e.MarketDataType == MarketDataType.Last)
    {
    if(e.Price >= e.Ask)
    buys += (Instrument.MasterInstrument.InstrumentType == Cbi.InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume(e.Volume) : e.Volume);
    else if (e.Price <= e.Bid)
    sells += (Instrument.MasterInstrument.InstrumentType == Cbi.InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume(e.Volume) : e.Volume);
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (CurrentBar < activeBar || CurrentBar <= BarsRequiredToPlot)
    return;
    
    if (CurrentBar != activeBar)
    {
    Buys[1] = buys - sells;
    TotalVol[1] = buys + sells;
    buys = 0;
    sells = 0;
    activeBar = CurrentBar;
    }
    
    Buys[0] = buys - sells;
    TotalVol[0] = buys + sells;
    
    if (Bars.BarsSinceNewTradingDay == 1)
    {
    CumNet[0] = Buys[1];
    CumVol[0] = TotalVol[1];
    }
    
    else if (Bars.BarsSinceNewTradingDay >= 1)
    {
    CumNet[0] = CumNet[1] + Buys[1];
    CumVol[0] = CumVol[1] + TotalVol[1];
    }
    
    if (Bars.BarsSinceNewTradingDay > 50)
    {
    Plot1[0] = ((CumNet[0] / CumVol[0]) * 100);
    Plot2[0] = 0;
    }
    }

    #2
    Hello HarvOS,

    The most simple way to code this would be to use the strategy builder initially to generate the code, you can then use the generated code in your indicator. It would also be best to make a second indicator for the purpose of adding the data series and combining the values, that leaves your original indicator code in its most simple state so you can easily change or debug it later. The steps to do that would be the following:
    1. Open the builder and create a new strategy
    2. Use the additional data tab to add the other instruments
    3. Create a condition in set 1 for each of the 4 indicators.
      1. For example create a first condition using your NetVolDeltaPercent indicator and leave the default settings.
      2. Create a second condition for the NetVolDeltaPercent indicator again but for its input series click where it says Default input and select the second instrument.
      3. Repeat this for each of the other instruments until you have 4 conditions in the set.
      4. The condition can be as simple as NetVolDeltaPercent equals misc -> numeric value, this is just to help generate the indicators code.
    4. After doing this you can click View Code

    You should see the 3 AddDataSeries statements in State.Configure and then 4 lines of code in State.SetDefaults which configures the indicators to the correct series. In OnBarUpdate you can then view the code used in the conditions you made which will show how to access each of the 4 indicators data. At that point you can copy the generated code into a new indicator. The main parts you would need to copy into a new indicator would be the private variables for your indicators, the AddDataSeries statements, the code in State.DataLoaded. You can then use the indicator variables in OnBarUpdate to access the various instruments data.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello HarvOS,

      The most simple way to code this would be to use the strategy builder initially to generate the code, you can then use the generated code in your indicator. It would also be best to make a second indicator for the purpose of adding the data series and combining the values, that leaves your original indicator code in its most simple state so you can easily change or debug it later. The steps to do that would be the following:
      1. Open the builder and create a new strategy
      2. Use the additional data tab to add the other instruments
      3. Create a condition in set 1 for each of the 4 indicators.
        1. For example create a first condition using your NetVolDeltaPercent indicator and leave the default settings.
        2. Create a second condition for the NetVolDeltaPercent indicator again but for its input series click where it says Default input and select the second instrument.
        3. Repeat this for each of the other instruments until you have 4 conditions in the set.
        4. The condition can be as simple as NetVolDeltaPercent equals misc -> numeric value, this is just to help generate the indicators code.
      4. After doing this you can click View Code

      You should see the 3 AddDataSeries statements in State.Configure and then 4 lines of code in State.SetDefaults which configures the indicators to the correct series. In OnBarUpdate you can then view the code used in the conditions you made which will show how to access each of the 4 indicators data. At that point you can copy the generated code into a new indicator. The main parts you would need to copy into a new indicator would be the private variables for your indicators, the AddDataSeries statements, the code in State.DataLoaded. You can then use the indicator variables in OnBarUpdate to access the various instruments data.
      That was extremely helpful! Works. Thank you very much for your help!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by fx.practic, 10-15-2013, 12:53 AM
      5 responses
      5,404 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by Shai Samuel, 07-02-2022, 02:46 PM
      4 responses
      95 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by DJ888, Yesterday, 10:57 PM
      0 responses
      8 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by MacDad, 02-25-2024, 11:48 PM
      7 responses
      160 views
      0 likes
      Last Post loganjarosz123  
      Started by Belfortbucks, Yesterday, 09:29 PM
      0 responses
      9 views
      0 likes
      Last Post Belfortbucks  
      Working...
      X