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

Random entry not random?

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

    Random entry not random?

    Hi,

    I'm creating a strategy that has random entry and a time stop of five periods. My problem is that % profitable generally becomes greater than 50% and I can't see why.

    I've created a variable that isn't used in the code and then optimized the strategy retrieving 20 results. When I view all of the 20 results the average accuracy is consistently over 50% and I seldom get a single result below 50%. I get this problem when testing on 7000-8000 trades.

    Any ideas?

    Here's my code:

    PHP Code:
    Random rnd = new Random();
            protected 
    int GetRandomInt(int minint max)
            {
            return 
    rnd.Next(min,max);
            }

            
    int myInt;

            protected 
    override void OnBarUpdate()
            {

                
    myInt GetRandomInt(02);
                        
                if(
    BarsSinceEntry() == 5)
                {
                    
    ExitLong("""");
                    
    ExitShort("""");
                }
                
                 if(
    myInt == && Position.MarketPosition == MarketPosition.Flat)
                 {
                    
    EnterShort(10000"");
                 }
                        
                  if(
    myInt == && Position.MarketPosition == MarketPosition.Flat)
                  {
                     
    EnterLong(10000"");
                  }
            } 

    #2
    db8r, could it just be that your computer randomly generates numbers that produce these results?

    Now I'm not exactly sure how GetRandomInt() works, but if you're only using the integers 0 and 1, maybe try GetRandomInt(0, 1)?
    AustinNinjaTrader Customer Service

    Comment


      #3
      Sounds like a problem with Random.

      Have you tried testing the code using an easier to test scenario, like incrementing an integer and taking the mod%2 to always produce 0 and 1 consecutives? If that scenario produces the 50% that you're looking for, then it's a problem with the numbers being generated by Random.

      Try changing your code to something similar to the following and see if it produces the result you expect (the following code won't exactly work because it will increment the count while you're in the middle of a trade, but you get the idea)...

      int count = 0;

      protected override void OnBarUpdate()
      {
      count++;

      if(BarsSinceEntry() == 5)
      {
      ExitLong("", "");
      ExitShort("", "");
      }

      if(count%2 == 1 && Position.MarketPosition == MarketPosition.Flat)
      {
      EnterShort(10000, "");
      }

      if(count%2 == 0 && Position.MarketPosition == MarketPosition.Flat)
      {
      EnterLong(10000, "");
      }
      }

      Comment


        #4
        Problem persists

        Thanks for your replies guys. I've printed the random numbers and checked them in excel. There's seems to be nothing wrong. I've also tested different markets and time frames, and I've tried switching the rules for longs and shorts (which should reveal a problem with random numbers).

        I constantly get better than average results. If I test on smaller time frames (and hence test more trades) I get even better results. If I test my EURUSD data between 2000 and 2009 on a 5 min time frame (that's over 230 000 trades) my 20 different optimization results range from 54.82%-55.24%. If I use an hourly time frame results range from 51.24%-52.28%.

        I just can't figure out what's causing this. Maybe my fx data is curved, but how could curved data possibly generate better than average results when using a random entry and a time stop?

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by aa731, Today, 02:54 AM
        0 responses
        4 views
        0 likes
        Last Post aa731
        by aa731
         
        Started by thanajo, 05-04-2021, 02:11 AM
        3 responses
        470 views
        0 likes
        Last Post tradingnasdaqprueba  
        Started by Christopher_R, Today, 12:29 AM
        0 responses
        10 views
        0 likes
        Last Post Christopher_R  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        166 responses
        2,237 views
        0 likes
        Last Post sidlercom80  
        Started by thread, Yesterday, 11:58 PM
        0 responses
        5 views
        0 likes
        Last Post thread
        by thread
         
        Working...
        X