Property Copyright
Property Copyright
//| EA MQ5.mq5 |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int handle;
int handleRSI;
int handleMA;
double upperBuffer[];
double baseBuffer[];
double lowerBuffer[];
double bufferRSI[];
double bufferMA[];
MqlTick currentTick;
CTrade trade;
datetime OpenTimeBuy = 0;
datetime OpenTimeSell = 0;
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int OnInit(){
// check inputs
if(InpMagicnumber<=0) {
return INIT_PARAMETERS_INCORRECT;
if(InpLotSize<=0 || InpLotSize>10) {
return INIT_PARAMETERS_INCORRECT;
if(InpPeriod<=1) {
return INIT_PARAMETERS_INCORRECT;
}
if(InpDeviation<=0) {
return INIT_PARAMETERS_INCORRECT;
if(InpRSIPeriod<=1) {
return INIT_PARAMETERS_INCORRECT;
if(InpRSILevel>=100 || InpRSILevel<=50) {
return INIT_PARAMETERS_INCORRECT;
if(InpMAPeriod<=1) {
return INIT_PARAMETERS_INCORRECT;
if(InpStoploss<0) {
return INIT_PARAMETERS_INCORRECT;
if(InpTakeProfit<0) {
return INIT_PARAMETERS_INCORRECT;
trade.SetExpertMagicNumber(InpMagicnumber);
handle = iBands(_Symbol,PERIOD_CURRENT,InpPeriod,1,InpDeviation,PRICE_CLOSE);
if(handle == INVALID_HANDLE) {
return INIT_FAILED;
handleRSI = iRSI(_Symbol,PERIOD_CURRENT,InpRSIPeriod,PRICE_OPEN);
if(handleRSI == INVALID_HANDLE) {
return INIT_FAILED;
handleMA = iMA(_Symbol,InpMATimeframe,InpMAPeriod,0,MODE_SMA,PRICE_OPEN);
if(handleMA == INVALID_HANDLE) {
return INIT_FAILED;
ArraySetAsSeries(upperBuffer,true);
ArraySetAsSeries(baseBuffer,true);
ArraySetAsSeries(lowerBuffer,true);
ArraySetAsSeries(bufferRSI,true);
ArraySetAsSeries(bufferMA,true);
return(INIT_SUCCEEDED);
//+-----------------------------------------------------------------+
//+-----------------------------------------------------------------+
if(handle!=INVALID_HANDLE) {IndicatorRelease(handle);}
if(handleRSI!=INVALID_HANDLE) {IndicatorRelease(handleRSI);}
if(handleMA!=INVALID_HANDLE) {IndicatorRelease(handleMA);}
//+-----------------------------------------------------------------+
//+-----------------------------------------------------------------+
void OnTick() {
if(!IsNewBar()) {return;}
if(!SymbolInfoTick(_Symbol,currentTick)) {
return;
+ CopyBuffer(handle,1,0,1,upperBuffer)
+ CopyBuffer(handle,2,0,1,lowerBuffer);
if(values!=3) {
return;
Comment("up[0]:",upperBuffer[0],
"\nbase[0]:",baseBuffer[0],
"\nlow[0]:",lowerBuffer[0]);
if(!CountOpenPositions(countBuy,countSell)) {return;}
OpenTimeBuy = iTime(_Symbol,PERIOD_CURRENT,0);
if(!NormalizePrice(sl,sl)) {return;}
if(!NormalizePrice(tp,tp)) {return;}
trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,InpLotSize,currentTick.ask,sl,tp,"Bollinger band
EA");
OpenTimeSell = iTime(_Symbol,PERIOD_CURRENT,0);
if(!NormalizePrice(sl,sl)) {return;}
if(!NormalizePrice(tp,tp)) {return;}
trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,InpLotSize,currentTick.bid,sl,tp,"Bollinger band
EA");
if(!CountOpenPositions(countBuy,countSell)) {return;}
values = CopyBuffer(handleRSI,0,0,2,bufferRSI);
if(values!=2) {
return;
// get ma value
values = CopyBuffer(handleMA,0,0,1,bufferMA);
if(values!=1) {
return;
Comment("bufferRSI[0]:",bufferRSI[0],
"\nbufferRSI[1]:",bufferRSI[1],
"\nbufferMA[0]:",bufferMA[0]);
if(!CountOpenPositions(countBuy,countSell)) {return;}
if(!NormalizePrice(sl,sl)) {return;}
if(!NormalizePrice(tp,tp)) {return;}
trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,InpLotSize,currentTick.ask,sl,tp,"RSI MA filter
EA");
}
// check for sell position
if(!NormalizePrice(sl,sl)) {return;}
if(!NormalizePrice(tp,tp)) {return;}
trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,InpLotSize,currentTick.bid,sl,tp,"RSI MA filter
EA");
//+-------------------------------------------------------------------+
//+-------------------------------------------------------------------+
bool IsNewBar() {
if(previousTime!=currentTime) {
previousTime=currentTime;
return true;
return false;
countBuy = 0;
countSell = 0;
int total = PositionsTotal();
if(positionTicket<=0) {
return false;
if(!PositionSelectByTicket(positionTicket)) {
return false;
long magic;
if(!PositionGetInteger(POSITION_MAGIC,magic)) {
return false;
if(magic==InpMagicnumber) {
long type;
if(!PositionGetInteger(POSITION_TYPE,type)) {
return false;
if(type==POSITION_TYPE_BUY) {countBuy++;}
if(type==POSITION_TYPE_SELL) {countSell++;}
return true;
// normalize price
if(!SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE,tickSize)) {
return false;
normalizedPrice = NormalizeDouble(MathRound(price/tickSize)*tickSize,_Digits);
return true;
// close positions
if(positionTicket<=0) {
return false;
if(!PositionSelectByTicket(positionTicket)) {
return false;
long magic;
if(!PositionGetInteger(POSITION_MAGIC,magic)) {
return false;
if(magic==InpMagicnumber) {
long type;
if(!PositionGetInteger(POSITION_TYPE,type)) {
trade.PositionClose(positionTicket);
if(trade.ResultRetcode() !=TRADE_RETCODE_DONE) {
" result:",(string)trade.ResultRetcode()+":",trade.ResultRetcode());
return false;
return true;