Hey Enivid
Massive thanks for your great Position Size Calculator! It's an outstanding tool. I've studied it very keenly while also reading articles on MQL5.com and familiarizing myself with the MQL GUI-Library so that I can learn how to make my own GUI. Right now, I am adapting your GUI to my own personal needs, moving buttons and labels around, resizing and arranging everything, adding preset-SL-buttons, buy and sell buttons etc.
While going through your code, I may have stumbled upon an error. I'm refering to the MT4 version "PositionSizeCalculator.mq4".
In OnInit, I believe you've written the following code to prevent the attachment of a duplicate indicator, so that there won't be two instances of PositionSizeCalculator on the same chart:
This works to prevent two separate panels from being created during timeframe changes, but the code under the "else" statement doesn't work to prevent a second indicator named PositionSizeCalculator from being attached to the chart. When the user attempts to add a second instance of PSC to the chart:
Let me know if my analysis is correct. I've tried it on my terminal and was able to attach two PSC indicators on the same chart, which obviously produces errors.
In any case, keep up the outstanding work!! I've learned a lot from studying your code.
heggna
Massive thanks for your great Position Size Calculator! It's an outstanding tool. I've studied it very keenly while also reading articles on MQL5.com and familiarizing myself with the MQL GUI-Library so that I can learn how to make my own GUI. Right now, I am adapting your GUI to my own personal needs, moving buttons and labels around, resizing and arranging everything, adding preset-SL-buttons, buy and sell buttons etc.
While going through your code, I may have stumbled upon an error. I'm refering to the MT4 version "PositionSizeCalculator.mq4".
In OnInit, I believe you've written the following code to prevent the attachment of a duplicate indicator, so that there won't be two instances of PositionSizeCalculator on the same chart:
MQL4:
// Prevent attachment of second panel if it is not a timeframe/parameters change. if (GlobalVariableGet("PSC-" + IntegerToString(ChartID()) + "-Flag") > 0) GlobalVariableDel("PSC-" + IntegerToString(ChartID()) + "-Flag"); else { int indicators_total = ChartIndicatorsTotal(0, 0); for (int i = 0; i < indicators_total; i++) { if (ChartIndicatorName(0, 0, i) == "Position Size Calculator") { Print("Position Size Calculator is already attached."); return(INIT_FAILED); } } }
This works to prevent two separate panels from being created during timeframe changes, but the code under the "else" statement doesn't work to prevent a second indicator named PositionSizeCalculator from being attached to the chart. When the user attempts to add a second instance of PSC to the chart:
- The code if (ChartIndicatorName(0, 0, i) == "Position Size Calculator") will return false because you've changed the short-name of the first indicator to "Position Size Calculator"+ChartID().
- The above method of checking whether the same indicator is already running is incorrect: When you add an indicator to the chart, it is included in the list of indicators, even if initialization has not been completed. The above code would fail even if there were no indicators on the chart and you tried to add the first instance of PSC, because ChartIndicatorName(0, 0, i) will return PSC name, since PSC has already been added to the list of indicators. If you add a second indicator, ChartIndicatorsTotal will count a total of 2 indicators even before OnInit is done executing, and so on.
MQL4:
//Check if there are 2 instances of the indicator. If there are, the 2nd instance of the indicator fails its initialization and exits from the chart. int totalIndicatorsOnChart = ChartIndicatorsTotal(0,0); int instances = 0; for (int i = 0; i < totalIndicatorsOnChart; i++) { //Iterate through all indicators, and if you happen upon an instance of the indicator, increment "instances" by 1. if (ChartIndicatorName(0,0,i) == "Indicator_Name") instances++; } if (instances >= 2) return(INIT_FAILED);
Let me know if my analysis is correct. I've tried it on my terminal and was able to attach two PSC indicators on the same chart, which obviously produces errors.
In any case, keep up the outstanding work!! I've learned a lot from studying your code.
heggna
Last edited: