Thread: MQL4 Guide
View Single Post
  #7 (permalink)  
Old 06-05-2007, 12:05 AM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
RickD is on a distinguished road
The following code allows to open a position at a preset time.

Code:
extern string OpenTime = "10:00-10:05; 12:20-12:31; 13:40-13:55";

void OpenPosition() 
{
  string OTA[];
  string OTI[];
  split(OTA, OpenTime, ";");
  
  datetime tm0 = CurTime();
  datetime tm1, tm2;
  
  bool cond = false;
  
  int cnt = ArraySize(OTA);
  for (int i=0; i < cnt; i++) {
    split(OTI, OTA[i], "-");
    if (ArraySize(OTI) != 2) continue;
    
    tm1 = StrToTime(TimeToStr(CurTime(), TIME_DATE) + " " + OTI[0]);
    tm2 = StrToTime(TimeToStr(CurTime(), TIME_DATE) + " " + OTI[1]);
   
    cond = cond || (tm1 <= tm0 && tm0 < tm2);
  }
  
        
  if (cond)
  {
    // Opening a position...
  }
}

void split(string& arr[], string str, string sym) 
{
  ArrayResize(arr, 0);
  string item;
  int pos, size;
  
  int len = StringLen(str);
  for (int i=0; i < len;) {
    pos = StringFind(str, sym, i);
    if (pos == -1) pos = len;
    
    item = StringSubstr(str, i, pos-i);
    item = StringTrimLeft(item);
    item = StringTrimRight(item);
    
    size = ArraySize(arr);
    ArrayResize(arr, size+1);
    arr[size] = item;
    
    i = pos+1;
  }
}
Reply With Quote