Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Tradingview Script different results from Ninjatrader

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

    Tradingview Script different results from Ninjatrader

    Using the exact same backtesting range, timeframe, ticker I get wildly different results.
    On TradingView there are only ~700 trades
    On Ninjatrader there are 5700

    I'm assuming these are the exact same indicators, so I don't know what's going wrong.

    Ninjascriptcode:

    if (CrossBelow(StochRSI(14), 80, 1));
    ExitLong("Long");
    EnterShort("Short");

    if (CrossAbove(StochRSI((14)), 20, 1));
    EnterLong("Long");
    ExitShort("Short");​

    ----------------------
    And this is my Pinescript code

    //Long
    long = ta.crossover(k, 20)

    //Short
    short = ta.crossunder(k, 80)

    //Long Entry and Exit
    if long
    strategy.entry("Long",strategy.long)
    strategy.close("Short")

    //Short Entry and Exit
    if short
    strategy.entry("Short",strategy.short)
    strategy.close("Long")



    #2
    Hello unpronounceable1700,

    This would be a situation where you would have to add debugging into both platforms to make sure all values are identical in both tests. In general you can see differences between platforms for a variety of reasons, our support wouldn't be able to answer any questions specific to tradingview, if they have some means of debugging similar to the Print in NinjaTrader you would need to use that in the script and then compare the output between platforms.

    As a side note you do not need to use ExitShort and ExitLong in NinjaTrader to reverse, you just call the opposite entry method.


    if (CrossBelow(StochRSI(14), 80, 1));
    EnterShort("Short");

    if (CrossAbove(StochRSI((14)), 20, 1));
    EnterLong("Long");

    That is also one reason it would be different, in C# you need to use curly braces if you have multiple commands in an if statement, if you do not only the first is executed based on the condition.


    if (CrossBelow(StochRSI(14), 80, 1));
    {
    EnterShort("Short");
    // other commands
    }

    ​​

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello unpronounceable1700,

      This would be a situation where you would have to add debugging into both platforms to make sure all values are identical in both tests. In general you can see differences between platforms for a variety of reasons, our support wouldn't be able to answer any questions specific to tradingview, if they have some means of debugging similar to the Print in NinjaTrader you would need to use that in the script and then compare the output between platforms.

      As a side note you do not need to use ExitShort and ExitLong in NinjaTrader to reverse, you just call the opposite entry method.


      if (CrossBelow(StochRSI(14), 80, 1));
      EnterShort("Short");

      if (CrossAbove(StochRSI((14)), 20, 1));
      EnterLong("Long");

      That is also one reason it would be different, in C# you need to use curly braces if you have multiple commands in an if statement, if you do not only the first is executed based on the condition.


      if (CrossBelow(StochRSI(14), 80, 1));
      {
      EnterShort("Short");
      // other commands
      }

      ​​
      I mean even if the code isn't directly translatable for some reason. The StochRSI indicator on Ninjatrader matches up with Tradingview's. And Tradingview executes the codes when there are crossover events.

      So the StochRSI code by itself shouldn't be executing this much

      The average bars in trade for Ninjatrader was 1.5 while Tradingview was 18

      Comment


        #4
        Originally posted by NinjaTrader_Jesse View Post
        Hello unpronounceable1700,

        This would be a situation where you would have to add debugging into both platforms to make sure all values are identical in both tests. In general you can see differences between platforms for a variety of reasons, our support wouldn't be able to answer any questions specific to tradingview, if they have some means of debugging similar to the Print in NinjaTrader you would need to use that in the script and then compare the output between platforms.

        As a side note you do not need to use ExitShort and ExitLong in NinjaTrader to reverse, you just call the opposite entry method.


        if (CrossBelow(StochRSI(14), 80, 1));
        EnterShort("Short");

        if (CrossAbove(StochRSI((14)), 20, 1));
        EnterLong("Long");

        That is also one reason it would be different, in C# you need to use curly braces if you have multiple commands in an if statement, if you do not only the first is executed based on the condition.


        if (CrossBelow(StochRSI(14), 80, 1));
        {
        EnterShort("Short");
        // other commands
        }

        ​​
        I think what may be happening is it may just be starting a new short or long whenever the condition is met instead of waiting til a reversal trade. I'm not sure though if that's how the programming works, just a theory.

        Comment


          #5
          Hello unpronounceable1700,

          You would need to use prints to see how the logic works in both use cases and to check the values that the strategy sees in both use cases. Our support cannot help with trading view specific items because we do not have any information about how that platform operates, it could be working different from NinjaTrader.

          Comment


            #6
            Originally posted by NinjaTrader_Jesse View Post
            Hello unpronounceable1700,

            You would need to use prints to see how the logic works in both use cases and to check the values that the strategy sees in both use cases. Our support cannot help with trading view specific items because we do not have any information about how that platform operates, it could be working different from NinjaTrader.
            I debugged it a bit. Added a few print conditions. The strategy is switching between long and flat every single bar.

            Print(Position.MarketPosition.ToString() + " " + Position.Quantity.ToString() + " ");
            Print(string.Format("{0};{1};{2};{3};{4}", Time[0], Open[0], High[0], Low[0], Close[0]));

            if (CrossAbove(StochRSI((14)), 20, 1));
            {
            EnterLong();
            }

            if (CrossBelow(StochRSI(14), 80, 1));
            {
            ExitLong();
            }​

            Bar - Long
            Bar - Flat
            Bar - Long
            and so on

            Comment


              #7
              Hello unpronounceable1700,

              That is because you have used a semi colon at the end of the if statement, I had not noticed that when you originally posted the code so I had accidently included that in what I copied from your post. In C# an if statement is terminated meaning its not used if you put a semicolon at the end, you just need to remove those.

              if (CrossAbove(StochRSI((14)), 20, 1)); < remove this ;
              {
              EnterLong();
              }

              if (CrossBelow(StochRSI(14), 80, 1)); < remove this ;
              {
              ExitLong();
              }​​

              Comment


                #8
                Originally posted by NinjaTrader_Jesse View Post
                Hello unpronounceable1700,

                That is because you have used a semi colon at the end of the if statement, I had not noticed that when you originally posted the code so I had accidently included that in what I copied from your post. In C# an if statement is terminated meaning its not used if you put a semicolon at the end, you just need to remove those.

                if (CrossAbove(StochRSI((14)), 20, 1)); < remove this ;
                {
                EnterLong();
                }

                if (CrossBelow(StochRSI(14), 80, 1)); < remove this ;
                {
                ExitLong();
                }​​
                Now the debug output doesn't show any trades going through, just flat. I mean all my code is, is literally just this. A simple crossabove and crossbelow on StochRSI. Do you know if this works on your end?

                Comment


                  #9
                  Hello unpronounceable1700

                  You can use a Print to get a better idea of what is happening.

                  Code:
                  Print(Time[0] + " " + StochRSI(14)[0] + " " + CrossAbove(StochRSI((14)), 20, 1));
                  if (CrossAbove(StochRSI((14)), 20, 1))
                  {
                  EnterLong();
                  }


                  Comment


                    #10
                    Originally posted by NinjaTrader_Jesse View Post
                    Hello unpronounceable1700

                    You can use a Print to get a better idea of what is happening.

                    Code:
                    Print(Time[0] + " " + StochRSI(14)[0] + " " + CrossAbove(StochRSI((14)), 20, 1));
                    if (CrossAbove(StochRSI((14)), 20, 1))
                    {
                    EnterLong();
                    }


                    https://support.ninjatrader.com/s/ar...language=en_US
                    This appeared in the output (just section of it)
                    8/23/2024 4:20:00 PM 0.744408820048994 False
                    8/23/2024 4:22:00 PM 0.795703500710935 False
                    8/23/2024 4:24:00 PM 0.849281843554466 False
                    8/23/2024 4:26:00 PM 1 False
                    8/23/2024 4:28:00 PM 1 False
                    8/23/2024 4:30:00 PM 0.671284129771316 False
                    8/23/2024 4:32:00 PM 0.223085304795476 False
                    8/23/2024 4:34:00 PM 0.280938824590977 False
                    8/23/2024 4:36:00 PM 0.455111459017016 False
                    8/23/2024 4:38:00 PM 0.455111459017016 False
                    8/23/2024 4:40:00 PM 0.202523829673582 False
                    8/23/2024 4:42:00 PM 0.0860033800885741 False
                    8/23/2024 4:44:00 PM 0.0860033800885741 False
                    8/23/2024 4:46:00 PM 0.0860033800885741 False
                    8/23/2024 4:48:00 PM 0 False
                    8/23/2024 4:50:00 PM 0 False
                    8/23/2024 4:52:00 PM 0.102708788179123 False
                    8/23/2024 4:54:00 PM 0.102708788179123 False
                    8/23/2024 4:56:00 PM 0 False
                    8/23/2024 4:58:00 PM 0 False​

                    Edit: Oh I just realized it. It's in decimal form.. sigh
                    Last edited by unpronounceable1700; 08-27-2024, 04:54 AM.

                    Comment


                      #11
                      Just wanted to give you a Heads-up:
                      The same named functions in TV do not always return the same values...
                      I have also "translated" from Ninja to TV and in order to get close, I recoded functions on BOTH platforms so that they return the same values,,
                      Also, TV has aggregated Volumes, (which will throw things off) and, I used IB (for Ninja data) which reports Volume differently...
                      NOTE: I would down-load the community version of Visual studio for doing any NINJ coding.

                      Walt
                      >> this is NOT something I would advise for a new coder; I've been coding for 40+ years and am still being challenged LOL!
                      Last edited by waltFX; 09-26-2024, 03:53 PM.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      110 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      59 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      37 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      41 views
                      0 likes
                      Last Post TheRealMorford  
                      Started by Mindset, 02-28-2026, 06:16 AM
                      0 responses
                      78 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Working...
                      X