Download MetaTrader 5

Can I call MathSrand() multiple times in EA?

To add comments, please log in or register
Ian Tavener
1347
Ian Tavener  

I use current month+ current day+account number as a seed for MathSrand() to generate a pseudo-random magic number each day. I would want to be able to look at trade history and create a report of all trades that hit SL (for example), so I would want to get a new seed for each day in history to be able to find all trades created by this EA.  Would something like this work?

int Magics_[];


void MakeMagicNumbers(datetime date)
{
   if(Auto_Gen_Magic)
   {
      MathSrand(TimeDay(date)+TimeMonth(date));
      Magic_Number_0=(int)MathCeil(AccountInfoInteger(ACCOUNT_LOGIN)/rand());
      Magic_Number_1=Magic_Number_0+1;
      Magic_Number_2=Magic_Number_1+1;
      Magic_Number_3=Magic_Number_2+1;
   }
   ArrayResize(Magics_,4);
   Magics_[0]=Magic_Number_0;Magics_[1]=Magic_Number_1;Magics_[2]=Magic_Number_2;Magics_[3]=Magic_Number_3;
}


void FindOurOrders()
{
    
   for(int k=OrdersHistoryTotal()-1;k>=0; k--)
   {
      if(!OrderSelect(k,SELECT_BY_POS,MODE_HISTORY)
         continue;
      datetime orderdate=OrderOpenTime();
      MakeMagicNumbers(orderdate); 
      for(int i=0;i<ArraySize(Magics_);i++)
      {
         if(OrderMagicNumber()!=Magics_[i])
            continue;
         MakeHistoryReport(OrderTicket());
         break;
      }    
   }
}

 

I didn't compile it, sorry if there is error.  Just want to give an idea to you of how many times I would want to reseed (if reseeding if possible) to find the predictable pseudorandom magic numbers from historic dates when calling rand().  FindOurOrders() would only be called from a CHARTEVENT_OBJECT_CLICK, so it would run very infrequently.

SanjayBalraj
48
SanjayBalraj  
Ian Tavener:

I use current month+ current day+account number as a seed for MathSrand() to generate a pseudo-random magic number each day. I would want to be able to look at trade history and create a report of all trades that hit SL (for example), so I would want to get a new seed for each day in history to be able to find all trades created by this EA.  Would something like this work?

 

I didn't compile it, sorry if there is error.  Just want to give an idea to you of how many times I would want to reseed (if reseeding if possible) to find the predictable pseudorandom magic numbers from historic dates when calling rand().  FindOurOrders() would only be called from a CHARTEVENT_OBJECT_CLICK, so it would run very infrequently.

Seed once. 

srand(_RandomSeed);

and also limit the range of your magic numbers using the modulus operator

int magic = rand() % 10000 + 1;
Ian Tavener
1347
Ian Tavener  
SanjayBalraj:

Seed once. 

and also limit the range of your magic numbers using the modulus operator

I seed once at the start, when assigning magic numbers in OnInit.  But I'm asking if I am able to seed repeatedly to find what the magic numbers were from previous days, so that I can compare my order history.

I don't want truly random for my magic numbers, I want predictably pseudo-random, so that if EA is restarted midday, it assigns the same number from rand().

whroeder1
17116
whroeder1  
Ian Tavener: I don't want truly random for my magic numbers, I want predictably pseudo-random, so that if EA is restarted midday, it assigns the same number from rand().

You don't want random numbers at all! You want one, so you can identify the EAs trades from other EAs and manual trading. PERIOD!

Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
If you are using multiple internal strategies or multiple timeframes, use a range of numbers.
SanjayBalraj
48
SanjayBalraj  
Ian Tavener:

I seed once at the start, when assigning magic numbers in OnInit.  But I'm asking if I am able to seed repeatedly to find what the magic numbers were from previous days, so that I can compare my order history.

I don't want truly random for my magic numbers, I want predictably pseudo-random, so that if EA is restarted midday, it assigns the same number from rand().

Like Mr. whroeder says, that is a dead end strategy (wrong use of magic numbers). Just talking about random numbers, however, you only seed once and call rand for a random number. 

Ian Tavener
1347
Ian Tavener  

My EA trades all pairs from single chart (where chart symbol and TF are irrelevant because it checks indicators from specified pairs/timeframes).  All trades are opened and closed within the same (server) calendar day.  And with all due respect, it is my prerogative to decide how I want to assign magic numbers, whether others believe it to be best practice or not.  Alas, I'm only straying further away from my original question, now.

So, if anyone could help me to answer my original question:  Can I reseed with MathSrand() repeatedly as I explained above, or will this somehow cause problems with the predictability of rand() for specific seeds?


EDIT:Solved.  Thank you

Ralf Graf
1197
Ralf Graf  

Correct me when I am wrong, but I think you do need unique numbers instead of random numbers.

Why not use 20180212 for todays magic, and tomorrow 20180213 ?

Alain Verleyen
Moderator
29868
Alain Verleyen  
Ian Tavener:

My EA trades all pairs from single chart (where chart symbol and TF are irrelevant because it checks indicators from specified pairs/timeframes).  All trades are opened and closed within the same (server) calendar day.  And with all due respect, it is my prerogative to decide how I want to assign magic numbers, whether others believe it to be best practice or not.  Alas, I'm only straying further away from my original question, now.

So, if anyone could help me to answer my original question:  Can I reseed with MathSrand() repeatedly as I explained above, or will this somehow cause problems with the predictability of rand() for specific seeds?


EDIT:Solved.  Thank you

You want an answer but you are not ready to explain why you need it, and once your problem is solved you don't even say how ?

I am afraid you will not have much help in the future with such attitude.

Marco vd Heijden
Moderator
6136
Marco vd Heijden  

if you seed with the same seed then the sequence of pseudo generated numbers will be identical.

as you know in cyberspace nothing is truly random; it does not exist.

Ian Tavener
1347
Ian Tavener  
Alain Verleyen:

You want an answer but you are not ready to explain why you need it, and once your problem is solved you don't even say how ?

I am afraid you will not have much help in the future with such attitude.

Sorry.  I didn't mean to come across as being rude.  I was a little agitated because I felt the discussion was changing directions away from what my true issue was, and I see that affected my response.

The reason for my question was because if I'm making an EA I plan to share, I know some people that use it will have concerns about brokers tracking their trades, and wanted to provide as many options as possible for users to tailor their usage of the EA to their own personal needs.  I know it may seem like a petty concern, but I'm just trying to be as comprehensive as possible and give options to everyone for their preference.  I was trying to create a way where every day would give a new pseudo-random number for a magic number.  But I also wanted to provide the option for a report printing of previous X days trading, and to do that I know I would need to include some functions for the EA to replicate the magic number from those previous days.

Embarrassingly enough, I solved the issue myself by writing a test EA.  This idea didn't come to me at first,  because my habits were to read as much as I could before actually trying something.  I left the short answer "Solved thank you" because my face was a little flush and I wanted to just kinda make the thread go away.  I hope anyone that I offended can forgive me, and I appreciate that you were all willing to come to this thread to address my question.

Sincerest regards.


For sake of posterity, here is the code I used to find my answer:

int OnInit()
  {
//---
   MakeNumbers();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void MakeNumbers()
{
int day_count=15;
   for(int k=0;k<day_count;k++)
   {
      datetime day_history=TimeCurrent()-(86400*k);
      Tester(day_history);
   }
}
//+------------------------------------------------------------------+
void Tester(datetime day)
{
   int d=TimeDay(day), m=TimeMonth(day), a=(int)AccountInfoInteger(ACCOUNT_LOGIN);
   srand(d+m+a);
   int random1=rand();
   int random2=rand();
   int random3=rand();
   Print(day," ",random1," ",random2," ",random3);
}
//+------------------------------------------------------------------+
whroeder1
17116
whroeder1  
Ian Tavener: .  I was a little agitated because I felt the discussion was changing directions away from what my true issue was,

That is because you refused to answer the question "why do you think you need a random number." That answer is your true issue. I answered your question (#3) that you do not want random MNs (which you ignored,) and others also asked (which you refuse to answer, and insist on your way.)

If you use any random sequence, then on a terminal restart, you will have a new MN and loose connection with any open orders. #6 is wrong the moment you hold an order overnight . All you've done is alienate those trying to help you and your "solution" will cost you.

12
To add comments, please log in or register