//+------------------------------------------------------------------+ //| RowLearnerCorrectedFinal.mq4 | //| © 2010, EarnForex.com | //| http://www.earnforex.com/ | //+------------------------------------------------------------------+ #property copyright "© 2010, EarnForex" #property link "http://www.earnforex.com/" #define BUY_MAP 1 #define SELL_MAP 0 #define HOLD_MAP 2 #define MAPBASE 10000 #define HOLDBASE 24000 #define VBASE 24 #define DIGITS 10000 int LastBars = 0; double vector[VBASE]; double vectorp[VBASE]; double MapBuy[MAPBASE][VBASE]; double MapSell[MAPBASE][VBASE]; double MapHold[HOLDBASE][VBASE]; extern double MinPips = 5; extern double MaxPips = 43; extern int TakeProfit = 150; extern int StopLoss = 100; extern double Lots = 1; extern double Slippage = 3; extern string MapPath = "rl.txt"; extern string EAName = "RowLearnerCorrectedFinal"; extern double MaxLotSize = 1; extern int MaxOrdNumber = 3; int Magic; double TLots; double NO; //Number of Orders //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { Magic = Period()+1937004; MinPips = MinPips*Point; MaxPips = MaxPips*Point; InitKohonenMap(); LoadKohonenMap(); TLots = Lots; return(0); } //+------------------------------------------------------------------+ //| main expert function | //+------------------------------------------------------------------+ int start() { if (Bars < 7) return(0); //Wait for the new Bar in a chart. if (LastBars == Bars) return(0); else LastBars = Bars; CloseAllOrders(); double abmu[3] = {0, 0, 0}; FormVector(vectorp, false); if ((NormalizeDouble((Open[0] - Open[1]), Digits) >= NormalizeDouble(MinPips, Digits)) && (NormalizeDouble((Open[0] - Open[1]), Digits) <= NormalizeDouble(MaxPips, Digits))) TeachMap(BUY_MAP, vectorp); else if ((NormalizeDouble((Open[0] - Open[1]), Digits) <= -NormalizeDouble(MinPips, Digits)) && (NormalizeDouble((Open[0] - Open[1]), Digits) >= -NormalizeDouble(MaxPips, Digits))) TeachMap(SELL_MAP, vectorp); else TeachMap(HOLD_MAP, vectorp); FormVector(vector); MapLookup(vector, abmu); TLots = (MathFloor(AccountBalance() * 1.5 / 1000)) / 10; NO = 0; if (TLots < 0.1) return(0); if (TLots > MaxLotSize) { NO = MathFloor(TLots / MaxLotSize); if (NO > MaxOrdNumber) NO = MaxOrdNumber; TLots = TLots - NO*MaxLotSize; TLots = TLots * 10; TLots = MathFloor(TLots); TLots = TLots / 10; } if ((abmu[0] < abmu[1]) && (abmu[0] < abmu[2])) Buy(); else if ((abmu[1] < abmu[0]) && (abmu[1] < abmu[2])) Sell(); return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { SaveKohonenMap(); } //+------------------------------------------------------------------+ //| Close all open orders | //+------------------------------------------------------------------+ void CloseAllOrders() { int total = OrdersTotal(); for(int pos = total-1;pos >= 0; pos--) { if(OrderSelect(pos, SELECT_BY_POS) == true) { if (OrderMagicNumber() == Magic) { if (OrderSelect(pos, SELECT_BY_POS, MODE_TRADES) == true) { if (OrderSymbol() == Symbol()) //Check for symbol { int err = 0; int count = 0; while ((err != 1) && (count < 10)) { if (OrdersTotal() == 0) return(0); count++; RefreshRates(); if (OrderType() == OP_SELL) err = OrderClose(OrderTicket(),OrderLots(),Ask,10); //Close position else if (OrderType() == OP_BUY) err = OrderClose(OrderTicket(),OrderLots(),Bid,10); //Close position } } } } } } } void intBuy(double mlots) { int result = -1; int count = 0; while ((result == -1) && (count < 10)) { RefreshRates(); count++; double SL = 0; double TP = 0; if (StopLoss == 0) SL = 0; else SL = Ask-StopLoss*Point; if (TakeProfit == 0) TP = 0; else TP = Ask+TakeProfit*Point; if (IsTradeAllowed()) result = OrderSend(Symbol(),OP_BUY,mlots,Ask,Slippage,SL,TP,EAName,Magic); if (result == -1) { int e = GetLastError(); Print(e); } } } //+------------------------------------------------------------------+ //| Buy | //+------------------------------------------------------------------+ void Buy() { for (int i = 0; i < NO; i++) { intBuy(MaxLotSize); } if ((TLots > 0) && (NO < MaxOrdNumber)) intBuy(TLots); } void intSell(double mlots) { int result = -1; int count = 0; while ((result == -1) && (count < 10)) { RefreshRates(); count++; double SL = 0; double TP = 0; if (StopLoss == 0) SL = 0; else SL = Bid+StopLoss*Point; if (TakeProfit == 0) TP = 0; else TP = Bid-TakeProfit*Point; if (IsTradeAllowed()) result = OrderSend(Symbol(),OP_SELL,mlots,Bid,Slippage,SL,TP,EAName,Magic); if (result == -1) { int e = GetLastError(); Print(e); } } } //+------------------------------------------------------------------+ //| Sell | //+------------------------------------------------------------------+ void Sell() { for (int i = 0; i < NO; i++) { intSell(MaxLotSize); } if ((TLots > 0) && (NO < MaxOrdNumber)) intSell(TLots); } 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 FormVector(double& invector[], bool current = true) { int k = 1; if (!current) k = 2; for (int i = 0; i < 6; i++) { invector[i * 4] = (High[k + i] - Open[1]) * DIGITS; invector[i * 4 + 1] = (Low[k + i] - Open[1]) * DIGITS; invector[i * 4 + 2] = (Close[k + i] - Open[1]) * DIGITS; invector[i * 4 + 3] = (Open[k + i] - Open[1]) * DIGITS; } } void MapLookup(double invector[], double& ABMU[]) { int i = 0; double bsum = 0; double ssum = 0; double hsum = 0; //BUY for (i = 0; i < MAPBASE; i++) { int z = 0; double vec[VBASE]; for (int v = 0; v < VBASE; v++) { if (MapBuy[i][v] == 0) z++; vec[v] = MapBuy[i][v]; } if (z == VBASE) break; double E = EuclidDistance(vec, invector); bsum += E; } ABMU[0] = bsum / (i+1); //SELL for (i = 0; i < MAPBASE; i++) { z = 0; for (v = 0; v < VBASE; v++) { if (MapSell[i][v] == 0) z++; vec[v] = MapSell[i][v]; } if (z == VBASE) break; E = EuclidDistance(vec, invector); ssum += E; } ABMU[1] = ssum / (i+1); //HOLD for (i = 0; i < HOLDBASE; i++) { z = 0; for (v = 0; v < VBASE; v++) { if (MapHold[i][v] == 0) z++; vec[v] = MapHold[i][v]; } if (z == VBASE) break; E = EuclidDistance(vec, invector); hsum += E; } ABMU[2] = hsum / (i+1); } double EuclidDistance(double VectorFromMap[], double invector[]) { double E = 0; double Coef; Coef = 1; for (int v = 0; v < VBASE; v++) { if (v > 19) Coef = 0.1; else if (v > 15) Coef = 0.2; else if (v > 11) Coef = 0.3; else if (v > 7) Coef = 0.4; else if (v > 3) Coef = 0.5; E += MathPow((VectorFromMap[v] - invector[v]), 2)*Coef; } E = MathSqrt(E); return(E); } void TeachMap(int Buy, double invector[]) { 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] = invector[v]; else if (Buy == 0) MapSell[x][v] = invector[v]; else MapHold[x][v] = invector[v]; } } void LoadKohonenMap() { 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); } FileReadArray(handle, MapBuy, 0, MAPBASE*VBASE); FileReadArray(handle, MapSell, 0, MAPBASE*VBASE); FileReadArray(handle, MapHold, 0, HOLDBASE*VBASE); FileClose(handle); } 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); } void PrintVector(double& invector[], int num, int digits = 4) { string print_str = ""; for (int i = 0; i < num; i++) { print_str = print_str + "Cell " + i + ": " + DoubleToStr(invector[i], digits) + " "; } Print(print_str); }