#property copyright "Copyright © 2009, aamb-forex" #property link "http://www.my-forex-ea.com" extern double InitLot = 0.01; extern double MaxLot = 0.3; extern int Slippage = 3; extern double TargetProfit = 1.0; extern int SpaceSize = 150; extern int BigShift = 10; extern int Magic = 2343234; string gs_116 = "2009.07.01 00:00"; string gs_124 = "This expert advisor works only in test mode until 2009.07.01 00:00. Visit http://www.my-forex-ea.com for more information"; int gi_unused_132 = 0; int gi_unused_136 = 0; double gd_unused_140 = 0.0; int gi_unused_148 = 0; double gd_unused_152 = 0.0; double gd_160 = 0.0; double g_free_magrin_168 = 10000000.0; int g_datetime_176 = 0; double gd_180 = 1.0; double g_ord_lots_188 = 0.0; double g_pips_196 = 400.0; int IsTestingMode() { if (TimeCurrent() >= StrToTime(gs_116)) return (0); return (IsTesting() || IsDemo()); } int init() { if (!IsTestingMode()) Alert(gs_124); else Alert("Visit http://www.my-forex-ea.com for updates regularly"); return (0); } int start() { int l_ticket_12; int l_pos_16; int li_unused_20; double ld_24; if (!IsTestingMode()) { Print(gs_124); return (0); } g_ord_lots_188 = 0; int li_unused_0 = MarketInfo(Symbol(), MODE_SPREAD); if (AccountFreeMargin() < g_free_magrin_168) g_free_magrin_168 = AccountFreeMargin(); if (!IsTesting() && IsTradeContextBusy()) return (0); int l_ticket_4 = 0; int l_pos_8 = 0; for (l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) { if (OrderSelect(l_pos_8, SELECT_BY_POS) != FALSE) { if (OrderSymbol() == Symbol()) { if (OrderMagicNumber() == Magic) { OrderSelect(l_pos_8, SELECT_BY_POS); if (OrderType() <= OP_SELL) { g_datetime_176 = TimeCurrent(); g_ord_lots_188 = OrderLots(); l_ticket_12 = 0; l_ticket_4 = OrderTicket(); l_pos_16 = 0; for (l_pos_16 = 0; l_pos_16 < OrdersTotal(); l_pos_16++) { if (OrderSelect(l_pos_16, SELECT_BY_POS) != FALSE) { if (OrderSymbol() == Symbol()) { if (OrderMagicNumber() == Magic) if (StrToInteger(OrderComment()) == l_ticket_4) l_ticket_12 = OrderTicket(); } } } if (OrderSelect(l_pos_8, SELECT_BY_POS) != FALSE) { if (OrderSelect(l_pos_8, SELECT_BY_POS) != FALSE) { if (OrderLots() <= MaxLot) { if (l_ticket_12 == 0) { li_unused_20 = OrderLots() / InitLot; if (OrderType() == OP_BUY) { Print("Setting protection for BUY"); WaitTradeContext(); l_ticket_12 = OrderSend(Symbol(), OP_SELLSTOP, OrderLots() + InitLot, OrderStopLoss(), Slippage, OrderStopLoss() + SpaceSize * Point, OrderStopLoss() - g_pips_196 * Point, "" + l_ticket_4, Magic); if (l_ticket_12 <= 0) Print("EXECUTION ERROR " + GetLastError()); } else { Print("Setting protection for Sell BUY"); WaitTradeContext(); l_ticket_12 = OrderSend(Symbol(), OP_BUYSTOP, OrderLots() + InitLot, OrderStopLoss(), Slippage, OrderStopLoss() - SpaceSize * Point, OrderStopLoss() + g_pips_196 * Point, "" + l_ticket_4, Magic); if (l_ticket_12 <= 0) Print("EXECUTION ERROR " + GetLastError()); } } } if (OrderSelect(l_pos_8, SELECT_BY_POS) != FALSE) { ld_24 = 0; if (OrderType() == OP_BUY) { ld_24 = g_pips_196 * Point - (OrderTakeProfit() - Bid); if (ld_24 > BigShift * Point) { WaitTradeContext(); if (!(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderStopLoss() + ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderTakeProfit() + ld_24, MarketInfo(Symbol(), MODE_DIGITS)), 0))) break; OrderSelect(l_ticket_12, SELECT_BY_TICKET); WaitTradeContext(); OrderModify(l_ticket_12, NormalizeDouble(OrderOpenPrice() + ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderStopLoss() + ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderTakeProfit() + ld_24, MarketInfo(Symbol(), MODE_DIGITS)), 0); break; } } if (OrderSelect(l_pos_8, SELECT_BY_POS) != FALSE) { if (OrderType() == OP_SELL) { ld_24 = g_pips_196 * Point - (Ask - OrderTakeProfit()); if (ld_24 > BigShift * Point) { WaitTradeContext(); if (OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderStopLoss() - ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderTakeProfit() - ld_24, MarketInfo(Symbol(), MODE_DIGITS)), 0)) { OrderSelect(l_ticket_12, SELECT_BY_TICKET); WaitTradeContext(); OrderModify(l_ticket_12, NormalizeDouble(OrderOpenPrice() - ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderStopLoss() - ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderTakeProfit() - ld_24, MarketInfo(Symbol(), MODE_DIGITS)), 0); break; } Print("Last error " + GetLastError()); break; } } if (OrderSelect(l_pos_8, SELECT_BY_POS) != FALSE) { if (OrderType() == OP_BUY && OrderLots() <= 1.0 * InitLot) { ld_24 = g_pips_196 * Point - (OrderTakeProfit() - Bid); if (ld_24 > 5.0 * Point) { WaitTradeContext(); if (!(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderStopLoss() + ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderTakeProfit() + ld_24, MarketInfo(Symbol(), MODE_DIGITS)), 0))) break; OrderSelect(l_ticket_12, SELECT_BY_TICKET); WaitTradeContext(); OrderModify(l_ticket_12, NormalizeDouble(OrderOpenPrice() + ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderStopLoss() + ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderTakeProfit() + ld_24, MarketInfo(Symbol(), MODE_DIGITS)), 0); break; } } if (OrderSelect(l_pos_8, SELECT_BY_POS) != FALSE) { if (!(OrderType() == OP_SELL && OrderLots() <= 1.0 * InitLot)) break; ld_24 = g_pips_196 * Point - (Ask - OrderTakeProfit()); if (ld_24 <= 5.0 * Point) break; WaitTradeContext(); if (!(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderStopLoss() - ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderTakeProfit() - ld_24, MarketInfo(Symbol(), MODE_DIGITS)), 0))) break; OrderSelect(l_ticket_12, SELECT_BY_TICKET); WaitTradeContext(); OrderModify(l_ticket_12, NormalizeDouble(OrderOpenPrice() - ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderStopLoss() - ld_24, MarketInfo(Symbol(), MODE_DIGITS)), NormalizeDouble(OrderTakeProfit() - ld_24, MarketInfo(Symbol(), MODE_DIGITS)), 0); break; } } } } } } } } } } } double ld_32 = GlobalProfit(); double ld_40 = LastRunProfitAndTime(); int l_price_48 = g_ord_lots_188 / InitLot; Comment("Lot " + l_price_48 + " Current Profit " + ld_32 + " LR " + ld_40 + "\n" + "Target " + TargetProfit + " IL " + InitLot + "\n" + "visit: http://www.my-forex-ea.com for updates"); if (ld_40 > TargetProfit * gd_180 && ld_32 > 0.0 && g_ord_lots_188 > InitLot) { Print("Current Profit " + ld_32 + " LR " + ld_40); Print("$$$$$$$$$$$$ Target Profit Reached"); CloseAll(); return (0); } if (TimeCurrent() - g_datetime_176 > 60) { Print("STARTING NEW RUN"); WaitTradeContext(); OrderSend(Symbol(), OP_BUY, InitLot, Ask, Slippage, Ask - SpaceSize * Point, Ask + g_pips_196 * Point, "START_AAMB", Magic); } return (0); } int CloseAll() { Print(" CLOSING ALL Positions "); int l_pos_0 = 0; for (l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) { if (OrderSelect(l_pos_0, SELECT_BY_POS) != FALSE) { if (OrderSymbol() == Symbol()) { if (OrderMagicNumber() == Magic) { if (OrderType() <= OP_SELL) { Print("*** CLosing Position " + OrderTicket()); WaitTradeContext(); if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, Slippage); else if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, Slippage); CloseAll(); break; } } } } } for (l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) { if (OrderSelect(l_pos_0, SELECT_BY_POS) != FALSE) { if (OrderSymbol() == Symbol()) { if (OrderMagicNumber() == Magic) { if (OrderType() > OP_SELL) { Print("*** CLosing Order " + OrderTicket()); WaitTradeContext(); if (OrderType() > OP_SELL) { OrderDelete(OrderTicket()); CloseAll(); break; } } } } } } return (0); } double GlobalProfit() { int l_pos_0 = 0; gd_160 = 0; for (l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) { if (OrderSelect(l_pos_0, SELECT_BY_POS) != FALSE) { if (OrderSymbol() == Symbol()) { if (OrderMagicNumber() == Magic) if (OrderType() <= OP_SELL) gd_160 += OrderProfit(); } } } return (gd_160); } double LastRunProfitAndTime() { double ld_ret_0 = 0; int l_ticket_8 = 0; int l_pos_12 = 0; for (l_pos_12 = 0; l_pos_12 < OrdersTotal(); l_pos_12++) { if (OrderSelect(l_pos_12, SELECT_BY_POS) != FALSE) { if (OrderSymbol() == Symbol()) { if (OrderMagicNumber() == Magic) { if (OrderType() <= OP_SELL) { l_ticket_8 = OrderTicket(); break; } } } } } while (l_ticket_8 != 0 && OrderSelect(l_ticket_8, SELECT_BY_TICKET)) { l_ticket_8 = StrToInteger(OrderComment()); if (l_ticket_8 > 0 || OrderProfit() < 0.0) ld_ret_0 += OrderProfit(); } return (ld_ret_0); } int WaitTradeContext() { if (IsTesting()) return (0); while (!IsTesting() && IsTradeContextBusy()) { Comment("Wait .."); Sleep(100); } RefreshRates(); Comment("GO..."); Print("Got for trading"); return (0); }