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

EMA and T3 error

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

    EMA and T3 error

    Hello

    I am coding a strategy that uses a crossover of two T3 moving averages and the strategy stops and throws the error message in the output window: "Error: Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object."

    At the same time, the Log shows: "Indicator EMA: Error on calling OnBarUpdate method on bar xxxx: You are trying to access an index with a value that is invalid since it is out of range. I E accessing a series [barsAgo] with an value of 5, when there are only 4 bars on the chart". This error usually comes up twice for the EMA and once for the T3, with the same message.

    The next error in the log is an identical message with the one in the strategy output window, as above.

    When I run the strategy in Debug mode, Visual Studio stops first at the EMA error, then when I click Continue, it stops at the T3 error, then if I click Continue again, it runs normal to the end of the test period. Needles to say, I don't really understand what the messages in the Visual Studio prompts mean

    I'm using Market Replay Data with Playback connection, but it does the same with the live market data connection.

    So far, it seems that this is happening only when I include in the triggering logic the condition,

    Code:
    (CrossAbove(T31b2, T32b2, 1)
    but I can.t figure it out why, or, if I made a mistake setting up the T3, how come it runs after that?

    This is my T3 code

    Code:
    public class ATM2 : Strategy
        {    
           private T3 T31b0;
            private T3 T32b0;
            private T3 T31b2;
            private T3 T32b2;
    [B]....[/B]
               else if (State == State.Configure)                
                    {
                        AddDataSeries(Data.BarsPeriodType.Tick, 1);
                        AddDataSeries(Data.BarsPeriodType.Minute, 5);
                    }
    
                else if (State == State.DataLoaded)
                {
    [B]....[/B]
                    T31b2                = T3(Closes[2], 5, 3, 0.7);
                    T32b2                = T3(Closes[2], 8, 3, 0.7);
                    T31b0            = T3(Closes[0], 5, 3, 0.7);
                    T32b0            = T3(Closes[0], 8, 3, 0.7);
                    T31b0.Plots[0].Brush = Brushes.Transparent;    
                    T32b0.Plots[0].Brush = Brushes.Transparent;                    
                    AddChartIndicator(T31b0);
                    AddChartIndicator(T32b0);
    Attached Files

    #2
    Hi itrader46, thanks for your post.

    From the screenshot it looks like you need to check for at least "Period" amount of bars before processing the EMA. So check for :

    if(CurrentBars[0] < "EMA Period")
    return;

    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      That's what it looks like, but the strategy starts 5am and the error appears around 9am... plenty of bars, I would have thought!

      Comment


        #4
        Hi itrader46,
        NinjaTrader does not have preloadbar functionality.
        Dear Experts, Can I preload a certain number of bars to ensure custom indicators used in the strategy are already up and running correctly when the strategy

        Please add your vote, if you would like to see this being changed.
        NT-Roland

        Comment


          #5
          This keeps happening! As you can see from the output window prints, it works fine, then at some point it realises: 'Hold on! I don't have enough bars here!... Error'. I thought the code below would prevent just that from happening?!?

          Code:
                      if (CurrentBars[0] < BarsRequiredToTrade
                      || CurrentBars[2] < BarsRequiredToTrade)
                          return;    
          [B].....[/B]
          
                     if (T31b2 == null || T32b2 == null)
                      {
                          T31b2 = T3(BarsArray[2], 5, 3 ,0.7);
                          T32b2 = T3(BarsArray[2], 8, 3 ,0.7);
                      }
          Output prints:

          Enabling NinjaScript strategy 'ATM2/178977517' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=True CancelExitsOnStrategyDisable=True Calculate=On each tick IsUnmanaged=False MaxRestarts=4 in 5 minutes
          ++++++++++
          Long condition at: 56.64 - Current Ask: 56.64 - S = 1 - Open: 56.6 - Current Bar: 5067 @ 11/11/2019 14:38:00
          Long ATM triggered at: 56.64 - Open: 56.6 - Current Bar: 5067 - Long order bar: 5067 - 11/11/2019 14:38:00
          ##########
          XX>>-- Long condition false price: 56.63 - Trigger Long Price: 56.64 - Current Bar: 5067 - 11/11/2019 14:38:00
          Long ATM cancel cond 1 price: 56.61 - Trigger Long Price: 56.64 - Open: 56.6 - Current Bar: 5067 - 11/11/2019 14:38:00
          >>> Long Order ID Reset Price: 56.61 - Open: 56.6 - Current Bar: 5067 - 11/11/2019 14:38:00
          >>## Long ATM ID Reset Price: 56.61 - Open: 56.6 - Current Bar: 5067 - 11/11/2019 14:38:00
          ++++++++++
          Long condition at: 56.59 - Current Ask: 56.59 - S = 1 - Open: 56.53 - Current Bar: 5071 @ 11/11/2019 14:42:00
          Long ATM triggered at: 56.59 - Open: 56.53 - Current Bar: 5071 - Long order bar: 5071 - 11/11/2019 14:42:00
          ##########
          Cond False Long Reset - Current Ask: 56.59 - Current Bar: 5071
          XX>>-- Long condition false price: 56.58 - Trigger Long Price: 56.59 - Current Bar: 5072 - 11/11/2019 14:43:00
          Long ATM cancel cond 4 price: 56.58 - Trigger Long Price: 56.59 - Long order bar: 5071 - Open: 56.58 - Current Bar: 5072 - 11/11/2019 14:43:00
          >>> Long Order ID Reset Price: 56.58 - Open: 56.58 - Current Bar: 5072 - 11/11/2019 14:43:00
          >>## Long ATM ID Reset Price: 56.59 - Open: 56.58 - Current Bar: 5072 - 11/11/2019 14:43:00
          ++++++++++
          Long condition at: 56.61 - Current Ask: 56.61 - S = 1 - Open: 56.6 - Current Bar: 5073 @ 11/11/2019 14:44:00
          'GetAtmStrategyEntryOrderStatus' method error: Order ID 'e13d2df6241f42f8930e0810c5e1bd2b' does not exist
          'GetAtmStrategyEntryOrderStatus' method error: Order ID 'e13d2df6241f42f8930e0810c5e1bd2b' does not exist
          'GetAtmStrategyEntryOrderStatus' method error: Order ID 'e13d2df6241f42f8930e0810c5e1bd2b' does not exist
          'GetAtmStrategyEntryOrderStatus' method error: Order ID 'e13d2df6241f42f8930e0810c5e1bd2b' does not exist
          'GetAtmStrategyEntryOrderStatus' method error: Order ID 'e13d2df6241f42f8930e0810c5e1bd2b' does not exist
          Long ATM triggered at: 56.61 - Open: 56.6 - Current Bar: 5073 - Long order bar: 5073 - 11/11/2019 14:44:00
          ##########
          Cond False Long Reset - Current Ask: 56.61 - Current Bar: 5073
          XX>>-- Long condition false price: 56.6 - Trigger Long Price: 56.61 - Current Bar: 5073 - 11/11/2019 14:44:00
          Cond False Long Reset - Current Ask: 56.62 - Current Bar: 5073
          >>> Long Order ID Reset Price: 56.64 - Open: 56.6 - Current Bar: 5073 - 11/11/2019 14:44:00
          >>## Long ATM ID Reset Price: 56.84 - Open: 56.8 - Current Bar: 5078 - 11/11/2019 14:49:00
          Strategy 'ATM2': Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object.
          Disabling NinjaScript strategy 'ATM2/178977517'
          11/11/2019 14:53:00 CancelAllOrders: BarsInProgress=0
          05/11/2019 23:00:00 CancelAllOrders: BarsInProgress=1
          05/11/2019 23:05:00 CancelAllOrders: BarsInProgress=2

          Comment


            #6
            Hello itrader46,

            You are receiving the following error which is disabling your strategy:

            Strategy 'ATM2': Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object.

            This would mean that you are referencing an object that is null. This is not the same thing as an index out of range error which would be seen if you are referencing some BarsAgo value before the script processes that many bars.

            I suggest debugging the code in your EventHandlerBarsUpdate method by adding prints so you can find out which specific line is creating the "Object reference not set to an instance of an object" error. (The output is hinting that this error is being thrown in your EventHandlerBarsUpdate method.)

            Debugging tips - https://ninjatrader.com/support/help...script_cod.htm

            Checking for null references - https://ninjatrader.com/support/help...references.htm

            Please let us know if you have any additional questions.
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by WHICKED, Today, 02:02 PM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by selu72, Today, 02:01 PM
            0 responses
            3 views
            0 likes
            Last Post selu72
            by selu72
             
            Started by f.saeidi, Today, 12:14 PM
            8 responses
            21 views
            0 likes
            Last Post f.saeidi  
            Started by Mikey_, 03-23-2024, 05:59 PM
            3 responses
            49 views
            0 likes
            Last Post Sam2515
            by Sam2515
             
            Started by Russ Moreland, Today, 12:54 PM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Erick  
            Working...
            X