order to execute code in optimization

Questions about MultiCharts and user contributed studies.
turbofib
Posts: 213
Joined: 11 May 2013
Has thanked: 67 times
Been thanked: 1 time

order to execute code in optimization

Postby turbofib » 14 Aug 2023

hi, i try to execute this simple code (it's a function called in signal)
var:index(0);

once ( getappinfo(aioptimizing) = 1 ) begin
index=Random(4);
print(" _ ",index);

end;

if LastBarOnChart then begin
print("**",index);
end;
output:

_ 3.71
_ 3.82
_ 1.60
** 3.82
** 3.71
** 1.60

i dont' understand the order execution during optimization


I thought the code ran in linear order like this:
(first loop)
_ 3.71
** 3.71

(second loop)
_ 3.82
** 3.82

(third loop)
_ 1.60
** 1.60


but i see an order casual to execution...why?

User avatar
Polly MultiCharts
Posts: 203
Joined: 20 Jul 2022
Has thanked: 1 time
Been thanked: 56 times

Re: order to execute code in optimization

Postby Polly MultiCharts » 15 Aug 2023

Hello turbofib,

During optimization, several sets of inputs are calculated in parallel in different threads. Therefore, it might be possible that the end of the calculation is not in the same order as the start.
If you’d like to distinguish optimization instances, you need to additionally print out the input on which the optimization takes place. Then you will be able to check if the calculation process is carried out as you expect inside one instance.
For example, if you optimize for I1 you need to add print(I1, " _ ", index); and print(I1, "**",index);

User avatar
Polly MultiCharts
Posts: 203
Joined: 20 Jul 2022
Has thanked: 1 time
Been thanked: 56 times

Re: order to execute code in optimization

Postby Polly MultiCharts » 18 Aug 2023

If you want to avoid parallel calculations, you can enable using only 1 thread when optimizing in the registry:
[HKEY_CURRENT_USER\Software\TS Support\Power Language\StudyRunner]
"NumberOfThreadsOnOptimization"=dword:00000001


Return to “MultiCharts”