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

Indicator doesn't use updated DataSeries values

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

    Indicator doesn't use updated DataSeries values

    Hi,
    I am having trouble with a DataSeries Object that holds Close prices. I hope that somebody can help.

    In a strategy, I Set the prices in each OnBarUpdate() . At various times I perform a While loop which adjusts and resets the most current price in the DataSeries Object. The purpose of the loop is to adjust the price until an indicator which is based on the price changes to a known value. Once that happens I then have the price at which the indicator will have the specified value in the future. I intend to use the resultant price as the trigger point for a future trade.

    I can correctly extract the prices while in the strategy. The problem occurs when I call an indicator using the prices in the DataSeries Object. It appears that the indicator uses only the first price entered for the current bar and not any of the subsequent prices that replace it, so returning the same values on every call in the loop.

    The following code illustrates the problem.
    #region Variables
    private DataSeries closePrice;
    #endregion

    protected override void Initialize()
    {
    closePrice = new DataSeries(this);
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] <= BarsRequired) return;
    closePrice.Set(Close[0]);
    if (CurrentBar != 10) return; // Only do the test for bar 10

    Print(closePrice[0].ToString("0.0000")+", EMA value = "+
    EMA(closePrice,5)[0].ToString()); // Use a simple EMA as example

    closePrice.Set(Close[0]+1.0); // Save and print for a different price value

    Print(closePrice[0].ToString("0.0000")+", EMA value = "+
    EMA(closePrice,5)[0].ToString());
    }

    Output file contains
    1.0609, EMA value = 1.0646201198249
    2.0609, EMA value = 1.0646201198249

    Any suggestions would be most welcome. Thanks, John

    #2
    Hello John,
    Thanks for writing in and I am happy to assist you.

    It’s due to how NinjaTrader calls any indicator internally and beyond what we could describe or support. But if you dispose the EMA before calling it again you will get the correct value.
    Code:
    Print(closePrice[0].ToString("0.0000")+", EMA value = "+
    EMA(closePrice,5)[0].ToString()); // Use a simple EMA as example
    
    closePrice.Set(Close[0] + 1.0); // Save and print for a different price value
    
    
    EMA(closePrice, 5).Dispose();	//dispose the ema to recalculate the values	
    
    Print(closePrice[0].ToString("0.0000")+", EMA value = "+
    EMA(closePrice,5)[0].ToString());
    Also please do remember when you reassign the custom data series (closePrice), the previous values (i.e. closePrice[1], closePrice[2] … etc) are set to that reassigned value. So please do take care on that too.

    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Just to let you know that I really, really appreciate your help.

      It works as described.

      Thank you so much,

      John

      Comment


        #4
        Hello John,
        Glad to know it worked.
        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Segwin, 05-07-2018, 02:15 PM
        10 responses
        1,767 views
        0 likes
        Last Post Leafcutter  
        Started by Rapine Heihei, 04-23-2024, 07:51 PM
        2 responses
        30 views
        0 likes
        Last Post Max238
        by Max238
         
        Started by Shansen, 08-30-2019, 10:18 PM
        24 responses
        943 views
        0 likes
        Last Post spwizard  
        Started by Max238, Today, 01:28 AM
        0 responses
        9 views
        0 likes
        Last Post Max238
        by Max238
         
        Started by rocketman7, Today, 01:00 AM
        0 responses
        7 views
        0 likes
        Last Post rocketman7  
        Working...
        X