//+------------------------------------------------------------------+ //| MyAlgo.mq4 | //| taken from Wayne Walker's book | //| | //+------------------------------------------------------------------+ extern int ShortEMA = 10; extern int LongEMA = 21; extern int VlongEMA = 60; extern double TakeProfit = 50; extern double StopLoss = 25; extern double LotSize = 0.01; double pips; extern int MoveToBreakEven = 50; extern int PipsProfitLock = 20; extern bool UseBreakEven = false; extern bool UseTrailingStop = true; extern int WhenToTrail = 50; extern int TrailAmount = 30; extern bool UseStoploss = true; extern bool UseTakeProfit = false; extern bool UsePosition = true; extern bool UseRiskReward = true; extern double reward_ratio = 2; extern int RiskPercent = 1; extern double RiskedAmount = 0; extern bool UseCandleClose = true; extern int CloseAfterCandles = 10; extern bool TradeLong = true; extern bool TradeShort = true; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- PipsFunction(); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if(IsNewCandle() ) { if(TotalOpenOrders() <1) { EntrySignal(); } } if(TotalOpenOrders()>0) { if(UseBreakEven) { BreakEven(); } if(UseTrailingStop) { TrailingStop(); } if(UseCandleClose) { CandleClose(); } } } //+------------------------------------------------------------------+ void NewOrder() { int Result = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, NormalizeDouble( Ask-StopLoss*pips,4), NormalizeDouble(Ask+TakeProfit*pips,4), NULL, 9999,0,clrNONE); return; } //+----------------------------------------------------------------------+ bool IsNewCandle() { static int BarsOnChart = 0; if(Bars == BarsOnChart) return (false); BarsOnChart = Bars; return (true); } //+----------------------------------------------------------------------+ int TotalOpenOrders() { int Trades = 0; int Total = OrdersTotal(); for(int i=Total; i>0; i--) { bool res=OrderSelect (i-1, SELECT_BY_POS, MODE_TRADES); if(OrderType() ==OP_BUY || OrderType () == OP_SELL) { Trades++; } } return(Trades); } //+----------------------------------------------------------------------+ void CloseAllOrders() { int Total = OrdersTotal(); for(int i=Total; i>0; i--) { if(OrderSelect(i-1, SELECT_BY_POS, MODE_TRADES)) { if(OrderType() == OP_SELL) { bool res1 = OrderClose( OrderTicket(), OrderLots(), Ask, 3, clrNONE); } if(OrderType() == OP_BUY) { bool res2 = OrderClose( OrderTicket(), OrderLots(), Bid, 3, clrNONE); } if(OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_SELLLIMIT) { bool res3 = OrderDelete(OrderTicket(),clrNONE); } } } return; } //+----------------------------------------------------------------------+ void PipsFunction() { double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE); if(ticksize == 0.00001 || ticksize == 0.0001) { pips = ticksize*10; } else { pips = ticksize; } return; } //+----------------------------------------------------------------------+ void BreakEven() { for (int i=OrdersTotal(); i>0; i--) { if (OrderSelect(i-1, SELECT_BY_POS, MODE_TRADES)) { if(OrderType()== OP_BUY) { if(Bid - OrderOpenPrice()>MoveToBreakEven*pips) { if(OrderOpenPrice() > OrderStopLoss()) { bool res4=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()+ PipsProfitLock*pips,OrderTakeProfit(),0,clrNONE); //Alert("yes"); } } } if(OrderType()== OP_SELL) { if(OrderOpenPrice()-Bid >MoveToBreakEven*pips) { if(OrderOpenPrice() < OrderStopLoss()) { bool res5=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()- PipsProfitLock*pips,OrderTakeProfit(),0,clrNONE); //Alert("yes"); } } } } } } //+----------------------------------------------------------------------+ void TrailingStop() { for(int i=OrdersTotal();i>0;i--) { if (OrderSelect(i-1, SELECT_BY_POS, MODE_TRADES)) { if(OrderType()== OP_BUY) { if(Bid - OrderOpenPrice()>WhenToTrail*pips) { if(OrderStopLoss() < Bid-TrailAmount*pips) { bool res6=OrderModify(OrderTicket(), OrderOpenPrice(), Bid- TrailAmount*pips,OrderTakeProfit(),0,clrNONE); } } } if(OrderType()== OP_SELL) { if(OrderOpenPrice()-Bid>WhenToTrail*pips) { if(OrderStopLoss() > Bid+TrailAmount*pips) { bool res7=OrderModify(OrderTicket(), OrderOpenPrice(), Bid+TrailAmount*pips,OrderTakeProfit(),0,clrNONE); } } } } } } //+----------------------------------------------------------------------+ void Trade(int Direction) { double SL; double TP; double Equity = AccountEquity(); double RiskedAmount = Equity*RiskPercent*.01; double Lots=0; if(Direction ==0) { if(UseStoploss) { SL=Bid-StopLoss*pips; } else { SL=0; } double PipsToBuyStoploss=StopLoss*pips; if(UseTakeProfit) { if(UseRiskReward && UseStoploss) { TP = (Bid-SL)*2+Bid; } else { TP=Bid+TakeProfit*pips; } } else { TP=0; } if(UsePosition && UseStoploss) { Lots = (RiskedAmount/(PipsToBuyStoploss/pips)); } else { Lots = LotSize; } int res= OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, NormalizeDouble(SL,4),NormalizeDouble(TP,4),NULL, 0,0,clrNONE); } if(Direction ==1) { if(UseStoploss) { SL=Ask+StopLoss*pips; } else { SL=0; } double PipsToSellStoploss=StopLoss*pips; if(UseTakeProfit) { if(UseRiskReward && UseStoploss) { TP = Ask - (SL-Ask)*2; } else { TP=Ask - TakeProfit*pips; } } else { TP=0; } if(UsePosition && UseStoploss) { Lots = (RiskedAmount/(PipsToSellStoploss/pips)); } else { Lots = LotSize; } int res9= OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, NormalizeDouble(SL,4),NormalizeDouble(TP,4),NULL, 0,0,clrNONE); } return; } //+----------------------------------------------------------------------+ void CandleClose() { int period=Period(); int period2 = 0; switch(period) { case 1:period2 = 60; break; case 5:period2 = 3000; break; case 15:period2 = 900; break; case 30:period2 = 1800; break; case 60:period2 = 3600; break; case 240:period2 = 14400; break; case 1440:period2 = 86400; break; case 10080:period2 = 604800; break; case 43200:period2 = 2592000; break; //default: Alert("Nothing"); } for(int i=OrdersTotal(); i>0;i--) { if(OrderSelect(i-1, SELECT_BY_POS, MODE_TRADES)) { if(TimeCurrent()-OrderOpenTime()>period2*CloseAfterCandles) { CloseAllOrders(); } } } return; } //+----------------------------------------------------------------------+ void EntrySignal() { double ShortEMACurrent = iMA(Symbol(), PERIOD_CURRENT,ShortEMA,0,MODE_EMA,PRICE_CLOSE,1); double LongEMACurrent = iMA(Symbol(), PERIOD_CURRENT,LongEMA,0,MODE_EMA,PRICE_CLOSE,1); double ShortEMAPrevious = iMA(Symbol(), PERIOD_CURRENT,ShortEMA,0,MODE_EMA,PRICE_CLOSE,2); double LongEMAPrevious = iMA(Symbol(), PERIOD_CURRENT,LongEMA,0,MODE_EMA,PRICE_CLOSE,2); if(TradeLong) { if (ShortEMAPrevious LongEMACurrent) { Trade(0); } } if(TradeShort) { if (ShortEMAPrevious>LongEMAPrevious && ShortEMACurrent < LongEMACurrent) { Trade(1); } } return; }