//+------------------------------------------------------------------+ //| TradeChannel.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 MQL4, the author: Alejandro Galindo and Tom Maneval, published on mql4.com by Scriptor (http://www.mql4.com/ru/users/Scriptor), link - http://codebase.mql4.com/ru/1711" #include #include #include #include CTrade Trade; CDealInfo Deal; CSymbolInfo Sym; CPositionInfo Pos; //+------------------------------------------------------------------+ //| Base class of the trade signals options | //+------------------------------------------------------------------+ class CTradeSignals{ protected: bool m_buy; bool m_sell; public: virtual bool Init(){ return(false); } virtual bool Refresh(){ return(false); } virtual void DeInit(){} bool SigBuy(){ return(m_buy); } bool SigSell(){ return(m_sell); } }; enum ESigType{ MACD=0, Pivot=1, SupRes=2, i_TrendRSI=3, i_TrendRSISto=4, i_TrRSIStoMFI=5 }; enum EBBLine{ Base=BASE_LINE, Upper=UPPER_BAND, Lower=LOWER_BAND }; input bool Trade_ON = true; /*Trade_ON*/ // Can open a position input double Lots = 0.1; /*Lots*/ // Lot, MaximumRisk parameter works with zero value. input double MaximumRisk = 0.05; /*MaximumRisk*/ // Risk (valid for Lots=0). input int StopLoss = 2500; /*StopLoss*/ // Stoploss in points, 0 - without stoploss. input int TakeProfit = 500; /*TakeProfit*/ // Start position takeprofit in points input int TakeProfit2 = 100; /*TakeProfit2*/ // Takeprofit when need to add in points input int MaxCount = 10; /*MaxCount*/ // Maximum number of openings in the same direction, -1 - unlimited input int DoubleCount = 5; /*DoubleCount*/ // Number of transactions with lot multiplication coefficient as 2, other open with 1.5 input int Pips = 500; /*Pips*/ // Level of adding in points input int Trailing = 0; /*Trailing*/ // Trailing level, if value is 0 - then trailing off. input int Shift = 1; /*Shift*/ // Bar on which indicators are checked: 0 - shaped bar, 1 - the first shaped bar input bool ReverseCondition = false; /*ReverseCondition*/ // Change the buy and sell signals input ESigType OPEN_POS_BASED_ON = MACD; /*OPEN_POS_BASED_ON*/ // Type of trade signals input int MACD_FastPeriod = 14; /*MACD_FastPeriod*/ // Period of fast лю MACD input int MACD_SlowPeriod = 26; /*MACD_SlowPeriod*/ // Period of slow лю MACD input ENUM_APPLIED_PRICE MACD_Price = PRICE_CLOSE; /*MACD_Price*/ // MACD price input int Pivot_DayStartHour = 0; /*Pivot_DayStartHour*/ // Hour of day start input int Pivot_DayStartMinute = 0; /*Pivot_DayStartMinute*/ // Minute of day start input bool Pivot_AttachSundToMond = true; /*Pivot_AttachSundToMond*/ // Attach the sunday bars to monday input int SupRes_iPeriod = 70; /*SupRes_iPeriod*/ // Support_and_Resistance indicator period input ENUM_APPLIED_PRICE iT_Price = PRICE_CLOSE; /*iT_Price*/ // type of price at which the calculated amount of price and Bollinger Bands input int iT_BBPeriod = 10; /*iT_BBPeriod*/ // BB period input int iT_BBShift = 0; /*iT_BBShift*/ // BB shift input double iT_BBDeviation = 2; /*iT_BBDeviation*/ // BB deviation input ENUM_APPLIED_PRICE iT_BBPrice = PRICE_CLOSE; /*iT_BBPrice*/ // BB price input EBBLine iT_BBLine = BASE_LINE; /*iT_BBLine*/ // Used line of the Bollinger Bands input int iT_BullsBearsPeriod = 6; /*iT_BullsBearsPeriod*/ // Bulls Bears Power period input int RSI_Period = 14; /*RSI_Period*/ // RSI period input ENUM_APPLIED_PRICE RSI_Price = PRICE_CLOSE; /*RSI_Price*/ // RSI price input int St_KPeriod = 8; /*St_KPeriod*/ // K stochastic period input int St_DPeriod = 3; /*St_DPeriod*/ // D stochastic period input int St_SPeriod = 4; /*St_SPeriod*/ // S stochastic period input ENUM_MA_METHOD St_Method = MODE_SMA; /*St_Method*/ // Stochastic method input ENUM_STO_PRICE St_Price = STO_LOWHIGH; /*St_Price*/ // Stochastic price input int St_UpperLevel = 80; /*St_UpperLevel*/ // Top level of stochastic input int St_LowerLevel = 20; /*St_LowerLevel*/ // Lower level of stochastic input int MFI_Period = 5; /*MFI_Period*/ // MFI period input ENUM_APPLIED_VOLUME MFI_Volume = VOLUME_TICK; /*MFI_Volume*/ // MFI volume int Handle=INVALID_HANDLE; datetime ctm[1]; datetime LastTime; double lot,slv,msl,tpv,mtp; int MACD_SignalPeriod = 1; bool Pivot_PivotsBufers = true; bool Pivot_MidpivotsBuffers = false; bool Pivot_CamarillaBuffers = false; bool Pivot_PivotsLines = false; bool Pivot_MidpivotsLines = false; bool Pivot_CamarillaLines = false; color Pivot_ClrPivot = clrOrange; color Pivot_ClrS = clrRed; color Pivot_ClrR = clrDeepSkyBlue; color Pivot_ClrM = clrBlue; color Pivot_ClrCamarilla = clrYellow; color Pivot_ClrTxt = clrWhite; CTradeSignals * TradeSignals; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit(){ switch(OPEN_POS_BASED_ON){ case MACD: TradeSignals=new CSigMACD(); break; case Pivot: TradeSignals=new CSigPivot(); break; case SupRes: TradeSignals=new CSigSupRes(); break; case i_TrendRSI: TradeSignals=new CSigi_TrendRSI(); break; case i_TrendRSISto: TradeSignals=new CSigi_TrendRSISto(); break; case i_TrRSIStoMFI: TradeSignals=new CSigi_TrRSIStoMFI(); break; } if(!TradeSignals.Init()){ Alert("Failed to loading the indicator, try again"); return(-1); } 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){ TradeSignals.DeInit(); delete(TradeSignals); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick(){ if(CopyTime(_Symbol,PERIOD_CURRENT,0,1,ctm)==-1){ return; } if(Shift==0 || ctm[0]!=LastTime){ // Indicators if(!TradeSignals.Refresh()){ return; } bool BuySig; bool SellSig; if(ReverseCondition){ BuySig=TradeSignals.SigSell(); SellSig=TradeSignals.SigBuy(); } else{ BuySig=TradeSignals.SigBuy(); SellSig=TradeSignals.SigSell(); } // Open if(!Pos.Select(_Symbol)){ if(BuySig && !SellSig && Trade_ON){ if(!Sym.RefreshRates())return; if(!SolveLots(lot))return; slv=0; if(MaxCount==1){ 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(SellSig && !BuySig && Trade_ON){ if(!Sym.RefreshRates())return; if(!SolveLots(lot))return; slv=0; if(MaxCount==1){ 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"); } } } else{ if(!Sym.RefreshRates())return; double Price,StartLots; int Index; int k=DoubleCount; switch(Pos.PositionType()){ case POSITION_TYPE_BUY: if(BuySig){ if(!FindLastInPrice(DEAL_TYPE_BUY,Price,Index)){ return; } if((Index=Sym.Point()*Pips){ if(!FindStartLots(DEAL_TYPE_BUY,StartLots))return; lot=StartLots*MathPow(2,MathMin(Index,k-1)); if(Index>k-1)lot=lot*MathPow(1.5,Index-k+1); lot=fLotsNormalize(lot); slv=0; if(StopLoss!=0){ if(Index+1==MaxCount){ slv=SolveBuySL(StopLoss); slv=MathMin(slv,Sym.NormalizePrice(BuyMSL()-Sym.Point())); } } tpv=0; if(TakeProfit2!=0){ tpv=(Pos.PriceOpen()*Pos.Volume()+Sym.Ask()*lot)/(Pos.Volume()+lot)+Sym.Point()*TakeProfit2; tpv=Sym.NormalizePrice(tpv); tpv=MathMax(tpv,Sym.NormalizePrice(BuyMTP()+Sym.Point())); } Trade.SetDeviationInPoints(Sym.Spread()*3); Trade.Buy(lot,_Symbol,0,slv,tpv,(Index+1)+"="); } } break; case POSITION_TYPE_SELL: if(SellSig){ if(!FindLastInPrice(DEAL_TYPE_SELL,Price,Index)){ return; } if((Index=Sym.Point()*Pips){ if(!FindStartLots(DEAL_TYPE_SELL,StartLots))return; lot=StartLots*MathPow(2,MathMin(Index,k-1)); if(Index>k-1)lot=lot*MathPow(1.5,Index-k+1); lot=fLotsNormalize(lot); slv=0; if(StopLoss!=0){ if(Index+1==MaxCount){ slv=SolveSellSL(StopLoss); slv=MathMax(slv,Sym.NormalizePrice(SellMSL()+Sym.Point())); } } tpv=0; if(TakeProfit2!=0){ tpv=(Pos.PriceOpen()*Pos.Volume()+Sym.Ask()*lot)/(Pos.Volume()+lot)-Sym.Point()*TakeProfit2; tpv=Sym.NormalizePrice(tpv); tpv=MathMin(tpv,Sym.NormalizePrice(SellMTP()-Sym.Point())); } Trade.SetDeviationInPoints(Sym.Spread()*3); Trade.Sell(lot,_Symbol,0,slv,tpv,(Index+1)+"="); } } break; } } LastTime=ctm[0]; } fSimpleTrailing(); } bool FindStartLots(long aType,double & aLots){ if(!SolveLots(aLots)){ return(false); } if(!HistorySelect(0,TimeCurrent())){ return(false); } for(int i=HistoryDealsTotal()-1;i>=0;i--){ if(Deal.SelectByIndex(i)){ if(Deal.Symbol()==_Symbol){ if(Deal.DealType()==aType){ if(Deal.Entry()==DEAL_ENTRY_IN){ int p=StringFind(Deal.Comment(),"=",0); if(p==-1){ aLots=Deal.Volume(); return(true); } } } } } else{ return(false); } } return(true); } bool FindLastInPrice(long aType,double & aPrice,int & aIndex){ aPrice=0; aIndex=1; if(!HistorySelect(0,TimeCurrent())){ return(false); } for(int i=HistoryDealsTotal()-1;i>=0;i--){ if(Deal.SelectByIndex(i)){ if(Deal.Symbol()==_Symbol){ if(Deal.DealType()==aType){ if(Deal.Entry()==DEAL_ENTRY_IN){ int p=StringFind(Deal.Comment(),"=",0); if(p==-1){ aIndex=1; } else{ aIndex=StringToInteger(StringSubstr(Deal.Comment(),0,p)); } aPrice=Deal.Price(); return(true); } } } } else{ return(false); } } return(true); } //+------------------------------------------------------------------+ //| Function of data copy for indicators and price | //+------------------------------------------------------------------+ bool Indicators(){ return(true); } //+------------------------------------------------------------------+ //| Function for determining buy signals | //+------------------------------------------------------------------+ bool SignalOpenBuy(){ return(false); } //+------------------------------------------------------------------+ //| Function for determining sell signals | //+------------------------------------------------------------------+ bool SignalOpenSell(){ return(false); } //+------------------------------------------------------------------+ //| 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=Sym.NormalizePrice(Pos.PriceOpen())){ if(nsl>Sym.NormalizePrice(Pos.StopLoss())){ tmsl=Sym.NormalizePrice(Sym.Bid()-_Point*Sym.StopsLevel()); if(nsltmsl){ Trade.PositionModify(_Symbol,nsl,Pos.TakeProfit()); } } } break; } } class CSigMACD:public CTradeSignals{ protected: int m_Handle; double m_val[1]; double m_val1[1]; public: bool Init(){ m_Handle=iMACD(NULL,PERIOD_CURRENT,MACD_FastPeriod,MACD_SlowPeriod,MACD_SignalPeriod,MACD_Price); return(m_Handle!=INVALID_HANDLE); } bool Refresh(){ if( CopyBuffer(m_Handle,0,Shift,1,m_val)==-1 || CopyBuffer(m_Handle,0,Shift+1,1,m_val1)==-1 )return(false); m_buy=(m_val[0]>m_val1[0]); // MACD is growing. m_sell=(m_val[0]m_val[0]); // The bar close price is greater than Pivot. m_sell=(m_cl[0]m_valR[0] && m_cl1[0]<=m_valR1[0]); // Crossing the line of resistance up. m_sell=(m_cl[0]=m_valS1[0]); // Crossing the line of support down. } return(true); } void DeInit(){ if(m_Handle!=INVALID_HANDLE)IndicatorRelease(m_Handle); } }; class CSigi_TrendRSI:public CTradeSignals{ protected: int m_iTrendHandle; int m_RSIHand; double m_it00[1]; double m_it10[1]; double m_it01[1]; double m_rsi_0[1]; double m_rsi_1[1]; public: bool Init(){ m_iTrendHandle=iCustom(_Symbol,PERIOD_CURRENT,"i_Trend",iT_Price,iT_BBPeriod,iT_BBShift,iT_BBDeviation,iT_BBPrice,iT_BBLine,iT_BullsBearsPeriod); m_RSIHand=iRSI(_Symbol,PERIOD_CURRENT,RSI_Period,RSI_Price); return(m_iTrendHandle!=INVALID_HANDLE && m_RSIHand!=INVALID_HANDLE); } bool Refresh(){ if( CopyBuffer(m_iTrendHandle,0,Shift,1,m_it00)==-1 || CopyBuffer(m_iTrendHandle,0,Shift+1,1,m_it01)==-1 || CopyBuffer(m_iTrendHandle,1,Shift,1,m_it10)==-1 || CopyBuffer(m_RSIHand,0,Shift,1,m_rsi_0)==-1 || CopyBuffer(m_RSIHand,0,Shift+1,1,m_rsi_1)==-1 )return(false); m_buy= (m_it00[0]>m_it10[0] && m_it00[0]>m_it01[0] && m_rsi_0[0]>m_rsi_1[0]); // The green is growing and more than red, RSI is growing. m_sell= (m_it00[0]m_it10[0] && m_it00[0]>m_it01[0] && m_st00[0]>m_st10[0] && m_st00[0]>m_st01[0] && m_st00[0]St_LowerLevel && m_rsi0[0]>m_rsi1[0]); // The green is growing and more than red, the main stochastic is growing and more than signal stochastic, located between the upper and lower levels, RSI is growing. m_buy=(m_it00[0]St_LowerLevel && m_rsi0[0]m_it10[0] && m_it00[0]>m_it01[0] && m_st00[0]>m_st10[0] && m_st00[0]>m_st01[0] && m_st00[0]St_LowerLevel && m_rsi0[0]>m_rsi1[0] && m_mfi0[0]>m_mfi1[0]); // The green is growing and more than red, the main stochastic is growing and more than signal stochastic, located between the upper and lower levels, RSI is growing, MFI is growing m_sell=(m_it00[0]St_LowerLevel && m_rsi0[0]