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 entries

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

    random entries

    Code:
    		protected override void OnBarUpdate()
    		{
    			if (CurrentBars[0] < 1)
    			return;
    			
    			
    			Random randNum = new Random();
    			
    			Print(randNum.Next(0,2));
    
    			 // Set 1
    			if (Position.MarketPosition == MarketPosition.Flat)
    			{
    				
    				//Long conditions
    				if( randNum.Next(0,2) == 0)
    			{
    				EnterLong(Convert.ToInt32(DefaultQuantity), @"long");
    			}
    			
    				else
    				
    				//Short conditions
    				if(randNum.Next(0,2) == 1)
    
    
    			{
    				EnterShort(Convert.ToInt32(DefaultQuantity), @"short");
    			}
    			
    			}
    			
    		}
    Why is this code not generating random trades? To be clear, the code works but my long/short trade distribution does not seem to be random. I ran the above (20 different times) for EURUSD from jan 1 2017 to present. And my results are something like:

    117 long positions and zero short positions (not random)
    100 long positions and 20 short positions (not random)
    and rarely, i'll get something like: 56 long positions and 45 short positions (this is kinda what i'd expect)
    200 long positions and zero short positions (not random)

    By "not random" I mean, how can It be that a coin flip (0,1) generate 200 long positions and zero short positions??

    #2
    Hello,
    Thanks for your post.

    After reviewing your code the first thing that I see is that your (Position.MarketPosition == MarketPosition.Flat) condition is restricting your trades, and that's why your results are consistently lop sided. It will also produce more consistent results if you declare your random number generator inside the class instead of OnBarUpdate(). I have attached the modified code for your reference.

    The following link will take you to the MSDN page on the C# Random Class.
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    Please let me know if you have any further questions.
    Attached Files
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      Hello,
      Thanks for your post.

      After reviewing your code the first thing that I see is that your (Position.MarketPosition == MarketPosition.Flat) condition is restricting your trades, and that's why your results are consistently lop sided. It will also produce more consistent results if you declare your random number generator inside the class instead of OnBarUpdate(). I have attached the modified code for your reference.

      The following link will take you to the MSDN page on the C# Random Class.
      https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

      Please let me know if you have any further questions.
      Thanks Josh. Your code worked perfect.

      Comment


        #4
        Originally posted by calhawk01 View Post
        Thanks Josh. Your code worked perfect.
        Here's some alternate random entry code if anybody's interested. link

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by cre8able, Today, 03:20 PM
        0 responses
        2 views
        0 likes
        Last Post cre8able  
        Started by Fran888, 02-16-2024, 10:48 AM
        3 responses
        46 views
        0 likes
        Last Post Sam2515
        by Sam2515
         
        Started by martin70, 03-24-2023, 04:58 AM
        15 responses
        114 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by The_Sec, Today, 02:29 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by jeronymite, 04-12-2024, 04:26 PM
        2 responses
        31 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Working...
        X