//+------------------------------------------------------------------+ //| StatGathererExample.mq4 | //| © 2008, EarnForex.com | //| http://www.earnforex.com/ | //+------------------------------------------------------------------+ // // This EA gathers statistics over a period of time and stores it to // MapPath file ("rl.txt" by default) in your /tester/files/ directory. // Copy it to /experts/files/ for further use. // #property copyright "© 2008, EarnForex" #property link "http://www.earnforex.com/" #define BUY_MAP 1 #define SELL_MAP 0 #define HOLD_MAP 2 //Map size for BUY and SELL #define MAPBASE 10000 //Map size for HOLD #define HOLDBASE 25000 //Number of map memory-cells for each bar #define VBASE 7 int LastBars = 0; double vectorp[VBASE]; //Vector with memory-cells for previous bar //3 Maps double MapBuy[MAPBASE][VBASE]; double MapSell[MAPBASE][VBASE]; double MapHold[HOLDBASE][VBASE]; //Min and Max amounts of pips of profit to consider teaching BUY and SELL maps extern int MinPips = 5; extern int MaxPips = 43; extern int TakeProfit = 150; extern int StopLoss = 100; extern double Lots = 1; extern double Slippage = 3; extern string MapPath = "rl.txt"; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { InitKohonenMap(); return(0); } //+------------------------------------------------------------------+ //| main expert function | //+------------------------------------------------------------------+ int start() { if (Bars < VBASE) return(0); //Wait for the new Bar in a chart. if (LastBars == Bars) return(0); else LastBars = Bars; //Calculating the Tom Demark's pivot points over last 5 bars double hi[5] = {0,0,0,0,0}; double lo[5] = {0,0,0,0,0}; hi[0] = High[2]; hi[1] = High[3]; hi[2] = High[4]; hi[3] = High[5]; hi[4] = High[6]; lo[0] = Low[2]; lo[1] = Low[3]; lo[2] = Low[4]; lo[3] = Low[5]; lo[4] = Low[6]; double H = hi[ArrayMaximum(hi)]; double L = lo[ArrayMinimum(lo)]; vectorp[0] = (H + L + Close[2]) / 3; //The difference between pivots and Open price is used to normalize statistics vectorp[1] = (2 * vectorp[0] - L) - Open[1]; vectorp[2] = (vectorp[0] + H - L) - Open[1]; vectorp[3] = (H + 2 * (vectorp[0] - L)) - Open[1]; vectorp[4] = (2 * vectorp[0] - H) - Open[1]; vectorp[5] = (vectorp[0] - H + L) - Open[1]; vectorp[6] = (L - 2 * (H - vectorp[0])) - Open[1]; vectorp[0] = vectorp[0] - Open[1]; //Teach Maps according to MinPips and MaxPips parameters if ((NormalizeDouble((Open[0] - Open[1]), Digits) >= NormalizeDouble((MinPips*Point), Digits)) && (NormalizeDouble((Open[0] - Open[1]), Digits) <= NormalizeDouble((MaxPips*Point), Digits))) TeachMap(BUY_MAP, vectorp); else if ((NormalizeDouble((Open[0] - Open[1]), Digits) <= -NormalizeDouble((MinPips*Point), Digits)) && (NormalizeDouble((Open[0] - Open[1]), Digits) >= -NormalizeDouble((MaxPips*Point), Digits))) TeachMap(SELL_MAP, vectorp); else TeachMap(HOLD_MAP, vectorp); return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { SaveKohonenMap(); } void InitKohonenMap() { for (int i = 0; i < MAPBASE; i++) { for (int v = 0; i < VBASE; i++) { MapSell[i][v] = 0; MapBuy[i][v] = 0; } } for (i = 0; i < HOLDBASE; i++) { for (v = 0; i < VBASE; i++) { MapHold[i][v] = 0; } } } void TeachMap(int Buy, double vector[]) { int BMUx = -1; int N; int x; if (Buy == 1) N = MAPBASE; else if (Buy == 0) N = MAPBASE; else N = HOLDBASE; for (x = 0; x < N; x++) { bool flag = false; for (int v = 0; v < VBASE; v++) { if (Buy == 1) {if (MapBuy[x][v] != 0) flag = true;} else if (Buy == 0) {if (MapSell[x][v] != 0) flag = true;} else {if (MapHold[x][v] != 0) flag = true;} } if (flag == false) break; } for (v = 0; v < VBASE; v++) { if (Buy == 1) MapBuy[x][v] = vector[v]; else if (Buy == 0) MapSell[x][v] = vector[v]; else MapHold[x][v] = vector[v]; } } void SaveKohonenMap() { int handle = FileOpen(MapPath, FILE_BIN|FILE_WRITE|FILE_READ); if (handle < 1) { Print("File couldn't be opened; the last error is ", GetLastError()); return(0); } FileWriteArray(handle, MapBuy, 0, MAPBASE*VBASE); FileWriteArray(handle, MapSell, 0, MAPBASE*VBASE); FileWriteArray(handle, MapHold, 0, HOLDBASE*VBASE); }