Update risk calculator

This commit is contained in:
Nkondog A. Venceslas
2023-01-11 23:35:38 +01:00
parent b3526242eb
commit 02ad3b93c6
13 changed files with 63 additions and 471 deletions
Binary file not shown.
-211
View File
@@ -1,211 +0,0 @@
//+------------------------------------------------------------------+
//| 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
#define KEY_B 66
#define KEY_S 83
//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;
double risk=0.0;
double StoplossPips=0.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);
int ticket;
//+------------------------------------------------------------------+
//| 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);
double StopAmount = StoplossPips * LotSize * TickValue;
string text ="Lot size for "+ InpMaxRiskPerTrade +"% = " + DoubleToString(LotSize,2) + " lot (" + DoubleToString(StopAmount, 2) + " " + AccountInfoString(ACCOUNT_CURRENCY) + ")";
string name = "Lot";
ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name,text, 14, "Corbel Bold", YellowGreen);
ObjectSet(name, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
ObjectSet(name, OBJPROP_XDISTANCE, 350);
ObjectSet(name, OBJPROP_YDISTANCE, 10);
}
//+------------------------------------------------------------------+
//| 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);
}
if(id==CHARTEVENT_KEYDOWN)
{
switch(lparam)
{
case KEY_B:
///SendOrder(TRADE_ACTION_DEAL, ORDER_TYPE_BUY,Symb,last_tick.ask,price,LotSize);
ticket = OrderSend(Symb, OP_BUY, LotSize, Ask, 1, price,0);
Alert("Buy " + LotSize + " lot " + Symb + " at " + Ask + " SL at " + price);
break;
case KEY_S:
ticket = OrderSend(Symb, OP_SELL, LotSize, Bid, 1, price,0);
Alert("Sell " + LotSize + " lot " + Symb + " at " + Bid + " SL at " + price);
break;
default:
//Print("Do nothing");
break;
}
if(ticket<=0)
{
int error=GetLastError();
//---- not enough money
if(error==134);
//---- 10 seconds wait
Sleep(10000);
//---- refresh price data
RefreshRates();
}
else
{
OrderSelect(ticket,SELECT_BY_TICKET);
OrderPrint();
}
}
}
//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;
//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));
StoplossPips = SL;
}
//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;
Alert("Lot size too small");
}
}
//+------------------------------------------------------------------+
Binary file not shown.
Binary file not shown.
+45 -28
View File
@@ -22,6 +22,13 @@ enum ENUM_RISK_BASE
RISK_BASE_INPUT=4, //INPUT BASE
};
//Enumerative for the default risk type
enum ENUM_RISK_DEFAULT_TYPE
{
FIXED=1, //FIXED
Percent=2, //AMOUNT BASE
};
//Enumerative for the default risk size
enum ENUM_RISK_DEFAULT_SIZE
{
@@ -30,22 +37,27 @@ enum ENUM_RISK_DEFAULT_SIZE
};
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 double InpDefaultLotSize=0.01; //Lot Size if fixed Position Size Mode = FIXED
input ENUM_RISK_BASE InpRiskBase=RISK_BASE_BALANCE; //Risk Base
input double InpMaxRiskPerTrade=0.5; //Percentage To Risk Each Trade
input ENUM_RISK_DEFAULT_TYPE InpRiskDefaultType=FIXED; //Risk Type
input double InpFixRiskAmount=10; //Max Account Risk ($) if risk type = FIXED
input double InpMaxLossPercent=1.0; //Max Account Risk (%)
input double InpTPMultiple=1; //TP multiple %
input double InpMinLotSize=0.01; //Minimum Position Size Allowed
input double InpMaxLotSize=100; //Maximum Position Size Allowedv
input double InpMinLotSize=0.01; //Minimum lot Size Allowed
input double InpMaxLotSize=100; //Maximum lot Size Allowed
input int InpSlippage=1; //Slippage
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string Symb = Symbol();
double RiskBaseAmount=InpFixRiskAmount;
double MaxRiskPerTrade=InpFixRiskAmount; //Percentage To Risk Each Trade
double LotSize=InpDefaultLotSize;
double stopLoss=0.0;
double TakeProfit=0.0;
double risk=0.0;
double StoplossPips=0.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);
@@ -73,6 +85,14 @@ int OnInit()
void OnTick()
{
stopLoss = NormalizeDouble(ObjectGetDouble(0, "sl", OBJPROP_PRICE), _Digits);
//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);
displayOnChart();
}
//+------------------------------------------------------------------+
@@ -151,14 +171,14 @@ void LotSizeCalculate(double sLoss)
if(sLoss < PriceAsk)
{
pipDiff = PriceAsk-sLoss;
SL = (PriceAsk-sLoss)/_Point;
SL = pipDiff /_Point;
//Print("TakeProfit ", TakeProfit, " PriceAsk ", PriceAsk, " pipDiff ", pipDiff, " InpTPMultiple ", InpTPMultiple, " spread ", spread);
TakeProfit = PriceAsk + (pipDiff * InpTPMultiple) + (spread*2);
}
if(sLoss > PriceAsk)
{
pipDiff = sLoss-PriceBid;
SL = (sLoss-PriceBid)/_Point;
SL = pipDiff /_Point;
//Print("TakeProfit ", TakeProfit, " PriceAsk ", PriceBid, " pipDiff ", pipDiff, " InpTPMultiple ", InpTPMultiple, " spread ", spread);
TakeProfit = PriceBid - (pipDiff * InpTPMultiple) - (spread*2);
}
@@ -166,33 +186,15 @@ void LotSizeCalculate(double sLoss)
TakeProfit = NormalizeDouble(TakeProfit, _Digits);
//Print("Stop loss distance ", SL);
StoplossPips = 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;
//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));
StoplossPips = SL;
}
//If the stop loss is zero then the lot size is the default one
if(SL==0)
{
LotSize=InpDefaultLotSize;
LotSize=((RiskBaseAmount*MaxRiskPerTrade/100)/(SL*TickValue));
}
}
//Normalize the Lot Size to satisfy the allowed lot increment and minimum and maximum position size
@@ -221,10 +223,25 @@ void LotSizeCalculate(double sLoss)
void displayOnChart()
{
double initialLoss = (RiskBaseAmount * InpMaxLossPercent) / 100;
if(InpRiskDefaultType == FIXED)
{
initialLoss = InpFixRiskAmount;
}
initialLoss = NormalizeDouble(initialLoss, 2);
MaxRiskPerTrade = NormalizeDouble((initialLoss * 100) / RiskBaseAmount, 2);
LotSizeCalculate(stopLoss);
//Comment("Lot size : ", LotSize);
double StopAmount = StoplossPips * LotSize * TickValue;
string text ="Lot size for "+ (string)InpMaxRiskPerTrade +"% = " + DoubleToString(LotSize,2) + " lot (" + DoubleToString(StopAmount, 2) + " " + AccountInfoString(ACCOUNT_CURRENCY) + ")";
if(InpRiskDefaultSize == RISK_DEFAULT_FIXED)
{
MaxRiskPerTrade = NormalizeDouble((StopAmount * 100) / RiskBaseAmount, 2);
}
string text ="Lot size for "+ (string)DoubleToString(MaxRiskPerTrade,2) +"% = " + DoubleToString(LotSize,2) + " lot (" + DoubleToString(StopAmount, 2) + " " + AccountInfoString(ACCOUNT_CURRENCY) + ")";
string name = "Lot";
ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name,text, 14, "Corbel Bold", YellowGreen);
Binary file not shown.
Binary file not shown.
@@ -29,13 +29,16 @@ input int inpMAPeriod = 200; //MA period
input int inpMASHift = 0; //MA shift
input ENUM_APPLIED_PRICE inpMAApplyedTo = PRICE_CLOSE; //MA applied to
double jaws, teeth, lips, sma, prevCandleHigh, prevCandleLow, currentPrice, candleClose;
double jaws, teeth, lips, sma, prevCandleHigh, prevCandleLow, currentPrice, prevClosePrice;
string comm = "";
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
@@ -45,7 +48,7 @@ int OnInit()
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
@@ -54,20 +57,20 @@ void OnTick()
{
//---
if(!newCandle())
return;
return;
jaws=iAlligator(NULL,0,inpJawsPeriod,inpJawsShift,inpTeethPeriod,inpTeethShift,inpLipsPeriod,inpLipsShift,MODE_SMMA,PRICE_MEDIAN,MODE_GATORJAW,0);
teeth=iAlligator(NULL,0,inpJawsPeriod,inpJawsShift,inpTeethPeriod,inpTeethShift,inpLipsPeriod,inpLipsShift,MODE_SMMA,PRICE_MEDIAN,MODE_GATORTEETH,0);
lips=iAlligator(NULL,0,inpJawsPeriod,inpJawsShift,inpTeethPeriod,inpTeethShift,inpLipsPeriod,inpLipsShift,MODE_SMMA,PRICE_MEDIAN,MODE_GATORLIPS,0);
sma = iMA(NULL,0,inpMAPeriod,inpMASHift,inpMAMethod,inpMAApplyedTo,1);
Print(" Jaws ", jaws, " teeth ", teeth, " lips ", lips, " sma ", sma);
//Get previous candle
//Get previous candle
prevCandleHigh = iHigh(NULL, PERIOD_CURRENT, 1);
prevCandleLow = iLow(NULL, PERIOD_CURRENT, 1);
currentPrice = iClose(NULL, PERIOD_CURRENT, 0);
candleClose = iLow(NULL, PERIOD_CURRENT, 0);
prevClosePrice = iClose(NULL, PERIOD_CURRENT, 1);
//comm = "jaws " + (string)jaws + " teeth " + (string)teeth + " lips " + (string)lips + " sma " + (string)sma;
comm = "Trade alert on " + Symbol();
@@ -77,20 +80,19 @@ void OnTick()
if(sma < currentPrice)
{
//Alert for bullish continuation signal
if(prevCandleHigh > jaws && prevCandleHigh > teeth && prevCandleHigh > lips)
if(prevClosePrice > jaws && prevClosePrice > teeth && prevClosePrice > lips)
{
if(prevCandleLow < jaws || prevCandleLow < teeth ||prevCandleLow < lips)
{
comm += " LONG CONTINUATION SIGNAL: Price above SMA just moves above Alligator. \n";
Notify(comm);
comm += " LONG CONTINUATION SIGNAL: Price above SMA just moves above Alligator. \n";
Notify(comm);
}
}
//Alert for bearish counter trend signal
if(lips > teeth && teeth > jaws)
{
if(candleClose < lips && candleClose < teeth && candleClose < jaws)
if(prevClosePrice < jaws && prevClosePrice < teeth && prevClosePrice < lips)
{
comm += " SHORT COUNTER TREND SIGNAL: Price above SMA moves below Alligator in a trending market \n";
Notify(comm);
@@ -102,7 +104,7 @@ void OnTick()
if(sma > currentPrice)
{
//Alert for bearish continuation signal
if(prevCandleLow < jaws && prevCandleLow < teeth && prevCandleLow < lips)
if(prevClosePrice < jaws && prevClosePrice < teeth && prevClosePrice < lips)
{
if(prevCandleHigh > jaws || prevCandleHigh > teeth ||prevCandleHigh > lips)
{
@@ -111,11 +113,11 @@ void OnTick()
}
}
//Alert for bearish counter trend signal
//Alert for bullish counter trend signal
if(lips < teeth && teeth < jaws)
{
if(candleClose > lips && candleClose > teeth && candleClose > jaws)
if(prevClosePrice > jaws && prevClosePrice > teeth && prevClosePrice > lips)
{
comm += " LONG COUNTER TREND SIGNAL: Price below SMA just closes above Alligator in a down trending market \n";
Notify(comm);
Binary file not shown.
-216
View File
@@ -1,216 +0,0 @@
//+------------------------------------------------------------------+
//| StarRiskCalculator.mq5 |
//| Copyright 2022, Nkondog Anselme Venceslas. |
//| https://www.linkedin.com/in/nkondog |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Nkondog Anselme Venceslas."
#property link "https://www.linkedin.com/in/nkondog"
#property version "1.00"
//Parameters
MqlTick last_tick;
//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 InpMaxLossPercent=4.0; //Max Account Risk %
input int InpLifeCount=20; //Number of losses
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
double InpMinLotSize=0.01; //Minimum Position Size Allowed
double InpMaxLotSize=100; //Maximum Position Size Allowed
double RiskBaseAmount=0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string Symb = Symbol();
string AccountCurr = AccountInfoString(ACCOUNT_CURRENCY);
double MaxRiskPerTrade=0.0; //Percentage To Risk Each Trade
double LotSize=InpDefaultLotSize;
double price=0.0;
double risk=0.0;
double StoplossPips=0.0;
double riskDiff=0.0;
double initialLoss=0.0;
double totalLoss=0.0;
double maxRiskPerLife=0.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);
//+------------------------------------------------------------------+
//| 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);
riskDiff = NormalizeDouble(RiskBaseAmount - InpBalance, 2);
initialLoss = (InpBalance * InpMaxLossPercent) / 100;
totalLoss = NormalizeDouble(riskDiff + initialLoss, 2);
maxRiskPerLife = NormalizeDouble(totalLoss /InpLifeCount, 2);
MaxRiskPerTrade = NormalizeDouble((maxRiskPerLife * 100) / RiskBaseAmount, 2);
Comment("Star Risk Calculator \nRiskDiff: " + riskDiff + " " + AccountCurr +"\nInitialLoss: " + initialLoss + " " + AccountCurr +"\nTotalLoss: " + totalLoss + " " + AccountCurr +"\nMaxRiskPerLife: " + maxRiskPerLife + " " + AccountCurr + "\nMaxRiskPerTrade: " + MaxRiskPerTrade +"%");
double StopAmount = StoplossPips * LotSize * TickValue;
string text ="Lot size for "+ MaxRiskPerTrade +"% = " + DoubleToString(LotSize,2) + " lot (" + NormalizeDouble(StopAmount, 2) + " " + AccountCurr + ")";
string name = "Lot";
string name2 = "risk";
ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0);
//ObjectSetText(name,text, 36, "Corbel Bold", YellowGreen);
ObjectSetInteger(0,name, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
ObjectSetInteger(0,name, OBJPROP_XDISTANCE, 550);
ObjectSetInteger(0,name, OBJPROP_YDISTANCE, 10);
ObjectSetString(0,name,OBJPROP_TEXT,text);
ObjectSetString(0,name,OBJPROP_FONT,"Arial");
ObjectSetInteger(0,name,OBJPROP_FONTSIZE,14);
ObjectSetInteger(0,name,OBJPROP_COLOR,clrYellowGreen);
//LabelDelete(0, name);
}
//+------------------------------------------------------------------+
//| 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)
{
SymbolInfoTick(_Symbol,last_tick);
double SL=0;
double PriceAsk=last_tick.ask;
double PriceBid=last_tick.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)
{
//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*MaxRiskPerTrade/100)/(SL*TickValue));
StoplossPips = SL;
}
//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");
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Delete a text label |
//+------------------------------------------------------------------+
bool LabelDelete(const long chart_ID=0, // chart's ID
const string name="Label") // label name
{
//--- reset the error value
ResetLastError();
//--- delete the label
if(!ObjectDelete(chart_ID,name))
{
Print(__FUNCTION__,
": failed to delete a text label! Error code = ",GetLastError());
return(false);
}
//--- successful execution
return(true);
}
Binary file not shown.
Binary file not shown.
Binary file not shown.