#property copyright "Tom Geller http://www.pipsleader.com modified 11/10/2008" #property link "http://www.pipsleader.com" extern double Lots = 0.1; extern int Slippage = 2; extern double HowMuchToMake = 0.0; extern double StopLoss = 0.0; extern double TrailingStop = 9999.0; extern string MyComment = "Dont change the below parameters"; extern bool FileDataDontChange = FALSE; extern double KBuySell = 0.0011; extern double KStayOut = 0.0079; extern int PLBreakEven = 9999; extern int BasketProfit = 75; extern int BasketLoss = 9999; extern int Fast_Period = 7; extern int Fast_Price = 1; extern int Slow_Period = 88; extern int Slow_Price = 1; int g_magic_168 = -1061260791; string g_comment_172 = "Pipsleader"; int g_time_180 = 0; int g_count_184 = 0; int g_count_188 = 0; int gi_192 = 0; bool gi_196 = TRUE; double gd_200; double gd_208; double gd_216; double gd_224; double gd_232; double gd_240; double gd_248; int init() { string l_dbl2str_4; for (int l_count_0 = 0; l_count_0 < Bars; l_count_0++) { l_dbl2str_4 = DoubleToStr(l_count_0, 0); ObjectDelete("myx" + l_dbl2str_4); ObjectDelete("myz" + l_dbl2str_4); } g_count_188 = 0; ObjectDelete("Cmmt"); ObjectCreate("Cmmt", OBJ_TEXT, 0, Time[20], High[20] + 5.0 * Point); ObjectSetText("Cmmt", "Trader1=0.0020", 10, "Arial", White); Print("Init happened ", TimeCurrent()); Comment(" "); return (0); } int deinit() { string l_dbl2str_4; for (int l_count_0 = 0; l_count_0 < Bars; l_count_0++) { l_dbl2str_4 = DoubleToStr(l_count_0, 0); ObjectDelete("myx" + l_dbl2str_4); ObjectDelete("myz" + l_dbl2str_4); } g_count_188 = 0; Print("MAX number of orders ", gd_200); Print("MAX equity ", gd_208); Print("MIN equity ", gd_216); Print("Close Everything ", gd_224); Print("Close Proc ", gd_232); Print("Proc Buy ", gd_240); Print("Proc Sell ", gd_248); Print("DE-Init happened ", TimeCurrent()); Comment(" "); return (0); } int start() { int l_file_36; double l_point_0 = Point; double ld_8 = Ask - Bid; int l_ord_total_16 = 0; int l_error_20 = 0; int l_count_24 = 0; int l_count_28 = 0; int l_count_32 = 0; double l_price_40 = 0; double l_price_48 = 0; double ld_56 = 0; double ld_64 = 0; bool li_72 = FALSE; bool li_76 = FALSE; if (g_time_180 != Time[0]) { g_time_180 = Time[0]; g_count_184++; g_count_188++; gi_196 = TRUE; } l_count_24 = 0; for (l_ord_total_16 = OrdersTotal(); l_ord_total_16 >= 0; l_ord_total_16--) { OrderSelect(l_ord_total_16, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_168) { l_count_24++; if (OrderType() == OP_BUY) l_count_28++; if (OrderType() == OP_SELL) l_count_32++; } } if (l_count_24 > gd_200) gd_200 = l_count_24; double ld_80 = divergence(Fast_Period, Slow_Period, Fast_Price, Slow_Price, 0); ObjectDelete("Cmmt"); ObjectCreate("Cmmt", OBJ_TEXT, 0, Time[0], High[0] + 10.0 * l_point_0); ObjectSetText("Cmmt", "Trader1=" + DoubleToStr(ld_80, 4), 10, "Arial", White); if (ld_80 >= KBuySell && ld_80 <= KStayOut) li_72 = TRUE; if (ld_80 <= (-1.0 * KBuySell) && ld_80 >= (-1.0 * KStayOut)) li_76 = TRUE; if (FileDataDontChange) { gi_192++; l_file_36 = FileOpen("iDivergence", FILE_CSV|FILE_WRITE|FILE_READ, ","); FileSeek(l_file_36, 0, SEEK_END); FileWrite(l_file_36, g_count_184, " ", gi_192, " ", ld_80); FileFlush(l_file_36); FileClose(l_file_36); } if (gi_196 && li_72) { if (StopLoss == 0.0) l_price_40 = 0; else l_price_40 = Ask - (StopLoss + 7.0) * Point; if (HowMuchToMake == 0.0) l_price_48 = 0; else l_price_48 = Ask + (HowMuchToMake + 7.0) * Point; OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, l_price_40, l_price_48, g_comment_172, g_magic_168, 16777215); l_error_20 = GetLastError(); if (l_error_20 == 0/* NO_ERROR */) { Print("BUY Ask=", Ask, " bartick=", g_count_184); ObjectCreate("myx" + DoubleToStr(g_count_188, 0), OBJ_TEXT, 0, Time[0], High[0] + 5.0 * l_point_0); ObjectSetText("myx" + DoubleToStr(g_count_188, 0), "B", 15, "Arial", Red); g_count_184 = 0; gi_196 = FALSE; } else Print("-----ERROR----- BUY Ask=", Ask, " error=", l_error_20, " bartick=", g_count_184); } if (gi_196 && li_76) { if (StopLoss == 0.0) l_price_40 = 0; else l_price_40 = Bid + (StopLoss + 7.0) * Point; if (HowMuchToMake == 0.0) l_price_48 = 0; else l_price_48 = Bid - (HowMuchToMake + 7.0) * Point; OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, l_price_40, l_price_48, g_comment_172, g_magic_168, 255); l_error_20 = GetLastError(); if (l_error_20 == 0/* NO_ERROR */) { Print("SELL Bid=", Bid, " bartick=", g_count_184); ObjectCreate("myx" + DoubleToStr(g_count_188, 0), OBJ_TEXT, 0, Time[0], High[0] + 5.0 * l_point_0); ObjectSetText("myx" + DoubleToStr(g_count_188, 0), "S", 15, "Arial", Red); g_count_184 = 0; gi_196 = FALSE; } else Print("-----ERROR----- SELL Bid=", Bid, " error=", l_error_20, " bartick=", g_count_184); } ld_64 = AccountEquity() - AccountBalance(); if (ld_64 > gd_208) gd_208 = ld_64; if (ld_64 < gd_216) gd_216 = ld_64; if (ld_64 >= BasketProfit || ld_64 <= (-1 * BasketLoss)) { CloseEverything(); gd_224++; } for (l_ord_total_16 = 0; l_ord_total_16 < OrdersTotal(); l_ord_total_16++) { OrderSelect(l_ord_total_16, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_168) { if (OrderType() == OP_BUY) { ld_56 = Bid - OrderOpenPrice(); if (ld_56 >= PLBreakEven * l_point_0 && OrderOpenPrice() > OrderStopLoss()) { l_price_40 = OrderOpenPrice() + 2.0 * ld_8; l_price_48 = OrderTakeProfit(); OrderModify(OrderTicket(), OrderOpenPrice(), l_price_40, l_price_48, 16777215); l_error_20 = GetLastError(); if (l_error_20 == 0/* NO_ERROR */) { Print("MODIFY BREAKEVEN BUY Bid=", Bid, " bartick=", g_count_184); ObjectCreate("myz" + DoubleToStr(g_count_188, 0), OBJ_TEXT, 0, Time[0], Low[0] - 7.0 * l_point_0); ObjectSetText("myz" + DoubleToStr(g_count_188, 0), "BE", 15, "Arial", White); } else Print("-----ERROR----- MODIFY BREAKEVEN BUY Bid=", Bid, " error=", l_error_20, " bartick=", g_count_184); } if (ld_56 >= TrailingStop * l_point_0) { l_price_40 = Bid - TrailingStop * l_point_0; l_price_48 = OrderTakeProfit(); OrderModify(OrderTicket(), OrderOpenPrice(), l_price_40, l_price_48, 16777215); l_error_20 = GetLastError(); if (l_error_20 == 0/* NO_ERROR */) { Print("MODIFY TrailingStop BUY StopLoss=", l_price_40, " bartick=", g_count_184, "OrderTicket=", OrderTicket(), " CurrProfit=", ld_56); ObjectCreate("myz" + DoubleToStr(g_count_188, 0), OBJ_TEXT, 0, Time[0], Low[0] - 7.0 * l_point_0); ObjectSetText("myz" + DoubleToStr(g_count_188, 0), "TS", 15, "Arial", White); } else Print("-----ERROR----- MODIFY TrailingStop BUY Bid=", Bid, " error=", l_error_20, " bartick=", g_count_184); } if ((HowMuchToMake > 0.0 && ld_56 >= HowMuchToMake * l_point_0) || (StopLoss > 0.0 && ld_56 <= (-1.0 * StopLoss) * l_point_0)) { OrderClose(OrderTicket(), Lots, Bid, Slippage, White); l_error_20 = GetLastError(); if (l_error_20 == 0/* NO_ERROR */) { Print("CLOSE BUY Bid=", Bid, " bartick=", g_count_184); ObjectCreate("myz" + DoubleToStr(g_count_188, 0), OBJ_TEXT, 0, Time[0], Low[0] - 7.0 * l_point_0); ObjectSetText("myz" + DoubleToStr(g_count_188, 0), "C", 15, "Arial", White); } else Print("-----ERROR----- CLOSE BUY Bid=", Bid, " error=", l_error_20, " bartick=", g_count_184); } } if (OrderType() == OP_SELL) { ld_56 = OrderOpenPrice() - Ask; if (ld_56 >= PLBreakEven * l_point_0 && OrderOpenPrice() < OrderStopLoss()) { l_price_40 = OrderOpenPrice() - 2.0 * ld_8; l_price_48 = OrderTakeProfit(); OrderModify(OrderTicket(), OrderOpenPrice(), l_price_40, l_price_48, 255); l_error_20 = GetLastError(); if (l_error_20 == 0/* NO_ERROR */) { Print("MODIFY BREAKEVEN SELL Ask=", Ask, " bartick=", g_count_184); ObjectCreate("myz" + DoubleToStr(g_count_188, 0), OBJ_TEXT, 0, Time[0], Low[0] - 7.0 * l_point_0); ObjectSetText("myz" + DoubleToStr(g_count_188, 0), "BE", 15, "Arial", Red); } else Print("-----ERROR----- MODIFY BREAKEVEN SELL Ask=", Ask, " error=", l_error_20, " bartick=", g_count_184); } if (ld_56 >= TrailingStop * l_point_0) { l_price_40 = Ask + TrailingStop * l_point_0; l_price_48 = OrderTakeProfit(); OrderModify(OrderTicket(), OrderOpenPrice(), l_price_40, l_price_48, 255); l_error_20 = GetLastError(); if (l_error_20 == 0/* NO_ERROR */) { Print("MODIFY TrailingStop SELL StopLoss=", l_price_40, " bartick=", g_count_184, "OrderTicket=", OrderTicket(), " CurrProfit=", ld_56); ObjectCreate("myz" + DoubleToStr(g_count_188, 0), OBJ_TEXT, 0, Time[0], Low[0] - 7.0 * l_point_0); ObjectSetText("myz" + DoubleToStr(g_count_188, 0), "TS", 15, "Arial", Red); } else Print("-----ERROR----- MODIFY TrailingStop SELL Ask=", Ask, " error=", l_error_20, " bartick=", g_count_184); } if ((HowMuchToMake > 0.0 && ld_56 >= HowMuchToMake * l_point_0) || (StopLoss > 0.0 && ld_56 <= (-1.0 * StopLoss) * l_point_0)) { OrderClose(OrderTicket(), Lots, Ask, Slippage, Red); l_error_20 = GetLastError(); if (l_error_20 == 0/* NO_ERROR */) { Print("CLOSE SELL Ask=", Ask, " bartick=", g_count_184); ObjectCreate("myz" + DoubleToStr(g_count_188, 0), OBJ_TEXT, 0, Time[0], Low[0] - 7.0 * l_point_0); ObjectSetText("myz" + DoubleToStr(g_count_188, 0), "C", 15, "Arial", Red); } else Print("-----ERROR----- CLOSE SELL Ask=", Ask, " error=", l_error_20, " bartick=", g_count_184); } } } } return (0); } int CloseEverything() { double l_ask_0; double l_bid_8; int l_ticket_16; double l_ord_lots_20; int l_cmd_28; bool li_36 = FALSE; for (int l_ord_total_32 = OrdersTotal(); l_ord_total_32 >= 0; l_ord_total_32--) { OrderSelect(l_ord_total_32, SELECT_BY_POS); if (OrderMagicNumber() == g_magic_168) { if (Symbol() == OrderSymbol()) { l_ask_0 = MarketInfo(OrderSymbol(), MODE_ASK); l_bid_8 = MarketInfo(OrderSymbol(), MODE_BID); l_ticket_16 = OrderTicket(); l_ord_lots_20 = OrderLots(); l_cmd_28 = OrderType(); switch (l_cmd_28) { case OP_BUY: if (l_ticket_16 == 0) li_36 = TRUE; else li_36 = OrderClose(l_ticket_16, l_ord_lots_20, l_bid_8, Slippage, Red); gd_240++; break; case OP_SELL: if (l_ticket_16 == 0) li_36 = TRUE; else li_36 = OrderClose(l_ticket_16, l_ord_lots_20, l_ask_0, Slippage, Red); gd_248++; break; case OP_BUYLIMIT: case OP_BUYSTOP: case OP_SELLLIMIT: case OP_SELLSTOP: li_36 = OrderDelete(OrderTicket()); } if (li_36 == FALSE) { Alert("Order ", l_ticket_16, " failed to close. Error:", GetLastError()); Print("Order ", l_ticket_16, " failed to close. Error:", GetLastError()); Sleep(3000); } Sleep(1000); gd_232++; } } } return (0); } double divergence(int a_period_0, int a_period_4, int a_applied_price_8, int a_applied_price_12, int ai_16) { double l_ima_24 = iMA(Symbol(), 0, a_period_0, 0, MODE_SMA, a_applied_price_8, ai_16); double l_ima_40 = iMA(Symbol(), 0, a_period_4, 0, MODE_SMA, a_applied_price_12, ai_16); double ld_56 = l_ima_24 - l_ima_40; double l_ima_32 = iMA(Symbol(), 0, a_period_0, 0, MODE_SMA, a_applied_price_8, ai_16 + 1); double l_ima_48 = iMA(Symbol(), 0, a_period_4, 0, MODE_SMA, a_applied_price_12, ai_16 + 1); double ld_64 = l_ima_24 - l_ima_40 - (l_ima_32 - l_ima_48); return (ld_56 - ld_64); }