Skip to content

Commit

Permalink
3.02
Browse files Browse the repository at this point in the history
1. Fixed a bug that could result in wrong position size calculation when switching the chart's symbol in MT5.
2. Fixed the Commentary and Auto-Suffix fields which weren't working correctly in MT5.
3. Fixed a bug that prevented newly set default input parameters to change the respective fields on the panel.
4. Fixed a bug that could result in reward-related Main tab elements to show up when some other tab was selected.
5. Fixed a bug that sometimes prevented the Position Size field change from registering when the panel was calculating the Risk based on the Position Size.
6. Moved the ShowATROptions input parameter to the Compactness inputs group.
7. Removed the redundant Stop Price Label Color input parameter as there is no such label in MT5.
8. Made some purely cosmetic changes.
  • Loading branch information
EarnForex committed Aug 12, 2022
1 parent 765c85e commit 7ff4ae2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 39 deletions.
15 changes: 7 additions & 8 deletions MQL4/Experts/Position Sizer/Position Sizer.mq4
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link "https://www.earnforex.com/metatrader-expert-advisors/Position-Sizer/"
#property version "3.01"
string Version = "3.01";
#property version "3.02"
string Version = "3.02";
#property strict

#property description "Calculates risk-based position size for your account."
Expand All @@ -27,6 +27,7 @@ input bool HideAccSize = false; // HideAccSize: Hide account size?
input bool ShowPointValue = false; // ShowPointValue: Show point value?
input bool ShowMaxPSButton = false; // ShowMaxPSButton: Show Max Position Size button?
input bool StartPanelMinimized = false; // StartPanelMinimized: Start the panel minimized?
input bool ShowATROptions = false; // ShowATROptions: If true, SL and TP can be set via ATR.
input group "Fonts"
input color sl_label_font_color = clrLime; // SL Label Color
input color tp_label_font_color = clrYellow; // TP Label Font Color
Expand All @@ -53,6 +54,8 @@ input int DefaultATRPeriod = 14; // ATRPeriod: Default ATR period.
input double DefaultATRMultiplierSL = 0; // ATRMultiplierSL: Default ATR multiplier for SL.
input double DefaultATRMultiplierTP = 0; // ATRMultiplierTP: Default ATR multiplier for TP.
input ENUM_TIMEFRAMES DefaultATRTimeframe = PERIOD_CURRENT; // ATRTimeframe: Default timeframe for ATR.
input bool DefaultSpreadAdjustmentSL = false; // SpreadAdjustmentSL: Adjust SL by Spread value in ATR mode.
input bool DefaultSpreadAdjustmentTP = false; // SpreadAdjustmentTP: Adjust TP by Spread value in ATR mode.
input double DefaultCommission = 0; // Commission: Default one-way commission per 1 lot.
input ACCOUNT_BUTTON DefaultAccountButton = Balance; // AccountButton: Balance/Equity/Balance-CPR
input double DefaultRisk = 1; // Risk: Initial risk tolerance in percentage points
Expand Down Expand Up @@ -81,10 +84,8 @@ input ENUM_BASE_CORNER DefaultPanelPositionCorner = CORNER_LEFT_UPPER; // PanelP
input bool DefaultTPLockedOnSL = false; // TPLockedOnSL: Lock TP to (multiplied) SL distance.
input int DefaultTrailingStop = 0; // TrailingStop: For the Trading tab.
input int DefaultBreakEven = 0; // BreakEven: For the Trading tab.
input bool DefaultSpreadAdjustmentSL = false; // SpreadAdjustmentSL: Adjust SL by Spread value in ATR mode.
input bool DefaultSpreadAdjustmentTP = false; // SpreadAdjustmentTP: Adjust TP by Spread value in ATR mode.
input int DefaultMaxNumberOfTrades = 0; // MaxNumberOfTrades: For the Trading tab. 0 - no limit.
input int DefaultAllSymbols = true; // AllSymbols: For Trading tab, true - count trades in all symbols.
input bool DefaultAllSymbols = true; // AllSymbols: For Trading tab, true - count trades in all symbols.
input group "Miscellaneous"
input double TP_Multiplier = 1; // TP Multiplier for SL value, appears in Take-profit button.
input bool UseCommissionToSetTPDistance = false; // UseCommissionToSetTPDistance: For TP button.
Expand All @@ -93,7 +94,6 @@ input double AdditionalFunds = 0; // AdditionalFunds: Added to account balance f
input double CustomBalance = 0; // CustomBalance: Overrides AdditionalFunds value.
input bool SLDistanceInPoints = false; // SLDistanceInPoints: SL distance in points instead of a level.
input bool TPDistanceInPoints = false; // TPDistanceInPoints: TP distance in points instead of a level.
input bool ShowATROptions = false; // ShowATROptions: If true, SL and TP can be set via ATR.
input CANDLE_NUMBER ATRCandle = Current_Candle; // ATRCandle: Candle to get ATR value from.
input int TakeProfitsNumber = 1; // TakeProfitsNumber: More than 1 target to split trades.
input bool CalculateUnadjustedPositionSize = false; // CalculateUnadjustedPositionSize: Ignore broker's restrictions.
Expand All @@ -119,6 +119,7 @@ int PrevChartWidth;

