OWn TrailStop Function does not work

 

Hi community,

I developed my own trail-Stop function in addition to my already working trading class and now got the problem that i wont work.

When it places a Stop for a Buy-Position, the SL shots past the current price and the position closes and in case of a Sell Position simply nothing happens: (The Function is executed every tick)

bool CTrade::TrailStop(string pSymbol, int pTrailPoints, int pMinProfit=0, int pStep=10)
{
   if (PositionSelect(pSymbol) == true && pTrailPoints > 0)
   {
        int digits           = (int)SymbolInfoInteger(pSymbol,SYMBOL_DIGITS);
         double   currentStop = PositionGetDouble(POSITION_SL);
               currentStop = NormalizeDouble(currentStop,digits);   
          double point         = SymbolInfoDouble(pSymbol,SYMBOL_POINT);

      
         if(pStep < 10) pStep = 10;
        double step      = pStep * point;
          double minProfit = pMinProfit * point;
          double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);

      
      long posType = PositionGetInteger(POSITION_TYPE);
      
      if       (posType == POSITION_TYPE_BUY)   
      {  
         double trailStop      = BuyTrailStop(pSymbol,pTrailPoints,pStep);
         double currentProfit = SymbolInfoDouble(pSymbol,SYMBOL_BID) - openPrice;
         
         if (trailStop > currentStop + step && currentProfit >= minProfit)
         {
            return modifyPosition(pSymbol,trailStop);
         }
         else return false;   
      }
      
      else if  (posType == POSITION_TYPE_SELL)  
      {
         double trailStop      = SellTrailStop(pSymbol,pTrailPoints,pStep);
         double currentProfit = openPrice - SymbolInfoDouble(pSymbol,SYMBOL_ASK);
         
         if (trailStop < currentStop - step && currentProfit >= pMinProfit)
         {
            return modifyPosition(pSymbol,trailStop);
         }
         else return false;
      }
      
      else return false;
   }
   
   else return false;

I've searched the whole day and simply can't find my mistake - im sure someone of you will :)

Greetings and thanks in advance,

 
Distinctive:
...

Here there is an error :

      else if  (posType == POSITION_TYPE_SELL)
          ... 
          if (trailStop < currentStop - step && currentProfit >= pMinProfit MUST BE minProfit)
 

I don't get it - currentProfit has to be same or bigger as minProfit so that it starts to trail if the minimum of profit is reached .. ?

So do you mean its right like this:

    if (trailStop < currentStop - step && currentProfit = pMinProfit)
Thanks so far
 
Distinctive:

I don't get it - currentProfit has to be same or bigger as minProfit so that it starts to trail if the minimum of profit is reached .. ?

So do you mean its right like this:

Thanks so far

No. I mean you don't use the right variable.

bool CTrade::TrailStop(string pSymbol, int pTrailPoints, int pMinProfit=0, int pStep=10)
{
   if (PositionSelect(pSymbol) == true && pTrailPoints > 0)
   {
        int digits           = (int)SymbolInfoInteger(pSymbol,SYMBOL_DIGITS);
         double   currentStop = PositionGetDouble(POSITION_SL);
               currentStop = NormalizeDouble(currentStop,digits);   
          double point         = SymbolInfoDouble(pSymbol,SYMBOL_POINT);

      
         if(pStep < 10) pStep = 10;
        double step      = pStep * point;
          double minProfit = pMinProfit * point;
          double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);

      
      long posType = PositionGetInteger(POSITION_TYPE);
      
      if       (posType == POSITION_TYPE_BUY)   
      {  
         double trailStop      = BuyTrailStop(pSymbol,pTrailPoints,pStep);
         double currentProfit = SymbolInfoDouble(pSymbol,SYMBOL_BID) - openPrice;
         
         if (trailStop > currentStop + step && currentProfit >= minProfit)
         {
            return modifyPosition(pSymbol,trailStop);
         }
         else return false;   
      }
      
      else if  (posType == POSITION_TYPE_SELL)  
      {
         double trailStop      = SellTrailStop(pSymbol,pTrailPoints,pStep);
         double currentProfit = openPrice - SymbolInfoDouble(pSymbol,SYMBOL_ASK);
         
         if (trailStop < currentStop - step && currentProfit >= pMinProfit)
         {
            return modifyPosition(pSymbol,trailStop);
         }
         else return false;
      }
      
      else return false;
   }
   
   else return false;
 

Corrected it . But it does not work anyways. A Buy TrailStop always gets bigger then the actual price and closes out the position. In Sell a Trailstop does not even appear.

 
Distinctive:

Corrected it . But it does not work anyways. A Buy TrailStop always gets bigger then the actual price and closes out the position. In Sell a Trailstop does not even appear.

Please provide a code that compiles, so I can test it.
 

I attached my Trade-Libary and an Expert-Advisor for testing the TrailStop-Function (You'll have to modify the include reference to Trade.mqh contained in TestTrailStop.mq5)

So when onInit i call trade.Sell no StopLoss appears / in case of trade.Buy the StopLoss passes the price and closes the position out (as already mentioned).

Thanks in advance

#include <Neuro\Trade.mqh>

input string SYMBOL      = "EURUSD";

input int    TRAILPOINTS = 300;
input int    MINPROFIT   = 0;
input int    STEP        = 10;

input double VOLUME      = 0.10;
CTrade trade;

int OnInit()
  {
   trade.Sell(SYMBOL,VOLUME,0,0);
   return(INIT_SUCCEEDED);
  }

void OnTick() { trade.TrailStop(SYMBOL,TRAILPOINTS,MINPROFIT,STEP); }
 
 
Distinctive:

I attached my Trade-Libary and an Expert-Advisor for testing the TrailStop-Function (You'll have to modify the include reference to Trade.mqh contained in TestTrailStop.mq5)

So when onInit i call trade.Sell no StopLoss appears / in case of trade.Buy the StopLoss passes the price and closes the position out (as already mentioned).

Thanks in advance

#include <Neuro\Trade.mqh>

input string SYMBOL      = "EURUSD";

input int    TRAILPOINTS = 300;
input int    MINPROFIT   = 0;
input int    STEP        = 10;

input double VOLUME      = 0.10;
CTrade trade;

int OnInit()
  {
   trade.Sell(SYMBOL,VOLUME,0,0);
   return(INIT_SUCCEEDED);
  }

void OnTick() { trade.TrailStop(SYMBOL,TRAILPOINTS,MINPROFIT,STEP); }
 

You don't place an initial stoploss, so this condition is always false :

         if (trailStop < currentStop - step && currentProfit >= minProfit)

and your stoploss is never modified. I haven't checked further.

 

Thanks - that helped me a lot !