Download MetaTrader 5
To add comments, please log in or register
Deposit to your account in any convenient way. There are 4 of them here!
polymath
181
polymath 2012.08.28 14:22 

Hi guys,

I'm still having problem with my trailing stops.  The stoploss level is supposed to be changed only if the price minus the trailing stop point is greater than the existing stoploss point.  But in my program, the stop loss is being adjusted as I can see it in the visual tester.  The problem is that myposition.Stoploss() is not changing values and this is the one that I use in my if condition. Any suggestions?

if(Buy_opened==true)
    {
      if((SymbolInfoDouble(_Symbol,SYMBOL_ASK)-TSTP*_Point) >= myposition.StopLoss())
      { double newSLpoint=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK)-TSTP*_Point,_Digits);
        double newTPpoint=NormalizeDouble(myposition.TakeProfit(),_Digits);
        Print(myposition.StopLoss(),newSLpoint,newTPpoint,myposition.PriceOpen());
        Print("Previous Stoploss:",myposition.StopLoss());
     
        if(mytrade.PositionModify(_Symbol,newSLpoint,newTPpoint)==true)  
        {
          //--- Request successfully completed    
         Alert("An opened position has been successfully modified!!");
         Print("New Stoploss:",myposition.StopLoss());
         return;
        }
      else
        {
         Alert("The position modify request could not be completed - error: ",mytrade.ResultRetcodeDescription());
         return;
        }
      }
    }
    if(Sell_opened==true)
    {
      if((SymbolInfoDouble(_Symbol,SYMBOL_BID)+TSTP*_Point) <= myposition.StopLoss())
      { double newSLpoint=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK)+TSTP*_Point,_Digits);
        double newTPpoint=NormalizeDouble(myposition.TakeProfit(),_Digits);
        Print(myposition.StopLoss(),newSLpoint,newTPpoint,myposition.PriceOpen());
        Print("Previous Stoploss:",myposition.StopLoss());
        if(mytrade.PositionModify(_Symbol,newSLpoint,newTPpoint)==true)  
        {
          //--- Request successfully completed    
          Print("New Stoploss:",myposition.StopLoss());
         Alert("An opened position has been successfully modified!!");
         return;
        }
      else
        {
         Alert("The position modify request could not be completed - error: ",mytrade.ResultRetcodeDescription());
        
         return;
        }
      }

 

I printed the myposition.Stoploss() to see if the value is really changing, turns out it does not change that's why my code continuously goes through the entire thing of modifying the existing position.

Any suggestions for the replacement of myposition.Stoploss since it does not give the most current stoploss level (only the original sl level)? 

Visualize a Strategy in the MetaTrader 5 Tester
Visualize a Strategy in the MetaTrader 5 Tester
  • 2012.06.11
  • MetaQuotes Software Corp.
  • www.mql5.com
We all know the saying "Better to see once than hear a hundred times". You can read various books about Paris or Venice, but based on the mental images you wouldn't have the same feelings as on the evening walk in these fabulous cities. The advantage of visualization can easily be projected on any aspect of our lives, including work in the market, for example, the analysis of price on charts using indicators, and of course, the visualization of strategy testing. This article contains descriptions of all the visualization features of the MetaTrader 5 Strategy Tester.
meisme
39
meisme 2012.08.28 19:07  

Hi,

Seems from the log, your myposition.StopLoss() value is changing?

1.34987
1.34985
1.34984

But the pip difference is too small.  You should increase it.


Also for your reference, please check this hard trailing coding example.  There is some useful comment on the code to make it easy to understand. It's written in C++ style.

https://www.mql5.com/en/forum/6226/166552#comment_166552


Cheers.

Help me find this....
Help me find this....
  • www.mql5.com
I don't need it to open new trades, only adjust my T/P just before the market hits the one I originally set.
/
To add comments, please log in or register