Add LotCal.mq4 and LotCal.mq5

This commit is contained in:
Nkondog Anselme
2022-05-11 15:54:01 +01:00
parent 1640247b61
commit 75d65fb1f3
7 changed files with 172 additions and 1 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -63,6 +63,7 @@ input string Comment_01="----------------------"; //Stop l
input ENUM_MODE_SL InpStopLossMode=SL_FIXED; //Stop Loss Mode
input int InpDefaultStopLoss=200; //Default Stop Loss In Points (0=No Stop Loss)
input int InpAutoStopLossCandlesAmount=10; //Auto SL amount of candles
input int InpAlertDistanceToMA=50; //Distance to alert
input int InpMinStopLoss=0; //Minimum Allowed Stop Loss In Points
input int InpMaxStopLoss=5000; //Maximum Allowed Stop Loss In Points
input bool InpDisableStopLoss=false; //Disable Stop Loss
+170
View File
@@ -0,0 +1,170 @@
//+------------------------------------------------------------------+
//| LotCal.mq4 |
//| Copyright 2022, Nkondog Anselme Venceslas. |
//| https://www.linkedin/in/nkondog.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Nkondog Anselme Venceslas."
#property link "https://www.linkedin/in/nkondog.com "
#property version "1.00"
#property strict
//Parameters
//Enumerative for the base used for risk calculation
enum ENUM_RISK_BASE
{
RISK_BASE_EQUITY=1, //EQUITY
RISK_BASE_BALANCE=2, //BALANCE
RISK_BASE_FREEMARGIN=3, //FREE MARGIN
RISK_BASE_INPUT=4, //INPUT BASE
};
//Enumerative for the default risk size
enum ENUM_RISK_DEFAULT_SIZE
{
RISK_DEFAULT_FIXED=1, //FIXED SIZE
RISK_DEFAULT_AUTO=2, //AUTOMATIC SIZE BASED ON RISK
};
input ENUM_RISK_DEFAULT_SIZE InpRiskDefaultSize=RISK_DEFAULT_AUTO; //Position Size Mode
input double InpBalance=10000.0; //Balance
input double InpDefaultLotSize=0.01; //Position Size (if fixed or if no stop loss defined)
input ENUM_RISK_BASE InpRiskBase=RISK_BASE_BALANCE; //Risk Base
input double InpMaxRiskPerTrade=0.5; //Percentage To Risk Each Trade
input double InpMinLotSize=0.01; //Minimum Position Size Allowed
input double InpMaxLotSize=100; //Maximum Position Size Allowedv
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string Symb = Symbol();
double LotSize=InpDefaultLotSize;
double price=0.0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Print("The Expert Advisor with name ",MQLInfoString(MQL_PROGRAM_NAME)," is running");
//--- enable object create events
ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_CREATE,true);
//--- enable object delete events
ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_DELETE,true);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
LotSizeCalculate(price);
//Comment("Lot size : ", LotSize);
string text ="Lot size for "+ InpMaxRiskPerTrade +"% = " + DoubleToString(LotSize,2);
string name = "Lot";
ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name,text, 36, "Corbel Bold", YellowGreen);
ObjectSet(name, OBJPROP_CORNER, 2);
ObjectSet(name, OBJPROP_XDISTANCE, 15);
ObjectSet(name, OBJPROP_YDISTANCE, 1);
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, // Event identifier
const long& lparam, // Event parameter of long type
const double& dparam, // Event parameter of double type
const string& sparam) // Event parameter of string type
{
//--- the object has been deleted
if(id==CHARTEVENT_OBJECT_DELETE)
{
Print("The object with name ",sparam," has been deleted");
}
//--- the object has been created
if(id==CHARTEVENT_OBJECT_CREATE)
{
Print("The object with name ",sparam," has been created");
}
//--- the object has been moved or its anchor point coordinates has been changed
if(id==CHARTEVENT_OBJECT_DRAG)
{
price = ObjectGetDouble(0, sparam, OBJPROP_PRICE, 0);
Print("The anchor point coordinates of the object with name ",sparam," has been changed. Price ", price);
}
}
//Lot Size Calculator
void LotSizeCalculate(double stopLoss)
{
double SL=0;
double PriceAsk=MarketInfo(0,MODE_ASK);
double PriceBid=MarketInfo(0,MODE_BID);
if(stopLoss < PriceAsk)
{
SL = (PriceAsk-stopLoss)/_Point;
}
if(stopLoss > PriceAsk)
{
SL = (stopLoss-PriceBid)/_Point;
}
Print("Stop loss distance ", SL);
//If the position size is dynamic
if(InpRiskDefaultSize==RISK_DEFAULT_AUTO)
{
//If the stop loss is not zero then calculate the lot size
if(SL!=0)
{
double RiskBaseAmount=0;
//TickValue is the value of the individual price increment for 1 lot of the instrument, expressed in the account currenty
double TickValue=SymbolInfoDouble(Symb,SYMBOL_TRADE_TICK_VALUE);
//Define the base for the risk calculation depending on the parameter chosen
if(InpRiskBase==RISK_BASE_BALANCE)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_BALANCE);
if(InpRiskBase==RISK_BASE_EQUITY)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_EQUITY);
if(InpRiskBase==RISK_BASE_FREEMARGIN)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_FREEMARGIN);
if(InpRiskBase==RISK_BASE_INPUT)
RiskBaseAmount=InpBalance;
//Calculate the Position Size
Print("RiskBaseAmount ", RiskBaseAmount, " MaxRiskPerTrade ", InpMaxRiskPerTrade, "Stop loss ", SL, " TickValue ", TickValue);
LotSize=((RiskBaseAmount*InpMaxRiskPerTrade/100)/(SL*TickValue));
}
//If the stop loss is zero then the lot size is the default one
if(SL==0)
{
LotSize=InpDefaultLotSize;
}
}
//Normalize the Lot Size to satisfy the allowed lot increment and minimum and maximum position size
LotSize=MathFloor(LotSize/SymbolInfoDouble(Symb,SYMBOL_VOLUME_STEP))*SymbolInfoDouble(Symb,SYMBOL_VOLUME_STEP);
//Limit the lot size in case it is greater than the maximum allowed by the user
if(LotSize>InpMaxLotSize)
LotSize=InpMaxLotSize;
//Limit the lot size in case it is greater than the maximum allowed by the broker
if(LotSize>SymbolInfoDouble(Symb,SYMBOL_VOLUME_MAX))
LotSize=SymbolInfoDouble(Symb,SYMBOL_VOLUME_MAX);
Print("Lot ", LotSize, " Max lot ", SymbolInfoDouble(Symb,SYMBOL_VOLUME_MAX));
//If the lot size is too small then set it to 0 and don't trade
if(LotSize < SymbolInfoDouble(Symb,SYMBOL_VOLUME_MIN))
{
LotSize=0;
Print("Lot size too small");
}
}
//+------------------------------------------------------------------+
+1 -1
View File
@@ -1 +1 @@
New-Item -ItemType Junction -Path "C:\Users\Nkondog\AppData\Roaming\MetaQuotes\Terminal\DBE9B8B347D025DD139E103EE3B63FD8\MQL5\Include\Nkanven" -Target "D:\ITprojects\sat_bot\Include\Nkanven"
New-Item -ItemType Junction -Path "C:\Users\Nkondog\AppData\Roaming\MetaQuotes\Terminal\10CE948A1DFC9A8C27E56E827008EBD4\MQL5\Experts\Nkanven" -Target "D:\ITprojects\sat_bot\Experts\Nkanven"