Author: Orangetree
Price Data Components
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
Miscellaneous
It plays sound alerts
0 Views
0 Downloads
0 Favorites
Step_TSL
ÿþ//+------------------------------------------------------------------+ 

//|                              Step_TSL.mql4                       | 

//|                              OrangeTree(c)                       | 

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

#property copyright "Orangetree"

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict



input int TSL;                                  // trailing in Point()

input int BE      =0;                         // break-even in Point()

input double DIV  =1;                                // profit divisor

input int STEP    =1;                            // trailing stop step

input bool Sound  =false;                           // Trailing Signal

input int Magic   =-1;                                 // Magic Number

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

//| Expert initialization function                                   |

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





int OnInit()

  {

  

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {



   

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   /* frequnecy limitation ------------+

   static datetime time=0;

   datetime current_time =TimeCurrent();

   if(current_time-time<3) return;

   time =current_time;

   */ 

   STS(TSL,DIV,BE,STEP,Magic);

   }

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

void STS(int tsl,double delitel=1,int offset=0, int step=1,int magic=-1)

{



if(tsl==0) return;

if(step<=0) step=1;



bool   ch           =0;

double profit       =0;

double sl           =0;

int    points       =0;

   for(int i =0;i<OrdersTotal();i++)

   {



      if (OrderSelect(i,SELECT_BY_POS)== false)     continue;         

      if (OrderSymbol()!=Symbol())                  continue;

      if (OrderType()>1)                            continue;

      if (OrderMagicNumber()!=magic  && magic!=-1)  continue;

      

      if(OrderType()==OP_BUY)

       {

       profit =Bid-OrderOpenPrice()-tsl*Point();

       if(profit<0) continue;

       if(delitel==0)

           {sl =NormalizeDouble(OrderOpenPrice()+offset*Point(),Digits());}

       else

           {

           points =int((profit/delitel)/Point());                    //?@81K;L 2 ?C=:B0E

           points =(points/step)*step;

           sl =NormalizeDouble(OrderOpenPrice()+Point()*points+offset*Point(),Digits());

           }

       if(sl>OrderStopLoss())

           {

           ch =OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0);

           if(ch)

              {

              if(Sound) PlaySound("stops.wav");

              // one modification in a tick

              // or continue; 

              return;          

              } 

           }  

       }

 

       if(OrderType()==OP_SELL)

       {

       profit =OrderOpenPrice()-Ask-tsl*Point();

       if(profit<0) continue;

       if(delitel==0)

           {sl =NormalizeDouble(OrderOpenPrice()-offset*Point(),Digits());}

       else

           {

           points =int((profit/delitel)/Point());                    //?@81K;L 2 ?C=:B0E

           points =(points/step)*step;

           sl =NormalizeDouble(OrderOpenPrice()-Point()*points-offset*Point(),Digits());           

           }

       if(sl<OrderStopLoss()||OrderStopLoss()==0)

           {

           ch =OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0);

           if(ch)

              {

              if(Sound) PlaySound("stops.wav");

              // one modification in a tick 

              // or continue;

              return;          

              }  

           }

        }

   }

}   

Comments