Smart-Lot-Calculator/Scripts/SmartLotCalculatorExample.mq5

145 lines
3.8 KiB
MQL5
Raw Permalink Normal View History

//+------------------------------------------------------------------+
//| SmartLotCalculatorExample.mq5 |
//| Example script for the Smart Lot Calculator library |
//+------------------------------------------------------------------+
#property script_show_inputs
#property version "1.00"
#property description "Calculates a recommended volume without opening trades."
#include "../Include/SmartLotCalculator.mqh"
// Symbol left empty means the current chart symbol.
input string InpSymbol = "";
input ENUM_ORDER_TYPE InpOrderType = ORDER_TYPE_BUY;
input double InpRiskPercent = 1.0;
input ENUM_RISK_BASE InpRiskBase = RISK_BASE_EQUITY;
input int InpStopLossPoints = 500;
//+------------------------------------------------------------------+
//| Script start |
//+------------------------------------------------------------------+
void OnStart()
{
string symbol = InpSymbol;
if(symbol == "")
symbol = _Symbol;
if(InpStopLossPoints <= 0)
{
Print("Error: Stop Loss points must be greater than zero.");
return;
}
if(!SymbolSelect(symbol, true))
{
Print("Error: could not select symbol ", symbol, ".");
return;
}
MqlTick tick;
if(!SymbolInfoTick(symbol, tick))
{
Print(
"Error: could not obtain current prices for ",
symbol,
". MetaTrader error code: ",
GetLastError()
);
return;
}
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
if(point <= 0.0)
{
Print("Error: invalid point size for ", symbol, ".");
return;
}
double entry_price = 0.0;
double stop_price = 0.0;
if(InpOrderType == ORDER_TYPE_BUY)
{
entry_price = tick.ask;
stop_price = entry_price - (InpStopLossPoints * point);
}
else if(InpOrderType == ORDER_TYPE_SELL)
{
entry_price = tick.bid;
stop_price = entry_price + (InpStopLossPoints * point);
}
else
{
Print("Error: select ORDER_TYPE_BUY or ORDER_TYPE_SELL.");
return;
}
int price_digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
entry_price = NormalizeDouble(entry_price, price_digits);
stop_price = NormalizeDouble(stop_price, price_digits);
string error = "";
double recommended_volume =
CSmartLotCalculator::CalculateByRiskPercent(
symbol,
InpOrderType,
entry_price,
stop_price,
InpRiskPercent,
InpRiskBase,
error
);
if(recommended_volume <= 0.0)
{
Print("Calculation failed: ", error);
return;
}
double potential_loss =
CSmartLotCalculator::CalculatePotentialLoss(
symbol,
InpOrderType,
recommended_volume,
entry_price,
stop_price,
error
);
if(potential_loss <= 0.0)
{
Print("Loss verification failed: ", error);
return;
}
string direction =
InpOrderType == ORDER_TYPE_BUY ? "BUY" : "SELL";
Print("--------------------------------------------------");
Print("SMART LOT CALCULATOR RESULT");
Print("Symbol: ", symbol);
Print("Direction: ", direction);
Print("Entry price: ", DoubleToString(entry_price, price_digits));
Print("Stop Loss: ", DoubleToString(stop_price, price_digits));
Print("Stop distance: ", InpStopLossPoints, " points");
Print("Selected risk: ", DoubleToString(InpRiskPercent, 2), "%");
Print(
"Recommended volume: ",
DoubleToString(recommended_volume, 8),
" lots"
);
Print(
"Estimated loss at Stop Loss: ",
DoubleToString(potential_loss, 2),
" ",
AccountInfoString(ACCOUNT_CURRENCY)
);
Print("No trade was opened.");
Print("--------------------------------------------------");
}