order_escort

Author: Aternion
Price Data Components
Orders Execution
It automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
order_escort
ÿþ//+------------------------------------------------------------------+

//|                                                 order_escort.mq5 |

//|                                                         Aternion |

//|                                             https://www.mql5.com |

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

#property copyright "Aternion"

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

#property version   "1.00"

#property strict

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

//|                                                                  |

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

enum traling_type                   // ?5@5G8A;5=85 8<5=>20==KE :>=AB0=B 

  {

   linear,

   parabolic,

   exponential

  };

                                    // ?0@0<5B@K <8H5=8

input double target_bar=35;         // A<5I5=85 >B=>A8B5;L=> B5:CI59 :>>@48=0BK, 2 10@0E 

input double delta_points=80;       // 87<5=5=85 F5=K >B=>A8B5;L=> B5:CI59, 2 ?8?A0E



input traling_type type=linear;     // :@820O A>?@>2>645=8O

input double exponent=0.5;           // ?>:070B5;L AB5?5=8 4;O AB5?5==>9 DC=:F88

input double e=2.718;               // >A=>20=85 M:A?>=5=F80;L=>9 :@82>9

input bool tp_escort=true;          // ?5@5<5I0BL B59:?@>D8B ?>78F88

input int close_bar=15;             // 70:@KB85 ?>78F88 G5@57 n-=>5 :>;8G5AB2> 10@>2









datetime date1=TimeCurrent();

int bar_counter=0;



double k0=(delta_points/(target_bar));                                //>?@545;5=85 :>MDD8F85=B>2 4;O :@82KE A>?@>2>645=8O

double k1=( delta_points/ MathPow( (target_bar),exponent) );

double k2=( delta_points/ MathPow( e ,(target_bar)) );









double sl_null,tp_null; ulong ticket;



double traling=0;



bool flag_oder=true;



int OnInit() { return(0); }

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

//|                                                                  |

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

void OnTick()

  {



   int i;



   double sl1=0,tp1=0;



   if(isNewBar())       //?5@5@0AAG5B ?@>872>48BAO B>;L:> ?>A;5 ?>O2;5=85 =>2>3> 10@0

     {

      bar_counter++;



      switch(type)      // @0AAG8BK205BAO 01A>;NB=>5 A<5I5=85 AB>?@8:07>2, CG8BK20O >?@545;5==K5 @0=55 :>MDD8F85=BK

        {

         case linear:

            traling=k0*(bar_counter);

            break;

         case parabolic:

            traling=k1*MathPow((bar_counter),exponent);

            break;

         case exponential:

            traling=k2*MathPow(e,(bar_counter));

            break;

        }



     }



   MqlTradeRequest request;

   MqlTradeResult  result;



   int total=PositionsTotal();



   for(i=total-1; i>=0; i--)

     {



      ulong  position_ticket=PositionGetTicket(i);

      string position_symbol=PositionGetString(POSITION_SYMBOL);

      ulong  magic=PositionGetInteger(POSITION_MAGIC);



      double sl=PositionGetDouble(POSITION_SL);

      double tp=PositionGetDouble(POSITION_TP);



      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS);



      double volume=PositionGetDouble(POSITION_VOLUME);



      ENUM_POSITION_TYPE typ=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);



      if(flag_oder && position_symbol==Symbol())            // 70?><8=05< ?0@0<5B@K ?>78F88 ?> B5:CI5<C A8<2>;C

        {   sl_null=sl;    tp_null=tp;  ticket=position_ticket;      flag_oder=false;    }



      if(sl!=0)

        {



         if(typ==POSITION_TYPE_BUY)                         //2KG8A;NBAO B5:CI85 7=0G5=8O AB>?;>AA0 8 B59:?@>D8B0

           {

            sl1=sl_null+traling*Point();

            if(tp>0.00000001 && tp_escort) tp1=tp_null+traling*Point();

           }

         if(typ==POSITION_TYPE_SELL)

           {

            sl1=sl_null-traling*Point();

            if(tp>0.00000001 && tp_escort) tp1=tp_null-traling*Point();

           }



         ZeroMemory(request);

         ZeroMemory(result);



         request.action  =TRADE_ACTION_SLTP;

         request.position=position_ticket;

         request.symbol=position_symbol;



         request.sl=sl1;

         request.tp=tp1;





         request.magic=magic;



         if(MathAbs(PositionGetDouble(POSITION_SL)-request.sl)>5*Point()) //AB>??@8:07K A<5I0NBAO B>;L:> ?@8 87<5=5=88 8E G8A;>2KE 7=0G5=89 1>;55 G5< =0 5 ?8?A>2.

           { if(OrderSend(request,result)) continue;      }



        }



      if(bar_counter==close_bar)                                          // 70:@KB85 ?>78F88 G5@57 n-=>5 :>;8G5AB2> 10@>2

        {



         ZeroMemory(request);

         ZeroMemory(result);



         request.action=TRADE_ACTION_DEAL;

         request.position=PositionGetTicket(i);

         request.symbol=PositionGetString(POSITION_SYMBOL);

         request.volume=PositionGetDouble(POSITION_VOLUME);

         request.deviation=50;

         request.magic=OrderGetInteger(ORDER_MAGIC);



         if(typ==POSITION_TYPE_BUY)

           {

            request.price=SymbolInfoDouble(position_symbol,SYMBOL_BID);

            request.type =ORDER_TYPE_SELL;

           }

         else

           {

            request.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK);

            request.type =ORDER_TYPE_BUY;

           }

         if(OrderSend(request,result)) continue;



        }



     }



  }

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

//|                                                                  |

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

bool isNewBar()                              //DC=:F8O >?@545;5=8O ?>O2;5=8O =>2>3> 10@0

  {



   static datetime last_time=0;



   datetime lastbar_time=SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);



   if(last_time==0)

     {

      last_time=lastbar_time;

      return(false);

     }



   if(last_time!=lastbar_time)

     {



      last_time=lastbar_time;

      return(true);

     }



   return(false);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---