Line_Profit

Author: Yuriy Tokman (YTG)
Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
Line_Profit
ÿþ//+------------------------------------------------------------------+

//|                                                  Line_Profit.mq4 |

//|                                               Yuriy Tokman (YTG) |

//|                       https://www.mql5.com/ru/users/satop/seller |

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

#property copyright "Yuriy Tokman (YTG)"

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

#property version   "1.00"

#property strict

#property indicator_chart_window

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   

//---

   return(INIT_SUCCEEDED);

  }

//----

void OnDeinit(const int reason) 

   {

    GetDellName();

    Comment("");

   }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---

   LineProfit(); 

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

//----+

 void LineProfit()

  {

 //---

 double sum = 0;

  for(int cnt=0;cnt<=OrdersTotal();cnt++){

  if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)){

  if (OrderSymbol()==Symbol()){

  if (OrderType()==OP_BUY || OrderType()==OP_SELL)sum ++;

 }}}  

  //---- 

  if(sum>0)

   {

    if(ObjectFind(0,"ytg_line_pf")<0)

    HLine(0,"ytg_line_pf",0,Bid,clrBlue,0,2,false,true);

    double pric_line = 0;

    if(ObjectFind(0,"ytg_line_pf")!=-1)

    pric_line = ObjectGetDouble(0,"ytg_line_pf",OBJPROP_PRICE);

    double profit_ln = ProfitByPrice(-1,-1,pric_line);



    TextCreate2(0,"ytg_p_l",0,Time[0]+Period()*60*5,pric_line,

    DoubleToStr(profit_ln,1)+" "+AccountCurrency(),"Arial",14,clrBlue,0.0,0);        

   }

    else 

     {

      if(ObjectFind(0,"ytg_line_pf")!=-1)ObjectDelete(0,"ytg_line_pf");

      if(ObjectFind(0,"ytg_p_l")!=-1)ObjectDelete(0,"ytg_p_l");

     }

  //----

}

//----

void GetDellName(string name_n="ytg_")

  {

   string vName;

   for(int i=ObjectsTotal()-1; i>=0;i--)

     {

      vName=ObjectName(i);

      if(StringFind(vName,name_n)!=-1) ObjectDelete(vName);

     }

  }

//----+

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

//| !>7405B >1J5:B ""5:AB"                                           | 

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

bool TextCreate2(const long              chart_ID=0,               // ID 3@0D8:0 

                const string            name="Text",              // 8<O >1J5:B0 

                const int               sub_window=0,             // =><5@ ?>4>:=0 

                datetime                time=0,                   // 2@5<O B>G:8 ?@82O7:8 

                double                  price=0,                  // F5=0 B>G:8 ?@82O7:8 

                const string            text="Text",              // A0< B5:AB 

                const string            font="Arial Black",             // H@8DB 

                const int               font_size=10,             // @07<5@ H@8DB0 

                const color             clr=clrDodgerBlue,               // F25B 

                const double            angle=0.0,                // =0:;>= B5:AB0 

                const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT, // A?>A>1 ?@82O7:8 

                const bool              back=false,               // =0 704=5< ?;0=5 

                const bool              selection=false,          // 2K45;8BL 4;O ?5@5<5I5=89 

                const bool              hidden=false,              // A:@KB 2 A?8A:5 >1J5:B>2 

                const long              z_order=0)                // ?@8>@8B5B =0 =060B85 <KHLN 

  { 

//--- CAB0=>28< :>>@48=0BK B>G:8 ?@82O7:8, 5A;8 >=8 =5 7040=K 

   if(ObjectFind(chart_ID,name)>=0)ObjectDelete(chart_ID,name);

//--- A1@>A8< 7=0G5=85 >H81:8 

   ResetLastError(); 

//--- A>74048< >1J5:B ""5:AB" 

   if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price)) 

     { 

      Print(__FUNCTION__, 

            ": =5 C40;>AL A>740BL >1J5:B \""5:AB\"! >4 >H81:8 = ",GetLastError()); 

      return(false); 

     } 

//--- CAB0=>28< B5:AB 

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text); 

//--- CAB0=>28< H@8DB B5:AB0 

   ObjectSetString(chart_ID,name,OBJPROP_FONT,font); 

//--- CAB0=>28< @07<5@ H@8DB0 

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size); 

//--- CAB0=>28< C3>; =0:;>=0 B5:AB0 

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle); 

//--- CAB0=>28< A?>A>1 ?@82O7:8 

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor); 

//--- CAB0=>28< F25B 

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); 

//--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5 

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); 

//--- 2:;NG8< (true) 8;8 >B:;NG8< (false) @568< ?5@5<5I5=8O >1J5:B0 <KHLN 

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); 

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); 

//--- A:@>5< (true) 8;8 >B>1@078< (false) 8<O 3@0D8G5A:>3> >1J5:B0 2 A?8A:5 >1J5:B>2 

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); 

//--- CAB0=>28< ?@8>@8B5B =0 ?>;CG5=85 A>1KB8O =060B8O <KH8 =0 3@0D8:5 

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); 

//--- CA?5H=>5 2K?>;=5=85 

   return(true); 

  } 

//----

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

//| !>7405B 3>@87>=B0;L=CN ;8=8N                                     | 

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

bool HLine(const long            chart_ID=0,        // ID 3@0D8:0 

                 const string          name="HLine",      // 8<O ;8=88 

                 const int             sub_window=0,      // =><5@ ?>4>:=0 

                 double                price=0,           // F5=0 ;8=88 

                 const color           clr=clrRed,        // F25B ;8=88 

                 const ENUM_LINE_STYLE style=STYLE_SOLID, // AB8;L ;8=88 

                 const int             width=1,           // B>;I8=0 ;8=88 

                 const bool            back=false,        // =0 704=5< ?;0=5 

                 const bool            selection=false,    // 2K45;8BL 4;O ?5@5<5I5=89 

                 const bool            hidden=false,       // A:@KB 2 A?8A:5 >1J5:B>2 

                 const long            z_order=0)         // ?@8>@8B5B =0 =060B85 <KHLN 

  { 

//--- 5A;8 F5=0 =5 7040=0, B> CAB0=>28< 55 =0 C@>2=5 B5:CI59 F5=K Bid 

   //if(ObjectFind(chart_ID,name)!=-1)ObjectDelete(chart_ID,name); 

//--- A1@>A8< 7=0G5=85 >H81:8 

   ResetLastError(); 

//--- A>74048< 3>@87>=B0;L=CN ;8=8N 

   if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price)) 

     { 

      Print(__FUNCTION__, 

            ": =5 C40;>AL A>740BL 3>@87>=B0;L=CN ;8=8N! >4 >H81:8 = ",GetLastError()); 

      return(false); 

     } 

//--- CAB0=>28< F25B ;8=88 

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); 

//--- CAB0=>28< AB8;L >B>1@065=8O ;8=88 

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style); 

//--- CAB0=>28< B>;I8=C ;8=88 

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width); 

//--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5 

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); 

//--- 2:;NG8< (true) 8;8 >B:;NG8< (false) @568< ?5@5<5I5=8O ;8=88 <KHLN 

//--- ?@8 A>740=88 3@0D8G5A:>3> >1J5:B0 DC=:F859 ObjectCreate, ?> C<>;G0=8N >1J5:B 

//--- =5;L7O 2K45;8BL 8 ?5@5<5I0BL. =CB@8 65 MB>3> <5B>40 ?0@0<5B@ selection 

//--- ?> C<>;G0=8N @025= true, GB> ?>72>;O5B 2K45;OBL 8 ?5@5<5I0BL MB>B >1J5:B 

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); 

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); 

//--- A:@>5< (true) 8;8 >B>1@078< (false) 8<O 3@0D8G5A:>3> >1J5:B0 2 A?8A:5 >1J5:B>2 

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); 

//--- CAB0=>28< ?@8>@8B5B =0 ?>;CG5=85 A>1KB8O =060B8O <KH8 =0 3@0D8:5 

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); 

//--- CA?5H=>5 2K?>;=5=85 

   return(true); 

  } 

//----

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

//|  ?8A0=85 : >72@0I05B ?@>D8B 2 20;NB5 45?>78B0, 5A;8 B5:CI85 ?>78F88      |

//|             1C4CB 70:@KBK ?> F5=5, ?5@540==>9 2 :0G5AB25 ?0@0<5B@0.        |

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

//|  0@0<5B@K:                                                                |

//|    op - >?5@0F8O                             (-1 - ;N10O ?>78F8O)          |

//|    mn - MagicNumber                          (-1 - ;N1>9 <038:)            |

//|    cp - F5=0 ?@54?>;0305<>3> 70:@KB8O (Bid)  ( 0 - B5:CI0O F5=0)           |

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

double ProfitByPrice(int op=-1, int mn=-1, double cp=0) {

  double pr=0;

  double po=MarketInfo(Symbol(), MODE_POINT);

  double sp=MarketInfo(Symbol(), MODE_SPREAD);

  double tv=MarketInfo(Symbol(), MODE_TICKVALUE);

  int    i, k=OrdersTotal();



  RefreshRates();

  if (cp<=0) cp=Bid;

  for (i=0; i<k; i++) {

    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

      if (OrderSymbol()==Symbol() && (op<0 || OrderType()==op)) {

        if (mn<0 || OrderMagicNumber()==mn) {

          if (OrderType()==OP_BUY) {

            pr+=(cp-OrderOpenPrice())/po*OrderLots()*tv;

            pr+=OrderCommission()+OrderSwap();

          }

          if (OrderType()==OP_SELL) {

            pr+=(OrderOpenPrice()-cp-Ask+Bid)/po*OrderLots()*tv;

            pr+=OrderCommission()+OrderSwap();

          }

        }

      }

    }

  }

  return(pr);

}

//----

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 ---