Announcement

Collapse
No announcement yet.

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 min, int max)
            {
            return rnd.Next(min,max);
            }
    
            int myInt;
    
            protected override void OnBarUpdate()
            {
    
                myInt = GetRandomInt(0, 2);
                        
                if(BarsSinceEntry() == 5)
                {
                    ExitLong("", "");
                    ExitShort("", "");
                }
                
                 if(myInt == 1 && Position.MarketPosition == MarketPosition.Flat)
                 {
                    EnterShort(10000, "");
                 }
                        
                  if(myInt == 0 && 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 Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        650 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        370 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        109 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        574 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        577 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X