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

Standard Deviation

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

    Standard Deviation

    I've looked through the forums for this but haven't seen much that would help, aside from a link to the MSDN site for Math functions...and that just opens up an even bigger can of unfamiliarity...

    Simply put, I would like to generate the StdDev for the past x EMA values. Is there a simple way to do this? I've been playing with it a little and the "official word" from the Help Guide (http://www.ninjatrader-support.com/H...ndardDeviation) provides examples that don't really appear to apply to my situation here.

    Isn't there some way to use StdDev like "std_dev = (StdDev(EMA[0], EMA[1], EMA[2], EMA[3]....)

    Any help would be much appreciated!

    Gary

    #2
    Gary, the StdDev method would take a DataSeries as input as well so you can run it on the EMA value, too -

    Code:
    double sDev = StdDev(EMA, 10)[0]);
    - while the 10 would equal the period to calculate on, your X.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Terrific, thanks very much!

      Comment


        #4
        Originally posted by NinjaTrader_Bertrand View Post
        Gary, the StdDev method would take a DataSeries as input as well so you can run it on the EMA value, too -

        Code:
        double sDev = StdDev(EMA, 10)[0]);
        - while the 10 would equal the period to calculate on, your X.
        Is there a DataSeries which contains all the winning trades of a strategy? And one for the losing trades?

        I'm looking to use the standard deviation in the Output Window after a backtest, to give better insight in the performance of the strategy. I noticed however that
        Code:
        [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
        [SIZE=2][FONT=Consolas][COLOR=black]Performance.AllTrades.LosingTrades.TradesPerformance.Currency.StdDev[/COLOR][/FONT][/SIZE]
        [/SIZE][/FONT][/SIZE][/FONT]
        doesn't give the standard deviation of the losing trades. So I was wondering, is there another way to access / find the standard deviation?

        Regards,

        Comment


          #5
          I'm not sure I follow you - what exact issues are you having with the call snippet you've shown?
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Bertrand View Post
            I'm not sure I follow you - what exact issues are you having with the call snippet you've shown?

            Thanks for your quick reply Bertrand.

            I have in my test strategy (which consists of a simple MA crossover) the following code snippet:
            Code:
            [COLOR=#282828][FONT=Arial]Print("Avg winning trade:\t\t\t" + Math.Round(Performance.AllTrades.WinningTrades.TradesPerformance.Currency.AvgProfit,2));[/FONT][/COLOR]
            [COLOR=#282828][FONT=Arial]Print("Std winning trades:\t\t\t" + Math.Round(Performance.AllTrades.WinningTrades.TradesPerformance.Currency.StdDev,2));[/FONT][/COLOR]
            [COLOR=#282828][FONT=Arial]Print("Avg losing trade:\t\t\t" + Math.Round(Performance.AllTrades.LosingTrades.TradesPerformance.Currency.AvgProfit,2));[/FONT][/COLOR]
            [COLOR=#282828][FONT=Arial]Print("Std losing trades:\t\t\t" + Math.Round(Performance.AllTrades.LosingTrades.TradesPerformance.Currency.StdDev,2));[/FONT][/COLOR]
            This results in the following output:
            Code:
            [COLOR=#282828][FONT=Arial]Avg winning trade:                               307,21[/FONT][/COLOR]
            [COLOR=#282828][FONT=Arial]Std winning trades:                              2,42[/FONT][/COLOR]
            [COLOR=#282828][FONT=Arial]Avg losing trade:                                 -266,34[/FONT][/COLOR]
            [COLOR=#282828][FONT=Arial]Std losing trades:                                2,59[/FONT][/COLOR]
            If I export the trades tab into Excel, and calculate the standard deviation of the losing trades (in Currency), I get 267,5395. When backtesting with the ‘Percent’ setting, and then exporting the Trades into Excel, I get an standard deviation of a little more than 6%, so, I’m wondering where the 2.59 from NinjaTrader comes from.

            (Btw, removing the Math.Round has no effect, and produces an standard deviation of 2,59022866697798)

            Btw, The losing trades of this strategy (in Currency) are as follows:
            -3
            -219
            -91
            -35
            -294
            -145
            -198
            -175
            -162
            -212
            -354
            -324
            -216
            -54
            -229
            -125
            -277
            -1047
            -1197
            -670
            -350
            -596
            -161
            -10
            -255
            -50
            -123
            -378
            -86
            -214
            -25
            -248
            -179

            As this list shows, the standard deviation can never be something as small as 2.59.

            Thanks in advance for your time/effort,

            Regards,

            Comment


              #7
              I can see what you mean and will check into J_o_s - what symbol and feed were your tests on?
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Bertrand View Post
                I can see what you mean and will check into J_o_s - what symbol and feed were your tests on?

                Thanks Betrand, the above example is the stock symbol JPM (EOD) downloaded from the KineTick feed, on the time period 1-1-2008 to 1-1-2010.

                However, I also noticed the error on data downloaded from Interactive Brokers.

                When backtested on AH (Ahold N.V. stock) with data downloaded from IB, I got the following output (Currency):
                Code:
                [COLOR=#282828][FONT=Arial]Avg losing trade:                                 -5,18[/FONT][/COLOR]
                [COLOR=#282828][FONT=Arial]Std losing trades:                                0,0290977445458264[/FONT][/COLOR]
                According to Excel, the losing trade is -5.18197, with an standard deviation of 3.037187. (backtest on 30 minute bars, from 1-1-2008 to 1-10-2010, no commission or slippage, and backtest set to Currency).

                I did notice that, when backtesting the same strategy on intraday Futures data (in this case FESX data, bought from an external source so no known data provider) that the output is correct. The output window returned:
                Code:
                [COLOR=#282828][FONT=Arial]Avg losing trade:                                 -112,75[/FONT][/COLOR]
                [COLOR=#282828][FONT=Arial]Std losing trades:                                73,5053036681098[/FONT][/COLOR]
                Which is almost the exact same number as Excel rapported: average losing trade: -111,222 and a standard deviation of losing trades of 74,60767. (Backtest from 1-1-2004 to 1-1-2006, 30 minute time frame, no commission or slippage, and backtest set to Currency).

                When backtesting on the ES future from 1-1-2008 to 1-10-2010 (same settings as with the other tests), I got the following output:
                Code:
                [SIZE=3][COLOR=#000000][FONT=Calibri]Avg losing trade:                                             -887,94[/FONT][/COLOR][/SIZE]
                [SIZE=3][COLOR=#000000][FONT=Calibri]Std losing trades:                                            929,194116036397[/FONT][/COLOR][/SIZE]
                According to Excel, the average losing trade is -875.524, with an standard deviation of 941.2821.

                I would say, judging from these examples, that the standard deviation for stocks need to be multiplied by 100 to get an approximation of the standard deviation. Perhaps even with an number such as 103 – 106.

                Let me know if I can be of further help,

                Regards,

                Comment


                  #9
                  I could reproduce what you see here working with stocks - we'll look into and get back to you.

                  Thanks for reporting it in.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    We looked into it: the underlying reason is that everything is calculated on per 1 unit basis, for example 1 share for stocks as in your case or 1 futures contract.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Bertrand View Post
                      We looked into it: the underlying reason is that everything is calculated on per 1 unit basis, for example 1 share for stocks as in your case or 1 futures contract.
                      Thanks for your quick response, and thanks for looking into this. So multiplying it times the average position size should do the trick.

                      Regards,

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Brevo, Today, 01:45 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post Brevo
                      by Brevo
                       
                      Started by aussugardefender, Today, 01:07 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post aussugardefender  
                      Started by pvincent, 06-23-2022, 12:53 PM
                      14 responses
                      242 views
                      0 likes
                      Last Post Nyman
                      by Nyman
                       
                      Started by TraderG23, 12-08-2023, 07:56 AM
                      9 responses
                      384 views
                      1 like
                      Last Post Gavini
                      by Gavini
                       
                      Started by oviejo, Today, 12:28 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post oviejo
                      by oviejo
                       
                      Working...
                      X