nouvelle version du Rengou
Code
//+------------------------------------------------------------------+
//| RENGOU LOTSCALP V2.mq4 |
//| Copyright 2019, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//#property strict
//+------------------------------------------------------------------+
extern int MagicNumber=25102019;
double lot;
extern double StopLoss=120;
double TakeProfit=20;
int TrailingStop=20;
extern int Slippage=3;
double point = MarketInfo(Symbol(), MODE_POINT);
int spread = MarketInfo(Symbol(), MODE_SPREAD);
extern int size = 0;
extern double LotFactor =40; //lotsize factor
extern double LotFactor2 =40; //lotsize factor
extern int ecartask=14;
extern int ecartbid=2;
int Amount = 250;
int LockDown = 310;
int Start_Hour = 0;
int End_Hour = 0;
extern string Name_EA = " RENGOU-LOTSCALP V2";
bool TrailingStop_ = true;
string tp="TP IN MONEY";
bool Tp_in_Money = true;
extern double TP_in_money = 1;//agir sur ce levier
extern int volume1=1;
extern int volume0=0;
extern int adxperiod= 17;
extern int adxthreshold=24;
extern int rsiperiod=14;
extern int rsilower =30;
extern int rsiupper =70;
double pt;
int ras;
double TP=0,SL=0,TR;
//+------------------------------------------------------------------+
//| Hidden StopLoss Calculations |
//+------------------------------------------------------------------+
void StpLoss()
{
double MyPoint=Point;
if(Digits==3 || Digits==5) MyPoint=Point*10;
TP=TakeProfit*MyPoint;
SL=StopLoss*MyPoint;
double OrdP=0,OrdTP=0,OrdSL=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()==MagicNumber && Symbol()==OrderSymbol())
{
OrdP=OrderProfit()-MathAbs(OrderSwap())-MathAbs(OrderCommission());
OrdSL=(-1)*SL*OrderLots()*MarketInfo(OrderSymbol(),MODE_TICKVALUE)/Point;
OrdTP=TP*OrderLots()*MarketInfo(OrderSymbol(),MODE_TICKVALUE)/Point;
if(OrdP>OrdTP || OrdP<OrdSL)
{
if(OrderType()==OP_BUY)
bool OrdClP=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen);
if(OrderType()==OP_SELL)
bool OrdClL=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrYellow);
}
}
}
}
//+------------------------------------------------------------------+
//| Trailing Stop |
//+------------------------------------------------------------------+
void trail()
{
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(Bid - OrderOpenPrice() > TrailingStop * MarketInfo(OrderSymbol(),MODE_POINT))
{
if(OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT))
{
bool modify1=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MarketInfo(OrderSymbol(),MODE_POINT),OrderTakeProfit(),Red);
}
}
} else if(OrderType()==OP_SELL) {
if(OrderOpenPrice()-Ask>TrailingStop*MarketInfo(OrderSymbol(),MODE_POINT))
{
if((OrderStopLoss()>Ask+TrailingStop*MarketInfo(OrderSymbol(),MODE_POINT)) ||
(OrderStopLoss()==0))
{
bool modify2=OrderModify(OrderTicket(),OrderOpenPrice(),
Ask+TrailingStop*MarketInfo(OrderSymbol(),MODE_POINT),OrderTakeProfit(),Red);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
// expert start function
//+------------------------------------------------------------------+
int start()
{
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 ;
if(TrailingStop_)dtrailing();
if(Tp_in_Money && TP_in_money<=money()) closeall();
double MyPoint=Point;
if(Digits==3 || Digits==5) MyPoint=Point*10;
double TheStopLoss=0;
double TheTakeProfit=0;
if( TotalOrdersCount()==0 )
{
int result=0;
//+------------------------------------------------------------------+
// Here is your open buy rule
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
// Here is the beginning of your open buy rule strategy
//+------------------------------------------------------------------+
if((Bid>iOpen(Symbol(),0,0)))
//+------------------------------------------------------------------+
// Here is the end of your buy rule strategy
//+------------------------------------------------------------------+
{
result= OrderSend(Symbol(),OP_BUYSTOP, (AccountEquity() * 0.01 /LotFactor2),Ask+ecartask*Point,5,(Ask+ecartask*Point)-StopLoss*Point,(Ask+ecartask*Point)+TakeProfit*Point,"RENGOU-LOTSCALP V2",MagicNumber,0,clrBlue);
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
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
// Here is the beginning of your open sell rule strategy
//+------------------------------------------------------------------+
if((Bid<iOpen(Symbol(),0,0)))
//+------------------------------------------------------------------+
// Here is the end of your sell rule strategy
//+------------------------------------------------------------------+
//&&(!IsTradeContextBusy() && IsTradeAllowed())
{
result= OrderSend(Symbol(),OP_SELLSTOP, (AccountEquity() * 0.01 /LotFactor2),Bid-ecartbid*Point,5,(Bid-ecartbid*Point)+StopLoss*Point,(Bid-ecartbid*Point)-TakeProfit*Point,"RENGOU-LOTSCALP V2",MagicNumber,0,clrBlue);
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)
{
if(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))
{
if(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);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int Hour_trade()
{
bool trade=false;
if(Start_Hour>End_Hour)
{
if(Hour()>=Start_Hour || Hour()<End_Hour) trade=true;
}
else
if(Hour()>=Start_Hour && Hour()<End_Hour) trade=true;
return (trade);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int totalorder(int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber || OrderType()!=tipe) continue;
total++;
}
return(total);
}
//+------------------------------------------------------------------+
//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);
}
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void dtrailing()
{
int i,r;double tpn;
for(i=0; i<OrdersTotal(); i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber) continue;
tpn=OrderTakeProfit();
if(OrderType()==OP_BUY)
{
if(Bid-OrderOpenPrice()>pt*TrailingStop)
{
if((OrderStopLoss()<Bid-pt*TrailingStop) || (OrderStopLoss()==0))
{
if(tpn) r=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-pt*TrailingStop,OrderTakeProfit(),0,Green);
}
}
}
if(OrderType()==OP_SELL)
{
if((OrderOpenPrice()-Ask)>(pt*TrailingStop))
{
if(OrderStopLoss()>(Ask+pt*TrailingStop) || (OrderStopLoss()==0))
{
if(tpn) r=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+pt*TrailingStop,OrderTakeProfit(),0,Red);
}
}
}
}
}
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void closeall()
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber) continue;
if(OrderType()>1) ras=OrderDelete(OrderTicket());
else
{
if(OrderType()==0) ras=OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE);
else ras=OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE);
}
}
}
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
double money()
{
double dp=0;
int i;
for(i=0; i<OrdersTotal(); i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber) continue;
dp+=OrderProfit();
}
return(dp);
}
//+------------------------------------------------------------------+
bool CheckMoneyForTrade(string symb,double lots,int type)
{
double free_margin=AccountFreeMarginCheck(symb,type, lots);
//-- if there is not enough money
if(free_margin<0)
{
string oper=(type==OP_BUY)? "Buy":"Sell";
//fPrint("Not enough money for ", oper," ",lots, " ", symb, " Error code=",GetLastError());
return(false);
}
//--- checking successful
return(true);
}
//---------------------------------------------------------------------------
int CheckForClose()
{
if(getProfit()>=Amount)//{CloseAll();}
if(LockDown>0)
{
for(int TradeNumber = OrdersTotal(); TradeNumber >= 0; TradeNumber--)
{
if (OrderSelect(TradeNumber, SELECT_BY_POS, MODE_TRADES)&&(LockDown>0))
{ int Pos=OrderType();
if((Pos==OP_BUY)&&(Bid-OrderOpenPrice()>Point*LockDown)&&(OrderStopLoss() == 0))
{bool modif1=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Point,OrderTakeProfit(),0,CLR_NONE);}
if((Pos==OP_SELL)&&(OrderOpenPrice()-Ask>Point*LockDown)&&(OrderStopLoss() == 0))
{bool modif2=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-Point,OrderTakeProfit(),0,CLR_NONE);}
}
}
}
return(0);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
double getProfit()
{
double Profit = 0;
for (int TradeNumber = OrdersTotal(); TradeNumber >= 0; TradeNumber--)
{
if (OrderSelect(TradeNumber, SELECT_BY_POS, MODE_TRADES))
Profit = Profit + OrderProfit() + OrderSwap();
}
return (Profit);
}
//+------------------------------------------------------------------+
//insuring its a new candle function
//+------------------------------------------------------------------+
bool IsNewCandle()
{
static int BarsOnChart=0;
if (Bars == BarsOnChart)
return (false);
BarsOnChart = Bars;
return(true);
}