Même si cet EA fonctionne très bien ,je pense que l'on peut encore l' améliorer.
Par exemple ,il existe une erreur [modify Error 1]qui n'entrave pas son fonctionnement ,mais que je n'arrive pas à corriger.
Par exemple ,il existe une erreur [modify Error 1]qui n'entrave pas son fonctionnement ,mais que je n'arrive pas à corriger.
Code
//+------------------------------------------------------------------+
//| Scalper-GG-14.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
//+------------------------------------------------------------------+
extern string mn="Scalper-GG-14";
double lot;
extern int shifthighrecent=5;
extern int shiftlowrecent=2;
extern int shifthigh=5;
extern int shiftlow=7;
extern bool trail=true;
extern int TrailingStop=2;
extern int barnumber =24;
extern int barnumberrecent=33;
extern int MagicNumber=14072020;
//extern double TakeProfit=53;
extern double atrMultiple = 3;
extern int TrailingStart=21;
double stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10;
int TS=TrailingStart-TrailingStop;
extern int volume1=1;
extern int volume0=0;
extern int adxperiod= 31;
extern int adxthreshold=23;
extern int rsiperiod=21;
extern int rsilower =48;
extern int rsiupper =80;
extern int Slippage=3;
extern int Indicatorperiod = 14;
input int MaxSpreadAllow = 10; // Enter Maximum allowed spread
extern double LotFactor =60; //lotsize factor
extern int ecartask=2;
extern int ecartbid=10;
extern int Start_Time = 0; // Time to allow trading to start ( hours of 24 hr clock ) 0 for both disables
extern int Finish_Time = 0; // Time to stop trading ( hours of 24 hr clock ) 0 for both disables
extern double bidbid =600;
extern double askask =300;
extern int AddPriceGap=1;
//+------------------------------------------------------------------+
// expert start function
//+------------------------------------------------------------------+
int start()
{
if(TimeHour(TimeCurrent())>=Start_Time && TimeHour(TimeCurrent())<=Finish_Time )return(0);
double MyPoint=Point;
if(Digits==3 || Digits==5) MyPoint=Point*10;
double TheStopLoss=0;
double TheTakeProfit=0;
double booster = Volume[volume1]> Volume[volume0]
&&iADX(Symbol(),0,adxperiod,PRICE_CLOSE,MODE_MAIN,0)> adxthreshold
&&iRSI(Symbol(),0,rsiperiod,PRICE_CLOSE,0)>rsilower && iRSI(Symbol(),0,rsiperiod,PRICE_CLOSE,0)<rsiupper ;
static datetime tmeBar0;
bool newBar = false;
if ( tmeBar0 != Time[0] )
{
tmeBar0 = Time[0];
newBar = true;
}
// Get Ask and Bid for the currency
double ask = MarketInfo ( Symbol(), MODE_ASK );
double bid = MarketInfo ( Symbol(), MODE_BID );
double Spread = MarketInfo(Symbol(), MODE_SPREAD); // This will Obtain broker Spread for current pair
if(Spread > 0 && Spread <= MaxSpreadAllow) // This part compares broker spread with maximum allowed spread, and refuse trade if maxSpread is exceeded
if ( OrdersTotal () == 0 )
if(AccountFreeMargin()>(1000*(AccountEquity() * 0.01 /LotFactor)))
if(booster)
//+------------------------------------------------------------------+
// Dynamic stoploss and takeprofit
//+------------------------------------------------------------------+
double ema = iMA(NULL, 0, 300, 0, MODE_EMA, PRICE_CLOSE, 0);
double atr = iATR(NULL, 0, 3, 0);
double StopLoss = (atr * atrMultiple / Point);
double TakeProfit = StopLoss;
//+------------------------------------------------------------------+
//Here is how Keltner Channels are calculated:
//Upper Band = EMA + (ATR x multiplier)
//Middle Band = EMA
//Lower Band = EMA - (ATR x multiplier)
//The EMA period can be set to anything you want. For day trading, an EMA of 15 to 40 is typical.
//A common multiplier for the ATR is 2, meaning the upper band will be plotted 2 x ATR above the EMA, and the lower band will be plotted 2 x ATR below the EMA.
//
//UpperBand=ema+(atr*atrmultiplier);
//MiddleBand=ema;
//LowerBand=ema-(atr*atrmultiplier);
//
//
//
//+------------------------------------------------------------------+
new_del () ;
if( TotalOrdersCount()==0 )
{
int result=0;
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
// Here is your open buy rule//Ici votre strategie d'achat
double HighOfLastBarsrecent = iHigh ( Symbol (), PERIOD_M1 , iHighest ( Symbol (), PERIOD_M1 , MODE_HIGH, barnumberrecent , shifthighrecent ));
double LowOfLastBarsrecent = iLow ( Symbol (), PERIOD_M1 , iLowest ( Symbol (), PERIOD_M1 , MODE_LOW, barnumberrecent , shiftlowrecent ));
double HighOfLastBars = iHigh ( Symbol (), PERIOD_M1 , iHighest ( Symbol (), PERIOD_M1 , MODE_HIGH, barnumber , shifthigh ));
double LowOfLastBars = iLow ( Symbol (), PERIOD_M1 , iLowest ( Symbol (), PERIOD_M1 , MODE_LOW, barnumber , shiftlow ));
double AverageBars=(HighOfLastBars +LowOfLastBars)/2 ;
double PartialLow=((LowOfLastBars+AverageBars)/2);
double PartialHigh = ((HighOfLastBars+AverageBars)/2);
// Calculate a channel on Moving Averages, and check the price
double iMaLow = iMA ( Symbol(), PERIOD_M1, Indicatorperiod, 0, MODE_LWMA, PRICE_LOW, 0 );
double iMaHigh = iMA ( Symbol(), PERIOD_M1, Indicatorperiod, 0, MODE_LWMA, PRICE_HIGH, 0 );
double iMaAverage = (iMaHigh + iMaLow)/2;
double PartialiMalow = (iMaLow + iMaAverage) / 2.0;
double PartialiMahigh =(iMaHigh + iMaAverage) / 2.0;
double UpperPart=(PartialiMahigh+PartialHigh)/2;
double LowerPart= ( PartialiMalow+ PartialLow)/2;
if (((bid>= PartialiMahigh )
&&(bid>= PartialHigh )
&&(booster)))
if (bid>= HighOfLastBars )
if (HighOfLastBarsrecent>= HighOfLastBars)
//+------------------------------------------------------------------+
// Dynamic stoploss and takeprofit
//+------------------------------------------------------------------+
double ema = iMA(NULL, 0, 300, 0, MODE_EMA, PRICE_CLOSE, 0);
double ema = iMA(NULL, 0, 300, 0, MODE_EMA, PRICE_CLOSE, 0);
if (Ask + askask * Point < ema)
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
{
result=OrderSend(Symbol(),OP_BUYSTOP, NR(Lot_Volume()),Ask +ecartask*Point,5,PartialLow -StopLoss*Point-AddPriceGap,PartialHigh +TakeProfit*Point+AddPriceGap,"Scalper-GG-9",MagicNumber,0,clrLimeGreen);
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);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
// Here is your open Sell rule//Ici votre strategie de vente
if (((PartialiMalow <= bid)&&(PartialLow <= bid)&&(booster)))
if (LowOfLastBars<= bid)
if( LowOfLastBarsrecent<=LowOfLastBars )
//+------------------------------------------------------------------+
// Dynamic stoploss and takeprofit
//+------------------------------------------------------------------+
if (Bid - bidbid * Point > ema)
//+------------------------------------------------------------------+
{
result=OrderSend(Symbol(),OP_SELLSTOP, NR(Lot_Volume()),Bid-ecartbid*Point,5,PartialHigh +StopLoss*Point+AddPriceGap,PartialLow -TakeProfit*Point-AddPriceGap,"Scalper-GG-14",MagicNumber,0,clrRed);
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);
}
}
//+------------------------------------------------------------------+
// Trailing Start
//+------------------------------------------------------------------+
double Pip=Point*10;
if(TS<stoplevel) TrailingStart=(int)stoplevel+TrailingStop;
if(!IsTradeAllowed()){
MessageBox("This Expert Advisor requires Auto Trading. Please reload the EA or right click on"+
"\nthe chart to bring up the inputs window. Under the common tab check the box to"+
"\nallow live trading");
Sleep(50000);
}
if(!IsExpertEnabled()){
MessageBox("This Expert Advisor requires Auto Trading. Please click the button at the top");
Sleep(50000);
}
int ticket=0,buy_ticket=0,sell_ticket=0;
for(int i=OrdersTotal()-1; i>=0; i--)
if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==Symbol()){
ticket++;
if(OrderType()==OP_BUY) buy_ticket=OrderTicket();
if(OrderType()==OP_SELL) sell_ticket=OrderTicket();
}
if(OrderType()==OP_BUY){
if(OrderSelect(buy_ticket,SELECT_BY_TICKET))
{
if(Bid - OrderOpenPrice() > TrailingStop *10* MarketInfo(OrderSymbol(),MODE_POINT))
if((Bid-OrderOpenPrice())>(TrailingStart*Pip))
{
if(((OrderStopLoss() < Bid - TrailingStop *10* MarketInfo(OrderSymbol(), MODE_POINT))&& ((Bid-OrderOpenPrice())>(Point*TrailingStop)))||
(OrderStopLoss()==0))
{
bool modify1=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT),OrderTakeProfit(),clrGreenYellow);
Print("Buy = ",GetLastError());
return(0);
RefreshRates();
}
}
}
}
if(OrderType()==OP_SELL)
{
if(OrderSelect(sell_ticket,SELECT_BY_TICKET))
{
if(OrderOpenPrice()-Ask>TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT))
if((OrderOpenPrice()-Ask)>(TrailingStart*Pip))
{
if(((OrderStopLoss()>Ask+TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT))&& ((OrderOpenPrice()-Ask)>(Point*TrailingStop)))||
(OrderStopLoss()==0))
bool modify2=OrderModify(OrderTicket(),OrderOpenPrice(), Ask+TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT),OrderTakeProfit(),clrGreenYellow);
if(OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailingStop*Pip),OrderTakeProfit(),Red))
Print("Sell = ",GetLastError());
return(0);
RefreshRates();
}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
return(0);
}//start
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);
}
//+------------------------------------------------------------------+
// ( to close pending order)
//+------------------------------------------------------------------+
int new_del()
{
if(trail) trail();
int i,a;
int total = OrdersTotal();
string comentario,par;
for (i=total-1; i >=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if (OrderType()==OP_BUY || OrderType()==OP_SELL)
{
for (a=total-1; a >=0; a--)
{
if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
if(OrderType()==OP_SELLSTOP)
{
bool modify1= OrderDelete(OrderTicket());
Print("Deleting SELL_STOP"," Ordertype:",OrderType());
return(1);
}
if(OrderType()==OP_BUYSTOP)
{
bool modify2= OrderDelete(OrderTicket());
Print("Deleting BUY_STOP"," Ordertype:",OrderType());
return(1);
}
}
}
}
return ( 0 );
}
//+------------------------------------------------------------------+
//| Trailing Stoploss after Breakeven |
//+------------------------------------------------------------------+
void trail()
{
for (int i = OrdersTotal()-1; i >= 0; i --)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(Bid - OrderOpenPrice() > TrailingStop *10* MarketInfo(OrderSymbol(),MODE_POINT))
{
if(((OrderStopLoss() < Bid - TrailingStop *10* MarketInfo(OrderSymbol(), MODE_POINT))&& ((Bid-OrderOpenPrice())>(Point*TrailingStop)))||
(OrderStopLoss()==0))
{
bool modify1=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT),OrderTakeProfit(),clrGreenYellow);
}
}
} else if(OrderType()==OP_SELL) {
if(OrderOpenPrice()-Ask>TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT))
{
if(((OrderStopLoss()>Ask+TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT))&& ((OrderOpenPrice()-Ask)>(Point*TrailingStop)))||
(OrderStopLoss()==0))
{
bool modify2=OrderModify(OrderTicket(),OrderOpenPrice(),
Ask+TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT),OrderTakeProfit(),clrGreenYellow);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//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=AccountEquity() * 0.01 /LotFactor ;
return(lot);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+