Currency2
Download scriptThis script is a specialized trading dashboard tool designed to help traders calculate and visualize their risk exposure in real-time. Its primary purpose is to answer the question: "Based on my current account balance and chosen trade volume, how many simultaneous positions can I safely open without exceeding a specific margin limit?"
Core Functionality
The logic operates by analyzing your account's "Free Margin" (the capital available for trading) against the specific requirements of the currency pair you are currently viewing. It takes several user-defined parameters into account:
- Risk Percentage: You set a target percentage (e.g., 80% of your available margin) to define your maximum safety threshold.
- Trade Volume: You specify the number of "Lots" you intend to trade per position.
- Stop Loss Factor: The script calculates potential losses based on a multiplier provided by the user, allowing it to simulate "worst-case" scenarios for multiple open trades.
How the Logic Works
The script performs a three-step calculation automatically:
- Initial Capacity Check: It first determines how many lots you can afford to trade immediately using only your current free margin, based on your risk percentage.
- Sequential Simulation (The "Loop"): This is the most advanced part of the logic. The script doesn't just look at your balance once; it simulates opening trades one by one. It subtracts the required margin and the potential loss from your remaining balance for each subsequent trade. It continues to "add" trades in this simulation until the calculated risk exceeds your defined percentage or the balance runs out.
- Dynamic Visual Feedback: The script then displays this information directly on your chart as a text dashboard.
Visual Indicators
The tool provides an interactive visual summary on your screen:
- Current Capacity: It shows exactly how many lots you can currently afford at your chosen risk level.
- Trade Count: It displays the maximum number of simultaneous trades you can hold.
- Color-Coded Alerts: The script uses a dynamic coloring system to provide instant warnings. Depending on how close you are to your limits, the text color changes (e.g., turning Red, Lime, or Yellow). This allows a trader to see at a glance if their current trading plan is becoming too risky as they open more positions.
In summary, this script acts as a Risk Management Calculator. It automates the complex math of margin requirements and "drawdown" simulations so that a trader can make informed decisions about how many positions to open without over-leveraging their account.
//+------------------------------------------------------------------+
//| Currency2.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, s05"
#property link "selivanov.05@mail.ru"
#property indicator_chart_window
extern int Çàëîã_îò_áàëàíñà = 80;
extern int Îáüåì = 1;
extern int Ñòîï_ëîññ = 15;
extern int Óãîë_ïðèâÿçêè = 1;
extern int Âûñîòà_ïðèâÿçêè = 15;
extern int Øèðèíà_ïðèâÿçêè = 5;
color color_0 = White;
extern color color_1 = Red;
extern color color_2 = Lime;
extern color color_3 = Yellow;
extern int color_U1 = 3;
extern int color_U2 = 10;
extern int color_U3 = 999;
int Ðàçìåð_øðèôòà = 7;
string Symbol30 = "";
string Symbol40 = "";
string Symbol50 = "";
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
ObjectCreate("Symbol30",OBJ_LABEL,0,0,0,0,0);
ObjectCreate("Symbol40",OBJ_LABEL,0,0,0,0,0);
ObjectCreate("Symbol50",OBJ_LABEL,0,0,0,0,0);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
ObjectDelete ("Symbol30");
ObjectDelete ("Symbol40");
ObjectDelete ("Symbol50");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
//---- Ñòðîêà 2
double sym_3 =(AccountFreeMargin()/(MarketInfo(Symbol(),MODE_MARGINREQUIRED)+MarketInfo(Symbol(),MODE_SPREAD)))*(Çàëîã_îò_áàëàíñà*0.01);
string Symbol_3 =DoubleToStr(sym_3, 2)+" ëîò ïðè "+Çàëîã_îò_áàëàíñà+"%";
ObjectSetText("Symbol30",Symbol_3,Ðàçìåð_øðèôòà,"Time New Roman",White);
ObjectSet("Symbol30",OBJPROP_XDISTANCE,Øèðèíà_ïðèâÿçêè);
ObjectSet("Symbol30",OBJPROP_YDISTANCE,Âûñîòà_ïðèâÿçêè+13);
ObjectSet("Symbol30",OBJPROP_CORNER,Óãîë_ïðèâÿçêè);
//---- Ñòðîêà 1
double sym_4 = 0;
double sym_5 = 0;
double sym_6 = AccountFreeMargin()+sym_5;
double sym_7 = sym_6/(MarketInfo(Symbol(),MODE_MARGINREQUIRED)+MarketInfo(Symbol(),MODE_SPREAD))*Çàëîã_îò_áàëàíñà*0.01;
if(sym_7>Îáüåì)sym_4 = 1;
double sym_8 = Îáüåì*Ñòîï_ëîññ*(-10);
double sym_9 = sym_6+sym_8;
double sym_10 = sym_9/(MarketInfo(Symbol(),MODE_MARGINREQUIRED)+MarketInfo(Symbol(),MODE_SPREAD))*Çàëîã_îò_áàëàíñà*0.01;
if(sym_10>Îáüåì)sym_4 = sym_4+1;
// öèêë
double u=0;
double sym_11 = sym_9+sym_8;
while(u<999) // äî êàêîãî êîëè÷åñòâà äîñòóïíûõ ñäåëîê ïðîâåðÿòü +2
{
double sym_12 = sym_11/(MarketInfo(Symbol(),MODE_MARGINREQUIRED)+MarketInfo(Symbol(),MODE_SPREAD))*Çàëîã_îò_áàëàíñà*0.01;
if(sym_12>Îáüåì)sym_4 = sym_4+1; else break;
sym_11 =sym_11+sym_8;
u=u+1;
}
//---- Öâåòîâàÿ îêðàñêà ÊÎËÈ×ÅÑÒÂÀ ñäåëîê
if(sym_4<color_U1)color_0=color_1; else color_0=White;
if(sym_4>color_U2)color_0=color_2;
if(sym_4>color_U3)color_0=color_3;
//----
string Symbol_4=DoubleToStr(sym_4, 0);
//----
ObjectSetText("Symbol50",Symbol_4,Ðàçìåð_øðèôòà,"Time New Roman",color_0);
ObjectSet("Symbol50",OBJPROP_XDISTANCE,Øèðèíà_ïðèâÿçêè+161);
ObjectSet("Symbol50",OBJPROP_YDISTANCE,Âûñîòà_ïðèâÿçêè);
ObjectSet("Symbol50",OBJPROP_CORNER,Óãîë_ïðèâÿçêè);
//----
ObjectSetText("Symbol40"," ñäåëîê ïðè "+Çàëîã_îò_áàëàíñà+"% çàëîãà îáüåìå "+Îáüåì+" ëîò",Ðàçìåð_øðèôòà,"Time New Roman",White);
ObjectSet("Symbol40",OBJPROP_XDISTANCE,Øèðèíà_ïðèâÿçêè);
ObjectSet("Symbol40",OBJPROP_YDISTANCE,Âûñîòà_ïðèâÿçêè);
ObjectSet("Symbol40",OBJPROP_CORNER,Óãîë_ïðèâÿçêè);
//----
return(0);
}
//+------------------------------------------------------------------+
Comments