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

SystemPerformance causes onbarupdate error

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

    SystemPerformance causes onbarupdate error

    I have a multi-timeframe strategy which works without errors until I add the following code;

    if (SystemPerformance.AllTrades.Count > 1)
    {
    Trade lastTrade = SystemPerformance.AllTrades.LosingTrades[SystemPerformance.AllTrades.Count - 1];
    }

    This code is the exact sample provided in the NT8 Language Reference. Once I add this code I get the following error, "Error on calling 'OnBarUpdate' method on bar 314"
    This code is executed in the CurrentBars[0] section which is preceeded by the following code;

    if(CurrentBars[0] < 22)
    return;

    What is wrong with my implementation of this code?

    Thanks,

    Alma

    #2
    Hello Safetrader,

    Thank you for your note.

    I notice that you're using if (SystemPerformance.AllTrades.Count > 1) - this will check to see if any trades have been taken, not necessarily that there have actually been any losing trades yet. Try this instead:

    if (SystemPerformance.AllTrades.LosingTrades.Count > 1)

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      I just tried your suggestion and got the following error message; NinjaTrader.Cbi.Tradecollection does not contain a definition for "LosingTradesCount" and no extension method "LosingTradesCount accepting a first argument of type NinjaTrader.Cbi.TradeCollection could be found (are you missing a using directive or an assembly reference?)

      Comment


        #4
        BTW, this was the entire code section;
        if (SystemPerformance.AllTrades.LosingTradesCount > 1)
        {
        //Trade lastTrade = SystemPerformance.AllTrades.LosingTrades[SystemPerformance.AllTrades.Count - 1];
        }

        At first I didn't comment out the section in {}, but I later commented it out to see if the error still appeared with out it and it did.

        Comment


          #5
          I'm sorry, I made a mistake with all of my editing (adding / removing) lines to see what worked. The error only occurs when the above line that is commented out IS NOT commented out. This is the offending code;

          Trade lastTrade = SystemPerformance.AllTrades.LosingTrades[SystemPerformance.AllTrades.Count - 1];

          Comment


            #6
            Hello Safetrader,

            Thank you for your reply.

            Looks like there's not a period between LosingTrades and Count, and you're not specifically looking for the Count value of the losing trades only in the next line. If you try it like this do you still get the error on the Trade line?

            if (SystemPerformance.AllTrades.LosingTrades.Count > 1)
            {
            Trade lastTrade = SystemPerformance.AllTrades.LosingTrades[SystemPerformance.AllTrades.LosingTrades.Count - 1];
            }

            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              You are correct, this does not cause an error this way, but if I include "LosingTrades in both lines above, then "Trade lastTrade" will not indicate if the last trade was a losing trade, correct? It will go back to look for the last losing trade, right? My intention is to add to my strategy a new instruction only if the last trade was a losing trade.

              Comment


                #8
                Hello Safetrader,

                Thank you for your reply.

                You could implement code like the code in this help guide example:



                Specifically, you could check AllTrades instead of just losing trades, determine whether the PnL was positive or negative, and then do something if the pnl was negative, meaning it was a losing trade.

                Code:
                if (SystemPerformance.AllTrades.Count > 1)
                {
                Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
                
                if (lastTrade.ProfitCurrency < 0)
                {
                // do something here
                }
                }
                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks for your assistance and patience, that did the trick. Have a great day.

                  Alma

                  Comment


                    #10
                    Originally posted by Safetrader View Post
                    I have a multi-timeframe strategy which works without errors until I add the following code;

                    if (SystemPerformance.AllTrades.Count > 1)
                    {
                    Trade lastTrade = SystemPerformance.AllTrades.LosingTrades[SystemPerformance.AllTrades.Count - 1];
                    }

                    This code is the exact sample provided in the NT8 Language Reference. Once I add this code I get the following error, "Error on calling 'OnBarUpdate' method on bar 314"
                    This code is executed in the CurrentBars[0] section which is preceeded by the following code;

                    if(CurrentBars[0] < 22)
                    return;

                    What is wrong with my implementation of this code?

                    Thanks,

                    Alma
                    Just curious, is there a sample multi-timeframe strategy that you used? I combined a few strategies together with different time frames and it will only trade the current timeframe. The strategy is very simple, just moving average crossovers, so there's not much going on. NT tell me t add prints but I don't see the value as i know the condition is true, just the strategy is not recognizing it. I believe I am missing something simple but can't figure it out

                    Comment


                      #11
                      Hello Steve4616,

                      NT tell me t add prints but I don't see the value as i know the condition is true, just the strategy is not recognizing it. I believe I am missing something simple but can't figure it out
                      It sounds like you put a print inside the condition and then the condition was not true. That would only be helpful knowing that the condition is not true but not why. If you wanted to find out why you would need to move the print outside of that condition to output the values for each bar.

                      If you are having trouble with a script you are writing I would suggest to post the code you tried along with the prints you added so we can better assist.



                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_Jesse View Post
                        Hello Steve4616,

                        It sounds like you put a print inside the condition and then the condition was not true. That would only be helpful knowing that the condition is not true but not why. If you wanted to find out why you would need to move the print outside of that condition to output the values for each bar.

                        If you are having trouble with a script you are writing I would suggest to post the code you tried along with the prints you added so we can better assist.


                        Thanks Jesse. I realized today that there is a sample multi-timeframe strategy that comes with NT, but I deleted it a few months ago when I got a new VPS and was cleaning up all my strategies that I wasn;t using(because I suck lol) and must've deleted it too. I'm trying to get a friend to send me a copy of it. Can you post it here?

                        Comment


                          #13
                          Hello Steve4616,

                          If you deleted any system files then you should re run the installer to do a repair, that will restore missing files. You can download the installer from https://ninjatrader.com/PlatformDirect

                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            Thanks Jesse, I just downloaded it, but the sample strategy is still not showing up

                            Comment


                              #15
                              Ok so I found it, I just thought it could be done in the strategy builder but its just a script in the Editor. Is it possible to make the multi-timeframe strategy work from the builder or does it have to be scripted? Thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Stanfillirenfro, Today, 07:23 AM
                              1 response
                              2 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by cmtjoancolmenero, Yesterday, 03:58 PM
                              2 responses
                              20 views
                              0 likes
                              Last Post cmtjoancolmenero  
                              Started by olisav57, Yesterday, 07:39 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by cocoescala, 10-12-2018, 11:02 PM
                              7 responses
                              943 views
                              0 likes
                              Last Post Jquiroz1975  
                              Started by oviejo, Today, 12:28 AM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X