/*
Generated by EX4-TO-MQ4 decompiler FREEWARE V4.0.451.1 [-]
Website: http://www.metaquotes.net
E-mail : support@metaquotes.net
*/
#property copyright "� www.gridtradingcourse.com"
#property link "www.gridtradingcourse.com"
string g_comment_76 = "SMARTrades";
string gs_unused_84 = "1.1";
int gi_92 = 0;
int g_slippage_96 = 0;
int g_color_100 = Blue;
int g_color_104 = Red;
int g_color_108 = Gray;
int g_color_112 = Gray;
int g_color_116 = CLR_NONE;
int g_color_120 = CLR_NONE;
int g_color_124 = CLR_NONE;
int g_magic_128 = 0;
double gd_unused_132 = 1.0;
int gi_140 = 1;
string g_name_144 = "SMARTrades_log.txt";
string g_name_152 = "SMARTrades_errors.txt";
bool gi_160 = FALSE;
int gia_164[1000];
int gia_168[1000];
string gsa_172[1000];
double gda_176[1000];
double gda_180[1000];
double gda_184[1000];
double gda_188[1000];
int gia_192[1000];
bool gba_196[1000];
int g_index_200 = 0;
int g_count_204 = 0;
int g_count_208 = 0;
int g_count_212 = 0;
double gd_216 = 0.0;
int g_count_224 = 0;
double gd_228 = 0.0;
double gd_236 = 0.0;
int gi_244 = 0;
int gi_248 = 0;
double gd_252 = 0.0;
double g_pips_260 = 0.0;
double g_pips_268 = 0.0;
bool gi_276 = TRUE;
bool gi_280 = TRUE;
int gi_284 = 0;
int gi_288 = 0;
bool gba_292[100];
bool gba_296[100];
bool gi_300 = FALSE;
extern string header0 = "================= SYSTEM SETTINGS ==================";
extern bool CloseALL = FALSE;
extern int TradesID = 1;
extern int PendingTrades = 7;
extern int GridDirection = 0;
extern int DelayStartHours = 0;
extern string header1 = "============ INDIVIDUAL TRADES SETTINGS ============";
extern double LotSize = 0.2;
extern int TakeProfit = 90;
extern int StopLoss = 0;
extern string header2 = "=================== ALGO SETTINGS ==================";
extern int TotalTakeProfit = 0;
extern int TotalStopLoss = 0;
extern double GridBase = 0.0;
extern int GridStep = 30;
extern int RestartTakeProfit = 0;
extern int RestartStopLoss = 0;
extern int TrendTimeFrame = 0;
extern bool TrendByBarClose = FALSE;
extern bool TrendByBarMove = FALSE;
extern bool TrendByLineChart = FALSE;
extern bool TrendByPeaks = FALSE;
extern int TrendByGrid = 0;
extern int TradeSideways = 0;
extern string header10 = "================== MISC SETTINGS ==================";
extern int LogMode = 0;
bool gi_unused_432 = TRUE;
bool gi_436 = FALSE;
int gi_440 = 0;
int g_datetime_444 = 0;
int g_digits_448 = 0;
double g_point_452 = 0.0;
double gd_460 = 0.0;
double g_minlot_468 = 0.0;
double g_maxlot_476 = 0.0;
double g_lotstep_484 = 0.0;
double g_tickvalue_492 = 0.0;
double g_ticksize_500 = 0.0;
double g_swaplong_508 = 0.0;
double g_swapshort_516 = 0.0;
double g_margininit_524 = 0.0;
double g_marginmaintenance_532 = 0.0;
double g_marginhedged_540 = 0.0;
double g_marginrequired_548 = 0.0;
double g_freezelevel_556 = 0.0;
int gi_564 = 1;
int g_reason_568 = 0;
int gi_572 = 1;
double g_bid_576 = 0.0;
double gd_584 = 0.0;
double gd_592 = 0.0;
double gd_600 = 0.0;
int gi_608 = 0;
int gi_unused_612 = 0;
int gi_616 = 0;
double gd_620 = 0.0;
double gd_628 = 0.0;
int gi_636 = 0;
int gi_640 = EMPTY_VALUE;
bool gi_644 = FALSE;
int gi_648 = 0;
int g_count_652 = 0;
int g_count_656 = 0;
bool gi_660 = TRUE;
string gs_664 = "";
double gd_672 = 0.0;
double gd_680 = 0.0;
double gd_688 = 0.0;
double g_free_magrin_696 = 1410065407.0;
int SmartLog(string as_0, bool ai_8 = FALSE, int ai_12 = 2147483647) {
bool li_ret_16 = TRUE;
int file_20 = 0;
string ls_24 = TimeToStr(TimeCurrent()) + " (local:" + TimeToStr(TimeLocal()) + ") - ";
if (ai_12 == EMPTY_VALUE) ai_12 = gi_92;
else {
if (ai_12 == 3) {
if (gi_92 == 0) {
if (IsOptimization()) return (li_ret_16);
if (IsTesting()) ai_12 = 1;
else ai_12 = 2;
} else ai_12 = gi_92;
}
}
if (ai_12 < 0) return (li_ret_16);
if (ai_12 == 0) {
if (IsOptimization() || IsTesting()) return (li_ret_16);
ai_12 = 2;
}
if (ai_12 > 1) {
file_20 = FileOpen(g_name_144, FILE_CSV|FILE_WRITE|FILE_READ, ';');
if (file_20 > 0) {
FileSeek(file_20, 0, SEEK_END);
FileWrite(file_20, ls_24 + as_0);
}
FileClose(file_20);
}
if (ai_12 > 0) Print(as_0);
if (ai_8) Alert(as_0);
return (li_ret_16);
}
int SmartError(string as_0, bool ai_8 = FALSE) {
bool li_ret_12 = TRUE;
int li_16 = gi_92;
int file_20 = 0;
string ls_24 = TimeToStr(TimeCurrent()) + " (local:" + TimeToStr(TimeLocal()) + ") - ";
string ls_32 = "";
int error_40 = GetLastError();
if (error_40 != 0/* NO_ERROR */) ls_32 = " (GetLastError()=" + DoubleToStr(error_40, 0) + ")";
if (li_16 < 0) return (li_ret_12);
if (li_16 == 0) {
if (IsOptimization() || IsTesting()) return (li_ret_12);
li_16 = 2;
}
if (li_16 > 1) {
file_20 = FileOpen(g_name_152, FILE_CSV|FILE_WRITE|FILE_READ, ';');
if (file_20 > 0) {
FileSeek(file_20, 0, SEEK_END);
FileWrite(file_20, ls_24 + as_0 + ls_32);
}
FileClose(file_20);
}
SmartLog(as_0 + ls_32, ai_8);
return (li_ret_12);
}
int OpenSmartTrade(string as_0, int ai_8, double ad_12, double ad_20, double ad_28, double ad_36) {
int li_ret_44 = 1;
int li_48 = g_index_200;
int digits_52 = Digits;
if (ai_8 != 17 && ai_8 != 18) {
li_ret_44 = -20;
SmartError("OpenSmartTrade: ERROR: Invalid order type.");
} else {
if (ad_12 <= 0.0 || ad_20 <= 0.0 || ad_28 < 0.0 || ad_36 < 0.0) {
li_ret_44 = -10;
SmartError("OpenSmartTrade: ERROR: One (or more) of the parameters is out of range (zero or negative).");
} else {
if (li_48 >= 1000) {
li_ret_44 = -30;
SmartError("OpenSmartTrade: ERROR: Max allowed orders (" + DoubleToStr(1000, 0) + ") reached. Current order is declined.");
} else {
gia_164[li_48] = 0;
gia_168[li_48] = ai_8;
gsa_172[li_48] = as_0;
gda_176[li_48] = ad_12;
gda_180[li_48] = NormalizeDouble(ad_20, digits_52);
gda_184[li_48] = NormalizeDouble(ad_28, digits_52);
gda_188[li_48] = NormalizeDouble(ad_36, digits_52);
gia_192[li_48] = 0;
gba_196[li_48] = 0;
g_index_200++;
SmartLog("OpenSmartTrade: new trade " + SmartTradeToStr(li_48));
}
}
}
return (li_ret_44);
}
int UpdateSmartTrade(int ai_0, string as_4, int ai_12, double ad_16, double ad_24, double ad_32, double ad_40, bool ai_48 = TRUE) {
int li_ret_52 = 1;
int li_56 = g_index_200;
int digits_60 = Digits;
int index_64 = 0;
bool li_68 = FALSE;
if (ai_12 != 17 && ai_12 != 18) {
li_ret_52 = -20;
SmartError("OpenSmartTrade: ERROR: Invalid order type.");
} else {
if (ad_16 <= 0.0 || ad_24 <= 0.0 || ad_32 < 0.0 || ad_40 < 0.0) {
li_ret_52 = -10;
SmartError("OpenSmartTrade: ERROR: One (or more) of the parameters is out of range (zero or negative).");
} else {
if (li_56 >= 1000) {
li_ret_52 = -30;
SmartError("OpenSmartTrade: ERROR: Max allowed orders (" + DoubleToStr(1000, 0) + ") reached. Current order is declined.");
} else {
for (index_64 = 0; index_64 < g_index_200; index_64++) {
if (gia_164[index_64] == ai_0) {
li_56 = index_64;
li_68 = TRUE;
break;
}
}
gia_164[li_56] = ai_0;
gia_168[li_56] = ai_12;
gsa_172[li_56] = as_4;
gda_176[li_56] = ad_16;
gda_180[li_56] = NormalizeDouble(ad_24, digits_60);
gda_184[li_56] = NormalizeDouble(ad_32, digits_60);
gda_188[li_56] = NormalizeDouble(ad_40, digits_60);
gia_192[li_56] = 0;
gba_196[li_56] = ai_48;
if (li_68) SmartLog("UpdateSmartTrade: trade updated " + SmartTradeToStr(li_56));
else {
g_index_200++;
SmartLog("UpdateSmartTrade: trade added " + SmartTradeToStr(li_56));
}
}
}
}
return (li_ret_52);
}
string SmartTypeToStr(int ai_0) {
if (ai_0 == 0) return ("BUY");
if (ai_0 == 1) return ("SELL");
if (ai_0 == 17) return ("BUYSMART");
if (ai_0 == 18) return ("SELLSMART");
if (ai_0 == 4) return ("BUYSTOP");
if (ai_0 == 5) return ("SELLSTOP");
if (ai_0 == 2) return ("BUYLIMIT");
if (ai_0 == 3) return ("SELLLIMIT");
return ("");
}
string SmartTradeToStr(int ai_0) {
string ls_ret_4 = "";
if (ai_0 >= 0 && ai_0 < g_index_200) {
ls_ret_4 = SmartTypeToStr(gia_168[ai_0]) + " " + gsa_172[ai_0] + " " + DoubleToStr(gda_176[ai_0], 2) + "lots at " + DoubleToStr(gda_180[ai_0], Digits) + " SL:" + DoubleToStr(gda_184[ai_0],
Digits) + " TP:" + DoubleToStr(gda_188[ai_0], Digits);
if (gia_164[ai_0] == 0) ls_ret_4 = ls_ret_4 + " (not placed yet)";
else
if (gia_164[ai_0] > 0) ls_ret_4 = DoubleToStr(gia_164[ai_0], 0) + " " + ls_ret_4;
}
return (ls_ret_4);
}
int GetSmartTradesCount() {
return (g_index_200);
}
int CloseAllSmartTrades() {
bool li_ret_0 = TRUE;
int index_4 = 0;
if (g_index_200 > 0) {
for (index_4 = 0; index_4 < g_index_200; index_4++) gia_192[index_4] = 2;
gi_160 = TRUE;
}
return (li_ret_0);
}
int DeleteAllPendingSmartTrades(bool ai_0 = TRUE, bool ai_4 = TRUE) {
bool li_ret_8 = TRUE;
int index_12 = 0;
int cmd_16 = 0;
if (g_index_200 > 0) {
for (index_12 = 0; index_12 < g_index_200; index_12++) {
if (gia_164[index_12] == FALSE) {
if (gia_168[index_12] == 17) {
if (!(ai_0)) continue;
SmartTradesRemove(index_12);
index_12--;
continue;
}
if (gia_168[index_12] == 18) {
if (ai_4) {
SmartTradesRemove(index_12);
index_12--;
}
}
} else {
if (OrderSelect(gia_164[index_12], SELECT_BY_TICKET) == TRUE) {
if (OrderCloseTime() != 0) {
SmartTradesRemove(index_12);
index_12--;
continue;
}
cmd_16 = OrderType();
if (cmd_16 == OP_BUYSTOP || cmd_16 == OP_BUYLIMIT) {
if (!(ai_0)) continue;
gia_192[index_12] = 1;
continue;
}
if (cmd_16 == OP_SELLSTOP || cmd_16 == OP_SELLLIMIT)
if (ai_4) gia_192[index_12] = 1;
}
}
}
}
return (li_ret_8);
}
int SmartTradesRemove(int ai_0) {
int li_ret_4 = 1;
int index_8 = 0;
if (ai_0 < 0 || ai_0 >= g_index_200) li_ret_4 = -10;
for (index_8 = ai_0; index_8 < g_index_200; index_8++) {
gia_164[index_8] = gia_164[index_8 + 1];
gia_168[index_8] = gia_168[index_8 + 1];
gsa_172[index_8] = gsa_172[index_8 + 1];
gda_176[index_8] = gda_176[index_8 + 1];
gda_180[index_8] = gda_180[index_8 + 1];
gda_184[index_8] = gda_184[index_8 + 1];
gda_188[index_8] = gda_188[index_8 + 1];
gia_192[index_8] = gia_192[index_8 + 1];
gba_196[index_8] = gba_196[index_8 + 1];
}
if (g_index_200 > 0) g_index_200--;
gia_164[g_index_200] = 0;
gia_168[g_index_200] = 0;
gsa_172[g_index_200] = "";
gda_176[g_index_200] = 0;
gda_180[g_index_200] = 0;
gda_184[g_index_200] = 0;
gda_188[g_index_200] = 0;
gia_192[g_index_200] = 0;
gba_196[g_index_200] = 0;
return (li_ret_4);
}
int SmartCountTrades() {
bool li_ret_0 = TRUE;
int index_4 = 0;
if (g_index_200 > 0) {
g_count_204 = 0;
g_count_208 = 0;
g_count_212 = 0;
gd_216 = 0;
g_count_224 = 0;
gd_228 = 0;
for (index_4 = 0; index_4 < g_index_200; index_4++) {
if (gia_164[index_4] > 0) {
if (OrderSelect(gia_164[index_4], SELECT_BY_TICKET) == TRUE) {
if (OrderCloseTime() == 0) {
if (OrderType() == OP_BUY) {
g_count_204++;
g_count_212++;
gd_216 += OrderLots();
continue;
}
if (OrderType() == OP_SELL) {
g_count_204++;
g_count_224++;
gd_228 += OrderLots();
continue;
}
g_count_208++;
}
}
} else g_count_208++;
}
}
return (li_ret_0);
}
int SmartTradesInit(string a_comment_0, string as_8, int a_magic_16 = 0, int ai_20 = 0) {
bool li_ret_24 = TRUE;
if (g_index_200 <= 0) {
g_index_200 = 0;
gi_160 = FALSE;
g_comment_76 = a_comment_0;
gs_unused_84 = as_8;
g_magic_128 = a_magic_16;
gi_92 = ai_20;
ArrayInitialize(gia_164, 0);
ArrayInitialize(gia_168, 0);
ArrayInitialize(gda_176, 0);
ArrayInitialize(gda_180, 0);
ArrayInitialize(gda_184, 0);
ArrayInitialize(gda_188, 0);
ArrayInitialize(gia_192, 0);
ArrayInitialize(gba_196, 0);
}
g_name_144 = g_comment_76 + "_log.txt";
g_name_152 = g_comment_76 + "_errors.txt";
if (Digits == 4 || Digits == 2) {
gd_unused_132 = 1;
gi_140 = 1;
} else {
if (Digits == 5 || Digits == 3) {
gd_unused_132 = 0.1;
gi_140 = 10;
}
}
return (li_ret_24);
}
int SmartSetLogFilename(string as_0) {
bool li_ret_8 = TRUE;
g_name_144 = as_0 + "_log.txt";
g_name_152 = as_0 + "_errors.txt";
return (li_ret_8);
}
int SmartTradesProcess() {
bool li_ret_0 = TRUE;
int index_4 = 0;
int li_8 = 0;
double price_12 = 0;
double price_20 = 0;
int cmd_28 = 0;
double price_32 = 0;
int li_40 = 0;
if (g_index_200 > 0) {
if (gi_160) {
gi_160 = FALSE;
for (index_4 = 0; index_4 < g_index_200; index_4++) {
if (gia_192[index_4] == 2) {
if (gia_164[index_4] > 0) {
RefreshRates();
if (OrderSelect(gia_164[index_4], SELECT_BY_TICKET) == TRUE) {
if (OrderCloseTime() != 0) {
SmartTradesRemove(index_4);
index_4--;
continue;
}
if (OrderType() == OP_BUY) {
if (OrderClose(gia_164[index_4], OrderLots(), Bid, g_slippage_96 * gi_140, g_color_108) == 1) {
SmartLog("SmartTradesProcess: Order CLOSED - " + SmartTradeToStr(index_4));
SmartTradesRemove(index_4);
index_4--;
continue;
}
SmartError("ERROR: SmartTradesProcess: Can NOT CLOSE order, will try again - " + SmartTradeToStr(index_4));
gi_160 = TRUE;
continue;
}
if (OrderType() == OP_SELL) {
if (OrderClose(gia_164[index_4], OrderLots(), Ask, g_slippage_96 * gi_140, g_color_112) == 1) {
SmartLog("SmartTradesProcess: Order CLOSED - " + SmartTradeToStr(index_4));
SmartTradesRemove(index_4);
index_4--;
continue;
}
SmartError("ERROR: SmartTradesProcess: Can NOT CLOSE order, will try again - " + SmartTradeToStr(index_4));
gi_160 = TRUE;
}
}
}
}
}
}
for (index_4 = 0; index_4 < g_index_200; index_4++) {
if (gia_164[index_4] == 0 && gia_192[index_4] == 0) {
price_12 = NormalizeDouble(gda_188[index_4], Digits);
price_20 = NormalizeDouble(gda_184[index_4], Digits);
RefreshRates();
if (gia_168[index_4] == 17) {
li_8 = NormalizeDouble(MathAbs(Ask - gda_180[index_4]) / Point / gi_140, 0);
if (li_8 >= 5) {
if (gda_180[index_4] > Ask) cmd_28 = 4;
else cmd_28 = 2;
gia_164[index_4] = OrderSend(gsa_172[index_4], cmd_28, gda_176[index_4], gda_180[index_4], g_slippage_96, price_20, price_12, g_comment_76, g_magic_128, 0, g_color_100);
if (gia_164[index_4] < 0) {
SmartError("ERROR: SmartTradesProcess: Can NOT place pending order - " + SmartTradeToStr(index_4));
gia_164[index_4] = 0;
continue;
}
gba_196[index_4] = 1;
continue;
}
if (!(Ask >= gda_180[index_4] && li_8 <= g_slippage_96)) continue;
gia_164[index_4] = OrderSend(gsa_172[index_4], OP_BUY, gda_176[index_4], Ask, g_slippage_96, price_20, price_12, g_comment_76, g_magic_128, 0, g_color_100);
if (gia_164[index_4] < 0) {
SmartError("ERROR: SmartTradesProcess: Can NOT place market order - " + SmartTradeToStr(index_4));
gia_164[index_4] = 0;
continue;
}
gba_196[index_4] = 1;
continue;
}
li_8 = NormalizeDouble(MathAbs(gda_180[index_4] - Bid) / Point / gi_140, 0);
if (li_8 >= 5) {
if (gda_180[index_4] > Bid) cmd_28 = 3;
else cmd_28 = 5;
gia_164[index_4] = OrderSend(gsa_172[index_4], cmd_28, gda_176[index_4], gda_180[index_4], g_slippage_96, price_20, price_12, g_comment_76, g_magic_128, 0, g_color_104);
if (gia_164[index_4] < 0) {
SmartError("ERROR: SmartTradesProcess: Can NOT place pending order - " + SmartTradeToStr(index_4));
gia_164[index_4] = 0;
continue;
}
gba_196[index_4] = 1;
continue;
}
if (Bid <= gda_180[index_4] && li_8 <= g_slippage_96) {
gia_164[index_4] = OrderSend(gsa_172[index_4], OP_SELL, gda_176[index_4], Bid, g_slippage_96, price_20, price_12, g_comment_76, g_magic_128, 0, g_color_104);
if (gia_164[index_4] < 0) {
SmartError("ERROR: SmartTradesProcess: Can NOT place market order - " + SmartTradeToStr(index_4));
gia_164[index_4] = 0;
continue;
}
gba_196[index_4] = 1;
}
}
}
for (index_4 = 0; index_4 < g_index_200; index_4++) {
if (gia_164[index_4] > 0 && gia_192[index_4] == 0) {
if (OrderSelect(gia_164[index_4], SELECT_BY_TICKET) == TRUE) {
if (OrderCloseTime() == 0) {
if (!(!gba_196[index_4])) continue;
price_12 = NormalizeDouble(OrderTakeProfit(), Digits);
price_20 = NormalizeDouble(OrderStopLoss(), Digits);
cmd_28 = OrderType();
price_32 = 0;
li_40 = 0;
RefreshRates();
if (gda_184[index_4] == price_20 && gda_188[index_4] == price_12) {
gba_196[index_4] = 1;
continue;
}
if (gda_184[index_4] != price_20) {
if (cmd_28 == OP_BUY || cmd_28 == OP_BUYSTOP || cmd_28 == OP_BUYLIMIT) {
if (cmd_28 == OP_BUY && Bid <= gda_184[index_4]) price_32 = Bid;
else {
li_8 = NormalizeDouble(MathAbs(Bid - gda_184[index_4]) / Point / gi_140, 0);
if (li_8 >= 5) li_40 = 1;
}
} else {
if (cmd_28 == OP_SELL && Ask >= gda_184[index_4]) price_32 = Ask;
else {
li_8 = NormalizeDouble(MathAbs(gda_184[index_4] - Ask) / Point / gi_140, 0);
if (li_8 >= 5) li_40 = 1;
}
}
}
if (gda_188[index_4] != price_12) {
if (cmd_28 == OP_BUY || cmd_28 == OP_BUYSTOP || cmd_28 == OP_BUYLIMIT) {
if (cmd_28 == OP_BUY && Bid >= gda_188[index_4]) price_32 = Bid;
else {
li_8 = NormalizeDouble(MathAbs(Bid - gda_188[index_4]) / Point / gi_140, 0);
if (li_8 >= 5) li_40 = 1;
}
} else {
if (cmd_28 == OP_SELL && Ask <= gda_188[index_4]) price_32 = Ask;
else {
li_8 = NormalizeDouble(MathAbs(gda_188[index_4] - Ask) / Point / gi_140, 0);
if (li_8 >= 5) li_40 = 1;
}
}
}
if (price_32 > 0.0) {
if (OrderClose(gia_164[index_4], OrderLots(), price_32, g_slippage_96, g_color_108) == 1) {
SmartLog("SmartTradesProcess: Order CLOSED (at modify) - " + SmartTradeToStr(index_4));
SmartTradesRemove(index_4);
index_4--;
continue;
}
SmartError("ERROR: SmartTradesProcess: Can NOT CLOSE order (at modify) - " + SmartTradeToStr(index_4));
gia_192[index_4] = 2;
continue;
}
if (li_40 <= 0) continue;
if (OrderModify(gia_164[index_4], OrderOpenPrice(), gda_184[index_4], gda_188[index_4], 0, g_color_124) == TRUE) {
SmartLog("SmartTradesProcess: Order modified - " + SmartTradeToStr(index_4));
gba_196[index_4] = 1;
continue;
}
SmartError("ERROR: SmartTradesProcess: Can NOT MODIFY order, will continue trying - " + SmartTradeToStr(index_4));
continue;
}
SmartTradesRemove(index_4);
index_4--;
}
}
}
for (index_4 = 0; index_4 < g_index_200; index_4++) {
if (gia_192[index_4] >= 1) {
if (gia_164[index_4] > 0) {
if (OrderSelect(gia_164[index_4], SELECT_BY_TICKET) != TRUE) continue;
if (OrderType() == OP_BUYSTOP || OrderType() == OP_BUYLIMIT) {
if (OrderDelete(gia_164[index_4], g_color_116) == 1) {
SmartLog("SmartTradesProcess: Order DELETED - " + SmartTradeToStr(index_4));
SmartTradesRemove(index_4);
index_4--;
continue;
}
SmartError("ERROR: SmartTradesProcess: Can NOT DELETE pending order - " + SmartTradeToStr(index_4) + ".Will try again on the next tick.");
continue;
}
if (!(OrderType() == OP_SELLSTOP || OrderType() == OP_SELLLIMIT)) continue;
if (OrderDelete(gia_164[index_4], g_color_120) == 1) {
SmartLog("SmartTradesProcess: Order DELETED - " + SmartTradeToStr(index_4));
SmartTradesRemove(index_4);
index_4--;
continue;
}
SmartError("ERROR: SmartTradesProcess: Can NOT DELETE pending order - " + SmartTradeToStr(index_4) + ".Will try again on the next tick.");
continue;
}
SmartTradesRemove(index_4);
index_4--;
}
}
}
return (li_ret_0);
}
int SmartTradesDeInit() {
bool li_ret_0 = TRUE;
return (li_ret_0);
}
int Smart_GridLineByPrice(double ad_0, double ad_8, double ad_16) {
bool li_ret_24 = FALSE;
double ld_28 = 0;
if (ad_0 < 0.0 || ad_8 <= 0.0 || ad_16 < 0.0) return (0);
ld_28 = (ad_16 - ad_0) / Point;
ld_28 = NormalizeDouble(ld_28 / ad_8, 4);
ld_28 = MathFloor(ld_28);
li_ret_24 = NormalizeDouble(ld_28, 0);
return (li_ret_24);
}
double Smart_PriceByGridLine(int ai_0, double ad_4 = 0.0, double ad_12 = 0.0) {
double ld_20 = 0;
if (ad_4 <= 0.0) ad_4 = gd_236;
if (ad_12 <= 0.0) ad_12 = gi_244;
else ad_12 = NormalizeDouble(ad_12 * gi_140, 0);
ld_20 = NormalizeDouble(ad_4 + ai_0 * ad_12 * Point, Digits);
return (ld_20);
}
int Smart_GRIDSetup(double ad_0, int ai_8, int ai_12, double ad_16) {
int li_ret_24 = 1;
if (ad_0 < 0.0 || ai_8 <= 0 || ai_12 <= 0 || ad_16 <= 0.0) li_ret_24 = -10;
gd_236 = ad_0;
gi_244 = NormalizeDouble(ai_8 * gi_140, 0);
gi_248 = ai_12;
gd_252 = ad_16;
return (li_ret_24);
}
int Smart_SetTakeProfit(int ai_0, bool ai_unused_4 = FALSE) {
int li_ret_8 = 1;
if (ai_0 < 0) li_ret_8 = -10;
g_pips_260 = NormalizeDouble(ai_0 * gi_140, 0);
return (li_ret_8);
}
int Smart_SetStopLoss(int ai_0, bool ai_unused_4 = FALSE) {
int li_ret_8 = 1;
if (ai_0 < 0) li_ret_8 = -10;
g_pips_268 = NormalizeDouble(ai_0 * gi_140, 0);
return (li_ret_8);
}
int Smart_GRIDDirection(int ai_0, int ai_4) {
bool li_ret_8 = TRUE;
if (gi_276 != ai_0 || gi_280 != ai_4) gi_300 = TRUE;
gi_276 = ai_0;
gi_280 = ai_4;
if ((!ai_0) || !ai_4) DeleteAllPendingSmartTrades(!ai_0, !ai_4);
return (li_ret_8);
}
int SMART_GRIDForceTradesPlacement() {
bool li_ret_0 = TRUE;
gi_300 = TRUE;
return (li_ret_0);
}
int Smart_GRIDProcess(bool ai_0 = TRUE) {
bool li_ret_4 = TRUE;
int index_8 = 0;
int li_12 = 0;
int li_16 = 0;
int li_20 = 0;
int li_24 = 0;
double ld_28 = 0;
double ld_36 = 0;
double ld_44 = 0;
double ld_52 = 0;
double ld_60 = 0;
gi_288 = Smart_GridLineByPrice(gd_236, gi_244, Bid);
if (gi_288 != gi_284 || gi_300) {
gi_300 = FALSE;
ld_28 = (Ask - Bid) / Point / gi_140;
li_24 = Smart_GridLineByPrice(0, gi_244, ld_28 * Point * gi_140);
li_12 = gi_288 + MathFloor(gi_248 / 2);
li_16 = li_12 - gi_248 + 1;
li_12 += li_24;
ArrayInitialize(gba_292, 0);
ArrayInitialize(gba_296, 0);
if (ai_0) {
if (g_index_200 > 0) {
for (index_8 = 0; index_8 < g_index_200; index_8++) {
if (gia_192[index_8] == 0) {
li_20 = Smart_GridLineByPrice(gd_236, gi_244, gda_180[index_8]);
if (li_20 >= li_16 && li_20 <= li_12) {
if (gia_168[index_8] == 17 || gia_168[index_8] == 0 || gia_168[index_8] == 4 || gia_168[index_8] == 2) {
gba_292[li_20 - li_16] = 1;
continue;
}
gba_296[li_20 - li_16] = 1;
}
}
}
}
if (gi_276) {
for (index_8 = 0; index_8 < gi_248; index_8++) {
if (!(gba_292[index_8 + li_24])) {
ld_36 = NormalizeDouble(gd_236 + ld_28 * Point * gi_140 + (li_16 + index_8) * gi_244 * Point, Digits);
if (g_pips_260 <= 0.0) ld_60 = 0;
else ld_60 = NormalizeDouble(ld_36 + g_pips_260 * Point, Digits);
if (g_pips_268 <= 0.0) ld_52 = 0;
else ld_52 = NormalizeDouble(ld_36 - g_pips_268 * Point, Digits);
ld_44 = gd_252;
OpenSmartTrade(Symbol(), 17, ld_44, ld_36, ld_52, ld_60);
SmartLog("New order: BUY " + DoubleToStr(ld_44, 2) + " " + Symbol() + " at:" + DoubleToStr(ld_36, Digits) + " SL:" + DoubleToStr(ld_52, Digits) + " TP:" + DoubleToStr(ld_60,
Digits) + " BID/ASK:" + DoubleToStr(Bid, Digits) + " / " + DoubleToStr(Ask, Digits));
}
}
}
if (gi_280) {
for (index_8 = 0; index_8 < gi_248; index_8++) {
if (!gba_296[index_8]) {
ld_36 = NormalizeDouble(gd_236 + (li_16 + index_8) * gi_244 * Point, Digits);
if (g_pips_260 <= 0.0) ld_60 = 0;
else ld_60 = NormalizeDouble(ld_36 - g_pips_260 * Point, Digits);
if (g_pips_268 <= 0.0) ld_52 = 0;
else ld_52 = NormalizeDouble(ld_36 + g_pips_268 * Point, Digits);
ld_44 = gd_252;
OpenSmartTrade(Symbol(), 18, ld_44, ld_36, ld_52, ld_60);
SmartLog("New order: SELL " + DoubleToStr(ld_44, 2) + " " + Symbol() + " at:" + DoubleToStr(ld_36, Digits) + " SL:" + DoubleToStr(ld_52, Digits) + " TP:" + DoubleToStr(ld_60,
Digits) + " BID/ASK:" + DoubleToStr(Bid, Digits) + " / " + DoubleToStr(Ask, Digits));
}
}
}
}
SmartCountTrades();
gi_284 = gi_288;
}
return (li_ret_4);
}
void objectCreate(string a_name_0, int a_x_8, int a_y_12, string a_text_16 = "", int a_fontsize_24 = 12, string a_fontname_28 = "Arial", color a_color_36 = -1) {
int window_40 = WindowOnDropped();
if (window_40 >= 0) {
ObjectCreate(a_name_0, OBJ_LABEL, window_40, 0, 0);
ObjectSet(a_name_0, OBJPROP_CORNER, 0);
ObjectSet(a_name_0, OBJPROP_COLOR, a_color_36);
ObjectSet(a_name_0, OBJPROP_XDISTANCE, a_x_8);
ObjectSet(a_name_0, OBJPROP_YDISTANCE, a_y_12);
ObjectSetText(a_name_0, a_text_16, a_fontsize_24, a_fontname_28, a_color_36);
}
}
int init() {
int pos_0 = 0;
int order_total_4 = 0;
string ls_8 = "";
int li_16 = 0;
int li_20 = EMPTY_VALUE;
SmartTradesInit("GRIDAlgo", "1.2", TradesID + 181818, LogMode);
SmartSetLogFilename("GRIDAlgo" + "_" + Symbol() + "_" + DoubleToStr(TradesID, 0));
SmartLog("GRIDAlgo" + " starting...", 0, 3);
SmartLog("(c) www.GridTradingCourse.com, all rights reserved");
SmartLog("Account: " + DoubleToStr(AccountNumber(), 0) + " (" + AccountName() + ") in " + AccountCurrency() + " at " + AccountCompany(), 0, 3);
g_digits_448 = MarketInfo(Symbol(), MODE_DIGITS);
g_point_452 = MarketInfo(Symbol(), MODE_POINT);
g_minlot_468 = MarketInfo(Symbol(), MODE_MINLOT);
g_maxlot_476 = MarketInfo(Symbol(), MODE_MAXLOT);
g_lotstep_484 = MarketInfo(Symbol(), MODE_LOTSTEP);
g_tickvalue_492 = MarketInfo(Symbol(), MODE_TICKVALUE);
g_ticksize_500 = MarketInfo(Symbol(), MODE_TICKSIZE);
g_swaplong_508 = MarketInfo(Symbol(), MODE_SWAPLONG);
g_swapshort_516 = MarketInfo(Symbol(), MODE_SWAPSHORT);
g_margininit_524 = MarketInfo(Symbol(), MODE_MARGININIT);
g_marginmaintenance_532 = MarketInfo(Symbol(), MODE_MARGINMAINTENANCE);
g_marginhedged_540 = MarketInfo(Symbol(), MODE_MARGINHEDGED);
g_marginrequired_548 = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
g_freezelevel_556 = MarketInfo(Symbol(), MODE_FREEZELEVEL);
SmartLog("Leverage: " + DoubleToStr(AccountLeverage(), 0) + " , MinLot= " + DoubleToStr(g_minlot_468, 4) + " , MaxLot= " + DoubleToStr(g_maxlot_476, 4) + " , LotStep= " +
DoubleToStr(g_lotstep_484, 4) + " , Digits= " + DoubleToStr(g_digits_448, 0), 0, 3);
SmartLog("MarginInit= " + DoubleToStr(g_margininit_524, 2) + " , MarginMaintenance= " + DoubleToStr(g_marginmaintenance_532, 2) + " , MarginHedged= " + DoubleToStr(g_marginhedged_540,
2) + " , MarginRequired= " + DoubleToStr(g_marginrequired_548, 2), 0, 3);
SmartLog("SwapLong= " + DoubleToStr(g_swaplong_508, 2) + " , SwapShort= " + DoubleToStr(g_swapshort_516, 2) + " , TickValue= " + DoubleToStr(g_tickvalue_492, Digits) +
" , TickSize= " + DoubleToStr(g_ticksize_500, Digits) + " , FreezeLevel= " + DoubleToStr(g_freezelevel_556, 2), 0, 3);
if (g_digits_448 == 4 || g_digits_448 == 2) {
SmartLog("Regular digits (" + DoubleToStr(g_digits_448, 0) + ") account detected.");
gi_564 = 1;
} else {
if (g_digits_448 == 5 || g_digits_448 == 3) {
SmartLog("Extra digit (" + DoubleToStr(g_digits_448, 0) + ") account detected. The EA will adjust its calculations to accomodate it.");
gi_564 = 10;
} else {
gi_436 = TRUE;
SmartError("ERROR: Unknown digits-size account detected(" + DoubleToStr(g_digits_448, 0) + "). This EA can work with 4(5) or 2(3) digits only!");
}
}
if (MathMod(PendingTrades, 2) != 1.0) {
gi_436 = TRUE;
SmartError("ERROR: PendingTrades setting MUST be an ODD number - 5,7,9,11,etc.");
}
if (PendingTrades >= 100) {
gi_436 = TRUE;
SmartError("ERROR: PendingTrades setting value MUST be below 100!");
}
if (GridBase < -1.0) {
gi_436 = TRUE;
SmartError("ERROR: GridBase setting value MUST be >= -1!");
}
if (GridBase == -1.0) GridBase = NormalizeDouble((iHigh(Symbol(), PERIOD_D1, 1) + iLow(Symbol(), PERIOD_D1, 1)) / 2.0, Digits);
if (TakeProfit < 0) {
gi_436 = TRUE;
SmartError("ERROR: TakeProfit setting value MUST be >= 0!");
}
if (StopLoss < 0) {
gi_436 = TRUE;
SmartError("ERROR: StopLoss setting value MUST be >= 0!");
}
if (TotalTakeProfit != 0) {
if (TotalTakeProfit <= AccountEquity()) {
gi_436 = TRUE;
SmartError("ERROR: TotalTakeProfit setting value MUST be > current account EQUITY (" + DoubleToStr(AccountEquity(), 2) + ")!");
}
}
if (TotalStopLoss != 0) {
if (TotalStopLoss >= AccountEquity()) {
gi_436 = TRUE;
SmartError("ERROR: TotalStopLoss setting value MUST be < current account EQUITY (" + DoubleToStr(AccountEquity(), 2) + ")!");
}
}
if (GridStep <= 0) {
gi_436 = TRUE;
SmartError("ERROR: GridStep setting value MUST be > 0!");
}
if (RestartTakeProfit < 0) {
gi_436 = TRUE;
SmartError("ERROR: RestartTakeProfit setting value MUST be >= 0!");
}
if (RestartStopLoss < 0) {
gi_436 = TRUE;
SmartError("ERROR: RestartStopLoss setting value MUST be >= 0!");
}
if (TrendByGrid < -1) {
gi_436 = TRUE;
SmartError("ERROR: TrendByGrid setting value MUST be >= -1!");
}
if (TrendByGrid == -1) TrendByGrid = GridStep;
if (TrendByGrid == 0) gi_636 = 0;
if (!TrendByPeaks) gi_616 = 0;
if (TrendTimeFrame == -1) TrendTimeFrame = Period();
if (TrendTimeFrame != 0 && TrendTimeFrame != PERIOD_M1 && TrendTimeFrame != PERIOD_M5 && TrendTimeFrame != PERIOD_M15 && TrendTimeFrame != PERIOD_M30 && TrendTimeFrame != PERIOD_H1 &&
TrendTimeFrame != PERIOD_H4 && TrendTimeFrame != PERIOD_D1 && TrendTimeFrame != PERIOD_W1 && TrendTimeFrame != PERIOD_MN1) {
gi_436 = TRUE;
SmartError("ERROR: TrendTimeFrame can be 1,5,15,30,60,1440,10080,43200 only!");
}
if (gd_672 <= 0.0) gd_672 = AccountEquity();
if (gd_584 == 0.0) gd_584 = AccountEquity();
if (RestartTakeProfit > 0) {
gd_592 = gd_584 + RestartTakeProfit;
ls_8 = "RestartTakeProfit Equity=" + DoubleToStr(gd_592, 2);
}
if (RestartStopLoss > 0) {
gd_600 = gd_584 - RestartStopLoss;
ls_8 = ls_8 + " RestartStopLoss Equity=" + DoubleToStr(gd_600, 2);
}
SmartLog(ls_8);
SmartLog("Equity at init(StartEquity)= " + DoubleToStr(gd_584, 2));
Smart_GRIDSetup(GridBase, GridStep, PendingTrades, LotSize);
Smart_SetTakeProfit(TakeProfit);
Smart_SetStopLoss(StopLoss);
if (TrendTimeFrame == 0) gs_664 = "";
order_total_4 = OrdersTotal();
if (order_total_4 > 0) {
SmartLog("Found " + DoubleToStr(order_total_4, 0) + " active orders.");
for (pos_0 = 0; pos_0 < order_total_4; pos_0++) {
if (OrderSelect(pos_0, SELECT_BY_POS, MODE_TRADES) == TRUE) {
if (OrderSymbol() != Symbol() || OrderMagicNumber() != TradesID + 181818) continue;
if (OrderType() == OP_BUY || OrderType() == OP_BUYSTOP || OrderType() == OP_BUYLIMIT) li_16 = 17;
else li_16 = 18;
UpdateSmartTrade(OrderTicket(), OrderSymbol(), li_16, OrderLots(), OrderOpenPrice(), OrderStopLoss(), OrderTakeProfit());
}
}
}
if (GridDirection > 0) {
Smart_GRIDDirection(1, 0);
gs_664 = "GridDirection: UP (" + DoubleToStr(GridDirection, 0) + "). LONG (buy) trades only!";
SmartLog("GridDirection set to UP (" + DoubleToStr(GridDirection, 0) + "). Allowing LONG (buy) trades only!");
} else {
if (GridDirection < 0) {
Smart_GRIDDirection(0, 1);
gs_664 = "GridDirection: DOWN (" + DoubleToStr(GridDirection, 0) + "). SHORT (sell) trades only!";
SmartLog("GridDirection set to DOWN (" + DoubleToStr(GridDirection, 0) + "). Allowing SHORT (sell) trades only!");
}
}
if (DelayStartHours == 0) {
gi_648 = 0;
gi_644 = FALSE;
} else {
if (gi_648 == 0) {
gi_648 = TimeCurrent() + 3600.0 * MathAbs(DelayStartHours);
SmartLog("Activating initial delay till " + TimeToStr(gi_648));
if (DelayStartHours < 0) gi_644 = TRUE;
} else
if (gi_648 > 0) SmartLog("Initial delay is still active till " + TimeToStr(gi_648));
}
if (TrendByGrid > 0 && (!gi_644)) {
if (gi_640 == EMPTY_VALUE) {
for (pos_0 = 0; pos_0 < iBars(NULL, PERIOD_M1) && li_20 == EMPTY_VALUE; pos_0++) li_20 = CalcCrossedLine(PERIOD_M1, pos_0);
if (li_20 != EMPTY_VALUE) {
gi_640 = li_20;
SmartLog("Last grid line crossed was estimated to be at price: " + DoubleToStr(Smart_PriceByGridLine(li_20, GridBase, TrendByGrid), Digits));
} else SmartLog("Can not calculate the last crossed grid line. Will work with the incoming price ticks.");
}
}
if (g_bid_576 == 0.0) g_bid_576 = Bid;
return (0);
}
int deinit() {
SmartTradesDeInit();
SmartLog("Min Free Margin= " + DoubleToStr(g_free_magrin_696, 2) + ", MAX DrawDown= $" + DoubleToStr(gd_680, 2) + ", " + DoubleToStr(gd_688, 2) + "%", 0, 3);
SmartLog("MAX Equity achieved= $" + DoubleToStr(gd_672, 2), 0, 3);
Comment("");
if (ObjectFind("GridAlgo") >= 0) ObjectDelete("GridAlgo");
if (ObjectFind("GridAlgo Text") >= 0) ObjectDelete("GridAlgo Text");
g_reason_568 = UninitializeReason();
if (g_reason_568 == REASON_ACCOUNT) SmartLog("UninitializeReason: Other account activated.", 0, 3);
else {
if (g_reason_568 == REASON_PARAMETERS) SmartLog("UninitializeReason: Inputs parameters was changed by user.", 0, 3);
else {
if (g_reason_568 == REASON_CHARTCLOSE) SmartLog("UninitializeReason: Chart closed.", 0, 3);
else {
if (g_reason_568 == REASON_CHARTCHANGE) SmartLog("UninitializeReason: Symbol or timeframe changed on the chart.", 0, 3);
else {
if (g_reason_568 == REASON_RECOMPILE) SmartLog("UninitializeReason: Expert recompiled.", 0, 3);
else {
if (g_reason_568 == REASON_REMOVE) SmartLog("UninitializeReason: Expert removed from chart.", 0, 3);
else {
if (g_reason_568 == 0) SmartLog("UninitializeReason: Script finished its execution independently.", 0, 3);
else SmartLog("UninitializeReason: Unknown, code:" + DoubleToStr(g_reason_568, 0), 0, 3);
}
}
}
}
}
}
return (0);
}
int CalcCrossedLine(int a_timeframe_0, int ai_4) {
int li_8 = 0;
int li_12 = 0;
int li_16 = 0;
int li_20 = 0;
int li_ret_24 = EMPTY_VALUE;
double ld_28 = NormalizeDouble(TrendByGrid * gi_564, 0);
li_8 = Smart_GridLineByPrice(GridBase, ld_28, iHigh(NULL, a_timeframe_0, ai_4));
li_12 = Smart_GridLineByPrice(GridBase, ld_28, iLow(NULL, a_timeframe_0, ai_4));
if (li_8 != li_12) li_ret_24 = li_8;
else {
if (ai_4 > 0) {
li_16 = Smart_GridLineByPrice(GridBase, ld_28, iHigh(NULL, a_timeframe_0, ai_4 - 1));
li_20 = Smart_GridLineByPrice(GridBase, ld_28, iLow(NULL, a_timeframe_0, ai_4 - 1));
if (li_12 > li_16) li_ret_24 = li_12;
else
if (li_8 < li_20) li_ret_24 = li_20;
}
}
return (li_ret_24);
}
int CalcCrossedLineTick(double ad_0) {
int li_8 = 0;
int li_12 = 0;
int li_ret_16 = EMPTY_VALUE;
double ld_20 = NormalizeDouble(TrendByGrid * gi_564, 0);
li_8 = Smart_GridLineByPrice(GridBase, ld_20, ad_0);
li_12 = Smart_GridLineByPrice(GridBase, ld_20, Bid);
if (li_8 != li_12) li_ret_16 = MathMax(li_8, li_12);
return (li_ret_16);
}
int start() {
int li_unused_0 = 0;
int li_4 = 0;
string ls_8 = "";
double ld_16 = 0;
int li_unused_24 = 0;
string ls_28 = "";
string ls_36 = "";
double ihigh_44 = 0;
double ihigh_52 = 0;
double ld_60 = 0;
double ld_68 = 0;
int li_76 = 0;
bool li_80 = FALSE;
int li_84 = 0;
bool li_88 = FALSE;
double ld_unused_92 = 0;
double ld_unused_100 = 0;
bool li_108 = FALSE;
int count_112 = 0;
int count_116 = 0;
int li_120 = 0;
if (ObjectFind("GridAlgo") < 0) objectCreate("GridAlgo", 390, 0, "GridAlgo v1.2", 12, "Arial", Blue);
else {
ObjectSet("GridAlgo", OBJPROP_CORNER, 0);
ObjectSet("GridAlgo", OBJPROP_COLOR, Blue);
ObjectSet("GridAlgo", OBJPROP_XDISTANCE, 390);
ObjectSet("GridAlgo", OBJPROP_YDISTANCE, 0);
ObjectSetText("GridAlgo", "GridAlgo v1.2", 12, "Arial", Blue);
}
if (ObjectFind("GridAlgo Text") < 0) objectCreate("GridAlgo Text", 353, 20, "www.GridTradingCourse.com", 10, "Arial", Blue);
else {
ObjectSet("GridAlgo Text", OBJPROP_CORNER, 0);
ObjectSet("GridAlgo Text", OBJPROP_COLOR, Blue);
ObjectSet("GridAlgo Text", OBJPROP_XDISTANCE, 353);
ObjectSet("GridAlgo Text", OBJPROP_YDISTANCE, 20);
ObjectSetText("GridAlgo Text", "www.GridTradingCourse.com", 10, "Arial", Blue);
}
if (!IsConnected()) ls_36 = "NO connection (as reported by MT4)!";
else {
if (gi_436) ls_36 = "CRITICAL ERROR encountered. Please check the Experts Tab for more details.";
else {
if (gi_440 == 100) ls_36 = "All trades were closed. You need to restart the EA to resume trading.";
else {
if (gi_440 > 0 || CloseALL) ls_36 = "Closing trades";
else {
if (gi_648 > 0) ls_36 = "No trading allowed, delayed till " + TimeToStr(gi_648);
else ls_36 = "trading";
}
}
}
}
ls_28 = "\n"
+ "GRIDAlgo" + " v" + "1.2" + " running!\n";
ls_28 = ls_28
+ "\n";
if (RestartTakeProfit > 0) ls_28 = ls_28 + " Restart TP: " + DoubleToStr(gd_592, 2);
if (RestartStopLoss > 0) ls_28 = ls_28 + " Restart SL: " + DoubleToStr(gd_600, 2);
if (TotalStopLoss > 0) ls_28 = ls_28 + " Total SL: " + DoubleToStr(TotalStopLoss, 2);
if (TotalTakeProfit > 0) ls_28 = ls_28 + " Total TP: " + DoubleToStr(TotalTakeProfit, 2);
ls_28 = ls_28
+ "\n";
ls_28 = ls_28 + "Max Drawdown: " + DoubleToStr(gd_688, 2) + "% ($" + DoubleToStr(gd_680, 2) + ") , Highest Equity (achieved): " + DoubleToStr(gd_672, 2)
+ "\n";
ls_28 = ls_28 + "Long/buys: " + DoubleToStr(g_count_212, 0) + "(" + DoubleToStr(gd_216, 2) + " lots)" + " , Short/sells: " + DoubleToStr(g_count_224, 0) + "(" + DoubleToStr(gd_228, 2) + " lots)"
+ "\n";
if (IsTesting()) {
ls_28 = ls_28
+ "\nBalance: " + DoubleToStr(AccountBalance(), 2) + " , FreeMargin: " + DoubleToStr(AccountFreeMargin(), 2) + " , Equity: " + DoubleToStr(AccountEquity(), 2)
+ "\n";
}
if (StringLen(gs_664) > 0) {
ls_28 = ls_28
+ "\n"
+ gs_664
+ "\n";
}
ls_28 = ls_28
+ "\nSTATUS: " + ls_36 + StringSubstr("..........", 0, gi_572);
gi_572++;
if (gi_572 > 3) gi_572 = 1;
Comment(ls_28);
if (gd_672 < AccountEquity()) gd_672 = AccountEquity();
if (gd_672 - AccountEquity() > gd_680) {
gd_680 = gd_672 - AccountEquity();
gd_688 = 100.0 * (gd_680 / gd_672);
}
if (AccountFreeMargin() < g_free_magrin_696) g_free_magrin_696 = AccountFreeMargin();
if (!gi_436) {
gd_460 = MathRound((Ask - Bid) / Point / gi_564);
g_point_452 = MarketInfo(Symbol(), MODE_POINT);
g_digits_448 = MarketInfo(Symbol(), MODE_DIGITS);
if (CloseALL) {
if (GetSmartTradesCount() > 0) {
gi_440 = 1;
SmartLog("CloseALL activated - starting to close/delete all trades!");
}
}
if (TotalTakeProfit > 0) {
if (AccountEquity() >= TotalTakeProfit) {
if (GetSmartTradesCount() > 0) {
gi_440 = 10;
SmartLog("TotalTakeProfit level (" + DoubleToStr(TotalTakeProfit, 2) + ") reached (Equity=" + DoubleToStr(AccountEquity(), 2) + "). Closing all trades now!");
}
}
}
if (TotalStopLoss > 0) {
if (AccountEquity() <= TotalStopLoss) {
if (GetSmartTradesCount() > 0) {
gi_440 = 11;
SmartLog("TotalStopLoss level (" + DoubleToStr(TotalStopLoss, 2) + ") reached (Equity=" + DoubleToStr(AccountEquity(), 2) + "). Closing all trades now!");
}
}
}
if (RestartTakeProfit > 0) {
if (AccountEquity() >= gd_592) {
if (GetSmartTradesCount() > 0) {
gi_440 = 20;
SmartLog("RestartTakeProfit level at " + DoubleToStr(gd_592, 2) + " (" + DoubleToStr(RestartTakeProfit, 2) + ") reached (Equity=" + DoubleToStr(AccountEquity(), 2) +
"). Closing all trades now!");
}
}
}
if (RestartStopLoss > 0) {
if (AccountEquity() <= gd_600) {
if (GetSmartTradesCount() > 0) {
gi_440 = 21;
SmartLog("RestartStopLoss level at " + DoubleToStr(gd_600, 2) + " (" + DoubleToStr(RestartStopLoss, 2) + ") reached (Equity=" + DoubleToStr(AccountEquity(), 2) + "). Closing all trades now!");
}
}
}
gi_644 = FALSE;
if (gi_648 > 0) {
if (TimeCurrent() >= gi_648) {
SmartLog("Delay period of " + DoubleToStr(MathAbs(DelayStartHours), 0) + " hours has passed. Trading is activated now!");
gi_648 = -1;
gi_660 = TRUE;
gi_644 = FALSE;
} else {
if (DelayStartHours > 0) gi_660 = FALSE;
else gi_644 = TRUE;
}
}
if (!gi_644) {
if (gi_440 == 0) {
if (TrendTimeFrame > 0) {
if (iTime(NULL, TrendTimeFrame, 0) != g_datetime_444) {
li_108 = FALSE;
g_count_652 = 0;
g_count_656 = 0;
if (TrendByBarClose) {
g_count_652++;
if (iClose(NULL, TrendTimeFrame, 1) > iOpen(NULL, TrendTimeFrame, 1)) g_count_656++;
if (iClose(NULL, TrendTimeFrame, 1) < iOpen(NULL, TrendTimeFrame, 1)) g_count_656--;
}
if (TrendByBarMove) {
g_count_652++;
if (iHigh(NULL, TrendTimeFrame, 1) > iHigh(NULL, TrendTimeFrame, 2) && iLow(NULL, TrendTimeFrame, 1) > iLow(NULL, TrendTimeFrame, 2)) g_count_656++;
if (iHigh(NULL, TrendTimeFrame, 1) < iHigh(NULL, TrendTimeFrame, 2) && iLow(NULL, TrendTimeFrame, 1) < iLow(NULL, TrendTimeFrame, 2)) g_count_656--;
}
if (TrendByLineChart) {
g_count_652++;
if (iClose(NULL, TrendTimeFrame, 1) > iClose(NULL, TrendTimeFrame, 2)) g_count_656++;
else
if (iClose(NULL, TrendTimeFrame, 1) < iClose(NULL, TrendTimeFrame, 2)) g_count_656--;
}
if (TrendByPeaks) {
li_4 = 2;
gd_620 = 0;
ld_60 = 0;
gd_628 = 0;
ld_68 = 10000000;
li_76 = 0;
li_80 = FALSE;
li_84 = 0;
li_88 = FALSE;
while (gd_620 == 0.0 || gd_628 == 0.0 && li_4 < Bars) {
if (gd_620 == 0.0) {
ihigh_44 = iHigh(NULL, TrendTimeFrame, li_4);
ihigh_52 = iHigh(NULL, TrendTimeFrame, li_4 - 1);
if (ihigh_44 < ihigh_52) li_76 = -1;
if (ihigh_44 > ihigh_52) li_76 = 1;
if (li_80 == TRUE && li_76 == -1) gd_620 = ihigh_52;
else {
ld_16 = MathMax(ihigh_44, ihigh_52);
if (ld_16 > ld_60) ld_60 = ld_16;
}
li_80 = li_76;
}
if (gd_628 == 0.0) {
ihigh_44 = iLow(NULL, TrendTimeFrame, li_4);
ihigh_52 = iLow(NULL, TrendTimeFrame, li_4 - 1);
if (ihigh_44 < ihigh_52) li_84 = -1;
if (ihigh_44 > ihigh_52) li_84 = 1;
if (li_88 == -1 && li_84 == 1) gd_628 = ihigh_52;
else {
ld_16 = MathMin(ihigh_44, ihigh_52);
if (ld_16 < ld_68) ld_68 = ld_16;
}
li_88 = li_84;
}
li_4++;
}
if (gd_620 > 0.0 && gd_628 > 0.0) {
SmartLog("HIGH Peak found at: " + DoubleToStr(gd_620, Digits) + " , LOW Peak found at: " + DoubleToStr(gd_628, Digits));
if (ld_60 > gd_620 && ld_68 >= gd_628) {
gd_620 = 0;
gi_616 = 1;
li_108 = TRUE;
} else {
if (ld_60 <= gd_620 && ld_68 < gd_628) {
gd_628 = 0;
gi_616 = -1;
li_108 = TRUE;
}
}
}
}
if (GridDirection != 0) {
if (GridDirection > 0) {
Smart_GRIDDirection(1, 0);
gs_664 = "GridDirection: UP (" + DoubleToStr(GridDirection, 0) + "). LONG (buy) trades only!";
SmartLog("GridDirection set to UP (" + DoubleToStr(GridDirection, 0) + "). Allowing LONG (buy) trades only!");
} else {
Smart_GRIDDirection(0, 1);
gs_664 = "GridDirection: DOWN (" + DoubleToStr(GridDirection, 0) + "). SHORT (sell) trades only!";
SmartLog("GridDirection set to DOWN (" + DoubleToStr(GridDirection, 0) + "). Allowing SHORT (sell) trades only!");
}
} else li_108 = TRUE;
g_datetime_444 = iTime(NULL, TrendTimeFrame, 0);
}
if (GridDirection == 0) {
if (TrendByPeaks) {
count_112++;
if (gd_620 > 0.0) {
if (Bid > gd_620) {
gd_620 = 0;
gi_616 = 1;
li_108 = TRUE;
}
}
if (gd_628 > 0.0) {
if (Bid < gd_628) {
gd_628 = 0;
gi_616 = -1;
li_108 = TRUE;
}
}
if (gi_616 == 1) count_116++;
else
if (gi_616 == -1) count_116--;
}
if (TrendByGrid > 0) {
count_112++;
li_120 = CalcCrossedLineTick(g_bid_576);
if (li_120 != EMPTY_VALUE) {
if (gi_640 != EMPTY_VALUE) {
if (li_120 > gi_640) {
gi_636 = 1;
li_108 = TRUE;
} else {
if (li_120 < gi_640) {
gi_636 = -1;
li_108 = TRUE;
}
}
}
gi_640 = li_120;
}
if (gi_636 == 1) count_116++;
else
if (gi_636 == -1) count_116--;
}
if (li_108) {
if (g_count_652 + count_112 > 0) {
if (g_count_652 + count_112 == g_count_656 + count_116) {
gi_608 = 1;
if (TradeSideways < 0) {
Smart_GRIDDirection(0, 0);
gs_664 = "Trend detected: UP. NO trades allowed.";
SmartLog("Trend detected: UP. NO trades allowed (TrendSideways < 0)!");
} else {
Smart_GRIDDirection(1, 0);
gs_664 = "Trend detected: UP. LONG (buy) trades only.";
SmartLog("Trend detected: UP. Allowing LONG (buy) trades only!");
}
} else {
if (g_count_652 + count_112 == MathAbs(g_count_656 + count_116)) {
gi_608 = -1;
if (TradeSideways < 0) {
Smart_GRIDDirection(0, 0);
gs_664 = "Trend detected: DOWN. NO trades allowed.";
SmartLog("Trend detected: DOWN. NO trades allowed (TrendSideways < 0)!");
} else {
Smart_GRIDDirection(0, 1);
gs_664 = "Trend detected: DOWN. SHORT (sell) trades only.";
SmartLog("Trend detected: DOWN. Allowing SHORT (sell) trades only!");
}
} else {
gi_608 = 0;
if (TradeSideways != 0) {
Smart_GRIDDirection(1, 1);
gs_664 = "NO trend (sideways). Both LONG and SHORT trades allowed.";
SmartLog("NO Trend detected - sideways market. Allowing both LONG (buy) and SHORT (sell) trades!");
} else {
Smart_GRIDDirection(0, 0);
gs_664 = "NO trend (sideways). No trades allowed.";
SmartLog("NO Trend detected - sideways market. No trades are allowed!");
}
}
}
}
}
}
}
Smart_GRIDProcess(gi_660);
} else {
if (gi_440 != 100) {
if (GetSmartTradesCount() > 0) CloseAllSmartTrades();
else {
if (gi_440 == 20 || gi_440 == 21 || gi_440 == 30 || gi_440 == 50 || gi_440 == 60) {
SmartLog("All trades were closed/deleted! Account Balance:" + DoubleToStr(AccountBalance(), 2) + " Equity:" + DoubleToStr(AccountEquity(), 2) + " Margin:" + DoubleToStr(AccountMargin(),
2) + " FreeMargin:" + DoubleToStr(AccountFreeMargin(), 2) + " Trades:" + DoubleToStr(OrdersTotal(), 0));
gi_440 = 0;
gd_584 = AccountEquity();
if (RestartTakeProfit > 0) {
gd_592 = gd_584 + RestartTakeProfit;
ls_8 = "RestartTakeProfit Equity=" + DoubleToStr(gd_592, 2);
}
if (RestartStopLoss > 0) {
gd_600 = gd_584 - RestartStopLoss;
ls_8 = ls_8 + " RestartStopLoss Equity=" + DoubleToStr(gd_600, 2);
}
SMART_GRIDForceTradesPlacement();
SmartLog("Resetting/restarting trading again (StartEquity=" + DoubleToStr(gd_584, 2) + ")");
SmartLog(ls_8);
} else {
SmartLog("All trades were closed/deleted! Account Balance:" + DoubleToStr(AccountBalance(), 2) + " Equity:" + DoubleToStr(AccountEquity(), 2) + " Margin:" + DoubleToStr(AccountMargin(),
2) + " FreeMargin:" + DoubleToStr(AccountFreeMargin(), 2) + " Trades:" + DoubleToStr(OrdersTotal(), 0), 1);
gi_440 = 100;
SmartLog("Trading is discontinued permanently. You need to remove " + "GRIDAlgo" + " from the chart and start it again to resume it.", 1);
}
}
}
}
}
gi_unused_612 = gi_608;
g_bid_576 = Bid;
SmartTradesProcess();
}
return (0);
}