Limiting the opening of multiple orders on new bar

Back to topics list  | 1 2 To post a new topic, please log in or register
avatar
19
Tony227 2015.12.09 03:29 #
 

Keelan, Your idea works fine. I have it running on a demo account. I used the following for generating the random delay:

x+(y-x)*MathRand()/32767, x and y being the lower and upper limits of the random number generated. I found this elsewhere in this forum.

Thanks for your help, once again 

 

avatar
12985
WHRoeder 2015.12.09 16:55 #
 
You random just reduces the race conditions. Eliminate it with a mutex.
Not compiled, not tested.
input string GV_Mutex = "Mutex"; 

class Mutex{ 
 Public: 
   void                 Mutex()                     : mName(GV_Mutex), 
                                                      mLocked(false){   Lock();} 
   void                 Mutex(const string& aName)  : mName(aName), 
                                                      mLocked(false){   Lock();} 
   void                ~Mutex(){                                     Release();} 
   void                 Lock(void){ 
      if(mLocked) return 
      SECONDS     startWaitingTime  = 0; 
      if(!GlobalVariableTemp(mName){                     // Attempt to create 
      && _LastError != ERR_GLOBAL_VARIABLES_PROCESSING)  DisableTrading( 
         StringFormat("Mutex: GVSet('%s') Failed: %i",s, _LastError) ); return;} 
      while(true){ 
         if(!IsTradeContextBusy() 
         && GlobalVariableSetOnCondition(mName, 0, 1) ){ 
            if(startWaitingTime != 0){ 
               Comment(WindowExpertName(),": ", VERSION);   WindowRedraw(); 
               SetNeedToRefresh();  // I slept 
            } 
            RefreshIf();   // RefreshRates if slept or previous OrderSend/Modify 
            SetNeedToRefresh();     // Assume will do a OrderSend/Modify/Close. 
            mLocked = true; 
            return; 
         } 
         if(IsStopped() ){ 
            Alert("Mutex: The expert was terminated by the user!");  return;  } 
         if(startWaitingTime == 0) 
            startWaitingTime  = GetTickCount(); // Remember start time. 
         else{ 
            SECONDS  delay = GetTickCount() - startWaitingTime; 
   #define MUTEX_TIMEOUT_MSEC    60000 // Trade context busy maximum delay 
            if(delay > MUTEX_TIMEOUT_MSEC){              // Abort. 
               DisableTrading(StringFormat("Wait time (%i sec) exceeded!", 
                              MUTEX_TIMEOUT_MSEC / 1000) ); return; 
            } 
            Comment(WindowExpertName(), ": ", 
            "Wait until another expert finishes trading... ", delay / 1000.); 
            WindowRedraw(); 
         } 
         Sleep(1000); 
      }  // while 
      //NOT REACHED 
   }  // Lock 
   void                 Release(void){ 
      if(mLocked){ 
         if(!GlobalVariableSet(mName, 0) ) 
            Alert("Mutex: GlobalVariableSet('%s',!LOCK): Error #", 
                  mName, _LastError); 
         mLocked=false; 
      } 
   } 
   void                 IsLocked(void)             const{return mLocked;    } 
 Private: 
   string               mName; 
   bool                 mLocked; 
}; // Mutex 
Not compiled, not tested.

avatar
19
Tony227 2015.12.09 19:32 #
 
Thank you. I will have a go sometime.
Back to topics list   | 1 2  

To add comments, please log in or register