SLAndLots
Download scriptThis tool is designed to help traders calculate the appropriate trade size (lot volume) automatically based on a specific risk percentage and their desired stop-loss distance. Instead of manually calculating how much capital to risk on every trade, this script handles the math for you in real-time.
Core Functionality
1. Visual Price Tracking The tool places two distinct visual markers on your trading chart:
- Entry Line (Blue): This marks your intended opening price.
- Stop-Loss Line (Red): This marks where your trade will be closed if it goes against you.
These lines allow you to see the "risk gap" visually as you move them around, providing a clear sense of how much room you are giving the market for the trade to breathe.
2. Automatic Lot Calculation The primary logic of the script is to determine your position size. It takes the following factors into account:
- Risk Percentage: You can define what percentage of your account's free margin you want to risk on a single trade.
- Price Distance: It measures the exact distance between your Entry Line and your Stop-Loss Line.
- Instrument Type: The logic is sophisticated enough to recognize different types of currency pairs (Direct, Indirect, or Crosses) to ensure the calculations remain accurate regardless of which currency is the base or quote.
3. Safety Constraints To protect the account from errors, the script includes built-in safety checks:
- Broker Limits: It automatically ensures that the calculated lot size never exceeds the maximum allowed by your broker, nor falls below the minimum required lot.
- Margin Warning: The system monitors your available balance; if a calculated trade size would use too much of your free margin (exceeding 80%), it will issue an internal alert to prevent over-leveraging.
User Experience
As you move the visual lines on your chart, the tool updates a text display instantly. This display provides a live summary of your settings, showing your chosen risk percentage, the current entry and stop-loss prices, and—most importantly—the precise lot size you should use to execute that trade based on your risk parameters.
//+------------------------------------------------------------------+
//| SLAndLots.mq4 |
//| Copyright © 2009, DKeN |
//| http://www.dken.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, DKeN"
#property link "http://www.dken.biz"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 0.1
extern double cfg_iRiskPercent=1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
InitAll();
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
if(ObjectFind("SLAndLots_SL")!=-1) ObjectDelete("SLAndLots_SL");
if(ObjectFind("SLAndLots_Open")!=-1) ObjectDelete("SLAndLots_Open");
if(ObjectFind("SLAndLots_Comment")!=-1) ObjectDelete("SLAndLots_Comment");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
double sl=0,open=0;
//----
if(ObjectFind("SLAndLots_SL")!=-1){
sl=ObjectGet("SLAndLots_SL",OBJPROP_PRICE1);
}else{
if(ObjectCreate("SLAndLots_SL",OBJ_HLINE,0,0,Bid-500*Point)){
ObjectSet("SLAndLots_SL",OBJPROP_STYLE,STYLE_DOT);
ObjectSet("SLAndLots_SL",OBJPROP_COLOR,Red);
}
}
if(ObjectFind("SLAndLots_Open")!=-1){
open=ObjectGet("SLAndLots_Open",OBJPROP_PRICE1);
}else{
if(ObjectCreate("SLAndLots_Open",OBJ_HLINE,0,0,Ask)){
ObjectSet("SLAndLots_Open",OBJPROP_STYLE,STYLE_DOT);
ObjectSet("SLAndLots_Open",OBJPROP_COLOR,Blue);
}
}
if(ObjectFind("SLAndLots_Comment")==-1)
if(ObjectCreate("SLAndLots_Comment",OBJ_LABEL,WindowFind("SLAndLots"),0,Bid)){
ObjectSetText("SLAndLots_Comment","Test text", 9, "Times New Roman", Red);
}
double lot=get_lot(Symbol(),sl,open);
//Print(sl,open);
// Comment("SLAndLots v1.0 Risk = ",cfg_iRiskPercent," Open = ",open, " SL = ",sl, " Lot = ",lot);
string cmt="Risk = "+DoubleToStr(cfg_iRiskPercent,2)+
"% Open = "+DoubleToStr(open,Digits)+
" SL = "+DoubleToStr(sl,Digits)+
" Lot = "+DoubleToStr(lot,2);
ObjectSet("SLAndLots_Comment",OBJPROP_CORNER,0);
ObjectSet("SLAndLots_Comment",OBJPROP_YDISTANCE,5);
ObjectSetText("SLAndLots_Comment",cmt, 9, "Times New Roman", Red);
//----
return(0);
}
void InitAll(){
if(ObjectCreate("SLAndLots_Open",OBJ_HLINE,0,0,Ask)){
ObjectSet("SLAndLots_Open",OBJPROP_STYLE,STYLE_DOT);
ObjectSet("SLAndLots_Open",OBJPROP_COLOR,Blue);
Print("Open");
}
if(ObjectCreate("SLAndLots_SL",OBJ_HLINE,0,0,Bid-500*Point)){
ObjectSet("SLAndLots_SL",OBJPROP_STYLE,STYLE_DOT);
ObjectSet("SLAndLots_SL",OBJPROP_COLOR,Red);
}
}
double get_lot(string name,double stoplevel,double openlevel){
double lot=0;
if(stoplevel<=0 || openlevel<=0) return (0);
int TipInstrumenta=0;
double BazCours=0;
double dCof=1;
string SMB;
double stop=0;
// --------- âû÷èñëÿåì êàêàÿ ïàðà (ïðÿìàÿ, îáðàòíàÿ, êðîññ) --------------
TipInstrumenta=StringFind(name,"USD",0);
SMB=StringSubstr(name,0,3); //áàçîâàÿ âàëþòà
BazCours=MarketInfo(SMB+"USD",MODE_BID);
double Dig10=1;
if(MarketInfo(name,MODE_DIGITS)==5) Dig10=10;
double dBid=MarketInfo(name,MODE_BID);
if(TipInstrumenta==3){//ïàðà ïðÿìàÿ, íàïð. EurUsd
dCof=10;
Dig10=10;
}
if(TipInstrumenta==0){//ïàðà îáðàòíàÿ, íàïð. UsdJpy
dCof=10/dBid;
Dig10=10;
}
if(TipInstrumenta==-1){//Êðîññ-ïàðà, íàïð EurJpy
dCof=10*BazCours/dBid;
Dig10=10;
}
double dPoint=MarketInfo(name,MODE_POINT);
lot=NormalizeDouble(0.001*cfg_iRiskPercent*AccountFreeMargin()*dPoint*Dig10/MathAbs(openlevel-stoplevel),1);
if(lot>MarketInfo(name,MODE_MAXLOT)) lot=MarketInfo(name,MODE_MAXLOT);
if(lot<MarketInfo(name,MODE_MINLOT)) lot=MarketInfo(name,MODE_MINLOT);
//Print(lot);
if(lot*100*dBid>0.8*AccountFreeMargin()) Print("LOT: ",lot," ActiveMargin > FreeMargin");
return (lot);
}
//+------------------------------------------------------------------+
Comments