Simple_Trailing_Stop_Loss

Author: Copyright 2022, MrBrooklin
0 Views
0 Downloads
0 Favorites
Simple_Trailing_Stop_Loss
ÿþ//+------------------------------------------------------------------+

//|                                    Simple_Trailing_Stop_Loss.mq5 |

//|                                       Copyright 2022, MrBrooklin |

//|                         https://www.mql5.com/ru/users/mrbrooklin |

//+------------------------------------------------------------------+

#property copyright "Copyright 2022, MrBrooklin"

#property link      "https://www.mql5.com/ru/users/mrbrooklin"

#property description "@>AB>9 B59;8=3 AB>? ;>AA0"

#property version   "1.00"

//---

#include <Trade/PositionInfo.mqh>

#include <Trade/Trade.mqh>

//---

CPositionInfo pos_info;

CTrade        trade;

//+------------------------------------------------------------------+

//| %+  " +                                                |

//+------------------------------------------------------------------+

input int Tral_Start=300; // 0AAB>O=85 >B B5:CI59 F5=K

input int Tral_Step=50;   //(03

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

//---

   if(!PositionSelect(_Symbol)) // 5A;8 =0 B5:CI5< A8<2>;5 =5B >B:@KB>9 ?>78F88

      return; // B> 2KE>48<

   Trailing_Stop_Loss(); // 0 5A;8 5ABL, B> B@59;8< AB>? ;>AA

  }

//+------------------------------------------------------------------+

//| Trailing Stop Loss function                                      |

//+------------------------------------------------------------------+

bool Trailing_Stop_Loss() // A>74048< DC=:F8N B@59;8=30 AB>? ;>AA0 C >B:@KB>9 ?>78F88

  {

   for(int i=PositionsTotal()-1; i>=0; i--) // 70?CAB8< F8:; 8 ?5@515@Q< 2A5 >B:@KBK5 ?>78F88

     {

      if(pos_info.SelectByIndex(i)) // 5A;8 2K1@0=0 ?>78F8O

        {

         if(pos_info.Symbol()==Symbol()) // 8 5A;8 A8<2>; A>2?0405B

           {

            if(pos_info.Profit()<0.0) // ?@>25@O5< ?>78F8N 8 5A;8 5Q ?@>D8B <5=LH5 =C;O,

               continue; // B> =0G8=05< 2AQ 70=>2>

            //---

            if(pos_info.PositionType()==POSITION_TYPE_BUY) // 5A;8 2K1@0==0O ?>78F8O BUY

              {

               if(pos_info.PriceCurrent()-(Tral_Start+Tral_Step)*_Point>pos_info.StopLoss())

                 {

                  trade.PositionModify(pos_info.Ticket(),

                                       pos_info.PriceCurrent()-Tral_Start*_Point,

                                       pos_info.TakeProfit()); // ?5@5<5I05< AB>? ;>AA

                 }

              }

            else

               if(pos_info.PositionType()==POSITION_TYPE_SELL) // 5A;8 2K1@0==0O ?>78F8O SELL

                 {

                  if(pos_info.PriceCurrent()+(Tral_Start+Tral_Step)*_Point<pos_info.StopLoss())

                    {

                     trade.PositionModify(pos_info.Ticket(),

                                          pos_info.PriceCurrent()+Tral_Start*_Point,

                                          pos_info.TakeProfit()); // ?5@5<5I05< AB>? ;>AA

                    }

                 }

           }

        }

     }

   return(true);

  }

//+------------------------------------------------------------------+

Comments