#include extern double Lots = 0.0; extern int RiskFactor = 2; extern int MaxLoss = 0; int g_magic_92 = 3200128; double gd_96 = 5.0; double gd_104 = 0.0; int gi_112 = 0; int g_count_116 = 0; double gd_120 = 0.0; int gi_128 = 0; int gi_132 = 0; double g_price_136 = 0.0; double g_ord_open_price_144; double gd_unused_152; double gd_160; int g_datetime_168; int g_datetime_172; int gi_176 = 0; bool gi_180 = FALSE; int g_ticket_184 = 0; void FatalErrorInfo(string a_text_0) { ObjectSetText("lbl_error", a_text_0, 10, "Courier New", Red); ObjectSet("lbl_error", OBJPROP_CORNER, 0); ObjectSet("lbl_error", OBJPROP_XDISTANCE, 300); ObjectSet("lbl_error", OBJPROP_YDISTANCE, 0); gi_176 = -1; } void ShowMessage(string a_text_0) { ObjectSetText("lbl_error", a_text_0, 10, "Courier New", Yellow); ObjectSet("lbl_error", OBJPROP_BACK, TRUE); ObjectSet("lbl_error", OBJPROP_CORNER, 0); ObjectSet("lbl_error", OBJPROP_XDISTANCE, 300); ObjectSet("lbl_error", OBJPROP_YDISTANCE, 0); } double CalcLots(double ad_0, double ad_8) { if (Lots > 0.0) return (Lots); double ld_16 = AccountBalance() * RiskFactor / 100.0; double ld_ret_24 = MathRound(10.0 * ld_16 / (MathAbs(ad_0 - ad_8) * MathPow(10, gi_112 + 1))) / 10.0; if (ld_ret_24 > gd_96) ld_ret_24 = gd_96; return (ld_ret_24); } double findStopLoss(bool ai_0) { int li_4; if (ai_0) { li_4 = Support(5, 1); if (!(li_4 != 0 && li_4 <= 10)) return (0); return (Low[li_4]); } li_4 = Resist(5, 1); if (li_4 != 0 && li_4 <= 10) return (High[li_4]); return (0); } double EMAH(int a_period_0, int ai_4) { return (iMA(NULL, 0, a_period_0, 0, MODE_EMA, PRICE_HIGH, ai_4)); } double EMAL(int a_period_0, int ai_4) { return (iMA(NULL, 0, a_period_0, 0, MODE_EMA, PRICE_LOW, ai_4)); } int PlaceLong() { int l_error_32; double l_ask_8 = Ask; double ld_unused_16 = Bid; double l_price_0 = findStopLoss(1); if (l_price_0 == 0.0 || l_price_0 > l_ask_8 - 30.0 * gd_104) l_price_0 = l_ask_8 - 30.0 * gd_104; double l_lots_24 = CalcLots(l_ask_8, l_price_0); if (l_lots_24 == 0.0) { Print("Can not open long position, risk too high, minimum balance needed/available:", 100.0 * ((l_ask_8 - l_price_0) * MathPow(10, gi_112 + 1)) / RiskFactor, "/", AccountBalance(), ", Risk:", MathAbs(l_ask_8 - l_price_0) * MathPow(10, gi_112 + 1), ", Lots:", RiskFactor * AccountBalance() / (100.0 * MathAbs(l_ask_8 - l_price_0) * MathPow(10, gi_112 + 1)), ", Ask:", l_ask_8, ", Stop Loss:", l_price_0); gi_176 = 1; return (0); } g_ticket_184 = OrderSend(Symbol(), OP_BUY, l_lots_24, l_ask_8, 3, l_price_0, 0, "forexTyphoon", g_magic_92, 0, Green); if (g_ticket_184 > 0) { if (OrderSelect(g_ticket_184, SELECT_BY_TICKET, MODE_TRADES)) { g_price_136 = l_price_0; g_ord_open_price_144 = OrderOpenPrice(); gd_unused_152 = l_price_0; gd_160 = (OrderOpenPrice() - l_price_0) * MathPow(10, gi_112 + 1); Print(" *** BUY order opened : #", g_ticket_184, " at ", OrderOpenPrice(), " stop loss:", g_price_136, ", risk:", gd_160); gi_176 = 2; } } else { g_ticket_184 = 0; l_error_32 = GetLastError(); ShowMessage(ErrorDescription(l_error_32)); Print(" *** Error opening BUY order : ", l_error_32, ", Lots:", l_lots_24, ", Ask:", l_ask_8, ", sl:", l_price_0); } return (0); } int PlaceShort() { int l_error_32; double ld_unused_8 = Ask; double l_bid_16 = Bid; double l_price_0 = findStopLoss(0); if (l_price_0 == 0.0 || l_price_0 < l_bid_16 + 30.0 * gd_104) l_price_0 = l_bid_16 + 30.0 * gd_104; double l_lots_24 = CalcLots(l_bid_16, l_price_0); if (l_lots_24 == 0.0) { Print("Can not open short position, risk too high, minimum balance needed/available:", 100.0 * ((l_price_0 - l_bid_16) * MathPow(10, gi_112 + 1)) / RiskFactor, "/", AccountBalance(), ",Risk:", MathAbs(l_bid_16 - l_price_0) * MathPow(10, gi_112 + 1), ", Lots:", RiskFactor * AccountBalance() / (100.0 * MathAbs(l_bid_16 - l_price_0) * MathPow(10, gi_112 + 1)), ", Bid:", l_bid_16, ", Stop Loss:", l_price_0); gi_176 = 1; return (0); } g_ticket_184 = OrderSend(Symbol(), OP_SELL, l_lots_24, l_bid_16, 3, l_price_0, 0, "forexTyphoon", g_magic_92, 0, Red); if (g_ticket_184 > 0) { if (OrderSelect(g_ticket_184, SELECT_BY_TICKET, MODE_TRADES)) { g_price_136 = l_price_0; g_ord_open_price_144 = OrderOpenPrice(); gd_unused_152 = l_price_0; gd_160 = (l_price_0 - OrderOpenPrice()) * MathPow(10, gi_112 + 1); Print(" *** SELL order opened : #", g_ticket_184, " at ", OrderOpenPrice(), " stop loss:", g_price_136, ", risk:", gd_160); gi_176 = 3; } } else { g_ticket_184 = 0; l_error_32 = GetLastError(); Print(" *** Error opening SELL order : ", l_error_32, ", Lots:", l_lots_24, ", Bid:", l_bid_16, ", sl:", l_price_0); gi_176 = 1; } return (0); } int OpenTrigger() { double l_icustom_16; double l_icustom_24; int li_32; int li_36; double lda_48[10]; double lda_52[10]; if (gi_176 == 1) { l_icustom_16 = iCustom(NULL, 0, "Heiken_Ashi_Smoothed", 2, 6, 3, 2, 0, 0, 0, 0, 1); l_icustom_24 = iCustom(NULL, 0, "Heiken_Ashi_Smoothed", 2, 6, 3, 2, 0, 0, 0, 1, 1); if (l_icustom_16 < l_icustom_24) li_32 = 1; else if (l_icustom_16 > l_icustom_24) li_32 = 2; l_icustom_16 = iCustom(NULL, 0, "Heiken_Ashi_Smoothed", 2, 6, 3, 2, 0, 0, 0, 0, 2); l_icustom_24 = iCustom(NULL, 0, "Heiken_Ashi_Smoothed", 2, 6, 3, 2, 0, 0, 0, 1, 2); if (l_icustom_16 < l_icustom_24) li_36 = 1; else if (l_icustom_16 > l_icustom_24) li_36 = 2; if (li_32 == 1 && li_36 == 2) { Print(" *** Long Trigger, val1:", l_icustom_16, " > val2: ", l_icustom_24, ", ", lda_48[3], ", ", lda_48[4], ", ", lda_48[5], ", ", lda_48[6], ", ", lda_48[7]); return (1); } if (li_32 == 2 && li_36 == 1) { Print(" *** Short Trigger, val1:", l_icustom_16, " < val2: ", l_icustom_24, ", ", lda_48[3], ", ", lda_48[4], ", ", lda_48[5], ", ", lda_48[6], ", ", lda_48[7]); return (2); } } return (0); } int CloseTrigger() { double l_icustom_0; double l_icustom_8; double lda_20[10]; if (gi_176 == 2) { l_icustom_0 = iCustom(NULL, 0, "Heiken_Ashi_Smoothed", 2, 6, 3, 2, 0, 0, 0, 0, 1); l_icustom_8 = iCustom(NULL, 0, "Heiken_Ashi_Smoothed", 2, 6, 3, 2, 0, 0, 0, 1, 1); if (l_icustom_0 > l_icustom_8) { gi_180 = FALSE; Print(" *** Closing long, val1:", l_icustom_0, " > val2: ", l_icustom_8, ", ", lda_20[1], ", ", lda_20[2], ", ", lda_20[3], ", ", lda_20[4], ", ", lda_20[5], ", ", lda_20[6], ", ", lda_20[7]); return (1); } if (!(gi_180)) return (0); if (Close[1] >= EMAH(21, 1)) return (0); gi_180 = FALSE; Print(" *** Closing long, Close[1]:", Close[1], " < EMAH(21,1): ", EMAH(21, 1)); return (3); } if (gi_176 == 3) { l_icustom_0 = iCustom(NULL, 0, "Heiken_Ashi_Smoothed", 2, 6, 3, 2, 0, 0, 0, 0, 1); l_icustom_8 = iCustom(NULL, 0, "Heiken_Ashi_Smoothed", 2, 6, 3, 2, 0, 0, 0, 1, 1); if (l_icustom_0 < l_icustom_8) { Print(" *** Closing short, val1:", l_icustom_0, " < val2: ", l_icustom_8); gi_180 = FALSE; return (2); } if (gi_180) { if (Close[1] > EMAL(21, 1)) { gi_180 = FALSE; Print(" *** Closing short, Close[1]:", Close[1], " > EMAL(21,1): ", EMAL(21, 1), ", ", lda_20[1], ", ", lda_20[2], ", ", lda_20[3], ", ", lda_20[4], ", ", lda_20[5], ", ", lda_20[6], ", ", lda_20[7]); return (4); } } } return (0); } int Support(int ai_0, int ai_4) { for (int li_ret_8 = ai_4 + 1; li_ret_8 < ai_4 + 72; li_ret_8++) if (iLowest(NULL, 0, MODE_LOW, ai_4 + ai_0 + 1, li_ret_8 - ai_4) == li_ret_8) return (li_ret_8); return (0); } int Resist(int ai_0, int ai_4) { for (int li_ret_8 = ai_4 + 1; li_ret_8 < ai_4 + 72; li_ret_8++) if (iHighest(NULL, 0, MODE_HIGH, ai_4 + ai_0 + 1, li_ret_8 - ai_4) == li_ret_8) return (li_ret_8); return (0); } int init() { if (Digits == 2 || Digits == 3) { gd_104 = 0.01; gi_112 = 2; gi_176 = 1; } else { if (Digits == 4 || Digits == 5) { gd_104 = 0.0001; gi_112 = 4; gi_176 = 1; } } ObjectCreate("lbl_error", OBJ_LABEL, 0, 0, 0); ObjectSetText("lbl_error", " ", 10, "Courier New", Red); ObjectSet("lbl_error", OBJPROP_CORNER, 0); ObjectSet("lbl_error", OBJPROP_XDISTANCE, 300); ObjectSet("lbl_error", OBJPROP_YDISTANCE, 0); return (0); } int deinit() { int l_datetime_0; if (g_ticket_184 > 0) { OrderSelect(g_ticket_184, SELECT_BY_TICKET, MODE_TRADES); if (OrderType() == OP_BUY) OrderClose(g_ticket_184, OrderLots(), Bid, 3, Violet); else OrderClose(g_ticket_184, OrderLots(), Ask, 3, Violet); l_datetime_0 = OrderOpenTime(); if (l_datetime_0 <= 0) Alert("Ticket ", g_ticket_184, " is orphaned. Please close it manually"); else { gd_120 += OrderProfit(); Print(" *** Order #", g_ticket_184, " closed on exit, price:", OrderClosePrice(), " ,P/L:", OrderProfit()); if (OrderProfit() < 0.0) { g_count_116++; gi_132++; } else { g_count_116 = 0; gi_128++; } g_ticket_184 = 0; } } ObjectDelete("lbl_error"); Print(" *** Total P/L:", gd_120); Print(" *** Total Wins/Loss:", gi_128, "/", gi_132); return (0); } int start() { int li_48; int l_shift_52; int l_shift_56; if (gd_104 == 0.0) FatalErrorInfo("Fatal error, pip value not intialized correctly"); if (MaxLoss != 0 && g_count_116 >= MaxLoss) FatalErrorInfo("Maximum Consecutive Loss Reached"); if (gi_176 < 0) return (0); if (Bars < 100) { Print(" *** bars less than 100"); return (0); } if (g_ticket_184 > 0) { OrderSelect(g_ticket_184, SELECT_BY_TICKET, MODE_TRADES); g_datetime_168 = OrderCloseTime(); if (g_datetime_168 != 0) { g_datetime_172 = OrderOpenTime(); Print(" *** Order #", g_ticket_184, " stopped out at ", OrderClosePrice(), ", P/L:", OrderProfit()); gd_120 += OrderProfit(); if (OrderProfit() < 0.0) { g_count_116++; gi_132++; } else { g_count_116 = 0; gi_128++; } Print(" *** Total P/L:", gd_120); g_ticket_184 = 0; if (MaxLoss != 0 && g_count_116 >= MaxLoss) gi_176 = -1; else gi_176 = 1; } } if (g_ticket_184 <= 0) { if (g_datetime_168 != 0) { l_shift_56 = iBarShift(NULL, 0, g_datetime_172, FALSE); l_shift_52 = iBarShift(NULL, 0, g_datetime_168, FALSE); if (l_shift_52 == l_shift_56 && l_shift_52 <= 1) return (0); g_datetime_168 = 0; } li_48 = OpenTrigger(); if (li_48 == 1) return (PlaceLong()); if (li_48 == 2) return (PlaceShort()); return (0); } if (g_ticket_184 > 0) { if (gi_176 == 2) { if (Low[1] - EMAH(21, 1) > EMAH(21, 1) - EMAL(21, 1)) gi_180 = TRUE; } else { if (gi_176 == 3) if (EMAL(21, 1) - High[1] > EMAH(21, 1) - EMAL(21, 1)) gi_180 = TRUE; } OrderSelect(g_ticket_184, SELECT_BY_TICKET, MODE_TRADES); li_48 = CloseTrigger(); if (OrderType() <= OP_SELL) { if (OrderType() == OP_BUY) { if (!(li_48 == 1 || li_48 == 3)) return (0); OrderClose(g_ticket_184, OrderLots(), Bid, 3, Violet); Print(" *** ticket #", g_ticket_184, " closed ,price:", OrderClosePrice(), " ,P/L:", OrderProfit()); gd_120 += OrderProfit(); if (OrderProfit() < 0.0) { g_count_116++; gi_132++; } else { g_count_116 = 0; gi_128++; } Print(" *** Total P/L:", gd_120); g_ticket_184 = 0; if (MaxLoss != 0 && g_count_116 >= MaxLoss) gi_176 = -1; else gi_176 = 1; return (0); } if (li_48 == 2 || li_48 == 4) { OrderClose(g_ticket_184, OrderLots(), Ask, 3, Violet); Print(" *** Closing short position #", g_ticket_184, " ,price:", OrderClosePrice(), " ,P/L:", OrderProfit()); gd_120 += OrderProfit(); if (OrderProfit() < 0.0) { g_count_116++; gi_132++; } else { g_count_116 = 0; gi_128++; } Print(" *** Total P/L:", gd_120); g_ticket_184 = 0; if (MaxLoss != 0 && g_count_116 >= MaxLoss) gi_176 = -1; else gi_176 = 1; return (0); } } } return (0); }