//+------------------------------------------------------------------+ //| Tradeview Envelopes.mq4 | //| Tradeview Forex | //| http://www.tradeviewforex.com | //+------------------------------------------------------------------+ #property copyright "Tradeview Forex" #property link "http://www.tradeviewforex.com" //+------------------------------------------------------------------+ extern int TakeProfitBuy=144; extern int TakeProfitSell=144; extern int TralingStopBuy=89; extern int TralingStopSell=89; extern int StopLossBuy=144; extern int StopLossSell=144; extern double volume=0.1; extern int SplipPage=0; //+------------------------------------------------------------------+ string NameExpert="EA Tradeview Envelopes Running"; int Intentos; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { Comment("Tradeview. EA Tradeview Envelopes Running"); Intentos=0; return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { string ResultSignal=VerifySignal(); if(ResultSignal!="") { if(Intentos==0) { Comment("Tradeview. EA Tradeview Envelopes Found Signal... Trying to Run Strategy"); if(ResultSignal=="buy") OpenBuy(); else if (ResultSignal=="sell") OpenSell(); } else Comment("Tradeview. EA Tradeview Envelopes Waiting new Signal"); } else { Intentos=0; Comment("Tradeview. EA Tradeview Envelopes Waiting new Signal"); } ModTralingStop(); return(0); } //+------------------------------------------------------------------+ string VerifySignal() { double ValorLineaArriba=iEnvelopes(Symbol(), PERIOD_M15, 40, MODE_EMA, 0, PRICE_CLOSE, 0.3, MODE_UPPER, 1); double ValorLineaAbajo=iEnvelopes(Symbol(), PERIOD_M15, 40, MODE_EMA, 0, PRICE_CLOSE, 0.3, MODE_LOWER, 1); double EMA_Bar2=iMA(Symbol(), PERIOD_M15, 15, 0, MODE_EMA, PRICE_CLOSE, 2); double EMA_Bar1=iMA(Symbol(), PERIOD_M15, 15, 0, MODE_EMA, PRICE_CLOSE, 1); double ValorOpen=iOpen(Symbol(), PERIOD_M15, 1); double ValorClose=iClose(Symbol(), PERIOD_M15, 1); if((ValorOpen>ValorLineaArriba || ValorClose>ValorLineaArriba) && EMA_Bar2EMA_Bar1) return("sell"); //Fuerte tendencia de Venta return(""); } //+------------------------------------------------------------------+ void OpenBuy() { while(true) { RefreshRates(); OrderSend(Symbol(), OP_BUY, volume, Ask, SplipPage, GetStopLossBuy(), GetTakeProfitBuy(),NameExpert,0,0,Blue); if(Fun_Error(GetLastError(), "BUY")==0) break; } Intentos++; } //+------------------------------------------------------------------+ void OpenSell() { while(true) { RefreshRates(); OrderSend(Symbol(), OP_SELL, volume, Bid, SplipPage, GetStopLossSell(), GetTakeProfitSell(),NameExpert,0,0,Red); if(Fun_Error(GetLastError(), "SELL")==0) break; } Intentos++; } //+------------------------------------------------------------------+ void ModTralingStop() { double Ticket, Price, SL, TP, TS; RefreshRates(); for(int i=0;i<=OrdersTotal()-1;i++) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if(OrderComment()==NameExpert) { Ticket=OrderTicket(); Price=OrderOpenPrice(); SL=OrderStopLoss(); TP=OrderTakeProfit(); if(OrderType()==OP_BUY) { TS=TralingStopBuy*Point; if (NormalizeDouble(SL,Digits)NormalizeDouble(Ask+TS,Digits)|| NormalizeDouble(SL,Digits)==0) { SL=Ask+TS; OrderModify(Ticket, Price, SL, TP, 0); } } } } } //+------------------------------------------------------------------+ double GetTakeProfitBuy() { if(TakeProfitBuy==0) return(0); else return(Ask+TakeProfitBuy*Point); } //+------------------------------------------------------------------+ double GetTakeProfitSell() { if(TakeProfitSell==0) return(0); else return(Bid-TakeProfitSell*Point); } //+------------------------------------------------------------------+ double GetStopLossBuy() { if(StopLossBuy==0) return(0); else return(Ask-StopLossBuy*Point); } //+------------------------------------------------------------------+ double GetStopLossSell() { if(StopLossSell==0) return(0); else return(Bid+StopLossSell*Point); } //+------------------------------------------------------------------+ int Fun_Error(int Error, string Operation) { switch(Error) { case 0: return(0); case 4: Comment("Trade server is busy. Trying once again... ",Operation); Sleep(500); return(1); case 128: Comment("Trade Timeout. Trying once again... ",Operation); Sleep(500); return(1); case 129: while(RefreshRates()==false) Sleep(1); Comment("Invalid Price. Trying once again... ",Operation); return(1); case 130: Alert("EA Tradeview Envelopes. Invalid Stops, please Resetting this EA"); return(0); case 135: //Price changed. Trying once again... RefreshRates(); return(1); case 136: //No prices. Waiting for a new tick... while(RefreshRates()==false) Sleep(1); return(1); case 137: Comment("Broker is busy. Trying once again... ",Operation); Sleep(500); return(1); case 138: while (!IsTradeAllowed()) Sleep(1000); Print("EA Tradeview Envelopes. Requotes... Trying once again..."); Comment("Requotes. Trying once again... ", Operation); return(1); case 146: Comment("Trading subsystem is busy. Trying once again... ",Operation); Sleep(500); return(1); case 2: Alert("EA Tradeview Envelopes. Common error. ",Operation); return(0); case 5: Alert("EA Tradeview Envelopes. Old terminal version. ",Operation); return(0); case 64: Alert("EA Tradeview Envelopes. Account blocked. ",Operation); return(0); case 133: Alert("EA Tradeview Envelopes. Trading forbidden. ",Operation); return(0); case 134: Alert("EA Tradeview Envelopes. Not enough money to execute operation. ",Operation); return(0); default: Alert("EA Tradeview Envelopes. Error occurred: ",Error); return(0); } }