Trailing Stop for MQL5

 

Hello,

I'm looking for Trailing Stop for MQL5.

I have one but It is not working.

Thanks

FXMan77

input int Inp_Trailing_FixedPips_StopLevel =3000; // Trailing::FixedPips::StopLevel
input int Inp_Trailing_FixedPips_ProfitLevel=30; // Trailing::FixedPips::ProfitLevel
//+------------------------------------------------------------------+
//| Class CTrailingFixedPips. |
//| Appointment: Class traling stops with fixed in pips stop. |
//| Derives from class CTrailing. |
//+------------------------------------------------------------------+
class CTrailingFixedPips : public CExpertTrailing
{
public:
//--- methods initialize protected data
virtual bool Init(CSymbolInfo* symbol,ENUM_TIMEFRAMES period,double adjusted_point);
//---
virtual bool CheckTrailingStopLong(CPositionInfo *position,double& sl,double& tp);
virtual bool CheckTrailingStopShort(CPositionInfo *position,double& sl,double& tp);
};
//+------------------------------------------------------------------+
//| Checking for input parameters and setting protected data. |
//| INPUT: symbol -pointer to the CSymbolInfo, |
//| period -working period, |
//| adjusted_point -adjusted point value. |
//| OUTPUT: true-if successful, false otherwise. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CTrailingFixedPips::Init(CSymbolInfo* symbol,ENUM_TIMEFRAMES period,double adjusted_point)
{
if(!CExpertTrailing::Init(symbol,period,adjusted_point)) return(false);
//--- initial data checks
if(Inp_Trailing_FixedPips_ProfitLevel*(adjusted_point/m_symbol.Point())<m_symbol.StopsLevel() &&
Inp_Trailing_FixedPips_ProfitLevel!=0)
{
printf("CTrailingFixedPips: Take Profit must be greater than %d",m_symbol.StopsLevel());
return(false);
}
if(Inp_Trailing_FixedPips_StopLevel*(adjusted_point/m_symbol.Point())<m_symbol.StopsLevel())
{
printf("CTrailingFixedPips: Trailing Stop must be greater than %d",m_symbol.StopsLevel());
return(false);
}
//--- ok
return(true);
}
//+------------------------------------------------------------------+
//| Checking trailing stop and/or profit for long position. |
//| INPUT: symbol -symbol. |
//| OUTPUT: true-if successful, false otherwise. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CTrailingFixedPips::CheckTrailingStopLong(CPositionInfo *position,double& sl,double& tp)
{
double delta;
double price=m_symbol.Bid();
//---
sl=EMPTY_VALUE;
tp=EMPTY_VALUE;
if(Inp_Trailing_FixedPips_StopLevel!=0)
{
delta=Inp_Trailing_FixedPips_StopLevel*m_adjusted_point;
if(position.StopLoss()==0.0)
{
if(price-position.PriceOpen()>delta) sl=price-delta;
}
else
{
if(price-position.StopLoss()>delta) sl=price-delta;
}
}
if(Inp_Trailing_FixedPips_ProfitLevel!=0)
{
delta=Inp_Trailing_FixedPips_ProfitLevel*m_adjusted_point;
if(sl!=EMPTY_VALUE) tp=price+delta;
}
//---
return(sl!=EMPTY_VALUE);
}
//+------------------------------------------------------------------+
//| Checking trailing stop and/or profit for short position. |
//| INPUT: symbol -symbol. |
//| OUTPUT: true-if successful, false otherwise. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CTrailingFixedPips::CheckTrailingStopShort(CPositionInfo *position,double& sl,double& tp)
{
double delta;
double price=m_symbol.Ask();
//---
sl=EMPTY_VALUE;
tp=EMPTY_VALUE;
if(Inp_Trailing_FixedPips_StopLevel!=0)
{
delta=Inp_Trailing_FixedPips_StopLevel*m_adjusted_point;
if(position.StopLoss()==0.0)
{
if(position.PriceOpen()-price>delta) sl=price+delta;
}
else
{
if(position.StopLoss()-price>delta) sl=price+delta;
}
}
if(Inp_Trailing_FixedPips_ProfitLevel!=0)
{
delta=Inp_Trailing_FixedPips_ProfitLevel*m_adjusted_point;
if(sl!=EMPTY_VALUE) tp=price-delta;
}
//---
return(sl!=EMPTY_VALUE);
}
//+------------------------------------------------------------------+

 

I think you will get more responses on MQL5.com
I think there is also an article on there with a trailing stop.
I haven't even looked at MQL5 yet, because brokers haven't set a date when they're rolling out MT5 onto live accounts.
It wont take long to learn so I will start nearer the time.

Reason: