Wierd NumToStr() behaviour [Solved]

Questions about MultiCharts and user contributed studies.
User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Wierd NumToStr() behaviour [Solved]

Postby JoshM » 03 Jul 2011

Edit 2: Problem solved, since the latest MC 7 Beta 5 does not seem to exhibit this problem.

Edit: See this post of this thread for a more clearer and shorter problem example.

-----------

I get the following output when formatting numbers with the ‘NumToStr(numeric variable, decimals)’ output. For some reason, NumToStr() does not display the last 4 variables it needs to print, but reuses the first four variables – I’ve highlighted these errors in yellow.

I’ve printed the same variables without NumToStr and once with the ‘:0:3’ formatting added to the variable. These do get printed correctly – underlined with blue.

Screenshot of problem.

I assume that, if there was an error in calculation of the variables, the other Print versions would also return some erroneous output. I’ve checked the calculated values with the Performance Report, and they are correct. I don't understand why NumToStr prints the first values correctly, but then reuses the first variables. As you can see in the two printouts following the NumToStr line, the variables do get printed when using something different than NumToStr.

What am I missing?

This NumToStr() behaviour has happened before, and since I've been able to replicate it using the newly written default code below, it might be a error worth further investigation (or I keep making the same errors. ;) ).

Complete code:

Code: Select all

Variables:
accountSize(InitialCapital), winLossRatio(0), winPercentage(0),
profit(0), loss(0), profitFactor(0), pessResult(0), prom(0), stdAfwTrades(0),
numOfTrades(0), nettoProfit(0), avgTrade(0),
numOfWins(0), numOfLosses(0), avgWin(0), avgLoss(0),
outputString(""), headerString(""), ws(";")
;

numOfTrades = TotalTrades;
nettoProfit = NetProfit;
profit = GrossProfit;
loss = GrossLoss;
numOfWins = NumWinTrades;
numOfLosses = NumLosTrades;

// Calculations
avgTrade = nettoProfit / numOfTrades;
avgWin = profit / numOfWins;
avgLoss = loss / numOfLosses;
winLossRatio = avgWin / absvalue(avgLoss);
profitFactor = profit / absvalue(loss);
pessResult = 0;
stdAfwTrades = 0;
prom = accountSize;
winPercentage = (numOfWins / numOfTrades) * 100;

{tmp}
ws = Spaces(4);

// Create string with strategy data
outputString = Text(
NumToStr(profit, 2), ws,
NumToStr(loss, 2), ws,
NumToStr(nettoProfit, 2), ws,
NumToStr(profitFactor, 4), ws,
NumToStr(pessResult, 2), ws,
NumToStr(prom, 3), ws,
NumToStr(avgWin, 3), ws,
NumToStr(avgLoss, 3), ws,
NumToStr(avgTrade, 3), ws,
NumToStr(stdAfwTrades, 4), ws,
NumToStr(winLossRatio, 4), ws,
NumToStr(numOfWins, 0), ws,
NumToStr(numOfLosses, 0), ws,
NumToStr(winPercentage, 3), ws
);

// Create header string
headerString = Text(
"GrossProfit", ws,
"GrossLoss", ws,
"NetProfit", ws,
"ProfitFactor", ws,
"PessimisticResult", ws,
"PROM", ws,
"AvgWin", ws,
"AvgLoss", ws,
"AvgTrade", ws,
"StdAvgTrade", ws,
"WinLossRatio", ws,
"WinningTrades", ws,
"LosingTrades", ws,
"WinPercentage", ws
);

Print(headerString, NewLine, outputString);

// Removing the NumToStr from the output string variable

// Create string with strategy data
outputString = Text(
profit, ws,
loss, ws,
nettoProfit, ws,
profitFactor, ws,
pessResult, ws,
prom, ws,
avgWin, ws,
avgLoss, ws,
avgTrade, ws,
stdAfwTrades, ws,
winLossRatio, ws,
numOfWins, ws,
numOfLosses, ws,
winPercentage, ws
);

Print(NewLine, " Now without NumToStr:");

Print(headerString, NewLine, outputString);

// Now with rounding through :0:3 etc.
outputString = Text(
profit:0:2, ws,
loss:0:2, ws,
nettoProfit:0:2, ws,
profitFactor:0:4, ws,
pessResult:0:2, ws,
prom:0:4, ws,
avgWin:0:4, ws,
avgLoss:0:4, ws,
avgTrade:0:4, ws,
stdAfwTrades:0:4, ws,
winLossRatio:0:4, ws,
numOfWins:0:0, ws,
numOfLosses:0:0, ws,
winPercentage:0:0, ws
);

Print(NewLine, " Now with formatting through :0:3 etc.");

Print(headerString, NewLine, outputString);
Just to add: I call the above code with the following statement:

Code: Select all

if LastBarOnChart_s = true then begin
MyTestFunction1();
end;
NumToStr_Behavior.PNG
(20.98 KiB) Downloaded 464 times
Last edited by JoshM on 04 Jul 2011, edited 3 times in total.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Wierd NumToStr() behaviour

Postby TJ » 03 Jul 2011

...
What am I missing?

...
I am weaving through the code and the descriptions and the lines of prints...

can you condense the "problem" to the specifics?

ie remove all the non-issue codes and post only the area that causes the "problem/error"?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Wierd NumToStr() behaviour

Postby JoshM » 03 Jul 2011

...
What am I missing?

...
I am weaving through the code and the descriptions and the lines of prints...

can you condense the "problem" to the specifics?

ie remove all the non-issue codes and post only the area that causes the "problem/error"?
Thanks TJ, good point.

I've tried to reduce the problem to specifics, but the behaviour of NumToStr() only happens starting at the 11th printed value, so it's still some code.

Code: Select all

// Variables
vars: myString("");

value1 = 1.023434;
value2 = 2.0345656;
value3 = 3.0098745;
value4 = 4.0000000000003;
value5 = 5.09876324;
value6 = 6.00324324;
value7 = 7.003423423432;
value8 = 8.0032434;
value9 = 9.0030003434;
value10 = 10.034234;
value11 = 11.032432423;
value12 = 12.0345454;
value13 = 13.0000324324;
value14 = 14.00000000000005;

// Print using NumToStr
myString = Text(
NumToStr(value1, 2), NewLine,
NumToStr(value2, 2), NewLine,
NumToStr(value3, 2), NewLine,
NumToStr(value4, 2), NewLine,
NumToStr(value5, 2), NewLine,
NumToStr(value6, 2), NewLine,
NumToStr(value7, 2), NewLine,
NumToStr(value8, 2), NewLine,
NumToStr(value9, 2), NewLine,
NumToStr(value10, 2), NewLine,
NumToStr(value11, 2), NewLine,
NumToStr(value12, 2), NewLine,
NumToStr(value13, 2), NewLine,
NumToStr(value14, 2), NewLine
);

Print(myString);

// Print using the default way
myString = Text(
value1, NewLine,
value2, NewLine,
value3, NewLine,
value4, NewLine,
value5, NewLine,
value6, NewLine,
value7, NewLine,
value8, NewLine,
value9, NewLine,
value10, NewLine,
value11, NewLine,
value12, NewLine,
value13, NewLine,
value14, NewLine
);

Print(NewLine);
Print(myString);
This gives the following output:

Code: Select all

1.02
2.03
3.01
4.00
5.10
6.00
7.00
8.00
9.00
10.03
1.02
2.03
3.01
4.00



1.02
2.03
3.01
4.00
5.10
6.00
7.00
8.00
9.00
10.03
11.03
12.03
13.00
14.00
As you can see, as soon as the NumToStr() reaches the eleventh value, it returns to the first variable, while the default way of printing continues till value 14.

Regards,
Josh

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Wierd NumToStr() behaviour

Postby TJ » 03 Jul 2011

...
What am I missing?

...
I am weaving through the code and the descriptions and the lines of prints...

can you condense the "problem" to the specifics?

ie remove all the non-issue codes and post only the area that causes the "problem/error"?
Thanks TJ, good point.

I've tried to reduce the problem to specifics, but the behaviour of NumToStr() only happens starting at the 11th printed value, so it's still some code.

Code: Select all

// Variables
vars: myString("");

value1 = 1.023434;
value2 = 2.0345656;
value3 = 3.0098745;
value4 = 4.0000000000003;
value5 = 5.09876324;
value6 = 6.00324324;
value7 = 7.003423423432;
value8 = 8.0032434;
value9 = 9.0030003434;
value10 = 10.034234;
value11 = 11.032432423;
value12 = 12.0345454;
value13 = 13.0000324324;
value14 = 14.00000000000005;

// Print using NumToStr
myString = Text(
NumToStr(value1, 2), NewLine,
NumToStr(value2, 2), NewLine,
NumToStr(value3, 2), NewLine,
NumToStr(value4, 2), NewLine,
NumToStr(value5, 2), NewLine,
NumToStr(value6, 2), NewLine,
NumToStr(value7, 2), NewLine,
NumToStr(value8, 2), NewLine,
NumToStr(value9, 2), NewLine,
NumToStr(value10, 2), NewLine,
NumToStr(value11, 2), NewLine,
NumToStr(value12, 2), NewLine,
NumToStr(value13, 2), NewLine,
NumToStr(value14, 2), NewLine
);

Print(myString);

// Print using the default way
myString = Text(
value1, NewLine,
value2, NewLine,
value3, NewLine,
value4, NewLine,
value5, NewLine,
value6, NewLine,
value7, NewLine,
value8, NewLine,
value9, NewLine,
value10, NewLine,
value11, NewLine,
value12, NewLine,
value13, NewLine,
value14, NewLine
);

Print(NewLine);
Print(myString);
This gives the following output:

Code: Select all

1.02
2.03
3.01
4.00
5.10
6.00
7.00
8.00
9.00
10.03
1.02
2.03
3.01
4.00



1.02
2.03
3.01
4.00
5.10
6.00
7.00
8.00
9.00
10.03
11.03
12.03
13.00
14.00
As you can see, as soon as the NumToStr() reaches the eleventh value, it returns to the first variable, while the default way of printing continues till value 14.

Regards,
Josh
This is the print out I have from your code.
ps. this is simply a print out after I applied the code to the chart, there is no live data at the moment.

MultiCharts Version 7.0 Beta 5 (Build 4456)

Code: Select all

1.02
2.03
3.01
4.00
5.10
6.00
7.00
8.00
9.00
10.03
11.03
12.03
13.00
14.00



1.02
2.03
3.01
4.00
5.10
6.00
7.00
8.00
9.00
10.03
11.03
12.03
13.00
14.00

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Wierd NumToStr() behaviour

Postby JoshM » 04 Jul 2011

This is the print out I have from your code.
ps. this is simply a print out after I applied the code to the chart, there is no live data at the moment.

MultiCharts Version 7.0 Beta 5 (Build 4456)

Code: Select all

1.02
2.03
3.01
4.00
5.10
6.00
7.00
8.00
9.00
10.03
11.03
12.03
13.00
14.00



1.02
2.03
3.01
4.00
5.10
6.00
7.00
8.00
9.00
10.03
11.03
12.03
13.00
14.00
Thanks for testing this. I've also applied it to historical data, but I'm not running MC 7.0 Beta 5. So this seems already solved in the latest beta. :)

Thanks for your help TJ!

Regards,
Josh


Return to “MultiCharts”