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

Strategy [0] doesn't return correct indicator value

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

    Strategy [0] doesn't return correct indicator value

    I am writing a custom strategy and have a custom indicator based on EMA so I can get the current building bar's value of the EMA.

    I am using an AddDataSeries, but I am filtering out events that don't match what is on the chart.

    I am using the Playback

    Here are my results printed in the output window
    Ind High: 2687.25 // Correct
    Ind Value: 2687.12467370361 // Correct
    Inst: ES 03-19 Globex // Correct
    Str BarCount: 330 // Correct
    Str High: 2687.25 // Correct
    Str aaEMAHigh0: 2686.52547760308 //WRONG, should be 2687.12467370361

    The Indicator (Ind) is displaying the correct values.
    This is the High of the current bar being passed in to the indicator and these values are being displayed from the indicator
    NinjaTrader.Code.Output.Process("Ind High: " + Input[0].ToString(), PrintTo.OutputTab1);
    The indicator value is correct when being displayed from the indictor into the Output window. The value also correctly shows on the chart:
    NinjaTrader.Code.Output.Process("Ind Value: " + dsEMA[0].ToString(), PrintTo.OutputTab1);

    In the Strategy, High or the current bar is correct and matches the value from the indicator.
    But the Strategy Value of the EMA is incorrect.

    Code:
    Indicator:
    OnBarUpdate (set to Calculate.OnEachTick)

    if (BarsInProgress == 1 ) return; // Using this to make sure I'm not having any conflicts.
    Values[0][0] = (CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period)) + (1 - (2.0 / (1 + Period))) * Values[0][1]);
    dsEMA[0] = (CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period)) + (1 - (2.0 / (1 + Period))) * dsEMA[1]);
    NinjaTrader.Code.Output.Process("Ind High: " + Input[0].ToString(), PrintTo.OutputTab1);
    NinjaTrader.Code.Output.Process("Ind Value: " + dsEMA[0].ToString(), PrintTo.OutputTab1);

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


    Strategy Code
    Set to Calculate.OnBarClose // I have tried to set this to OnEachTick and move the code into OnBarUpdate, but it still doesn't work
    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if(e.MarketDataType != MarketDataType.Last) {return;}
    if (e.Instrument.ToString().StartsWith("ES") ) {} else {return;} // Just making sure I'm using the ES values.
    NinjaTrader.Code.Output.Process("Inst: " + e.Instrument.ToString(), PrintTo.OutputTab1);

    // High[0] doesn't work, it shows the last bars
    try
    {
    if (e.Last &gt; currentBarsHigh[0]) { currentBarsHigh[0] = e.Last; }
    }
    catch { currentBarsHigh[0] = e.Last; }
    try
    {
    if (e.Last &lt; currentBarsLow[0]) { currentBarsLow[0] = e.Last; }
    }
    catch { currentBarsLow[0] = e.Last; }

    NinjaTrader.Code.Output.Process("Str BarCount: " + iBar.ToString(), PrintTo.OutputTab1);
    NinjaTrader.Code.Output.Process("Str High: " + currentBarsHigh[0].ToString(), PrintTo.OutputTab1);
    WHY DOESN'T THIS WORK!
    NinjaTrader.Code.Output.Process("Str aaEMAHigh0: " + aaEMA3(currentBarsHigh, 3).EMAValue[0].ToString(), PrintTo.OutputTab1);


    Any help would be greatly appreciate.

    #2
    Hello TradingDreamer,

    It may help to simplify the issue..

    Are you using a secondary series in the indicator?

    If so, below is a link to an example of this.
    https://ninjatrader.com/support/foru...96#post1059996

    Are you comparing the prints from the indicator called by the strategy with the prints from the indicator as it is directly added to a chart?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I got this to work. I had an issue in the strategy calculating the building bar high and bar low in the OnMarketData event. I had to reset the value for new bars! Then I was able to adjust the EMA custom code and add an OnMarketData event that calculates the current building bar values. I added an input parameter called BarsBack and I subtract that from the Input[0 - (-1 * intBarsBack)] to get the correct calculation.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by usazencort, Today, 01:16 AM
      0 responses
      1 view
      0 likes
      Last Post usazencort  
      Started by kaywai, 09-01-2023, 08:44 PM
      5 responses
      603 views
      0 likes
      Last Post NinjaTrader_Jason  
      Started by xiinteractive, 04-09-2024, 08:08 AM
      6 responses
      23 views
      0 likes
      Last Post xiinteractive  
      Started by Pattontje, Yesterday, 02:10 PM
      2 responses
      22 views
      0 likes
      Last Post Pattontje  
      Started by flybuzz, 04-21-2024, 04:07 PM
      17 responses
      230 views
      0 likes
      Last Post TradingLoss  
      Working...
      X