#property copyright "Copyright © 2008, Grid Trader, FAP-WINNER.com" #property link "http://www.FAP-WINNER.com" extern bool AllTrades_Closer = FALSE; extern bool AllOrders_Remover = FALSE; extern bool ReverseCondition = FALSE; extern double TargetProfit = 0.0; extern double MaximumDrawdown = 0.0; extern double StartGridPrice = 0.7517; extern int NumberOrders = 30; extern int Distance = 9; extern int StopLoss = 500; extern int TakeProfit = 6; extern string Money_Management; extern double Lots = 0.1; extern bool UseMM = FALSE; extern double Risk = 2.0; extern string Settings; extern string ExpertComment = "Grid Trader"; extern color ColorBuy = Blue; extern color ColorSell = Red; extern bool WriteDebugLogs = FALSE; string g_var_name_184 = "GridTrader_TP"; string g_var_name_192 = "GridTrader_MD"; int g_str2time_200; int gi_204; int gi_208; double gd_212; double g_maxlot_220; double g_minlot_228; double g_global_var_236; double g_global_var_244; string g_symbol_252; string gs_260; bool gba_268[]; bool gba_272[]; bool gi_276 = TRUE; int init() { g_symbol_252 = Symbol(); g_maxlot_220 = MarketInfo(g_symbol_252, MODE_MAXLOT); g_minlot_228 = MarketInfo(g_symbol_252, MODE_MINLOT); if (Digits < 4) { gd_212 = 0.01; gi_204 = 2; } else { gd_212 = 0.0001; gi_204 = 4; } g_str2time_200 = StrToTime("2009.12.01 00:00"); gs_260 = " " + AccountCurrency(); ArrayResize(gba_268, NumberOrders); ArrayResize(gba_272, NumberOrders); if (GlobalVariableCheck(g_var_name_184)) g_global_var_236 = GlobalVariableGet(g_var_name_184); if (GlobalVariableCheck(g_var_name_192)) g_global_var_244 = GlobalVariableGet(g_var_name_192); if (ReverseCondition) gi_208 = -1; else gi_208 = 1; return (0); } int deinit() { return (0); } int start() { double l_lots_4; int li_12; double l_price_20; color l_color_28; bool li_32; string ls_36; int l_count_48; double ld_52; int l_datetime_60; int l_datetime_64; int li_68; double l_price_72; double l_price_80; double l_price_88; if (TimeCurrent() > g_str2time_200) { if (gi_276) Alert("Grid Trader Has Expired. Please download the fresh version."); if (WriteDebugLogs) Print("Grid Trader Has Expired. Please download the fresh version."); gi_276 = FALSE; return (0); } int l_spread_0 = MarketInfo(g_symbol_252, MODE_SPREAD); if (UseMM) { l_lots_4 = NormalizeDouble(AccountEquity() * Risk / MarketInfo(g_symbol_252, MODE_LOTSIZE), 2); if (l_lots_4 < g_minlot_228) l_lots_4 = g_minlot_228; else if (l_lots_4 > g_maxlot_220) l_lots_4 = g_maxlot_220; } else l_lots_4 = Lots; if (AllTrades_Closer) { li_12 = OrdersTotal() - 1; for (int l_pos_16 = li_12; l_pos_16 >= 0; l_pos_16--) { if (!OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) { if (WriteDebugLogs) Print("AllTrades_Closer: OrderSelect() error = ", GetLastError()); } else { if (OrderType() <= OP_SELL) { if (OrderSymbol() == g_symbol_252) { if (OrderType() == OP_BUY) { l_price_20 = Bid; l_color_28 = ColorBuy; } else { l_price_20 = Ask; l_color_28 = ColorSell; } li_32 = OrderClose(OrderTicket(), OrderLots(), l_price_20, l_spread_0, l_color_28); if (!li_32) { if (WriteDebugLogs) { if (OrderType() == OP_BUY) ls_36 = "OP_BUY"; else ls_36 = "OP_SELL"; Print("AllTrades_Closer: OrderClose(", ls_36, ") error = ", GetLastError()); } } } } } } } if (AllOrders_Remover) { li_12 = OrdersTotal() - 1; for (l_pos_16 = li_12; l_pos_16 >= 0; l_pos_16--) { if (!OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) { if (WriteDebugLogs) Print("AllOrders_Remover: OrderSelect() error = ", GetLastError()); } else { if (OrderType() > OP_SELL) { if (OrderSymbol() == g_symbol_252) { li_32 = OrderDelete(OrderTicket()); if (!li_32) { if (WriteDebugLogs) { switch (OrderType()) { case OP_BUYSTOP: ls_36 = "OP_BUYSTOP"; break; case OP_SELLSTOP: ls_36 = "OP_SELLSTOP"; break; case OP_BUYLIMIT: ls_36 = "OP_BUYLIMIT"; break; case OP_SELLLIMIT: ls_36 = "OP_SELLLIMIT"; break; default: ls_36 = "???"; } Print("AllOrders_Remover: OrderDelete(", ls_36, ") error = ", GetLastError()); } } } } } } } if (!AllTrades_Closer && !AllOrders_Remover) { l_count_48 = 0; ld_52 = 0; l_datetime_60 = EMPTY_VALUE; li_12 = OrdersTotal() - 1; for (l_pos_16 = li_12; l_pos_16 >= 0; l_pos_16--) { if (!OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) { if (WriteDebugLogs) Print("Equity: OrderSelect() error = ", GetLastError()); } else { if (OrderSymbol() == g_symbol_252) { l_datetime_64 = OrderOpenTime(); if (l_datetime_60 > l_datetime_64) l_datetime_60 = l_datetime_64; ld_52 += OrderProfit() + OrderSwap() + OrderCommission(); l_count_48++; } } } if (l_count_48 > 0) { li_12 = OrdersHistoryTotal() - 1; for (l_pos_16 = li_12; l_pos_16 >= 0; l_pos_16--) { if (!OrderSelect(l_pos_16, SELECT_BY_POS, MODE_HISTORY)) { if (WriteDebugLogs) Print("EquityHystory: OrderSelect() error = ", GetLastError()); } else { if (OrderSymbol() == g_symbol_252) if (OrderCloseTime() >= l_datetime_64) ld_52 += OrderProfit() + OrderSwap() + OrderCommission(); } } } else { g_global_var_236 = NormalizeDouble(TargetProfit / 100.0 * AccountBalance(), 2); GlobalVariableSet(g_var_name_184, g_global_var_236); g_global_var_244 = NormalizeDouble((-1.0 * MaximumDrawdown) / 100.0 * AccountBalance(), 2); GlobalVariableSet(g_var_name_192, g_global_var_244); } Comment("Current Grid Profit = ", DoubleToStr(ld_52, 2), gs_260, "\n", "Target Profit = ", DoubleToStr(g_global_var_236, 2), gs_260, "\n", "Maximum Drawdown = ", DoubleToStr(g_global_var_244, 2), gs_260); if (MaximumDrawdown > 0.0) { if (NormalizeDouble(ld_52, 2) <= g_global_var_244) { if (WriteDebugLogs) Print("Stop Grid: to reach maximum drawdown."); AllTrades_Closer = TRUE; AllOrders_Remover = TRUE; return (0); } } if (TargetProfit > 0.0) { if (NormalizeDouble(ld_52, 2) >= g_global_var_236) { if (WriteDebugLogs) Print("Stop Grid: to reach target profit."); AllTrades_Closer = TRUE; AllOrders_Remover = TRUE; return (0); } } ArrayInitialize(gba_268, 1); ArrayInitialize(gba_272, 1); li_12 = OrdersTotal() - 1; for (l_pos_16 = li_12; l_pos_16 >= 0; l_pos_16--) { if (!OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) { if (WriteDebugLogs) Print("WatchGrid: OrderSelect() error = ", GetLastError()); } else { if (OrderSymbol() == g_symbol_252) { li_68 = OrderMagicNumber(); if (li_68 < 2000) { li_68 -= 1000; if (li_68 < NumberOrders && li_68 >= 0) gba_272[li_68] = 0; } else { li_68 -= 2000; if (li_68 < NumberOrders && li_68 >= 0) gba_268[li_68] = 0; } } } } for (l_pos_16 = 0; l_pos_16 < NumberOrders; l_pos_16++) { if (gba_272[l_pos_16]) { l_price_72 = NormalizeDouble(StartGridPrice + gi_208 * (l_pos_16 + 1) * Distance * gd_212, gi_204); if (StopLoss == 0) l_price_80 = 0; else l_price_80 = Bid + StopLoss * gd_212; if (TakeProfit == 0) l_price_88 = 0; else l_price_88 = l_price_72 - TakeProfit * gd_212; if (l_price_72 > Bid) OrderSend(Symbol(), OP_SELLLIMIT, l_lots_4, l_price_72, l_spread_0, l_price_80, l_price_88, ExpertComment, l_pos_16 + 1000, 0, ColorSell); else OrderSend(Symbol(), OP_SELLSTOP, l_lots_4, l_price_72, l_spread_0, l_price_80, l_price_88, ExpertComment, l_pos_16 + 1000, 0, ColorSell); } } for (l_pos_16 = 0; l_pos_16 < NumberOrders; l_pos_16++) { if (gba_268[l_pos_16]) { l_price_72 = NormalizeDouble(StartGridPrice - gi_208 * (l_pos_16 + 1) * Distance * gd_212, gi_204); if (StopLoss == 0) l_price_80 = 0; else l_price_80 = Ask - StopLoss * gd_212; if (TakeProfit == 0) l_price_88 = 0; else l_price_88 = l_price_72 + TakeProfit * gd_212; if (l_price_72 < Ask) OrderSend(Symbol(), OP_BUYLIMIT, l_lots_4, l_price_72, l_spread_0, l_price_80, l_price_88, ExpertComment, l_pos_16 + 2000, 0, ColorBuy); else OrderSend(Symbol(), OP_BUYSTOP, l_lots_4, l_price_72, l_spread_0, l_price_80, l_price_88, ExpertComment, l_pos_16 + 2000, 0, ColorBuy); } } } return (0); }