int OnInit()
{
TickSize = -1;
CtrlRequired = false;
ShiftRequired = false;
MainKey = 0;
Expand All @@ -141,7 +142,6 @@ int OnInit()
ArrayResize(sets.WasSelectedAdditionalTakeProfitLine, TakeProfitsNumber - 1); // -1 because the flag for the main TP is saved elsewhere.
}


// Check for symbol change.
int total = GlobalVariablesTotal();
for (int i = 0; i < total; i++)
Expand All @@ -154,7 +154,6 @@ int OnInit()
if (prev_symbol != Symbol()) // Symbol changed.
{
// Reset everything.
TickSize = -1;
OutputPointValue = ""; OutputSwapsType = "Unknown"; SwapsTripleDay = "?";
OutputSwapsDailyLongLot = "?"; OutputSwapsDailyShortLot = "?"; OutputSwapsDailyLongPS = "?"; OutputSwapsDailyShortPS = "?";
OutputSwapsYearlyLongLot = "?"; OutputSwapsYearlyShortLot = "?"; OutputSwapsYearlyLongPS = "?"; OutputSwapsYearlyShortPS = "?";
Expand Down
20 changes: 12 additions & 8 deletions MQL4/Experts/Position Sizer/Position Sizer.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public:
virtual void CheckAndRestoreLines();
virtual void Minimize();
virtual bool IsMinimized() {return m_minimized;}
virtual void IniFileLoad() {CAppDialog::IniFileLoad(); InitObjects();} // Need to init objects after ini file load.

// Remember the panel's location to have the same location for minimized and maximized states.
int remember_top, remember_left;
Expand Down Expand Up @@ -2091,12 +2092,15 @@ void CPositionSizeCalculator::ProcessTPChange(const bool tp_button_click)
{
if (!m_minimized)
{
m_LblRR.Show();
if (InputRR != "") m_EdtRR1.Show();
m_EdtRR2.Show();
m_LblReward.Show();
m_EdtReward1.Show();
m_EdtReward2.Show();
if (sets.SelectedTab == MainTab)
{
m_LblRR.Show();
if (InputRR != "") m_EdtRR1.Show();
m_EdtRR2.Show();
m_LblReward.Show();
m_EdtReward1.Show();
m_EdtReward2.Show();
}
}
if (sets.ShowLines)
{
Expand Down Expand Up @@ -3648,9 +3652,9 @@ bool CPositionSizeCalculator::LoadSettingsFromDisk()
}
else if (var_name == "Parameter_DefaultMaxNumberOfTrades")
{
if (StringToInteger(var_content) != DefaultBreakEven) sets.MaxNumberOfTrades = DefaultMaxNumberOfTrades;
if (StringToInteger(var_content) != DefaultMaxNumberOfTrades) sets.MaxNumberOfTrades = DefaultMaxNumberOfTrades;
}
else if (var_name == "Parameter_DefaultDefaultAllSymbols")
else if (var_name == "Parameter_DefaultAllSymbols")
{
if ((bool)StringToInteger(var_content) != DefaultAllSymbols) sets.AllSymbols = DefaultAllSymbols;
}
Expand Down
2 changes: 1 addition & 1 deletion MQL5/Experts/Position Sizer/Defines.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum ENTRY_TYPE
{
Instant,
Pending,
StopLimit
StopLimit // Stop Limit
};

enum ACCOUNT_BUTTON
Expand Down
Binary file modified MQL5/Experts/Position Sizer/Position Sizer Trading.mqh
Binary file not shown.
20 changes: 10 additions & 10 deletions MQL5/Experts/Position Sizer/Position Sizer.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link "https://www.earnforex.com/metatrader-expert-advisors/Position-Sizer/"
#property version "3.01"
string Version = "3.01";
#property version "3.02"
string Version = "3.02";

#property description "Calculates risk-based position size for your account."
#property description "Allows trade execution based the calculation results.\r\n"
Expand Down Expand Up @@ -34,10 +34,10 @@ input bool HideAccSize = false; // HideAccSize: Hide account size?
input bool ShowPointValue = false; // ShowPointValue: Show point value?
input bool ShowMaxPSButton = false; // ShowMaxPSButton: Show Max Position Size button?
input bool StartPanelMinimized = false; // StartPanelMinimized: Start the panel minimized?
input bool ShowATROptions = false; // ShowATROptions: If true, SL and TP can be set via ATR.
input group "Fonts"
input color sl_label_font_color = clrLime; // SL Label Color
input color tp_label_font_color = clrYellow; // TP Label Font Color
input color sp_label_font_color = clrPurple; // Stop Price Label Color
input color sl_label_font_color = clrLime; // SL Label Color
input color tp_label_font_color = clrYellow; // TP Label Color
input uint font_size = 13; // Labels Font Size
input string font_face = "Courier"; // Labels Font Face
input group "Lines"
Expand All @@ -64,6 +64,8 @@ input int DefaultATRPeriod = 14; // ATRPeriod: Default ATR period.
input double DefaultATRMultiplierSL = 0; // ATRMultiplierSL: Default ATR multiplier for SL.
input double DefaultATRMultiplierTP = 0; // ATRMultiplierTP: Default ATR multiplier for TP.
input ENUM_TIMEFRAMES DefaultATRTimeframe = PERIOD_CURRENT; // ATRTimeframe: Default timeframe for ATR.
input bool DefaultSpreadAdjustmentSL = false; // SpreadAdjustmentSL: Adjust SL by Spread value in ATR mode.
input bool DefaultSpreadAdjustmentTP = false; // SpreadAdjustmentTP: Adjust TP by Spread value in ATR mode.
input double DefaultCommission = 0; // Commission: Default one-way commission per 1 lot.
input ACCOUNT_BUTTON DefaultAccountButton = Balance; // AccountButton: Balance/Equity/Balance-CPR
input double DefaultRisk = 1; // Risk: Initial risk tolerance in percentage points
Expand Down Expand Up @@ -92,10 +94,8 @@ input ENUM_BASE_CORNER DefaultPanelPositionCorner = CORNER_LEFT_UPPER; // PanelP
input bool DefaultTPLockedOnSL = false; // TPLockedOnSL: Lock TP to (multiplied) SL distance.
input int DefaultTrailingStop = 0; // TrailingStop: For the Trading tab.
input int DefaultBreakEven = 0; // BreakEven: For the Trading tab.
input bool DefaultSpreadAdjustmentSL = false; // SpreadAdjustmentSL: Adjust SL by Spread value in ATR mode.
input bool DefaultSpreadAdjustmentTP = false; // SpreadAdjustmentTP: Adjust TP by Spread value in ATR mode.
input int DefaultMaxNumberOfTrades = 0; // MaxNumberOfTrades: For the Trading tab. 0 - no limit.
input int DefaultAllSymbols = true; // AllSymbols: For Trading tab, true - count trades in all symbols.
input bool DefaultAllSymbols = true; // AllSymbols: For Trading tab, true - count trades in all symbols.
input group "Miscellaneous"
input double TP_Multiplier = 1; // TP Multiplier for SL value, appears in Take-profit button.
input bool UseCommissionToSetTPDistance = false; // UseCommissionToSetTPDistance: For TP button.
Expand All @@ -104,7 +104,6 @@ input double AdditionalFunds = 0; // AdditionalFunds: Added to account balance f
input double CustomBalance = 0; // CustomBalance: Overrides AdditionalFunds value.
input bool SLDistanceInPoints = false; // SLDistanceInPoints: SL distance in points instead of a level.
input bool TPDistanceInPoints = false; // TPDistanceInPoints: TP distance in points instead of a level.
input bool ShowATROptions = false; // ShowATROptions: If true, SL and TP can be set via ATR.
input CANDLE_NUMBER ATRCandle = Current_Candle; // ATRCandle: Candle to get ATR value from.
input int TakeProfitsNumber = 1; // TakeProfitsNumber: More than 1 target to split trades.
input bool CalculateUnadjustedPositionSize = false; // CalculateUnadjustedPositionSize: Ignore broker's restrictions.
Expand All @@ -128,6 +127,7 @@ bool ShiftRequired;

int OnInit()
{
TickSize = -1;
CtrlRequired = false;
ShiftRequired = false;
MainKey = 0;
Expand Down Expand Up @@ -161,11 +161,11 @@ int OnInit()
if (prev_symbol != Symbol()) // Symbol changed.
{
// Reset everything.
TickSize = -1;
OutputPointValue = ""; OutputSwapsType = "Unknown"; SwapsTripleDay = "?";
OutputSwapsDailyLongLot = "?"; OutputSwapsDailyShortLot = "?"; OutputSwapsDailyLongPS = "?"; OutputSwapsDailyShortPS = "?";
OutputSwapsYearlyLongLot = "?"; OutputSwapsYearlyShortLot = "?"; OutputSwapsYearlyLongPS = "?"; OutputSwapsYearlyShortPS = "?";
OutputSwapsCurrencyDailyLot = ""; OutputSwapsCurrencyDailyPS = ""; OutputSwapsCurrencyYearlyLot = ""; OutputSwapsCurrencyYearlyPS = "";
SwapConversionSymbol = ""; MarginConversionPair = NULL; ProfitConversionPair = NULL;

// If you don't want to store panel values for the current symbol, check the Global Variable and delete the old settings file if symbol changed.
if (SymbolChangeReset)
Expand Down
42 changes: 30 additions & 12 deletions MQL5/Experts/Position Sizer/Position Sizer.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private:
string m_FileName;
double m_DPIScale;
bool NoPanelMaximization; // A crutch variable to prevent panel maximization when Maximize() is called at the indicator's initialization.
bool PosSizeEditing; // Prevent updating position size field while user is editing.
int ATR_handle;
int PanelWidth;

Expand Down Expand Up @@ -61,6 +62,7 @@ public:
virtual void CheckAndRestoreLines();
virtual void DummyObjectSelect(string dummy_name);
virtual bool IsMinimized() {return m_minimized;}
virtual void IniFileLoad() {CAppDialog::IniFileLoad(); InitObjects();} // Need to init objects after ini file load.

// Remember the panel's location to have the same location for minimized and maximized states.
int remember_top, remember_left;
Expand Down Expand Up @@ -106,6 +108,7 @@ private:
void OnEndEditEdtAccount();
void OnEndEditEdtRiskPIn();
void OnEndEditEdtRiskMIn();
void OnStartEdtPosSize();
void OnEndEditEdtPosSize();
void OnEndEditATRPeriod();
void OnEndEditATRMultiplierSL();
Expand Down Expand Up @@ -164,6 +167,7 @@ ON_EVENT(ON_END_EDIT, m_EdtCommissionSize, OnEndEditEdtCommissionSize)
ON_EVENT(ON_END_EDIT, m_EdtAccount, OnEndEditEdtAccount)
ON_EVENT(ON_END_EDIT, m_EdtRiskPIn, OnEndEditEdtRiskPIn)
ON_EVENT(ON_END_EDIT, m_EdtRiskMIn, OnEndEditEdtRiskMIn)
ON_EVENT(ON_START_EDIT, m_EdtPosSize, OnStartEdtPosSize)
ON_EVENT(ON_END_EDIT, m_EdtPosSize, OnEndEditEdtPosSize)
ON_EVENT(ON_END_EDIT, m_EdtATRPeriod, OnEndEditATRPeriod)
ON_EVENT(ON_END_EDIT, m_EdtATRMultiplierSL, OnEndEditATRMultiplierSL)
Expand Down Expand Up @@ -1459,7 +1463,7 @@ bool CPositionSizeCalculator::DisplayValues()
/* Risk/Reward 2 */ if (!m_EdtRR2.Text(OutputRR)) return false;
if (OutputRR == "Invalid TP") m_EdtRR2.Color(clrRed);
else m_EdtRR2.Color(m_EdtTP.Color());
/* Position size */ if (!m_EdtPosSize.Text(FormatDouble(DoubleToString(OutputPositionSize, LotStep_digits), LotStep_digits))) return false;
/* Position size */ if (!PosSizeEditing) if (!m_EdtPosSize.Text(FormatDouble(DoubleToString(OutputPositionSize, LotStep_digits), LotStep_digits))) return false;
if (OutputPositionSize > OutputMaxPositionSize)
{
m_EdtPosSize.Color(clrRed); // Calculated position size is greater than maximum position size by margin.
Expand Down Expand Up @@ -2287,12 +2291,15 @@ void CPositionSizeCalculator::ProcessTPChange(const bool tp_button_click)
{
if (!m_minimized)
{
m_LblRR.Show();
if (InputRR != "") m_EdtRR1.Show();
m_EdtRR2.Show();
m_LblReward.Show();
m_EdtReward1.Show();
m_EdtReward2.Show();
if (sets.SelectedTab == MainTab)
{
m_LblRR.Show();
if (InputRR != "") m_EdtRR1.Show();
m_EdtRR2.Show();
m_LblReward.Show();
m_EdtReward1.Show();
m_EdtReward2.Show();
}
}
if (sets.ShowLines)
{
Expand Down Expand Up @@ -3168,10 +3175,14 @@ void CPositionSizeCalculator::OnEndEditEdtRiskMIn()
}
}

void CPositionSizeCalculator::OnStartEdtPosSize()
{
PosSizeEditing = true;
}

void CPositionSizeCalculator::OnEndEditEdtPosSize()
{
sets.RiskFromPositionSize = true;

double d_val = StringToDouble(m_EdtPosSize.Text());
if (d_val >= 0)
{
Expand All @@ -3180,10 +3191,17 @@ void CPositionSizeCalculator::OnEndEditEdtPosSize()
OutputPositionSize = d_val;
sets.PositionSize = OutputPositionSize;
CalculateRiskAndPositionSize();
PosSizeEditing = false;
DisplayValues();
}
else sets.RiskFromPositionSize = false;
}
else m_EdtPosSize.Text(FormatDouble(DoubleToString(OutputPositionSize, LotStep_digits), LotStep_digits));
else
{
m_EdtPosSize.Text(FormatDouble(DoubleToString(OutputPositionSize, LotStep_digits), LotStep_digits));
PosSizeEditing = false;
}

}

void CPositionSizeCalculator::OnEndEditATRPeriod()
Expand Down Expand Up @@ -3588,7 +3606,7 @@ bool CPositionSizeCalculator::SaveSettingsOnDisk()
FileWrite(fh, IntegerToString(DefaultMagicNumber));
FileWrite(fh, "Parameter_DefaultCommentary");
FileWrite(fh, DefaultCommentary);
FileWrite(fh, "Parameter_DefaultAskForConfirmation");
FileWrite(fh, "Parameter_DefaultCommentAutoSuffix");
FileWrite(fh, IntegerToString(DefaultCommentAutoSuffix));
FileWrite(fh, "Parameter_DefaultDisableTradingWhenLinesAreHidden");
FileWrite(fh, IntegerToString(DefaultDisableTradingWhenLinesAreHidden));
Expand Down Expand Up @@ -3980,9 +3998,9 @@ bool CPositionSizeCalculator::LoadSettingsFromDisk()
}
else if (var_name == "Parameter_DefaultMaxNumberOfTrades")
{
if (StringToInteger(var_content) != DefaultBreakEven) sets.MaxNumberOfTrades = DefaultMaxNumberOfTrades;
if (StringToInteger(var_content) != DefaultMaxNumberOfTrades) sets.MaxNumberOfTrades = DefaultMaxNumberOfTrades;
}
else if (var_name == "Parameter_DefaultDefaultAllSymbols")
else if (var_name == "Parameter_DefaultAllSymbols")
{
if ((bool)StringToInteger(var_content) != DefaultAllSymbols) sets.AllSymbols = DefaultAllSymbols;
}
Expand Down

0 comments on commit 7ff4ae2

Please sign in to comment.