RiskManager_with_InfoPanel_and_Support

Author:
0 Views
0 Downloads
0 Favorites
RiskManager_with_InfoPanel_and_Support
ÿþ//+------------------------------------------------------------------+

//|    RiskManager_with_InfoPanel_and_Support_noDLL.mq5              |

//| 1J548=Q==K9 M:A?5@B: 8=D>@<0F8>==0O ?0=5;L A D>=>2K< ?@O<>C3>;L=8:><|

//| 157 8A?>;L7>20=8O DLL                                             |

//|                                                                  |

//| 2B>@: YourName                                                  |

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

#property copyright ""

#property link      ""

#property version   "1.02"

#property strict



#include <Trade\Trade.mqh>



// #40;Q= 8<?>@B DLL 8 DC=:F8O OpenURL



//======================================================================

// %+  " +  !-"

//======================================================================

input double RiskPercent         = 1.0;      

input double EntryPrice          = 1.1000;   

input double StopLossPercent     = 0.2;      

input double TakeProfitPercent   = 0.5;      

input double MaxDailyRiskPercent = 2.0;      



//======================================================================

//  " + $ / $

//======================================================================

input int    UpdateIntervalSeconds = 10;         

input int    InfoPanelXDistance    = 10;          

input int    InfoPanelYDistance    = 10;          

input int    InfoPanelWidth        = 350;         

input int    InfoPanelHeight       = 300;         

input int    InfoPanelFontSize     = 12;          

input string InfoPanelFontName     = "Arial";     

input color  InfoPanelFontColor    = clrWhite;    

input color  InfoPanelBackColor    = clrDarkGray; 



//======================================================================

//  " +     "

// A;8 E>B8B5 >AB028BL :=>?:C ?>445@6:8, >AB02LB5 55, 

// => C40;8B5 87 OnChartEvent 2K7>2 DC=:F88 >B:@KB8O URL

//======================================================================

input bool   UseSupportPanel       = true;       

input string SupportPanelText      = ">445@68 ?@>5:B, 70@538AB@8@C9AO!";

input color  SupportPanelFontColor = clrRed;   

input int    SupportPanelFontSize  = 10;          

input string SupportPanelFontName  = "Arial";     

input int    SupportPanelXDistance = 10;          

input int    SupportPanelYDistance = 320;         

input int    SupportPanelXSize     = 250;         

input int    SupportPanelYSize     = 30;          



//======================================================================

//  *"   $

//======================================================================

string RiskPanelBgName   = "RiskManagerInfoBg";

string RiskPanelName     = "RiskManagerInfo";

string SupportButtonName = "SupportButton";



//======================================================================

// +  +

//======================================================================

datetime lastUpdate = 0;



//======================================================================

// $C=:F88 CalculateLotSize, GetDailyProfit, BuildRiskPanelText

// >AB02;O5< 157 87<5=5=89 (:0: 2 ?@54K4CI8E ?@8<5@0E)

//======================================================================

double CalculateLotSize(double entry, double computedSL)

{

   double equity    = AccountInfoDouble(ACCOUNT_EQUITY);

   double riskMoney = equity * (RiskPercent / 100.0);

   double tick_size = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);

   double riskPips  = MathAbs(entry - computedSL) / tick_size;

   double tickValue      = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);

   double pipValuePerLot = tickValue / tick_size;

   double riskPerLot = riskPips * pipValuePerLot;

   if(riskPerLot <= 0) return 0;

   double lots = riskMoney / riskPerLot;

   return(lots);

}



double GetDailyProfit()

{

   double profit = 0.0;

   int totalDeals = HistoryDealsTotal();

   MqlDateTime dealTime, currentTime;

   TimeToStruct(TimeCurrent(), currentTime);

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

   {

      ulong ticket = HistoryDealGetTicket(i);

      datetime dealTimeRaw = (datetime)HistoryDealGetInteger(ticket, DEAL_TIME);

      TimeToStruct(dealTimeRaw, dealTime);

      if(dealTime.year == currentTime.year &&

         dealTime.mon  == currentTime.mon  &&

         dealTime.day  == currentTime.day)

      {

         profit += HistoryDealGetDouble(ticket, DEAL_PROFIT);

      }

   }

   return(profit);

}



string BuildRiskPanelText()

