PS_Calculator
Download scriptThe provided script is a specialized trading tool designed for the MetaTrader platform. Its primary purpose is to act as a dynamic Position Size Calculator, helping traders determine exactly how much they should invest in a trade based on their specific risk preferences and intended "Stop Loss" distances.
Here is a detailed breakdown of how the logic functions:
Core Functionality
The script automates the complex math involved in risk management. Instead of a trader having to manually calculate lot sizes for different scenarios, this tool provides a real-time visual table. It allows you to see multiple trade configurations simultaneously, showing you exactly what your position size and potential risk would look like if you chose different safety margins (Stop Losses).
Key Components of the Logic
-
Risk Management Foundation: The script starts by looking at your account's total equity and a user-defined "Percent Risk" (e.g., risking 2% of your account on a single trade). It then uses this to calculate the maximum amount of money you are willing to lose.
-
Multi-Scenario Analysis: The tool calculates four different trading scenarios at once based on four distinct Stop Loss distances provided by the user. For each scenario, it performs the following calculations:
- Lot Size Calculation: It determines the exact number of units (lots) to trade so that if the price hits your stop loss, your loss remains exactly within your desired percentage of the account equity.
- Pip Value: It translates those lot sizes into a "Pip Value," which tells you the monetary value of each price movement for that specific trade size.
- Real Risk Verification: It calculates the actual risk in currency to provide a final confirmation of what the loss would look like in real terms.
-
Live Market Integration: The script is "aware" of the market environment. It automatically detects:
- The specific asset you are trading (e.g., EURUSD, Gold, etc.).
- The current Spread (the cost of entering the trade), ensuring your calculations are grounded in current market conditions.
- The Tick Value, which ensures the math remains accurate regardless of which financial instrument is being traded.
Visual Presentation
Rather than just giving you a raw number, the script generates an organized visual dashboard on your chart. It displays a clean layout featuring:
- Stop Loss Targets: The distances you have defined.
- Calculated Lot Sizes: Highlighted in blue for quick identification.
- Pip Values & Real Risk: Clear figures showing the impact of each trade choice.
- Market Info: Labels for the current trading symbol and the active spread.
Summary for the User
In simple terms, this script removes the guesswork from trade execution. You tell the tool how much of your account you are willing to risk; the tool then provides a "menu" of four different trade sizes based on different safety distances. This allows you to make informed, disciplined trading decisions instantly, ensuring that you never over-leverage your account or exceed your personal risk limits.
//+------------------------------------------------------------------+
//| PS_calculator.mq3 |
//| Copyright © 2007, itLogics itlogics@itlogics.eu |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, itLogics"
#property link "http://www.powertradingfx.com"
#property indicator_separate_window
extern double percent_risk = 2;
extern int stoploss_1 = 10;
extern int stoploss_2 = 20;
extern int stoploss_3 = 30;
extern int stoploss_4 = 40;
extern color description_color = Black;
extern bool show_spread = true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int spread=MarketInfo(Symbol(),MODE_SPREAD);
double equity=AccountEquity( );
double risk;
double lotsize1;
double lotsize2;
double lotsize3;
double lotsize4;
double tickvalue=MarketInfo(Symbol(),MODE_TICKVALUE);
double pipvalue1;
double pipvalue2;
double pipvalue3;
double pipvalue4;
double risk1;
double risk2;
double risk3;
double risk4;
risk=percent_risk/100;
lotsize1=NormalizeDouble(equity*risk/stoploss_1/tickvalue,2);
lotsize2=NormalizeDouble(equity*risk/stoploss_2/tickvalue,2);
lotsize3=NormalizeDouble(equity*risk/stoploss_3/tickvalue,2);
lotsize4=NormalizeDouble(equity*risk/stoploss_4/tickvalue,2);
pipvalue1=(lotsize1*tickvalue);
pipvalue2=(lotsize2*tickvalue);
pipvalue3=(lotsize3*tickvalue);
pipvalue4=(lotsize4*tickvalue);
risk1=pipvalue1*stoploss_1/equity*100;
risk2=pipvalue2*stoploss_2/equity*100;
risk3=pipvalue3*stoploss_3/equity*100;
risk4=pipvalue4*stoploss_4/equity*100;
ObjectCreate("SLdesc", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("SLdesc","Stop Loss",10, "Arial Bold", description_color);
ObjectSet("SLdesc", OBJPROP_CORNER, 0);
ObjectSet("SLdesc", OBJPROP_XDISTANCE, 200);
ObjectSet("SLdesc", OBJPROP_YDISTANCE, 2);
ObjectCreate("LSdesc", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("LSdesc","Lot Size",10, "Arial Bold", description_color);
ObjectSet("LSdesc", OBJPROP_CORNER, 0);
ObjectSet("LSdesc", OBJPROP_XDISTANCE, 290);
ObjectSet("LSdesc", OBJPROP_YDISTANCE, 2);
ObjectCreate("PVdesc", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("PVdesc","Pip Value",10, "Arial Bold", description_color);
ObjectSet("PVdesc", OBJPROP_CORNER, 0);
ObjectSet("PVdesc", OBJPROP_XDISTANCE, 370);
ObjectSet("PVdesc", OBJPROP_YDISTANCE, 2);
ObjectCreate("RRdesc", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("RRdesc","Real Risk",10, "Arial Bold", description_color);
ObjectSet("RRdesc", OBJPROP_CORNER, 0);
ObjectSet("RRdesc", OBJPROP_XDISTANCE, 455);
ObjectSet("RRdesc", OBJPROP_YDISTANCE, 2);
ObjectCreate("stoploss_1", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("stoploss_1"," "+stoploss_1,10, "Arial Bold", Red);
ObjectSet("stoploss_1", OBJPROP_CORNER, 0);
ObjectSet("stoploss_1", OBJPROP_XDISTANCE, 218);
ObjectSet("stoploss_1", OBJPROP_YDISTANCE, 15);
ObjectCreate("stoploss_2", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("stoploss_2"," "+stoploss_2,10, "Arial Bold", Red);
ObjectSet("stoploss_2", OBJPROP_CORNER, 0);
ObjectSet("stoploss_2", OBJPROP_XDISTANCE, 218);
ObjectSet("stoploss_2", OBJPROP_YDISTANCE, 28);
ObjectCreate("stoploss_3", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("stoploss_3"," "+stoploss_3,10, "Arial Bold", Red);
ObjectSet("stoploss_3", OBJPROP_CORNER, 0);
ObjectSet("stoploss_3", OBJPROP_XDISTANCE, 218);
ObjectSet("stoploss_3", OBJPROP_YDISTANCE, 41);
ObjectCreate("stoploss_4", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("stoploss_4"," "+stoploss_4,10, "Arial Bold", Red);
ObjectSet("stoploss_4", OBJPROP_CORNER, 0);
ObjectSet("stoploss_4", OBJPROP_XDISTANCE, 218);
ObjectSet("stoploss_4", OBJPROP_YDISTANCE, 54);
ObjectCreate("lotsize1", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("lotsize1"," "+DoubleToStr(lotsize1,2),10, "Arial Bold", Blue);
ObjectSet("lotsize1", OBJPROP_CORNER, 0);
ObjectSet("lotsize1", OBJPROP_XDISTANCE, 295);
ObjectSet("lotsize1", OBJPROP_YDISTANCE, 15);
ObjectCreate("lotsize2", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("lotsize2"," "+DoubleToStr(lotsize2,2),10, "Arial Bold", Blue);
ObjectSet("lotsize2", OBJPROP_CORNER, 0);
ObjectSet("lotsize2", OBJPROP_XDISTANCE, 295);
ObjectSet("lotsize2", OBJPROP_YDISTANCE, 28);
ObjectCreate("lotsize3", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("lotsize3"," "+DoubleToStr(lotsize3,2),10, "Arial Bold", Blue);
ObjectSet("lotsize3", OBJPROP_CORNER, 0);
ObjectSet("lotsize3", OBJPROP_XDISTANCE, 295);
ObjectSet("lotsize3", OBJPROP_YDISTANCE, 41);
ObjectCreate("lotsize4", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("lotsize4"," "+DoubleToStr(lotsize4,2),10, "Arial Bold", Blue);
ObjectSet("lotsize4", OBJPROP_CORNER, 0);
ObjectSet("lotsize4", OBJPROP_XDISTANCE, 295);
ObjectSet("lotsize4", OBJPROP_YDISTANCE, 54);
ObjectCreate("pipvalue1", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("pipvalue1"," "+DoubleToStr(pipvalue1,2),10, "Arial Bold", Blue);
ObjectSet("pipvalue1", OBJPROP_CORNER, 0);
ObjectSet("pipvalue1", OBJPROP_XDISTANCE, 380);
ObjectSet("pipvalue1", OBJPROP_YDISTANCE, 15);
ObjectCreate("pipvalue2", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("pipvalue2"," "+DoubleToStr(pipvalue2,2),10, "Arial Bold", Blue);
ObjectSet("pipvalue2", OBJPROP_CORNER, 0);
ObjectSet("pipvalue2", OBJPROP_XDISTANCE, 380);
ObjectSet("pipvalue2", OBJPROP_YDISTANCE, 28);
ObjectCreate("pipvalue3", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("pipvalue3"," "+DoubleToStr(pipvalue3,2),10, "Arial Bold", Blue);
ObjectSet("pipvalue3", OBJPROP_CORNER, 0);
ObjectSet("pipvalue3", OBJPROP_XDISTANCE, 380);
ObjectSet("pipvalue3", OBJPROP_YDISTANCE, 41);
ObjectCreate("pipvalue4", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("pipvalue4"," "+DoubleToStr(pipvalue4,2),10, "Arial Bold", Blue);
ObjectSet("pipvalue4", OBJPROP_CORNER, 0);
ObjectSet("pipvalue4", OBJPROP_XDISTANCE, 380);
ObjectSet("pipvalue4", OBJPROP_YDISTANCE, 54);
ObjectCreate("risk1", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("risk1"," "+DoubleToStr(risk1,2),10, "Arial Bold", Blue);
ObjectSet("risk1", OBJPROP_CORNER, 0);
ObjectSet("risk1", OBJPROP_XDISTANCE, 465);
ObjectSet("risk1", OBJPROP_YDISTANCE, 15);
ObjectCreate("risk2", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("risk2"," "+DoubleToStr(risk2,2),10, "Arial Bold", Blue);
ObjectSet("risk2", OBJPROP_CORNER, 0);
ObjectSet("risk2", OBJPROP_XDISTANCE, 465);
ObjectSet("risk2", OBJPROP_YDISTANCE, 28);
ObjectCreate("risk3", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("risk3"," "+DoubleToStr(risk3,2),10, "Arial Bold", Blue);
ObjectSet("risk3", OBJPROP_CORNER, 0);
ObjectSet("risk3", OBJPROP_XDISTANCE, 465);
ObjectSet("risk3", OBJPROP_YDISTANCE, 41);
ObjectCreate("risk4", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("risk4"," "+DoubleToStr(risk4,2),10, "Arial Bold", Blue);
ObjectSet("risk4", OBJPROP_CORNER, 0);
ObjectSet("risk4", OBJPROP_XDISTANCE, 465);
ObjectSet("risk4", OBJPROP_YDISTANCE, 54);
ObjectCreate("symbol", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("symbol",Symbol(),15, "Arial Bold", description_color);
ObjectSet("symbol", OBJPROP_CORNER, 0);
ObjectSet("symbol", OBJPROP_XDISTANCE, 20);
ObjectSet("symbol", OBJPROP_YDISTANCE, 14);
if (show_spread)
{
ObjectCreate("spreaddesc", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("spreaddesc","Spread:",10, "Arial Bold", description_color);
ObjectSet("spreaddesc", OBJPROP_CORNER, 0);
ObjectSet("spreaddesc", OBJPROP_XDISTANCE, 60);
ObjectSet("spreaddesc", OBJPROP_YDISTANCE, 40);
ObjectCreate("spread", OBJ_LABEL, WindowFind("PS_Calculator"), 0, 0);
ObjectSetText("spread"," "+spread,30, "Arial Bold", Green);
ObjectSet("spread", OBJPROP_CORNER, 0);
ObjectSet("spread", OBJPROP_XDISTANCE, 100);
ObjectSet("spread", OBJPROP_YDISTANCE, 22);
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments