//+------------------------------------------------------------------+ //| e-TurboFx.mq5 | //| Copyright 2012, Integer | //| https://login.mql5.com/ru/users/Integer | //+------------------------------------------------------------------+ #property copyright "Integer" #property link "https://login.mql5.com/ru/users/Integer" #property description "" #property version "1.00" #property description "Expert rewritten from MQ4, the author is RickD (http://www.mql4.com/ru/users/RickD), link to original - http://codebase.mql4.com/ru/1305" #include #include #include #include CTrade Trade; CDealInfo Deal; CSymbolInfo Sym; CPositionInfo Pos; //--- input parameters input int N = 3; /*N*/ // number of bars in one direction with increasing body size input double Lots = 0.1; /*Lots*/ // Lot, MaximumRisk parameter works with zero value. input int StopLoss = 700; /*StopLoss*/ // Stoploss in points, 0 - without stoploss. input int TakeProfit = 1200; /*TakeProfit*/ // Takeprofit in points, 0 - without takeprofit. int Handle=INVALID_HANDLE; datetime ctm[1]; datetime LastTime; double lot,slv,msl,tpv,mtp; MqlRates R[]; int up_1,dn_1,up_2,dn_2; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit(){ ArrayResize(R,N); if(!Sym.Name(_Symbol)){ Alert("Failed to initialize CSymbolInfo, try again"); return(-1); } Print("Expert initialization was completed"); return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason){ if(Handle!=INVALID_HANDLE)IndicatorRelease(Handle); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick(){ if(CopyTime(_Symbol,PERIOD_CURRENT,0,1,ctm)==-1){ return; } if(ctm[0]!=LastTime){ // Indicators if(!Indicators()){ return; } // Signals bool CloseBuy=SignalCloseBuy(); bool CloseSell=SignalCloseSell(); bool OpenBuy=SignalOpenBuy(); bool OpenSell=SignalOpenSell(); // Close if(Pos.Select(_Symbol)){ if(CloseBuy && Pos.PositionType()==POSITION_TYPE_BUY){ if(!Sym.RefreshRates()){ return; } if(!Trade.PositionClose(_Symbol,Sym.Spread()*3)){ return; } } if(CloseSell && Pos.PositionType()==POSITION_TYPE_SELL){ if(!Sym.RefreshRates()){ return; } if(!Trade.PositionClose(_Symbol,Sym.Spread()*3)){ return; } } } // Open if(!Pos.Select(_Symbol)){ if(OpenBuy && !OpenSell && !CloseBuy){ if(!Sym.RefreshRates())return; if(!SolveLots(lot))return; slv=SolveBuySL(StopLoss); tpv=SolveBuyTP(TakeProfit); if(CheckBuySL(slv) && CheckBuyTP(tpv)){ Trade.SetDeviationInPoints(Sym.Spread()*3); if(!Trade.Buy(lot,_Symbol,0,slv,tpv,"")){ return; } } else{ Print("Buy position does not open, stoploss or takeprofit is near"); } } // Sell if(OpenSell && !OpenBuy && !CloseSell){ if(!Sym.RefreshRates())return; if(!SolveLots(lot))return; slv=SolveSellSL(StopLoss); tpv=SolveSellTP(TakeProfit); if(CheckSellSL(slv) && CheckSellTP(tpv)){ Trade.SetDeviationInPoints(Sym.Spread()*3); if(!Trade.Sell(lot,_Symbol,0,slv,tpv,"")){ return; } } else{ Print("Sell position does not open, stoploss or takeprofit is near"); } } } LastTime=ctm[0]; } } //+------------------------------------------------------------------+ //| Function of data copy for indicators and price | //+------------------------------------------------------------------+ bool Indicators(){ if( CopyRates(_Symbol,PERIOD_CURRENT,1,N,R)==-1 ){ return(false); } up_1=0; dn_1=0; for(int i=0;iR[i].open)dn_1++; } up_2=0; dn_2=0; for(int i=1;iMathAbs(R[i-1].close-R[i-1].open)){ up_2++; dn_2++; } } return(true); } //+------------------------------------------------------------------+ //| Function for determining buy signals | //+------------------------------------------------------------------+ bool SignalOpenBuy(){ return(up_1==N && up_2==N-1); } //+------------------------------------------------------------------+ //| Function for determining sell signals | //+------------------------------------------------------------------+ bool SignalOpenSell(){ return(dn_1==N && dn_2==N-1); } //+------------------------------------------------------------------+ //| Function for determining buy close signals | //+------------------------------------------------------------------+ bool SignalCloseBuy(){ return (false); } //+------------------------------------------------------------------+ //| Function for determining sell close signals | //+------------------------------------------------------------------+ bool SignalCloseSell(){ return (false); } //+------------------------------------------------------------------+ //| Function for calculation the buy stoploss | //+------------------------------------------------------------------+ double SolveBuySL(int StopLossPoints){ if(StopLossPoints==0)return(0); return(Sym.NormalizePrice(Sym.Ask()-Sym.Point()*StopLossPoints)); } //+------------------------------------------------------------------+ //| Function for calculation the buy takeprofit | //+------------------------------------------------------------------+ double SolveBuyTP(int TakeProfitPoints){ if(TakeProfitPoints==0)return(0); return(Sym.NormalizePrice(Sym.Ask()+Sym.Point()*TakeProfitPoints)); } //+------------------------------------------------------------------+ //| Function for calculation the sell stoploss | //+------------------------------------------------------------------+ double SolveSellSL(int StopLossPoints){ if(StopLossPoints==0)return(0); return(Sym.NormalizePrice(Sym.Bid()+Sym.Point()*StopLossPoints)); } //+------------------------------------------------------------------+ //| Function for calculation the sell takeprofit | //+------------------------------------------------------------------+ double SolveSellTP(int TakeProfitPoints){ if(TakeProfitPoints==0)return(0); return(Sym.NormalizePrice(Sym.Bid()-Sym.Point()*TakeProfitPoints)); } //+------------------------------------------------------------------+ //| Function for calculation the minimum stoploss of buy | //+------------------------------------------------------------------+ double BuyMSL(){ return(Sym.NormalizePrice(Sym.Bid()-Sym.Point()*Sym.StopsLevel())); } //+------------------------------------------------------------------+ //| Function for calculation the minimum takeprofit of buy | //+------------------------------------------------------------------+ double BuyMTP(){ return(Sym.NormalizePrice(Sym.Ask()+Sym.Point()*Sym.StopsLevel())); } //+------------------------------------------------------------------+ //| Function for calculation the minimum stoploss of sell | //+------------------------------------------------------------------+ double SellMSL(){ return(Sym.NormalizePrice(Sym.Ask()+Sym.Point()*Sym.StopsLevel())); } //+------------------------------------------------------------------+ //| Function for calculation the minimum takeprofit of sell | //+------------------------------------------------------------------+ double SellMTP(){ return(Sym.NormalizePrice(Sym.Bid()-Sym.Point()*Sym.StopsLevel())); } //+------------------------------------------------------------------+ //| Function for checking the buy stoploss | //+------------------------------------------------------------------+ bool CheckBuySL(double StopLossPrice){ if(StopLossPrice==0)return(true); return(StopLossPriceBuyMTP()); } //+------------------------------------------------------------------+ //| Function for checking the sell stoploss | //+------------------------------------------------------------------+ bool CheckSellSL(double StopLossPrice){ if(StopLossPrice==0)return(true); return(StopLossPrice>SellMSL()); } //+------------------------------------------------------------------+ //| Function for checking the sell takeprofit | //+------------------------------------------------------------------+ bool CheckSellTP(double TakeProfitPrice){ if(TakeProfitPrice==0)return(true); return(TakeProfitPrice