MathRand()

 

Hi all, could you give a criteria on how to use MathRand() function to open orders?

Something like this:


if ( MathRand() ....)

BUY


if ( MathRand() ...)

SELL


Betterif these two conditions have around 50% probability each one.

For istance, if MathRand() is an equal number BUY, else SELL.

Hope is clear!

Thank you!

 
MathRand function returns a pseudorandom integer in the range 0 to 0x7fff (32767)
bool buy = MathRand()/32768.0 >= 0.5,
     sell= !buy;
 

I don't understand how to proceed.

If I write

int random = MathRand() ; 
  double check =  random/32768 ;
  Print( check ) ;

I obtain 0.

 
Alberto_jazz:

I don't understand how to proceed.

If I write

I obtain 0.


Don't divide an int by an int and expect a double . . . read up on type casting

Try . . .

int random = MathRand() ; 
  double check =  random/32768.0 ;  //<----  32768.0
  Print( check ) ;
 

Why not just use:

int random = MathRand() ;
if(random % 2 ==0){Buy();}else{Sell();}
 


//define a variable e.g x

{

   int x;

   m_Symbol.RefreshRates();
   x=MathRand();
   Print("x = ",x,", Bid = ",m_Symbol.Bid(),", Ask = ",m_Symbol.Ask());
       
   if(x<16384) //this your choice --buy
     {               
      if(m_Trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,GetSize(),m_Symbol.Ask(),
         m_Symbol.Bid() - (Points * StopLoss),
         m_Symbol.Ask()+(Points*ProfitTarget))) 
        {
         OrderNumber=m_Trade.ResultOrder();
        
         return(true);
        }
      else 
        {
         OrderNumber=0;
        }
     }
      
   else     //------sell short
     {                                
      if(m_Trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,GetSize(),m_Symbol.Bid(),
         m_Symbol.Ask() + (Points * StopLoss),
         m_Symbol.Bid() -(Points * ProfitTarget))) 
        {
         OrderNumber=m_Trade.ResultOrder();
     
        }
      else 
        {
         OrderNumber=0;
        }
     }

   return(false);
  }

Reason: