//+------------------------------------------------------------------+ //| Robot Danu.mq4 | //| danusaputra © 2012, Ultimate Explorer Corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "danusaputra © 2012, Ultimate Explorer Corp." #property link "http://www.metaquotes.net" #define MAGICMA 20050610 input double Lots = 0.01; input int LotRatio = 5000; input int StopLoss = 0; input int TakeProfit = 0; extern double Risk = 0.02; extern double Period1 = 5; extern double Period2 = 12; extern double Period3 = 35; extern string Dev_Step_1 ="5,3"; extern string Dev_Step_2 ="5,3"; extern string Dev_Step_3 ="5,3"; extern int Symbol_1_Kod =159; extern int Symbol_2_Kod =159; extern int Symbol_3_Kod =82; input int PeriodOne = 13; // The period for the first EMA input int PeriodTwo = 50; // The period for the second EMA //+------------------------------------------------------------------+ //| 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; int orders=HistoryTotal(); // history orders total int losses=0; // number of losses orders without a break //---- select lot size lot = NormalizeDouble (AccountFreeMargin()/LotRatio,2); if (lot<0.01) lot = 0.01; return (lot); //---- return lot size } //Check for EMA Crossover int CheckForCross(double input1, double input2) { static int previous_direction = 0; static int current_direction = 0; // Up Direction = 1 if(input1 > input2){ current_direction = 1; } // Down Direction = 2 if(input1 < input2){ current_direction = 2; } // Detect a direction change if(current_direction != previous_direction){ previous_direction = current_direction; return (previous_direction); } else { return (0); } } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ int start() { double ZZ_1, ZZ_2; int res; //---- get 3 Level ZZ Semafor ZZ_1=iCustom(Symbol(),0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,5,3); ZZ_2=iCustom(Symbol(),0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,4,3); // Calculate the SMAs from the iMA indicator in MODE_SMMA using the close price double shortEMA, longEMA; shortEMA = iMA(NULL, 0, PeriodOne, 0, MODE_EMA, PRICE_CLOSE, 0); longEMA = iMA(NULL, 0, PeriodTwo, 0, MODE_EMA, PRICE_CLOSE, 0); // Check if there has been a cross on this tick from the two SMAs int isCrossed = CheckForCross(shortEMA, longEMA); // TP and SL Calcs double ShortSL, LongSL, ShortTP, LongTP; if (StopLoss > 0){ ShortSL = Bid+(StopLoss*Point); LongSL = Ask-(StopLoss*Point); } if (TakeProfit > 0){ ShortTP = Bid-(TakeProfit*Point); LongTP = Ask+(TakeProfit*Point); } for(int i=(OrdersTotal()-1);i>=0;i--) if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false){ Print("ERROR - Unable to select the order - ",GetLastError()); break; } if(ZZ_1 < ZZ_2 && OrderType()==OP_SELL){ OrderClose(OrderTicket(), OrderLots(), Ask, 3, NULL); } if(ZZ_1 > ZZ_2 && OrderType()==OP_BUY){ OrderClose(OrderTicket(), OrderLots(), Ask, 3, NULL); } //---- sell conditions if(ZZ_1 > ZZ_2 && isCrossed == 2 && OrdersTotal() < 1) { res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,ShortSL, ShortTP,"Robot Danu",MAGICMA,0,Red); return(0); } //---- buy conditions if(ZZ_1 < ZZ_2 && isCrossed == 1 && OrdersTotal() < 1) { res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3, LongSL, LongTP,"Robot Danu",MAGICMA,0,Blue); return(0); } return(0); }