{

   string txt;

   double balance = AccountInfoDouble(ACCOUNT_BALANCE);

   double equity  = AccountInfoDouble(ACCOUNT_EQUITY);

   double profit  = AccountInfoDouble(ACCOUNT_PROFIT);

   string login   = IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN));

   datetime curTime = TimeCurrent();

   string timeStr   = TimeToString(curTime, TIME_MINUTES);

   txt  = "Risk Manager for " + _Symbol + "\r\n";

   txt += "-----------------------------\r\n";

   txt += "!G5B: "      + login          + "\r\n";

   txt += "0;0=A: $"   + DoubleToString(balance,2) + "\r\n";

   txt += "-:28B8: $"   + DoubleToString(equity,2)  + "\r\n";

   txt += "@81K;L: $"  + DoubleToString(profit,2)  + "\r\n";

   txt += "@5<O: "     + timeStr        + "\r\n\r\n";

   txt += "Risk/Trade: " + DoubleToString(RiskPercent,2) + "%\r\n";

   txt += "Entry Price: " + DoubleToString(EntryPrice, _Digits) + "\r\n";

   double computedSL = EntryPrice - (EntryPrice * (StopLossPercent / 100.0));

   double computedTP = EntryPrice + (EntryPrice * (TakeProfitPercent / 100.0));

   txt += "Stop Loss: "  + DoubleToString(computedSL, _Digits) + " (" + DoubleToString(StopLossPercent,2) + "%)\r\n";

   txt += "Take Profit: "+ DoubleToString(computedTP, _Digits) + " (" + DoubleToString(TakeProfitPercent,2) + "%)\r\n\r\n";

   double tick_size = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);

   double riskPips  = MathAbs(EntryPrice - computedSL) / tick_size;

   txt += "Distance (pips): " + DoubleToString(riskPips,1) + "\r\n";

   double riskMoney = equity * (RiskPercent / 100.0);

   txt += "Risk ($): " + DoubleToString(riskMoney,2) + "\r\n";

   double lots = CalculateLotSize(EntryPrice, computedSL);

   txt += "Recommended Lot Size: " + DoubleToString(lots,2) + "\r\n";

   if(TakeProfitPercent > 0)

   {

      double rewardPips = MathAbs(computedTP - EntryPrice) / tick_size;

      string rr = (riskPips > 0) ? DoubleToString(rewardPips / riskPips,2) : "N/A";

      txt += "Reward:Risk Ratio: " + rr + "\r\n\r\n";

   }

   double dailyProfit    = GetDailyProfit();

   double dailyRiskLimit = equity * (MaxDailyRiskPercent / 100.0);

   txt += "Daily P/L: $" + DoubleToString(dailyProfit,2) + "\r\n";

   txt += "Daily Risk Limit: $" + DoubleToString(dailyRiskLimit,2) + "\r\n";

   if(dailyProfit < -dailyRiskLimit)

      txt += "\r\n*** DAILY RISK LIMIT EXCEEDED! Trading suspended.";

   return(txt);

}



//======================================================================

// $#&/: CreateRiskPanelBackground

// !>740QB D>=>2K9 >1J5:B 2 2845 ?@O<>C3>;L=8:0 4;O 8=D>@<0F8>==>9 ?0=5;8

//======================================================================

void CreateRiskPanelBackground()

{

   if(ObjectFind(0, RiskPanelBgName) < 0)

   {

      if(!ObjectCreate(0, RiskPanelBgName, OBJ_RECTANGLE, 0, 0, 0))

      {

         Print("H81:0 A>740=8O D>=0 8=D>@<0F8>==>9 ?0=5;8!");

         return;

      }

      ObjectSetInteger(0, RiskPanelBgName, OBJPROP_CORNER, CORNER_LEFT_UPPER);

      ObjectSetInteger(0, RiskPanelBgName, OBJPROP_XDISTANCE, InfoPanelXDistance - 5);

      ObjectSetInteger(0, RiskPanelBgName, OBJPROP_YDISTANCE, InfoPanelYDistance - 5);

      ObjectSetInteger(0, RiskPanelBgName, OBJPROP_XSIZE, InfoPanelWidth + 10);

      ObjectSetInteger(0, RiskPanelBgName, OBJPROP_YSIZE, InfoPanelHeight + 10);

      ObjectSetInteger(0, RiskPanelBgName, OBJPROP_STYLE, STYLE_SOLID);

      ObjectSetInteger(0, RiskPanelBgName, OBJPROP_WIDTH, 1);

      ObjectSetInteger(0, RiskPanelBgName, OBJPROP_COLOR, InfoPanelBackColor);

      ObjectSetInteger(0, RiskPanelBgName, OBJPROP_SELECTABLE, false);

   }

}



//======================================================================

// $#&/: UpdateRiskPanel

// 1=>2;O5B D>= 8 B5:AB 8=D>@<0F8>==>9 ?0=5;8

//======================================================================

void UpdateRiskPanel()

{

   string txt = BuildRiskPanelText();

   CreateRiskPanelBackground();

   if(ObjectFind(0, RiskPanelName) < 0)

   {

      if(!ObjectCreate(0, RiskPanelName, OBJ_TEXT, 0, 0, 0))

      {

         Print("H81:0 A>740=8O B5:AB>2>9 ?0=5;8!");

         return;

      }

      ObjectSetInteger(0, RiskPanelName, OBJPROP_CORNER, CORNER_LEFT_UPPER);

      ObjectSetInteger(0, RiskPanelName, OBJPROP_XDISTANCE, InfoPanelXDistance);

      ObjectSetInteger(0, RiskPanelName, OBJPROP_YDISTANCE, InfoPanelYDistance);

      ObjectSetInteger(0, RiskPanelName, OBJPROP_FONTSIZE, InfoPanelFontSize);

      ObjectSetString (0, RiskPanelName, OBJPROP_FONT, InfoPanelFontName);

      ObjectSetInteger(0, RiskPanelName, OBJPROP_COLOR, InfoPanelFontColor);

      ObjectSetInteger(0, RiskPanelName, OBJPROP_SELECTABLE, false);

   }

   ObjectSetString(0, RiskPanelName, OBJPROP_TEXT, txt);

   ChartRedraw();

}



//======================================================================

// $#&/: UpdateSupportButton

// 1=>2;O5B (8;8 A>740QB) :=>?:C ?>445@6:8 ?@>5:B0

//======================================================================

void UpdateSupportButton()

{

   if(UseSupportPanel)

   {

      if(ObjectFind(0, SupportButtonName) < 0)

      {

         if(!ObjectCreate(0, SupportButtonName, OBJ_BUTTON, 0, 0, 0))

         {

            Print("H81:0 A>740=8O :=>?:8 ?>445@6:8!");

            return;

         }

         ObjectSetInteger(0, SupportButtonName, OBJPROP_CORNER, CORNER_LEFT_UPPER);

         ObjectSetInteger(0, SupportButtonName, OBJPROP_XDISTANCE, SupportPanelXDistance);

         ObjectSetInteger(0, SupportButtonName, OBJPROP_YDISTANCE, SupportPanelYDistance);

         ObjectSetInteger(0, SupportButtonName, OBJPROP_FONTSIZE, SupportPanelFontSize);

         ObjectSetString (0, SupportButtonName, OBJPROP_FONT, SupportPanelFontName);

         ObjectSetString (0, SupportButtonName, OBJPROP_TEXT, SupportPanelText);

         ObjectSetInteger(0, SupportButtonName, OBJPROP_COLOR, SupportPanelFontColor);

         ObjectSetInteger(0, SupportButtonName, OBJPROP_BORDER_TYPE, BORDER_RAISED);

         ObjectSetInteger(0, SupportButtonName, OBJPROP_XSIZE, SupportPanelXSize);

         ObjectSetInteger(0, SupportButtonName, OBJPROP_YSIZE, SupportPanelYSize);

      }

   }

   else

   {

      if(ObjectFind(0, SupportButtonName) >= 0)

         ObjectDelete(0, SupportButtonName);

   }

}



//======================================================================

// 1@01>BG8: A>1KB89 3@0D8:0 (OnChartEvent)

// A;8 =C6=0 4>?>;=8B5;L=0O ;>38:0 4;O :=>?:8 ?>445@6:8, 4>102LB5 5Q

//======================================================================

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

{

   if(id == CHARTEVENT_OBJECT_CLICK && sparam == SupportButtonName)

   {

      // 57 DLL->B:@KB8O URL   <>6=> 2K25AB8 A>>1I5=85 8;8 @50;87>20BL 8=K< A?>A>1><

      Print("060B0 :=>?:0 ?>445@6:8. B:@>9B5 AAK;:C 2@CG=CN: https://partner.bybit.com/b/forexmt5");

   }

}



//======================================================================

// $C=:F8O OnTimer   >1=>2;O5B ?0=5;L 8 :=>?:C ?>445@6:8

//======================================================================

void OnTimer()

{

   UpdateRiskPanel();

   UpdateSupportButton();

   lastUpdate = TimeCurrent();

}



//======================================================================

// $C=:F8O OnInit   8=8F80;870F8O M:A?5@B0, A>740=85 >1J5:B>2 8 70?CA: B09<5@0

//======================================================================

int OnInit()

{

   Print("RiskManager_with_InfoPanel_and_Support_noDLL: =8F80;870F8O");

   lastUpdate = TimeCurrent();

   UpdateRiskPanel();

   UpdateSupportButton();

   EventSetTimer(UpdateIntervalSeconds);

   return(INIT_SUCCEEDED);

}



//======================================================================

// $C=:F8O OnDeinit   458=8F80;870F8O M:A?5@B0, C40;5=85 >1J5:B>2 8 >AB0=>2:0 B09<5@0

//======================================================================

void OnDeinit(const int reason)

{

   ObjectDelete(0, RiskPanelBgName);

   ObjectDelete(0, RiskPanelName);

   ObjectDelete(0, SupportButtonName);

   EventKillTimer();

   Print("RiskManager_with_InfoPanel_and_Support_noDLL: 58=8F80;870F8O");

}



//======================================================================

// $C=:F8O OnTick   4>?>;=8B5;L=0O ;>38:0 (?@8 =5>1E>48<>AB8)

//======================================================================

void OnTick()

{

   // 1=>2;5=85 ?@>872>48BAO G5@57 OnTimer()

}

Comments