//+------------------------------------------------------------------+ //| This MQL is generated by Expert Advisor Builder | //| http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/ | //| Trading Methodology Devised by TradeForex //| //| //| //| | //| In no event will author be liable for any damages whatsoever. | //| Use at your own risk. | //| | //+------------------- DO NOT REMOVE THIS HEADER --------------------+ #define SIGNAL_NONE 0 #define SIGNAL_BUY 1 #define SIGNAL_SELL 2 #define SIGNAL_CLOSEBUY 3 #define SIGNAL_CLOSESELL 4 #property copyright "Copyright © 2008, AKA TradeForexFX" #property link "http://www./" extern string Expert_Name = "---- Strategy Tester EA by TF ----"; //*********************** //int MagicNumber; // This is now calculated based on Symbol and Period extern int MagicNumber = 59768906; extern int Maximum_Spread = 8; extern bool SignalMail = False; extern bool EachTickMode = True; extern double Lots = 0.3; extern bool Close_Lots_Mode = True; extern double First_Close = 0.1 ; extern int First_Close_TP = 15 ; extern double Second_Close = 0.19 ; extern int Second_Close_TP = 30 ; extern double Third_Close = 0.01 ; extern int Third_Close_TP = 600 ; extern int Slippage = 3; extern bool StopLossMode = True; extern int StopLoss = 60; extern bool TakeProfitMode = True; extern int TakeProfit = 450; extern bool TrailingStopMode = True; extern int TrailingStop = 30; extern bool MoveStopOnce = True; extern int MoveStopWhenPrice = 12; extern int MoveStopTo = 1; //****************************************** // Inputs for trading hours extern string Trading_Parameters="--Trading Hour Parameters--"; extern string Trading_Hours="UseTradingHours - Enter 1 for YES, 0 for NO"; extern int UseTradingHours = 0; extern string Session1="Trade Session 1 - Enter 1 for YES, 0 for NO"; extern int TradeSession1 = 1; extern int Session1Start = 0001; // Start trades after time extern int Session1Stop = 1330; // Stop trading after time extern string Session2="Trade Session 2 - Enter 1 for YES, 0 for NO"; extern int TradeSession2 = 1; extern int Session2Start = 1330; // Start trades after time extern int Session2Stop = 2359; // Stop trading after time extern string Session3="Trade Session 3 - Enter 1 for YES, 0 for NO"; extern int TradeSession3 = 0; extern int Session3Start = 2130; // Start trades after time extern int Session3Stop = 2330; // Stop trading after time extern string Session4="Trade Session 4 - Enter 1 for YES, 0 for NO"; extern int TradeSession4 = 0; extern int Session4Start = 1330; // Start trades after time extern int Session4Stop = 1500; // Stop trading after time extern string Session5="Trade Session 5 - Enter 1 for YES, 0 for NO"; extern int TradeSession5 = 0; extern int Session5Start = 1700; // Start trades after time extern int Session5Stop = 1900; // Stop trading after time bool YesStop; double myPoint; // Trading Hours variables end here //************************************ int BarCount; int Current; bool TickCheck = False; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { BarCount = Bars; if (EachTickMode) Current = 0; else Current = 1; myPoint = SetPoint(Symbol()); return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { int Order = SIGNAL_NONE; int Total, Ticket; double StopLossLevel, TakeProfitLevel; if (EachTickMode && Bars != BarCount) TickCheck = False; Total = OrdersTotal(); Order = SIGNAL_NONE; if (Close_Lots_Mode) ClosePartialLots(); if (MoveStopOnce) MoveStopOnceMode(); //+------------------------------------------------------------------+ //| Variable Begin | //+------------------------------------------------------------------+ double myPoint; double LWMA_M15_7=iMA(NULL,PERIOD_M15,7,0,MODE_LWMA,PRICE_CLOSE,0); //Faster Blue Line double LWMA_M15_16=iMA(NULL,PERIOD_M15,16,0,MODE_LWMA,PRICE_CLOSE,1); //Slower Red Line double LWMA_M5_7=iMA(NULL,PERIOD_M5,7,0,MODE_LWMA,PRICE_CLOSE,0); //Faster Blue Line double LWMA_M5_16=iMA(NULL,PERIOD_M5,16,0,MODE_LWMA,PRICE_CLOSE,1); //Slower Red Line double EMA5_M15_Close =iMA(NULL,PERIOD_M15,7,0,MODE_LWMA,PRICE_CLOSE,0); //Faster Blue Line double EMA5_M15_Open =iMA(NULL,PERIOD_M15,16,0,MODE_LWMA,PRICE_OPEN,1); //Slower Red Line double HA_15_Dn = iCustom(NULL,PERIOD_M15,"Heiken_Ashi_Smoothed",2,6,3,2,2,0); double HA_15_Up = iCustom(NULL,PERIOD_M15,"Heiken_Ashi_Smoothed",2,6,3,2,3,0); double HA_5_Dn = iCustom(NULL,PERIOD_M5,"Heiken_Ashi_Smoothed",2,30,3,2,2,0); double HA_5_Up = iCustom(NULL,PERIOD_M5,"Heiken_Ashi_Smoothed",2,30,3,2,3,0); double PSAR_M15_0 = iSAR(NULL, PERIOD_M15, 0.035, 0.2, 0); double Middle = (Open[0] + Close[0]) / 2; double Middle1 = (Open[1] + Close[1]) / 2; double RSI_M5_0 = iRSI(NULL, PERIOD_M5, 8,PRICE_CLOSE ,0); double CCI_M15_34_0 = iCCI(NULL, PERIOD_M15,34,PRICE_TYPICAL,0); double CCI_M15_170_1 = iCCI(NULL, PERIOD_M15,170,PRICE_TYPICAL,1); string LWMADirection = "None"; if(LWMA_M15_7 > LWMA_M15_16 && LWMA_M5_7 > LWMA_M5_16 && (Bid + Ask)/2 > LWMA_M15_16 && EMA5_M15_Close > EMA5_M15_Open) LWMADirection = "Long"; if(LWMA_M15_7 < LWMA_M15_16 && LWMA_M5_7 < LWMA_M5_16 && (Bid + Ask)/2 < LWMA_M15_16 && EMA5_M15_Close < EMA5_M15_Open ) LWMADirection = "Short"; string HADirection = "None"; if(HA_15_Up > HA_15_Dn && HA_5_Up > HA_5_Dn) HADirection = "Long"; if(HA_15_Up < HA_15_Dn && HA_5_Up < HA_5_Dn) HADirection = "Short"; string HA5Direction = "None"; if(HA_5_Up > HA_5_Dn ) HA5Direction = "Long"; if(HA_5_Up < HA_5_Dn ) HA5Direction = "Short"; string PSARDirection = "None"; if(PSAR_M15_0 < Middle) PSARDirection = "Long"; if(PSAR_M15_0 > Middle) PSARDirection = "Short"; string RSIDirection = "None"; if(RSI_M5_0 > 56 ) RSIDirection = "Long"; if(RSI_M5_0 < 44 ) RSIDirection = "Short"; string CCIDirection = "None"; if(CCI_M15_34_0 > 10 && CCI_M15_34_0 > CCI_M15_170_1 ) CCIDirection = "Long"; if(CCI_M15_34_0 < -10 && CCI_M15_34_0 < CCI_M15_170_1 ) CCIDirection = "Short"; string CloseDirection = "None"; if(LWMA_M5_7 > LWMA_M5_16 && RSIDirection == "Long" && HA5Direction == "Long") CloseDirection = "CloseSell"; if(LWMA_M5_7 < LWMA_M5_16 && RSIDirection == "Short" && HA5Direction == "Short" ) CloseDirection = "CloseBuy"; //string DamDirection = "None"; //if(Dam_1_Trade_0 > Dam_1_Trade_1 ) DamDirection = "Trade"; double Spread = (Ask - Bid) / Point ; double Trade_0_1 = Maximum_Spread + 1 - Spread ; //double Trade_0_2 = Dam_30_Trade ; //+------------------------------------------------------------------+ //| Variable End | //+------------------------------------------------------------------+ //Check position bool IsTrade = False; for (int i = 0; i < Total; i ++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //******************************************* // Important to have code check MagicNumber and Symbol if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; //******************************************* if(OrderType() <= OP_SELL) { IsTrade = True; if(OrderType() == OP_BUY) { //Close // int QtyOrders = 0; // QtyOrders = CalculateCurrentOrders(); //+------------------------------------------------------------------+ //| Signal Begin(Exit Buy) | //+------------------------------------------------------------------+ if ( CloseDirection == "CloseBuy" ) Order = SIGNAL_CLOSEBUY; //+------------------------------------------------------------------+ //| Signal End(Exit Buy) | //+------------------------------------------------------------------+ if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen); if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy"); if (!EachTickMode) BarCount = Bars; IsTrade = False; continue; } //Trailing stop if(TrailingStopMode && TrailingStop > 0) { if(Bid - OrderOpenPrice() > Point * TrailingStop) { if(OrderStopLoss() < Bid - Point * TrailingStop) { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen); if (!EachTickMode) BarCount = Bars; continue; } } } //Close_Lots - Buy } else { //Close //+------------------------------------------------------------------+ //| Signal Begin(Exit Sell) | //+------------------------------------------------------------------+ if ( CloseDirection == "CloseSell" ) Order = SIGNAL_CLOSESELL; //+------------------------------------------------------------------+ //| Signal End(Exit Sell) | //+------------------------------------------------------------------+ if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange); if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell"); if (!EachTickMode) BarCount = Bars; IsTrade = False; continue; } //Trailing stop if(TrailingStopMode && TrailingStop > 0) { if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) { if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange); if (!EachTickMode) BarCount = Bars; continue; } } //Close_Lots - Sell } // Trailing Stop } // Close } // OrderType }// for //******************************************* // Check trading sessions int NumOrders = 0; NumOrders = CalculateCurrentOrders(); if(NumOrders == 0) { YesStop = false; if (UseTradingHours == 1) { YesStop = CheckTradingTimes(); //Check position if (YesStop == true) { Comment ( "", "\n", "Trading has been stopped ", "\n", "HA Direction ", HADirection, "\n", "HA 15 Min up minus Dn ", HA_15_Up - HA_15_Dn, "\n", "HA 5 Min up minus Dn ", HA_5_Up - HA_5_Dn, "\n", "Lots in Trade ", NumOrders, "\n", "Close Direction - ", CloseDirection ); } else { Comment ( "", "\n", "Trading has resumed ", "\n", "HA Direction ", HADirection, "\n", "HA 15 Min up minus Dn ", HA_15_Up - HA_15_Dn, "\n", "HA 5 Min up minus Dn ", HA_5_Up - HA_5_Dn, "\n", "Lots in Trade ", NumOrders, "\n", "Close Direction - ", CloseDirection ); } } if (YesStop == false) { //+------------------------------------------------------------------+ //| Signal Begin(Entry) && Buy3_1 > Buy3_2 && Sell3_1 < Sell3_2 && Buy6_1 > Buy6_2 && Sell6_1 < Sell6_2 | && Sell9_1 > Sell9_2 && Buy9_1 > Buy9_2 //+------------------------------------------------------------------+ if ( Trade_0_1 > 0 && // DamDirection == "Trade" && PSARDirection == "Long" && RSIDirection == "Long" && LWMADirection == "Long" && CCIDirection == "Long" ) Order = SIGNAL_BUY; if ( Trade_0_1 > 0 && // DamDirection == "Trade" && PSARDirection == "Short" && RSIDirection == "Short" && LWMADirection == "Short" && CCIDirection == "Short" ) Order = SIGNAL_SELL; //+------------------------------------------------------------------+ //| Signal End | //+------------------------------------------------------------------+ //Buy if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { if(!IsTrade) { //Check free margin if (AccountFreeMargin() < (1500 * Lots)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } if (StopLossMode) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0; if (TakeProfitMode) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0; Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Can Dude 15 Min #" + MagicNumber + ")", MagicNumber, 0, DodgerBlue); if(Ticket > 0) { if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) { Print("BUY order opened : ", OrderOpenPrice()); if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy"); } else { Print("Error opening BUY order : ", GetLastError()); } } if (EachTickMode) TickCheck = True; if (!EachTickMode) BarCount = Bars; return(0); } } //Sell if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { if(!IsTrade) { //Check free margin if (AccountFreeMargin() < (500 * Lots)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } if (StopLossMode) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0; if (TakeProfitMode) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0; Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Can Dude 15 Min #" + MagicNumber + ")", MagicNumber, 0, DeepPink); if(Ticket > 0) { if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) { Print("SELL order opened : ", OrderOpenPrice()); if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell"); } else { Print("Error opening SELL order : ", GetLastError()); } } if (EachTickMode) TickCheck = True; if (!EachTickMode) BarCount = Bars; return(0); } } } // YesStop } // NumOrders if (!EachTickMode) BarCount = Bars; return(0); } //+------------------------------------------------------------------+ // Return true if time is not in session for trading bool CheckOutsideSession(int SessionStart, int SessionStop, int ct) { if (SessionStart < SessionStop) { if (ct >= SessionStart && ct <= SessionStop) return (false); else return(true); } else { if (ct > SessionStop && ct < SessionStart) return (true); else return(false); } } bool CheckTradingTimes() { bool StopTrading; int ct; ct = Hour() * 100 + Minute(); StopTrading = true; if (TradeSession1 == 1) { StopTrading = CheckOutsideSession(Session1Start, Session1Stop, ct); } if (StopTrading == true) { if (TradeSession2 == 1) { StopTrading = CheckOutsideSession(Session2Start, Session2Stop, ct); } } if (StopTrading == true) { if (TradeSession3 == 1) { StopTrading = CheckOutsideSession(Session3Start, Session3Stop, ct); } } if (StopTrading == true) { if (TradeSession4 == 1) { StopTrading = CheckOutsideSession(Session4Start, Session4Stop, ct); } } if (StopTrading == true) { if (TradeSession5 == 1) { StopTrading = CheckOutsideSession(Session5Start, Session5Stop, ct); } } return(StopTrading); } int CalculateCurrentOrders() { int buys = 0, sells = 0, num = 0; for(int i=0;i HA_1__Dn && HA_5__Up > HA_5__Dn) HA_Direction = "Long"; if(HA_1__Up < HA_1__Dn && HA_5__Up < HA_5__Dn) HA_Direction = "Short"; Total = OrdersTotal(); //Check position bool IsTrade = False; // Better to do this from highest to lowest // for (int i = 0; i < Total; i ++) { for (int i = Total - 1; i >= 0; i --) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //******************************************* // Important to have code check MagicNumber and Symbol if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; //******************************************* if(OrderType() <= OP_SELL) { IsTrade = True; if(OrderType() == OP_BUY) QtyOrders = OrderLots(); if(OrderType() == OP_SELL) QtyOrderSells = OrderLots(); Comment ( "Quantity in trade - Buys ", QtyOrders, "\n", "Quantity in trade - Sells", QtyOrderSells, "\n", "Initial Order Quantity", Lots, "\n", "HA Direction ", HA_Direction, "\n", "HA 1 Min up minus Dn ", HA_1__Up - HA_1__Dn, "\n", "HA 5 Min up minus Dn ", HA_5__Up - HA_5__Dn ); if(OrderType() == OP_BUY) { //Close_Lots - Buy if(QtyOrders == Lots) { if(Bid - OrderOpenPrice() > myPoint * First_Close_TP) { OrderClose(OrderTicket(), First_Close , Bid, Slippage, MediumSeaGreen); continue; } } if( ( (QtyOrders - Third_Close) > 0.00 ) && (QtyOrders - (Second_Close + Third_Close ) <= 0.00)) { if(Bid - OrderOpenPrice() > (myPoint * Second_Close_TP)) { OrderClose(OrderTicket(), Second_Close , Bid, Slippage, MediumSeaGreen); continue; } } if((QtyOrders - Third_Close <= 0.00) && (QtyOrders > 0.00 )) { if(Bid - OrderOpenPrice() > myPoint * Third_Close_TP) { OrderClose(OrderTicket(), QtyOrders , Bid, Slippage, MediumSeaGreen); continue; } } } if(OrderType() == OP_SELL) { //Close_Lots - Sell // Sell trades must exit at Ask price if(QtyOrderSells == Lots) { if(OrderOpenPrice() - Ask > myPoint * First_Close_TP) { OrderClose(OrderTicket(), First_Close , Ask, Slippage, MediumSeaGreen); continue; } } if((QtyOrderSells - Third_Close > 0.00 ) &&(QtyOrderSells - (Second_Close + Third_Close ) <= 0.00) ) { if(OrderOpenPrice() - Ask > (myPoint * Second_Close_TP)) { OrderClose(OrderTicket(), Second_Close , Ask, Slippage, MediumSeaGreen); continue; } } if((QtyOrderSells - Third_Close <= 0.00 ) && (QtyOrderSells > 0.00 )) { if(OrderOpenPrice() - Ask > myPoint * Third_Close_TP) { OrderClose(OrderTicket(), QtyOrderSells , Ask, Slippage, MediumSeaGreen); continue; } } } } } } // Fix of Point for Brokers using 3 and 5 digit pricing double SetPoint(string mySymbol) { double mPoint, myDigits; myDigits = MarketInfo (mySymbol, MODE_DIGITS); if (myDigits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } // Modified to use a function for easier use in an EA void MoveStopOnceMode() { int Total; double QtyOrders = 0.00; double QtyOrderSells = 0.00; Total = OrdersTotal(); //Check position bool IsTrade = False; // Better to do this from highest to lowest // for (int i = 0; i < Total; i ++) { for (int i = Total - 1; i >= 0; i --) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //******************************************* // Important to have code check MagicNumber and Symbol if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; //******************************************* if(OrderType() <= OP_SELL) { IsTrade = True; if(OrderType() == OP_BUY) QtyOrders = OrderLots(); if(OrderType() == OP_SELL) QtyOrderSells = OrderLots(); if(OrderType() == OP_BUY) { //MoveOnce if(MoveStopOnce && MoveStopWhenPrice > 0) { if(Bid - OrderOpenPrice() >= Point * MoveStopWhenPrice) { if(OrderStopLoss() < OrderOpenPrice() + Point * MoveStopTo) { OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + Point * MoveStopTo, OrderTakeProfit(), 0, Red); if (!EachTickMode) BarCount = Bars; continue; }}} if(OrderType() == OP_SELL) { //MoveOnce if(MoveStopOnce && MoveStopWhenPrice > 0) { if(Bid - OrderOpenPrice() >= Point * MoveStopWhenPrice) { if(OrderStopLoss() < OrderOpenPrice() + Point * MoveStopTo) { OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + Point * MoveStopTo, OrderTakeProfit(), 0, Red); if (!EachTickMode) BarCount = Bars; continue; } }}}}}}}