//+------------------------------------------------------------------+ //| Wato_RobotTrader.mq4 | //| Copyright © 2011, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright © 2011, MetaQuotes Software Corp." #property link "http://www.metaquotes.net" extern double TakeProfit = 5; extern double Lots = 0.1; extern double StopLoss = 14; extern double MovingPeriod = 14; extern double MovingPeriodS = 21; extern double MovingShift = 5; extern double MovingShiftS = 3; extern double rsi = 4; extern double rsis = 4; extern double rsigd = 4; extern double rsiegs = 4; extern double rsieg = 4; extern double rsigds = 4; extern int res = 0; extern double MAGICMA; extern double MaximumRisk = 0.04; extern double DecreaseFactor = 3; //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { double ma; double mas; double ma5; double mas5; double rsi; double rsis; double rsi5; double rsis5; double EURUSD; double maeg; double maseg; double maeg5; double maseg5; double rsieg; double rsiegs; double rsieg5; double rsiegs5; double EURGBP; double magd; double masgd; double magd5; double masgd5; double rsigd; double rsigds; double rsigd5; double rsigds5; double GBPUSD; double res; int cnt, ticket, total; // initial data checks // it is important to make sure that the expert works with a normal // chart and the user did not make any mistakes setting external // variables (Lots, StopLoss, TakeProfit, // in our case, we check TakeProfit // on a chart of less than 100 bars if(Bars<100) { Print("bars less than 100"); return(0); } if(TakeProfit<10) { Print("TakeProfit less than 10"); return(0); // check TakeProfit } // to simplify the coding and speed up access // data are put into internal variables ma=iMA(EURUSD,8,MovingPeriod,MovingShift,MODE_LWMA,PRICE_WEIGHTED,0); mas=iMA(EURUSD,8,MovingPeriod,MovingShiftS,MODE_LWMA,PRICE_WEIGHTED,0); ma5=iMA(EURUSD,7,MovingPeriod,MovingShift,MODE_LWMA,PRICE_WEIGHTED,0); mas5=iMA(EURUSD,7,MovingPeriod,MovingShiftS,MODE_LWMA,PRICE_WEIGHTED,0); rsi=iRSI(EURUSD,8,0,PRICE_CLOSE,0); rsis=iRSI(EURUSD,8,0,PRICE_CLOSE,1); rsi5=iRSI(EURUSD,7,0,PRICE_CLOSE,0); rsis5=iRSI(EURUSD,7,0,PRICE_CLOSE,1); maeg=iMA(EURGBP,8,MovingPeriod,MovingShift,MODE_LWMA,PRICE_WEIGHTED,0); maseg=iMA(EURGBP,8,MovingPeriod,MovingShiftS,MODE_LWMA,PRICE_WEIGHTED,0); maeg5=iMA(EURGBP,7,MovingPeriod,MovingShift,MODE_LWMA,PRICE_WEIGHTED,0); maseg5=iMA(EURGBP,7,MovingPeriod,MovingShiftS,MODE_LWMA,PRICE_WEIGHTED,0); rsieg=iRSI(EURGBP,8,0,PRICE_CLOSE,0); rsiegs=iRSI(EURGBP,8,0,PRICE_CLOSE,1); rsieg5=iRSI(EURGBP,7,0,PRICE_CLOSE,0); rsiegs5=iRSI(EURGBP,7,0,PRICE_CLOSE,1); magd=iMA(GBPUSD,8,MovingPeriod,MovingShift,MODE_LWMA,PRICE_WEIGHTED,0); masgd=iMA(GBPUSD,8,MovingPeriod,MovingShiftS,MODE_LWMA,PRICE_WEIGHTED,0); magd5=iMA(GBPUSD,7,MovingPeriod,MovingShift,MODE_LWMA,PRICE_WEIGHTED,0); masgd5=iMA(GBPUSD,7,MovingPeriod,MovingShiftS,MODE_LWMA,PRICE_WEIGHTED,0); rsigd=iRSI(GBPUSD,8,0,PRICE_CLOSE,0); rsigds=iRSI(GBPUSD,8,0,PRICE_CLOSE,1); rsigd5=iRSI(GBPUSD,7,0,PRICE_CLOSE,0); rsigds5=iRSI(GBPUSD,7,0,PRICE_CLOSE,1); return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; //---- for(int i=0;i0) return(buys); else return(-sells); } //+------------------------------------------------------------------+ //| Calculate optimal lot size | //+------------------------------------------------------------------+ double LotsOptimized() { double lot=Lots; double MaximumRisk=0; int orders=HistoryTotal(); // history orders total int losses=0; // number of losses orders without a break //---- select lot size lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1); //---- calcuulate number of losses orders without a break if(DecreaseFactor>0) { for(int i=orders-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; } if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue; //---- if(OrderProfit()>0) break; if(OrderProfit()<0) losses++; } if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1); } //---- return lot size if(lot<0.1) lot=0.1; return(lot); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForOpen() { double ma; double rsi5; double rsis5; double rsigd5; double rsigds5; double rsigd; double rsigds; double rsieg5; double rsiegs5; int res; //---- go trading only for first tiks of new bar if(Volume[0]>1) return; //---- sell conditions if(rsis>50 && rsi<50 && rsiegs>50 && rsieg<50 && rsigds>50 && rsigd<50 && Volume[0]<1 && if(rsis5>50 && rsi5<50 && rsiegs5>50 && rsieg5<50 && rsigds5>50 && rsigd5<50) } res=OrderSend("EURUSD"(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red); return; } //---- buy conditions if(rsis<50 && rsi>50 && rsiegs<50 && rsieg>50 && rsigds<50 && rsigd>50 && Volume[0]<1 && { if(rsis5<50 && rsi5>50 && rsiegs5<50 && rsieg5>50 && rsigds5<50 && rsigd5>50) } res=OrderSend("EURUSD"(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue); return; } //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { // it is important to enter the market correctly, // but it is more important to exit it correctly... for(cnt=0;cnt