Code
//+------------------------------------------------------------------+
//| KELTNER_BANDS.mq4 |
//| Copyright 2020, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//...EURUSD ...1MN
//+------------------------------------------------------------------+
extern int MagicNumber=06112020;
extern double StopLoss=48;
extern double TakeProfit=72;
extern int TrailingStop=29;
extern int Slippage=3;
extern double LotFactor =155; //lotsize factor
extern int periode=8;
extern int RSIperiod=5;
extern double atrmultiplier=2;
double lot;
double MarginFree; // Free margin in percentage
extern double MinMarginLevel = 100; // Lowest allowed Margin level for new positions to be opened.
double ema0,atr0,imalow0,imahigh0,imadiff0,ema1,atr1,imalow1,imahigh1,imadiff1;
extern int volume1=1;
extern int volume0=0;
extern int adxperiod= 50;
extern int adxthreshold=10;
//+------------------------------------------------------------------+
// expert start function
//+------------------------------------------------------------------+
int start()
{
double MyPoint=Point;
if(Digits==3 || Digits==5) MyPoint=Point*10;
double TheStopLoss=0;
double TheTakeProfit=0;
//Calculate and check spread
double MaxSpreadInPoints = 30;
double Spread = Ask - Bid;
if(Spread>MaxSpreadInPoints*Point)
return(false);
//+------------------------------------------------------------------+
double am = 0.000000001; // Set variable to a very small number
double marginlevel;
// Get the Free Margin
MarginFree = AccountFreeMargin();
// Calculate Margin level
if ( AccountMargin() != 0 )
am = AccountMargin();
marginlevel = AccountEquity() / am * 100;
// Free Margin is less than the value of MinMarginLevel, so no trading is allowed
if ( marginlevel < MinMarginLevel )
{
Comment ( "Warning! Free Margin " + DoubleToStr ( marginlevel, 2 ) + " is lower than MinMarginLevel!" );
Alert ( "Warning! Free Margin " + DoubleToStr ( marginlevel, 2 ) + " is lower than MinMarginLevel!" );
return(0);
}
//+------------------------------------------------------------------+
if( TotalOrdersCount()==0 )
{
ema0 = iMA(NULL, 0, periode, 0, MODE_EMA, PRICE_CLOSE, 0);
atr0 = iATR(NULL, 0, periode, 0);
imalow0 = ema0-(atr0*atrmultiplier);
imahigh0 = ema0+(atr0*atrmultiplier);
imadiff0 = imahigh0 - imalow0;//iMA
ema1 = iMA(NULL, 0, periode, 0, MODE_EMA, PRICE_CLOSE, 1);
atr1 = iATR(NULL, 0, periode, 1);
imalow1 = ema1-(atr1*atrmultiplier);
imahigh1 = ema1+(atr1*atrmultiplier);
imadiff1 = imahigh1 - imalow1;//iMA
int result=0;
if((iMA(NULL,PERIOD_M1,1,0,MODE_EMA,PRICE_CLOSE,1)<imalow1)&&(iMA(NULL,PERIOD_M1,1,0,MODE_EMA,PRICE_CLOSE,0)>imalow0)) // Here is your open buy rule
if((Volume[volume1]> Volume[volume0])
&&(iADX(Symbol(),0,adxperiod,PRICE_CLOSE,MODE_MAIN,0)> adxthreshold) )
if((iRSI(NULL,0,RSIperiod,PRICE_CLOSE,1)<20)&&(iRSI(NULL,0,14,PRICE_CLOSE,0)>20))
{
if (!IsTradeContextBusy() && IsTradeAllowed())
result=OrderSend(Symbol(),OP_BUY,NR(Lot_Volume()),Ask,Slippage,0,0,"KELTNER_BANDS_EA",MagicNumber,0,Blue);
if(result>0)
{
TheStopLoss=0;
TheTakeProfit=0;
if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
if(OrderSelect(result,SELECT_BY_TICKET))
bool modif1=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
}
return(0);
}
if((iMA(NULL,PERIOD_M1,1,0,MODE_EMA,PRICE_CLOSE,1)>imahigh1)&&(iMA(NULL,PERIOD_M1,1,0,MODE_EMA,PRICE_CLOSE,0)<imahigh0)) // Here is your open Sell rule
if((Volume[volume1]> Volume[volume0])
&&(iADX(Symbol(),0,adxperiod,PRICE_CLOSE,MODE_MAIN,0)> adxthreshold) )
if((iRSI(NULL,0,RSIperiod,PRICE_CLOSE,1)>80)&&(iRSI(NULL,0,14,PRICE_CLOSE,0)<80))
{
if (!IsTradeContextBusy() && IsTradeAllowed())
result=OrderSend(Symbol(),OP_SELL,NR(Lot_Volume()),Bid,Slippage,0,0,"KELTNER_BANDS_EA",MagicNumber,0,Red);
if(result>0)
{
TheStopLoss=0;
TheTakeProfit=0;
if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
if(OrderSelect(result,SELECT_BY_TICKET))
bool modif2= OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
}
return(0);
}
}
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber
)
{
if(OrderType()==OP_BUY)
{
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
{
if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
{
bool modif3=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else
{
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
{
if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
{
bool modif4=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
int TotalOrdersCount()
{
int result=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS ,MODE_TRADES))
if (OrderMagicNumber()==MagicNumber) result++;
}
return (result);
}
//+------------------------------------------------------------------+
//Calculates Lot Size based on balance and factor
//+------------------------------------------------------------------+
double NR(double thelot)
{
double maxlots=MarketInfo(Symbol(),MODE_MAXLOT),
minilot=MarketInfo(Symbol(),MODE_MINLOT),
lstep=MarketInfo(Symbol(),MODE_LOTSTEP);
double lots=lstep*NormalizeDouble(thelot/lstep,0);
lots=MathMax(MathMin(maxlots,lots),minilot);
return (lots);
}
//+------------------------------------------------------------------+
double Lot_Volume()
{
lot=AccountBalance() * 0.01 /LotFactor ;
return(lot);
}
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+