#property copyright "Copyright © Matrix NN expert"
#property link ""
extern int MA_length = 10;
extern int MA_timeframe = 30;
extern int MAtype = 0;
extern double Percent = 0.1;
extern int TradeOnFriday = 0;
extern int slip = 100;
extern bool UseMM = FALSE;
extern double Risk = 5.0;
extern double Lots = 0.1;
extern int TakeProfit = 50;
extern int Stoploss = 150;
extern double TrailingStop = 0.0;
extern int MaxOpenOrders = 1;
extern double MinMarginLevel = 250.0;
extern int Fast_Period = 23;
extern int Fast_Price = 1;
extern int Slow_Period = 84;
extern int Slow_Price = 1;
extern double DivergenceLimit = 0.002;
extern bool Use_V63D_Divergence = TRUE;
extern int PipStep = 30;
extern double IncreasementType = 0.0;
double g_price_192 = 0.0;
double g_ord_stoploss_200 = 0.0;
double gd_208;
double gd_216;
double gd_224 = 0.0;
double gd_232;
int gi_240 = 0;
int gi_244 = 0;
int g_ord_total_248 = 0;
extern int DVLimit = 10;
extern int PipsGoal = 500;
extern int PipsLoss = 500;
int g_color_264 = DodgerBlue;
int gi_unused_268 = 16748574;
int gi_unused_272 = 16748574;
int g_color_276 = Red;
int gi_unused_280 = 255;
int gi_unused_284 = 255;
int gi_unused_288 = 16777215;
string gs_292 = "MATRIX NN 4.0";
string gs_unused_300 = "expert.wav";
int gi_unused_308 = 0;
int gi_unused_312 = 1;
int gi_unused_316 = 2;
extern int GMT = 1;
extern int DST = 1;
extern int OpeningHour = 0;
extern int ClosingHour = 23;
extern int writelog = 0;
int g_magic_340;
int gi_344;
double gd_348;
double g_price_356;
double gd_364;
int init() {
LogWrite(Symbol() + ",M" + Period());
return (0);
}
int start() {
int li_8;
int li_12;
int l_ord_total_16;
int li_20;
int l_day_of_year_32;
int li_0 = 684873;
LogWrite(TimeToStr(TimeCurrent()) + " - " + "Bid=" + Bid);
g_magic_340 = MagicfromSymbol();
Comment(g_magic_340);
if (UseMM == TRUE) {
Lots = AccountEquity() * Risk / 100.0 / 1000.0;
if (Lots > 0.1) NormalizeDouble(Lots, 1);
else NormalizeDouble(Lots, 2);
}
SetupGlobalVariables();
double ld_24 = iATR(NULL, PERIOD_D1, 21, 0);
if (Point == 0.01) ld_24 = 100.0 * ld_24;
if (Point == 0.0001) ld_24 = 10000.0 * ld_24;
TakeProfit = 2.0 * ld_24 / 10.0;
Stoploss = 2.0 * ld_24;
PipStep = ld_24 / 10.0;
if (DayOfWeek() == 6 && Hour() >= 20) {
Comment("weekend");
return (0);
}
if (!(IsTesting() || IsDemo())) {
if (LossCheck()) {
Alert("excessive loss!");
PlaySound("alert.wav");
return (0);
}
}
if (AccountBalance() - AccountEquity() > gi_244) gi_244 = AccountBalance() - AccountEquity();
if (MyOrdersTotal() > g_ord_total_248) g_ord_total_248 = OrdersTotal();
double ld_36 = divergence(Fast_Period, Slow_Period, Fast_Price, Slow_Price, 0);
Comment("maxDrawDown=", gi_244, " maxOrdersOpen=", g_ord_total_248, " Current Divergence = ", ld_36);
gd_232 = PipStep;
if (IncreasementType > 0.0) {
gd_232 = MathSqrt(MyOrdersTotal()) * PipStep;
gd_232 = MathPow(MyOrdersTotal(), IncreasementType) * PipStep;
}
LogWrite("CurrentPipStep=" + gd_232);
gi_344 = 0;
if (Day() != 5 || TradeOnFriday > 0) {
l_ord_total_16 = OrdersTotal();
li_20 = MyOrdersTotal();
LogWrite("OrdersTotal=" + l_ord_total_16);
LogWrite("MyOrdersTotal=" + li_20);
if (li_20 == 0) l_day_of_year_32 = DayOfYear();
if (li_20 > 0) gd_348 = GetPreviousOpenPrice();
else LogWrite("LastPrice=" + gd_348);
li_8 = CheckJustClosedOrder();
if (li_8 != 1) {
gi_240 = FALSE;
gd_348 = GetPreviousOpenPrice();
LogWrite("LastPrice=" + gd_348);
if (MathAbs(ld_36) <= DivergenceLimit) {
if (Hour() < OpeningHour + GMT + DST || Hour() > ClosingHour + GMT + DST) Comment("bad hours.");
else {
if (MAtype == FALSE) {
li_12 = EnterPositionBasedOnOpen();
if (li_12 == 1) {
gi_240 = TRUE;
gi_344 = 2;
}
if (li_12 == 2) {
gi_240 = TRUE;
gi_344 = 1;
}
}
if (MAtype == TRUE) {
li_12 = EnterPositionBasedOnHL();
if (li_12 == 1) {
gi_240 = TRUE;
gi_344 = 2;
}
if (li_12 == 2) {
gi_240 = TRUE;
gi_344 = 1;
}
}
}
}
}
li_20 = MyOrdersTotal();
if (li_20 > 0 && gi_240 == TRUE) {
gd_364 = CalculateAverageOpeningPrice(li_20);
Comment("AveragePrice: ", gd_364, " myTotal: ", li_20);
}
if (gi_240 == TRUE && li_20 > 1) ChangeOpenOrders(0, li_20, gd_364);
if (li_20 > 0) KeepTrackOfStopLoss(gd_364);
}
return (0);
}
void SetupGlobalVariables() {
switch (g_magic_340) {
case 26910:
Percent = 0.15;
TradeOnFriday = 1;
slip = 100;
PipStep = 5;
IncreasementType = 0.0;
DVLimit = 8;
OpeningHour = 6;
ClosingHour = 14;
return;
case 25803:
Percent = 0.25;
TradeOnFriday = 1;
slip = 100;
PipStep = 5;
IncreasementType = 0.0;
DVLimit = 8;
OpeningHour = 7;
ClosingHour = 15;
return;
case 26715:
MA_length = 18;
MA_timeframe = 30;
MAtype = FALSE;
Percent = 0.1;
TradeOnFriday = 1;
slip = 100;
Lots = 0.05;
TakeProfit = 50;
Stoploss = 150;
PipStep = 7;
IncreasementType = 0.0;
DVLimit = 6;
OpeningHour = 6;
ClosingHour = 14;
return;
return;
}
}
double GetPreviousOpenPrice() {
double l_ord_open_price_4;
for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
if(OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES));else;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_340 && OrderComment() == GetCommentForOrder()) {
l_ord_open_price_4 = OrderOpenPrice();
break;
}
}
return (l_ord_open_price_4);
}
int CheckJustClosedOrder() {
int l_datetime_4;
string ls_8;
int l_str2time_16;
int li_24;
int l_hist_total_20 = OrdersHistoryTotal();
bool li_ret_28 = FALSE;
for (int l_pos_0 = 0; l_pos_0 < l_hist_total_20; l_pos_0++) {
if (OrderSelect(l_pos_0, SELECT_BY_POS, MODE_HISTORY) == TRUE) {
if (OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_340 && OrderComment() == GetCommentForOrder()) {
l_datetime_4 = OrderCloseTime();
ls_8 = Year() + "-" + Month() + "-" + Day() + " " + Hour() + ":" + Minute() + ":" + Seconds();
l_str2time_16 = StrToTime(ls_8);
li_24 = l_str2time_16 - l_datetime_4;
if ((60 * Period()) << 1 > li_24) {
li_ret_28 = TRUE;
break;
}
}
}
}
return (li_ret_28);
}
int EnterPositionBasedOnOpen() {
int li_ret_0;
double l_ima_4 = iMA(NULL, MA_timeframe, MA_length, 0, MODE_SMA, PRICE_OPEN, 0);
double ld_12 = iRVI(NULL, 0, 10, MODE_MAIN, 0) - iRVI(NULL, 0, 10, MODE_MAIN, 1);
if (MyOrdersTotal() < MaxOpenOrders && AccountEquity() / (AccountMargin() + 0.0001) > MinMarginLevel / 100.0) {
if (l_ima_4 * (Percent / 100.0 + 1.0) < Bid && gi_344 != 22 && Bid >= gd_348 + gd_232 * Point || MyOrdersTotal() == 0&& ld_12 < 0.0) {
if (OrderSend(Symbol(), OP_SELL, Lots, Bid, slip, Bid + Stoploss * Point, Bid - TakeProfit * Point, GetCommentForOrder(), g_magic_340, 0, g_color_276));else;
li_ret_0 = 1;
PlaySound("ricochet.wav");
}
if (l_ima_4 * (1 - Percent / 100.0) > Ask && gi_344 != 11 && Ask <= gd_348 - gd_232 * Point || MyOrdersTotal() == 0 && ld_12 > 0.0) {
if(OrderSend(Symbol(), OP_BUY, Lots, Ask, slip, Ask - Stoploss * Point, Ask + TakeProfit * Point, GetCommentForOrder(), g_magic_340, 0, g_color_264));else;
li_ret_0 = 2;
PlaySound("ricochet.wav");
}
}
int l_ord_total_24 = OrdersTotal();
for (int l_pos_20 = 0; l_pos_20 < l_ord_total_24; l_pos_20++) {
if(OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES));else;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_340 && OrderComment() == GetCommentForOrder()) {
if (OrderType() == OP_BUY) {
if (TrailingStop > 0.0) {
if (Bid - OrderOpenPrice() > Point * TrailingStop) {
if (OrderStopLoss() < Bid - Point * TrailingStop) {
if(OrderModify(OrderTicket(), OrderOpenPrice(), Bid - 5.0 * Point, OrderTakeProfit(), 0, GreenYellow));else;
return (0);
}
}
}
}
if (OrderType() == OP_SELL) {
if (TrailingStop > 0.0) {
if (OrderOpenPrice() - Ask > Point * TrailingStop) {
if (OrderStopLoss() > Ask + Point * TrailingStop) {
if(OrderModify(OrderTicket(), OrderOpenPrice(), Ask + 5.0 * Point, OrderTakeProfit(), 0, Red));else ;
return (0);
}
}
}
}
}
}
return (li_ret_0);
}
int EnterPositionBasedOnHL() {
int li_ret_0;
if (MyOrdersTotal() < MaxOpenOrders && AccountEquity() / (AccountMargin() + 0.0001) > MinMarginLevel / 100.0) {
if (iMA(NULL, MA_timeframe, MA_length, 0, MODE_SMA, PRICE_HIGH, 0) * (Percent / 100.0 + 1.0) < Bid && gi_344 != 22 && Bid >= gd_348 + gd_232 * Point || MyOrdersTotal() == 0) {
if(OrderSend(Symbol(), OP_SELL, Lots, Bid, slip, Bid + Stoploss * Point, Bid - TakeProfit * Point, GetCommentForOrder(), g_magic_340, 0, g_color_276));else;
li_ret_0 = 1;
}
if (iMA(NULL, MA_timeframe, MA_length, 0, MODE_SMA, PRICE_LOW, 0) * (1 - Percent / 100.0) > Ask && gi_344 != 11 && Ask <= gd_348 - gd_232 * Point || MyOrdersTotal() == 0) {
if(OrderSend(Symbol(), OP_BUY, Lots, Ask, slip, Ask - Stoploss * Point, Ask + TakeProfit * Point, GetCommentForOrder(), g_magic_340, 0, g_color_264));else;
li_ret_0 = 2;
}
}
int l_ord_total_8 = OrdersTotal();
for (int l_pos_4 = 0; l_pos_4 < l_ord_total_8; l_pos_4++) {
if(OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES));else;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_340 && OrderComment() == GetCommentForOrder()) {
if (OrderType() == OP_BUY) {
if (TrailingStop > 0.0) {
if (Bid - OrderOpenPrice() > Point * TrailingStop) {
if (OrderStopLoss() < Bid - Point * TrailingStop) {
if(OrderModify(OrderTicket(), OrderOpenPrice(), Bid - 5.0 * Point, OrderTakeProfit(), 0, GreenYellow));else;
return (0);
}
}
}
}
if (OrderType() == OP_SELL) {
if (TrailingStop > 0.0) {
if (OrderOpenPrice() - Ask > Point * TrailingStop) {
if (OrderStopLoss() > Ask + Point * TrailingStop) {
if(OrderModify(OrderTicket(), OrderOpenPrice(), Ask + 5.0 * Point, OrderTakeProfit(), 0, Red));else;
return (0);
}
}
}
}
}
}
return (li_ret_0);
}
double CalculateAverageOpeningPrice(int ai_0) {
double ld_ret_8 = 0;
for (int l_pos_4 = OrdersTotal() - 1; l_pos_4 >= 0; l_pos_4--) {
if(OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES));else;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_340 && OrderComment() == GetCommentForOrder()) ld_ret_8 += OrderOpenPrice();
}
ld_ret_8 /= MathMax(ai_0, 1);
return (ld_ret_8);
}
void ChangeOpenOrders(bool ai_0, int ai_4, double ad_8) {
int l_ord_total_20 = OrdersTotal();
for (int l_pos_16 = 0; l_pos_16 < l_ord_total_20; l_pos_16++) {
if(OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES));else;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_340 && OrderComment() == GetCommentForOrder()) {
if (OrderType() == OP_BUY) {
g_price_356 = ad_8 + TakeProfit * Point;
g_price_192 = ad_8 - Stoploss * Point / ai_4;
}
if (OrderType() == OP_SELL) {
g_price_356 = ad_8 - TakeProfit * Point;
g_price_192 = ad_8 + Stoploss * Point / ai_4;
}
if (ai_0) if(OrderModify(OrderTicket(), 0, g_price_192, g_price_356, 0, Yellow));else;
}
}
}
void KeepTrackOfStopLoss(double ad_0) {
int l_cmd_8 = -1;
int l_ord_total_12 = OrdersTotal();
for (int l_pos_16 = 0; l_pos_16 < l_ord_total_12; l_pos_16++) {
if(OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES));else;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_340 && OrderComment() == GetCommentForOrder()) {
g_ord_stoploss_200 = OrderStopLoss();
l_cmd_8 = OrderType();
}
}
gd_208 = ad_0;
gi_344 = 0;
if (l_cmd_8 == OP_BUY) gi_344 = 1;
else
if (l_cmd_8 == OP_SELL) gi_344 = 2;
if (g_ord_stoploss_200 != 0.0) {
gd_224 = MathAbs(gd_208 - g_ord_stoploss_200) / 2.0;
gd_216 = (Bid + Ask) / 2.0;
if (g_ord_stoploss_200 < gd_216 + gd_224 && g_ord_stoploss_200 > gd_216 - gd_224) {
if (gi_344 == 1) gi_344 = 11;
if (gi_344 == 2) gi_344 = 22;
}
g_ord_stoploss_200 = 0;
}
}
int MagicfromSymbol() {
int li_ret_0 = 0;
for (int li_4 = 0; li_4 < 5; li_4++) li_ret_0 = 3 * li_ret_0 + StringGetChar(Symbol(), li_4);
li_ret_0 = 3 * li_ret_0 + Period();
return (li_ret_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 ld_64;
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);
if (Use_V63D_Divergence) ld_64 = l_ima_24 - l_ima_40 - (l_ima_32 - l_ima_48);
else ld_64 = l_ima_32 - l_ima_48;
if (Symbol() == "EURJPY" || Symbol() == "USDJPY" || Symbol() == "CHFJPY" || Symbol() == "AUDJPY" || Symbol() == "CADJPY" || Symbol() == "NZDJPY" || Symbol() == "GBPJPY" ||
Symbol() == "SGDJPY") return ((ld_56 - ld_64) / 100.0);
return (ld_56 - ld_64);
}
bool LossCheck() {
bool li_4;
int l_file_0 = FileOpen(LogFileName(), FILE_CSV|FILE_READ, ";");
if (l_file_0 > 0) {
li_4 = FileReadNumber(l_file_0);
FileClose(l_file_0);
} else li_4 = FALSE;
if (li_4 == 0) {
l_file_0 = FileOpen(LogFileName(), FILE_CSV|FILE_WRITE, ";");
FileWrite(l_file_0, AccountEquity());
FileClose(l_file_0);
}
if (li_4 - AccountEquity() >= PipsLoss * GetSizeLot()) return (TRUE);
return (FALSE);
}
string LogFileName() {
string l_dbl2str_0 = DoubleToStr(Year(), 0);
string l_dbl2str_8 = DoubleToStr(Month(), 0);
if (StringLen(l_dbl2str_8) < 2) l_dbl2str_8 = "0" + l_dbl2str_8;
string l_dbl2str_16 = DoubleToStr(Day(), 0);
if (StringLen(l_dbl2str_16) < 2) l_dbl2str_16 = "0" + l_dbl2str_16;
return (l_dbl2str_0 + l_dbl2str_8 + l_dbl2str_16 + ".log");
}
void LogWrite(string as_0) {
int l_file_8;
if (writelog == 1) {
l_file_8 = FileOpen(gs_292 + ".log", FILE_CSV|FILE_WRITE, ";");
FileSeek(l_file_8, 0, SEEK_END);
FileWrite(l_file_8, as_0);
FileFlush(l_file_8);
FileClose(l_file_8);
}
}
int MyOrdersTotal() {
int l_count_0 = 0;
for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
if (OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES))
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicfromSymbol() && OrderComment() == GetCommentForOrder()) l_count_0++;
}
return (l_count_0);
}
string GetCommentForOrder() {
return (gs_292);
}
double GetSizeLot() {
return (Lots);
}