//+------------------------------------------------------------------+ //| Egy_Cross_2_Ma_V1.mq4 | //| Copyright 2018, EgyTrading | //| EgyTrading@ymail.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2018, EgyTrading" #property link "EgyTrading@ymail.com" #property version "1.00" #property strict input string s1 = "Fast Ma"; input int period_1 = 5; input int shift_1 = 0; input ENUM_MA_METHOD MODE_1 = 0; input ENUM_APPLIED_PRICE apply_1 = 0; input string s2 = "Slow Ma"; input int period_2 = 75; input int shift_2 = 0; input ENUM_MA_METHOD MODE_2 = 0; input ENUM_APPLIED_PRICE apply_2 = 0; input double Lot = 0.01; input int TakeProfit = 50; input int StopLoss = 50; input int MinStep = 10; input int TrailingStop = 0; input int TrailingStep = 0; input bool Close_At_Next = true; input int MAGIC = 2018; int dig; double pt; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- if(Digits==5||Digits==4){pt=0.0001;dig=4;}else{pt=0.01;dig=2;} //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } int Ma() { int ma=0; double ma1=iMA(Symbol(),0,period_1,shift_1,MODE_1,apply_1,1), ma11=iMA(Symbol(),0,period_1,shift_1,MODE_1,apply_1,2), ma2=iMA(Symbol(),0,period_2,shift_2,MODE_2,apply_2,1), ma22=iMA(Symbol(),0,period_2,shift_2,MODE_2,apply_2,2); if(ma22>ma11&&ma2ma1)ma=-1; else if(ma2ma1)ma=-2; return(ma); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- int b=0,s=0; double BuyMax = 0, BuyMin = 0, SellMax = 0, SellMin = 0, ma=iMA(Symbol(),0,period_1,shift_1,MODE_1,apply_1,1); int up=0,dn=0; double ma1=0,ma11=0,ma2=0,ma22=0; for(int i=1;ima11&&ma2ma1)dn++; } string upcom= "My order"+up; string dncom= "My order"+dn; for(int i=OrdersTotal()-1; i>=0; i--) if(OrderSelect(i,SELECT_BY_POS)) if(OrderSymbol()==Symbol()) if(OrderMagicNumber()==MAGIC) { if(OrderType()==OP_BUY&&OrderComment()==upcom) { b++; if(OrderOpenPrice()>BuyMax || BuyMax==0) { BuyMax=OrderOpenPrice(); } if(OrderOpenPrice()SellMax || SellMax==0) { SellMax=OrderOpenPrice(); } } } Comment("Segnal = "+Ma(),"\nUP = "+up,"\nDN = "+dn, "\nTotalBuyProfit = "+profit(OP_BUY), "\nTotalSellProfit = "+profit(OP_SELL), "\nTotalProfitt = "+profit(-1) ); double Dist = NormalizeDouble(MinStep*pt,Digits()); double TP=0,SL=0; if((Ma()==1&&((b==0)||(b>0&&(Ask-BuyMax)>=Dist)))||(Ma()==2&&b>0&&(Ask-BuyMax)>=Dist)) { if(StopLoss==0){SL=0;}else{SL=Ask-StopLoss*pt;} if(TakeProfit==0){TP=0;}else{TP=Ask+TakeProfit*pt;} if(OrderSend(Symbol(),OP_BUY,Lot,Ask,30,SL,TP,upcom,MAGIC,0,clrGreen)<0) Print(" Error open Buy N ",GetLastError()); } if((Ma()==-1&&((s==0)||(s>0&&(SellMin-Bid)>=Dist)))||(Ma()==-2&&s>0&&(SellMin-Bid)>=Dist)) { if(StopLoss==0){SL=0;}else{SL=Bid+StopLoss*pt;} if(TakeProfit==0){TP=0;}else{TP=Bid-TakeProfit*pt;} if(OrderSend(Symbol(),OP_SELL,Lot,Bid,30,SL,TP,dncom,MAGIC,0,clrRed)<0) Print(" Error open Buy N ",GetLastError()); } if(Ma()==1) { if(profit()>0)CloseOrder(); else if(profit(-1)<0&&profit(OP_SELL)>0)CloseOrder(OP_SELL); else if(profit(-1)<0&&profit(OP_SELL)<0)Closeprofit(OP_SELL); } if(Ma()==-1) { if(profit()>0)CloseOrder(); else if(profit(-1)<0&&profit(OP_BUY)>0)CloseOrder(OP_BUY); else if(profit(-1)<0&&profit(OP_BUY)<0)Closeprofit(OP_BUY); } if(TrailingStop>0)MoveTrailingStop(); } //+------------------------------------------------------------------+ double profit(int type=-1){ double c=0; for(int i=0;i=0 ; cnt--) { if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MAGIC&&(OrderType()==type||type==-1)) { switch(OrderType()) { case OP_BUY : { if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0,Violet)) return(false); }break; case OP_SELL : { if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),0,Violet)) return(false); }break; } } } } return (true); } //+------------------------------------------------------------------+ bool Closeprofit(int type) { int total = OrdersTotal(); for (int cnt = total-1 ; cnt >=0 ; cnt--) { if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MAGIC&&OrderType()==type&&OrderProfit()>0) { switch(OrderType()) { case OP_BUY : { if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0,Violet)) return(false); }break; case OP_SELL : { if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),0,Violet)) return(false); }break; } } } } return (true); } //+------------------------------------------------------------------+ void MoveTrailingStop() { int cnt,total=OrdersTotal(); for(cnt=0;cnt0&&NormalizeDouble(Ask-TrailingStep*pt,dig)>NormalizeDouble(OrderOpenPrice()+TrailingStop*pt,dig)) { if((NormalizeDouble(OrderStopLoss(),dig)0&&NormalizeDouble(Bid+TrailingStep*pt,dig)(NormalizeDouble(Ask+TrailingStop*pt,dig)))||(OrderStopLoss()==0)) { int mod=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+TrailingStop*pt,dig),OrderTakeProfit(),0,Red); return; } } } } } }