https://www.mql5.com/en/code/26718
- Ajustement de la taille des lots en fonction de l' Equity
- mise en place d'un Trailing stop
//+------------------------------------------------------------------+
- Ajustement de la taille des lots en fonction de l' Equity
- mise en place d'un Trailing stop
//+------------------------------------------------------------------+
Code
//+------------------------------------------------------------------+
//| Grid_Template.mq4 |
//| Copyright 2019, DKP Sweden,CS Robots |
//| https://www.mql5.com/en/users/kenpar |
//+------------------------------------------------------------------+
//--------------------------------------------------------------------
#property copyright "Copyright 2019,DKP Sweden | CS Robots"
#property link "http://www.mql5.com/en/users/kenpar"
#property version "1.00"
#property description "Grid Template EA"
#property strict
//--External variables
extern int MagicNumber = 987654321;//Magic number
extern string EaComment = "Grid_Template";//Order comment
extern bool trail=true;
extern double TrailingStop=40;
extern double TakeProfit = 100.;//Take Profit in pips
extern double StopLoss = 80;//Stop loss in pips
extern double PriceDistance = 12;//Distance from price in pips
extern double GridStep = 4;//Step between grid orders in pips
extern int GridOrders = 5;//Amount of grid orders
int PendingExpiration = 4;//Pending expiration after xx hours
extern int LotFactor =80;
//--Internal variables
double PriceB,PriceS,StopB,StopS,
TakeB,TakeS,_points,PT,Lots;
int LotDigits;
datetime _e = 0;
int Ticket = 0;
//--
int OnInit()
{
//--Determine digits
_points=MarketInfo(Symbol(),MODE_POINT);
//--
if(Digits==5 || Digits==3)
PT = _points*10;
else
PT = _points;
//--
return(INIT_SUCCEEDED);
}
//--
void OnDeinit(const int reason)
{
}
//--
void OnTick()
{
//If trade allowed and no positions exists by any chart - open a new set of grid orders
if(IsTradeAllowed() && !IsTradeContextBusy())
if(trail) trail();
{
if(PosSelect()==0)
{
GridPos(PriceDistance,TakeProfit,StopLoss);//Place grid
}
return;
}
}
///////////////////////////////////////////////////////////
//Grid order send function
void GridPos(double _Dist,double _Take,double _Stop)
{
int i;
_e=TimeCurrent()+PendingExpiration*60*60;
//--
for(i=0; i<GridOrders; i++)
{
PriceB = NormalizeDouble(Ask+(_Dist*PT)+(i*GridStep*PT),Digits);
TakeB = PriceB + _Take * PT;
StopB = PriceB - _Stop * PT;
Ticket=OrderSend(Symbol(),OP_BUYSTOP,(AccountEquity() * 0.01 /LotFactor),PriceB,3,StopB,TakeB,EaComment,MagicNumber,_e,Green);
//--
PriceS = NormalizeDouble(Bid-(_Dist*PT)-(i*GridStep*PT),Digits);
TakeS = PriceS - _Take * PT;
StopS = PriceS + _Stop * PT;
Ticket=OrderSend(Symbol(),OP_SELLSTOP,(AccountEquity() * 0.01 /LotFactor),PriceS,3,StopS,TakeS,EaComment,MagicNumber,_e,Red);
}
if(Ticket<1)
{
Print("Order send error - errcode: ",GetLastError());
return;
}
else
{
Print("Grid placed successfully!");
}
return;
}
//////////////////////////////////////////////////////////
//PositionSelector - Determines open positions
int PosSelect()
{
int posi=0;
for(int k = OrdersTotal() - 1; k >= 0; k--)
{
if(!OrderSelect(k, SELECT_BY_POS))
{
Print("Order pos selection failed - errcode: ",GetLastError());
}
if(OrderSymbol()!=Symbol()&&OrderMagicNumber()!=MagicNumber)
{
continue;
}
if(OrderCloseTime() == 0 && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(OrderType() == OP_BUY)
posi = 1; //Long position
if(OrderType() == OP_SELL)
posi = -1; //Short positon
if(OrderType() == OP_BUYSTOP)
posi = 1; //Pending Long position
if(OrderType() == OP_SELLSTOP)
posi = -1; //Pending Short positon
}
}
return(posi);
}
//+----------------End of Grid Template EA-------------------+
//+------------------------------------------------------------------+
//| Trailing Stoploss après 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);
}
}
}
}
}
}
//+------------------------------------------------------------------+
Modifié le 2020-02-25 13:56:55 par
lefeuvr3
: erreur sur balise