Navigation:Home > Content >

CycleGenerator_v4_(2).mq4

Time: 2018-12-05 | Download file:CycleGenerator_v4_(2).mq4

//+------------------------------------------------------------------+
//|                                           CycleGenerator_v4.mq4  |
//|                                Copyright © 2013, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, TrendLaboratory"
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"

#include 

#property show_inputs
#define pi 3.14159265358979323846

extern string     FileName          = "!VarFreq";
extern int        FileMode          = 0;     //0-.hst,1-.csv
extern int        hstTimeFrame      = 2;     //History TimeFrame (ex.2)
extern string     Periods           = "10;20;30;50;70";  //Array of Periods 
extern string     Amplitudes        = "1";   //Array of Amplitudes
extern string     Phases            = "0";   //Array of Phases in degrees(ex.45;60;90)  
extern string     RandomAmplitudes  = "0";   //Array of Random Apmlitudes(ex.0.1;0;0.5)  
extern int        SinusoidMode      = 1;     //0-Sum of Sinusoids, 1-Frequency-Varying Sinusoid 
extern int        BodyMode          = 0;     //0-off,1-on
extern int        WicksMode         = 0;     //Array of Amplitudes
extern int        RandomWicksHeight = 0;     //0-off,1-on
extern double     WicksHeightPct    = 0;     //Wicks Height Percentage (ex.0.3)
extern double     StartPrice        = 4;     //Start Price  
extern int        digits            = 5;     //Digits
extern int        UseStartDate      = 0;     //0-off,1-on
extern datetime   StartDate         = D'2009.05.01 12:00';  
extern int        UseEndDate        = 0;     //0-off,1-on 
extern datetime   EndDate           = D'2009.05.01 12:00';  


double periods[];
double amps[];
double phases[];
double ramps[];
double sine[];

double prevclose;
//------- 


bool stringToDoubleArray(string text,string delim,double& array[],int check = 0) 
{
   int start;
   int pos;
   int cnt;
   
   if(StringFind(text, delim) < 0) 
   {
   ArrayResize(array, 1);
   array[0] = StrToDouble(text);
   } 
   else 
   {
   start = 0;
   pos = 0;
   cnt = 0;
      while(pos > -1) 
      {
      cnt++;
      pos = StringFind(text, delim, start);
      ArrayResize(array, cnt);
         if (pos > -1) 
         {
         if (pos - start > 0) array[cnt - 1] = StrToDouble(StringSubstr(text, start, pos - start));
         } else array[cnt - 1] = StrToDouble(StringSubstr(text, start, 0));
      
      start = pos + 1;
      }
   }
   
   if (check == 0 || check == ArraySize(array)) return (TRUE);
   
   return (FALSE);
}	         
 
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start() 
{ 
   int i, k;
   double open, high, low, close, value, body;
   
   
   stringToDoubleArray(Periods,";",periods);
   stringToDoubleArray(Amplitudes,";",amps);
   stringToDoubleArray(Phases,";",phases);
   stringToDoubleArray(RandomAmplitudes,";",ramps);
   
   
      if(FileMode == 1) 
      {
      int handle = FileOpen(FileName+".csv", FILE_READ | FILE_WRITE,",");
         if(handle > 0) 
         {
         FileClose(handle); 
         FileDelete(FileName+".csv");
         }
      
      handle = FileOpen(FileName+".csv", FILE_READ | FILE_WRITE,",");
      }
      else  
      {
      handle = FileOpenHistory(FileName + hstTimeFrame + ".hst", FILE_BIN|FILE_WRITE);
      if(handle >=0) FileClose(handle);
      handle = FileOpenHistory(FileName + hstTimeFrame + ".hst", FILE_BIN|FILE_WRITE|FILE_READ);   
      }
      
      
      if(handle < 0)
      {
         if(GetLastError()==4103) 
         Print("No file named ",FileName);
         else 
         Print("Error while opening file ",FileName);
         return; 
      }
      else
      if(handle > 0)
      {
         if(FileMode == 0)    
         {
         // write hst file header
	      int hstUnused[13];
	      FileWriteInteger(handle, 400, LONG_VALUE); 		      // Version
	      FileWriteString (handle, "", 64);					      // Copyright
	      FileWriteString (handle, FileName, 12);			      // Symbol
	      FileWriteInteger(handle, hstTimeFrame, LONG_VALUE);	// Period
	      FileWriteInteger(handle, digits, LONG_VALUE);	      // Digits
	      FileWriteInteger(handle, 0, LONG_VALUE);			      // Time Sign
	      FileWriteInteger(handle, 0, LONG_VALUE);			      // Last Sync
	      FileWriteArray  (handle, hstUnused, 0, 13);		      // Unused
         }
         
         double prevcnt = 0; k = 0;
         
         for(i = Bars-1; i >= 0; i--)  
         { 	
         value = 0;
         if(UseStartDate > 0 && Time[i] < StartDate) continue;
         if(UseEndDate   > 0 && Time[i] > EndDate  ) continue;
      
         
         
         if(SinusoidMode == 0) 
         {
            for(k = 0; k < ArraySize(periods); k++) 
            {
            double rand = 2*ramps[k]*(MathRand()/32767.0 - 0.5);
            
            value += amps[k]*MathSin(pi*(2*(Bars-1-i)/periods[k] + phases[k]/180)) + rand;  
            }
         }      
         else
         { 
            if(Bars-1-i < prevcnt + periods[k]) double cycle = periods[k]; 
            else 
            {  
            prevcnt += periods[k];
            k += 1;
            if(k > ArraySize(periods)-1) k = 0;
            cycle = periods[k]; 
            }
         
         value = amps[0]*MathSin(pi*2*(Bars-1-i-prevcnt)/cycle);
         }   
                        
         close = NormalizeDouble(StartPrice + value,digits);
             
         if(i==Bars-1) prevclose = close;
         
            if(BodyMode  > 0) open = NormalizeDouble(prevclose,digits); else open = close;
            if(WicksMode > 0)
            {
            body = MathAbs(open - close);
               if(RandomWicksHeight > 0)
               {
               high = NormalizeDouble(MathMax(close,open) + body*(MathRand()/32767.0),digits);
               low  = NormalizeDouble(MathMin(close,open) - body*(MathRand()/32767.0),digits);  
               }
               else
               {
               high = NormalizeDouble(MathMax(close,open) + body*WicksHeightPct,digits);
               low  = NormalizeDouble(MathMin(close,open) - body*WicksHeightPct,digits);  
               }
            }
            else {high = MathMax(open,close); low = MathMin(open,close);}
      
      
         bool found = FileSeek(handle, 0, SEEK_END);   
      
            if(found)
            {
               if(FileMode == 1) int h2 = FileWrite(handle,TimeToStr(Time[i],TIME_DATE),TimeToStr(Time[i],TIME_MINUTES),open, high, low, close, Volume[i]);
               else
               {
               FileWriteInteger(handle, Time [i], LONG_VALUE);
		         FileWriteDouble (handle,     open, DOUBLE_VALUE);
               FileWriteDouble (handle,      low, DOUBLE_VALUE);	  
               FileWriteDouble (handle,     high, DOUBLE_VALUE);
               FileWriteDouble (handle,    close, DOUBLE_VALUE);
               FileWriteDouble (handle,Volume[i], DOUBLE_VALUE);
               }
                  
            if(h2 < 0)
            {
            Print("Error writing to the file",GetLastError());
            FileClose(handle);
            return;
            }
         }           
      
      prevclose = close;
      }   

   FileClose(handle); 
   }
   
      
   return;
}   

Recommend