// ExpertTrendStrenghtEMA.mq4 // Ñîâåòíèê #property copyright "mandorr@gmail.com" extern int StopLoss=0; extern int TakeProfit=0; extern double Lots=1; extern bool MoneyManagement=false; extern int MarginPercent=20; void start() { if (Bars<1000 || IsTradeAllowed()==false) return; int trend=0; double x1=TrendStrength(1); if (x1>0) trend++; if (x1<0) trend--; if (TotalBuy () >0 && trend<0) CloseBuy (); if (TotalSell() >0 && trend>0) CloseSell(); if (TotalBuy ()==0 && TotalSell()==0 && trend>0) OpenBuy (); if (TotalBuy ()==0 && TotalSell()==0 && trend<0) OpenSell(); } double TrendStrength(int shift) { double x0, x1, x2, x3, x4, x5, x6, x7, sum; x0=iMA(NULL,0,11,0,MODE_EMA,PRICE_CLOSE,shift); x1=iMA(NULL,0, 5,0,MODE_EMA,PRICE_CLOSE,shift); x2=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,shift); x3=iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,shift); x4=iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,shift); x5=iMA(NULL,0,25,0,MODE_EMA,PRICE_CLOSE,shift); x6=iMA(NULL,0,30,0,MODE_EMA,PRICE_CLOSE,shift); x7=iMA(NULL,0,40,0,MODE_EMA,PRICE_CLOSE,shift); sum=(x0-x1)+(x0-x2)+(x0-x3)+(x0-x4)+(x0-x5)+(x0-x6)+(x0-x7); return (sum/7); } void CloseBuy() { for (int i=OrdersTotal()-1; i>=0; i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if (OrderSymbol()!=Symbol() || OrderType()!=OP_BUY) continue; OrderClose(OrderTicket(),OrderLots(),Bid,2); } } void CloseSell() { for (int i=OrdersTotal()-1; i>=0; i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if (OrderSymbol()!=Symbol() || OrderType()!=OP_SELL) continue; OrderClose(OrderTicket(),OrderLots(),Ask,2); } } void OpenBuy() { double lots=LotsCounting(); double loss=0; if (StopLoss>0) loss=Ask-StopLoss*Point; double profit=0; if (TakeProfit>0) profit=Ask+TakeProfit*Point; if (OrderSend(Symbol(),OP_BUY,lots,Ask,2,loss,profit,"",0,0)==0) Print("Open a order failed with error #",GetLastError()); } void OpenSell() { double lots=LotsCounting(); double loss=0; if (StopLoss>0) loss=Bid+StopLoss*Point; double profit=0; if (TakeProfit>0) profit=Bid-TakeProfit*Point; if (OrderSend(Symbol(),OP_SELL,lots,Bid,2,loss,profit,"",0,0)==0) Print("Open a order failed with error #",GetLastError()); } int TotalBuy() { int count=0; for (int i=0; i<OrdersTotal(); i++) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if (OrderSymbol()==Symbol() && OrderType()==OP_BUY) count++; } return (count); } int TotalSell() { int count=0; for (int i=0; i<OrdersTotal(); i++) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if (OrderSymbol()==Symbol() && OrderType()==OP_SELL) count++; } return (count); } double LotsCounting() { double lots=Lots; if (MoneyManagement) { lots=NormalizeDouble((MarginPercent*AccountFreeMargin()/100000),1); if (lots>10) lots=NormalizeDouble(lots,0); } if (lots<0.1) lots=0.1; return (lots); }
Sample
Analysis
Market Information Used:
Indicator Curves created:
Indicators Used:
Moving average indicator
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
It Closes Orders by itself
It automatically opens orders when conditions are reached
Other Features: