riskmanager

Author: Anatoly Sergeev
Orders Execution
Checks for the total of open orders
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
riskmanager
ÿþ//+------------------------------------------------------------------+

//|                                                 Risk Manager.mq5 |

//|                        Copyright 2015, MetaQuotes Software Corp. |

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

//|                                            		  Anatoly Sergeev |

//|                                            		       12/06/2014 |

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

#include<Trade\Trade.mqh>

#property copyright "Anatoly Sergeev"

#property link      "Anatoly.Sergeev@ya.ru"

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

//|                                                                  |

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

enum OrderFilling

  {

   type_1,         // A5/8G53>

   type_2,         // A5/'0AB8G=>

   type_3,         // 5@=CBL

  };



input double risk= 5.00;                  // 8A: =0 45=L 2 %:

input double riskTrade = 0.00;            // 8A: =0 A45;:C 2 %:

input double riskTrail = 0.00;            //"@59;8=3-AB>? 4=52=>9 ?@81K;8 2 %:

input OrderFilling typeFilling = type_1;  // 568< 8A?>;=5=8O >@45@>2:



CTrade  tr;            //1J5:B :;0AA0 CTrade

MqlDateTime dt;        //Enum 4;O 2K1>@0 4=O 70?@>A0 10;0=A0

double balance;        //0;0=A =0 =0G0;> 4=O

double maxDayProfit;

string prefix;         //#=8:0;L=K9 ?@5D8:A AG5B0

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

//|$C=:F8O 8=8F80;870F88                                             |

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

void OnInit()

  {

   if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))

     {

      Alert("@>25@LB5 2 =0AB@>9:0E B5@<8=0;0 @07@5H5=85 =0 02B><0B8G5A:CN B>@3>2;N!");   //?@>25@:0 @07@5H5=89 =0 B>@3>2;N

        }else{

      if(!MQLInfoInteger(MQL_TRADE_ALLOWED))

        {

         Alert("2B><0B8G5A:0O B>@3>2;O 70?@5I5=0 2 A2>9AB20E ?@>3@0<<K 4;O ",__FILE__);   //?@>25@:0 @07@5H5=89 =0 B>@3>2;N

        }

     }



   prefix=IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN))+"_";



   tr.SetDeviationInPoints(10000);            //4>?CAB8<>5 ?@>A:0;L7K20=85 2 ?C=:B0E ?@8 A>25@H5=88 ?>:C?:8/?@>4068



   switch(typeFilling)

     {                         //>;8B8:0 8A?>;=5=8O >@45@0

      case type_1:

         tr.SetTypeFilling(ORDER_FILLING_FOK);

         break;

      case type_2:

         tr.SetTypeFilling(ORDER_FILLING_IOC);

         break;

      case type_3:

         tr.SetTypeFilling(ORDER_FILLING_RETURN);

         break;

     }



   checkBalance();    //?5@28G=>5 70?>;=5=85 3;>10;L=KE ?5@5<5==KE B5@<8=0;0

   dayRisk();         //5@28G=K9 @0AG5B

  }

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

//|1@01>B:0 B8:>2                                                   |

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

void OnTick()

  {

   checkBalance();

   if(dayRisk())

     {

      if(riskTrade > 0.0)tradeRisk();

      if(riskTrail > 0.0)trailingStop();

     }

  }

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

//|654=52=>5 >1=>2;5=85 10;0=A0                                     |

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

void checkBalance()

  {

   TimeCurrent(dt);



   if(GlobalVariableCheck(prefix+"Balance") && GlobalVariableCheck(prefix+"Day") && GlobalVariableCheck(prefix+"Year")) //5A;8 ;8 ?5@5<5==K5?

     {

      if(dt.day_of_year>GlobalVariableGet(prefix+"Day")) //A;8 A;54CNI89 45=L, B> >1=>28BL 40==K5 10;0=A0 8 40BK

        {

         GlobalVariableSet(prefix+"Day",dt.day_of_year);

         GlobalVariableSet(prefix+"Year",dt.year);

         GlobalVariableSet(prefix+"Balance",AccountInfoDouble(ACCOUNT_BALANCE));

           }else{

         if(dt.year>GlobalVariableGet(prefix+"Year")) //@>25@:0 =0 =0ABC?;5=85 =>2>3> 3>40

           {

            GlobalVariableSet(prefix+"Day",dt.day_of_year);

            GlobalVariableSet(prefix+"Year",dt.year);

            GlobalVariableSet(prefix+"Balance",AccountInfoDouble(ACCOUNT_BALANCE));

           }

        }

        }else{                                                                             //5B. !>740BL.

      GlobalVariableSet(prefix+"Balance",AccountInfoDouble(ACCOUNT_BALANCE));

      GlobalVariableSet(prefix+"Day",dt.day_of_year);

      GlobalVariableSet(prefix+"Year",dt.year);

     }

  }

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

//| 0AG5B 4=52=>3> @8A:0                                             |

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

bool dayRisk()

  {

   balance=GlobalVariableGet(prefix+"Balance");      //AG8BK20=85 10;0=A0

   double currentRiskPercent=(balance -(AccountInfoDouble(ACCOUNT_BALANCE)+AccountInfoDouble(ACCOUNT_PROFIT)))/(balance/100);



   if(currentRiskPercent>0) //C1KB>:

     {

      if(currentRiskPercent>=risk) //5A;8 1>;LH5 <0:A. @8A:0 =0 45=L

        {

         closedAll();                  //70:@KBL 2A5 A45;:8

         Comment("\n>?CAB8<K9 4=52=>9 @8A:: ",DoubleToString(risk,2),"%\n",

                 isOnTradeRisk(),

                 isOnTrail(),

                 "0;0=A =0 =0G0;> 4=O: ",DoubleToString(balance,2)," ",AccountInfoString(ACCOUNT_CURRENCY),"\n",

                 "1I89 @57C;LB0B: -",DoubleToString(currentRiskPercent,2),"%\n",

                 "">@3>2;O 70?@5I5=0! @52KH5= <0:A8<0;L=K9 4=52=>9 @8A:!");

         return(false);                  //70?@5B B>@3>2;8

           }else{                        //5A;8 <8=CA 2 @0<:0E 4=52=>3> @8A:0

         Comment("\n>?CAB8<K9 4=52=>9 @8A:: ",DoubleToString(risk,2),"%\n",

                 isOnTradeRisk(),

                 isOnTrail(),

                 "0;0=A =0 =0G0;> 4=O: ",DoubleToString(balance,2)," ",AccountInfoString(ACCOUNT_CURRENCY),"\n",

                 "1I89 @57C;LB0B: -",DoubleToString(currentRiskPercent,2));

         return(true);

        }

        }else{                              //?@>D8B

      currentRiskPercent=MathAbs(currentRiskPercent);

      Comment("\n>?CAB8<K9 4=52=>9 @8A:: ",DoubleToString(risk,2),"%\n",

              isOnTradeRisk(),

              isOnTrail(),

              "0;0=A =0 =0G0;> 4=O: ",DoubleToString(balance,2)," ",AccountInfoString(ACCOUNT_CURRENCY),"\n",

              "1I89 @57C;LB0B: +",DoubleToString(currentRiskPercent,2));

      return(true);

     }

  }

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

//| 0AG5B @8A:0 =0 B@594                                             |

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

void tradeRisk()

  {

   double tradeRiskPercent;

   string symbl;

   balance=GlobalVariableGet(prefix+"Balance");            //AG8BK20=85 10;0=A0



   if(PositionsTotal()>0) //ABL ;8 >B:@KBK5 ?>78F88?

     {

      for(int loop=0; PositionsTotal()>=loop; loop++)

        {

         symbl= PositionGetSymbol(loop);                    //>;CG5=85 A8<2>;0 ?>78F88

         if(symbl != "")                                    //A;8 CA?5H=>

           {

            if(PositionGetDouble(POSITION_PROFIT)<0)

              {      //@>25@:0 =0 C1KB>:

               tradeRiskPercent=MathAbs(PositionGetDouble(POSITION_PROFIT)/(balance/100));      // 0AG5B %

               if(tradeRiskPercent>=riskTrade) //A;8 % C1KB:0 1>;LH5 8;8 @025= <0:A8<0;L=><C =0 A45;:C

                 {

                  if(tr.PositionClose(symbl))Print(">78F8O ?> ",symbl,"  +". @52KH5= @0A: =0 B@594!");      //0:@K205< ?>78F8N

                 }

              }

           }

        }

     }

  }

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

//|$C=:F8O 70:@KB8O 2A5E A45;>: 8 >@45@>2                            |

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

bool closedAll()

  {

   string symbl;

   ulong ticket;



   if(PositionsTotal()>0)

     {

      for(int loop=0; PositionsTotal()>=loop; loop++)

        {

         symbl=PositionGetSymbol(loop);

         if(symbl!="")

           {

            if(tr.PositionClose(symbl))Print(">78F8O ?> ",symbl,"  +".");

           }

        }

     }



   if(OrdersTotal()>0)

     {

      for(int loop=0; OrdersTotal()>=loop; loop++)

        {

         ticket=OrderGetTicket(loop);

         if(ticket!=0)

           {

            if(tr.OrderDelete(ticket))Print("B;>65==K9 >@45@ #",ticket," #.");

           }

        }

     }



   if(PositionsTotal()==0 && OrdersTotal()==0)

     {

      return(true);

        }else{

      return(false);

     }

  }

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

//|B>1@065=85 @568<0 @01>BK tradeRisk()                             |

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

string isOnTradeRisk()

  {

   if(riskTrade>0.0)

     {

      return(">?CAB8<K9 @8A: =0 A45;:C: " + DoubleToString(riskTrade, 2) + "%\n");

     }

   return("");

  }

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

//|B>1@065=85 @568<0 @01>BK B@59;8=3-AB>?0                          |

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

string isOnTrail()

  {

   if(riskTrail>0.0)

     {

      return(""@59;8=3-AB>? 4=52=>9 ?@81K;8: " + DoubleToString(riskTrail, 2) + "%\n");

     }

   return("");

  }

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

//|"@59;8=3-AB>? 4=52=>9 ?@81K;8                                     |

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

void trailingStop()

  {

   bool flag=false;

   double currentRiskPercent=((AccountInfoDouble(ACCOUNT_BALANCE)+AccountInfoDouble(ACCOUNT_PROFIT))-balance)/(balance/100);



   if(currentRiskPercent>=riskTrail && currentRiskPercent>=maxDayProfit)

     {

      maxDayProfit=currentRiskPercent;

      flag=true;

     }

   if(currentRiskPercent<=maxDayProfit-riskTrail && flag)

     {

      if(closedAll())

        {

         Alert("!@01>B0; B@59;8=3-AB>? ?> 4=52=>9 ?@81K;8!");

         flag=false;

         maxDayProfit=0;

        }

     }

  }

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

//|END                                                               |

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

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