Once..begin sometimes triggered twice  [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:

Once..begin sometimes triggered twice

Postby JoshM » 08 Jul 2012

Using MultiCharts Version 8.0 Release (Build 5620) I noticed that the once begin statement sometimes is triggered twice. For example, the following indicator:

Code: Select all

once begin

Print(FormatTime("HH:mm:ss", ComputerDateTime),
" DateTime of bar: ", FormatDate("dd-MM-yy", ELDateToDateTime(Date)),
FormatTime(" HH:mm:ss ", ELTimeToDateTime_s(Time_s)),
" BarStatus: ", BarStatus(1),
" BarNumber: ", CurrentBar + MaxBarsBack - 1);

end;
Gives:

Code: Select all

21:09:32 DateTime of bar: 03-10-11 08:00:04 BarStatus: 2.00 BarNumber: 1.00
(Which is correct). Now add a Plot() statement to the code:

Code: Select all

once begin

Print(FormatTime("HH:mm:ss", ComputerDateTime),
" DateTime of bar: ", FormatDate("dd-MM-yy", ELDateToDateTime(Date)),
FormatTime(" HH:mm:ss ", ELTimeToDateTime_s(Time_s)),
" BarStatus: ", BarStatus(1),
" BarNumber: ", CurrentBar + MaxBarsBack - 1);

end;


Plot1(XAverage(Close, 20), "EMA");
The output now becomes:

Code: Select all

21:10:25 DateTime of bar: 03-10-11 08:00:04 BarStatus: 2.00 BarNumber: 1.00
21:10:25 DateTime of bar: 03-10-11 08:00:30 BarStatus: 2.00 BarNumber: 6.00
How come the once..begin statement is triggered twice now? Who knows what I'm missing?

Workaround attempt #1
Using a "CurrentBar = 1" check in combination with once still gives twice the output:

Code: Select all

once (CurrentBar = 1) begin

Print(FormatTime("HH:mm:ss", ComputerDateTime),
" DateTime of bar: ", FormatDate("dd-MM-yy", ELDateToDateTime(Date)),
FormatTime(" HH:mm:ss ", ELTimeToDateTime_s(Time_s)),
" BarStatus: ", BarStatus(1),
" BarNumber: ", CurrentBar + MaxBarsBack - 1);

end;


Plot1(XAverage(Close, 20), "EMA");

Code: Select all

21:15:30 DateTime of bar: 03-10-11 08:00:04 BarStatus: 2.00 BarNumber: 1.00
21:15:30 DateTime of bar: 03-10-11 08:00:30 BarStatus: 2.00 BarNumber: 6.00
Workaround attempt #2
Using a variable to keep track of whether or not the code segment is already run also doesn't work:

Code: Select all

Variables:
IntraBarPersist OnceBeginTriggered(False);

if (OnceBeginTriggered = False) then begin

Print(FormatTime("HH:mm:ss", ComputerDateTime),
" DateTime of bar: ", FormatDate("dd-MM-yy", ELDateToDateTime(Date)),
FormatTime(" HH:mm:ss ", ELTimeToDateTime_s(Time_s)),
" BarStatus: ", BarStatus(1),
" BarNumber: ", CurrentBar + MaxBarsBack - 1);

OnceBeginTriggered = True;

end;

Plot1(XAverage(Close, 20), "EMA");
Gives:

Code: Select all

21:19:17 DateTime of bar: 03-10-11 08:00:04 BarStatus: 2.00 BarNumber: 1.00
21:19:17 DateTime of bar: 03-10-11 08:00:30 BarStatus: 2.00 BarNumber: 6.00

Dru
Posts: 107
Joined: 28 Aug 2007
Has thanked: 4 times
Been thanked: 171 times

Re: Once..begin sometimes triggered twice

Postby Dru » 10 Jul 2012

It is auto maximum number of bars back calculations ... Set up MaxBarsStudyWillReference manually to 20 and look on results.
The algorithm's work is looks like:
1) Set MaxBB = 0. Calculate. Detect that we need 1 BB. Break (Status Off).
2) Set MaxBB = 6. Calculate. Detect that we need *** BB. Break (Status Off).
3) Set MaxBB = *** + N. Calculate. Ok.

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

Re: Once..begin sometimes triggered twice

Postby JoshM » 10 Jul 2012

It is auto maximum number of bars back calculations ... Set up MaxBarsStudyWillReference manually to 20 and look on results.
The algorithm's work is looks like:
1) Set MaxBB = 0. Calculate. Detect that we need 1 BB. Break (Status Off).
2) Set MaxBB = 6. Calculate. Detect that we need *** BB. Break (Status Off).
3) Set MaxBB = *** + N. Calculate. Ok.
This is very insightful, thanks Dru.

You're right; as long as the SetMaxBarsBack > 1, the once begin is triggered once instead of twice. Apparently, the number of bars back needs to be more than one, irrespective of the amount of bars that are actually referenced by the indicator (20 in this example).

For example:

Code: Select all

SetMaxBarsBack(2);

once begin

Print(FormatTime("HH:mm:ss", ComputerDateTime),
" DateTime of bar: ", FormatDate("dd-MM-yy", ELDateToDateTime(Date)),
FormatTime(" HH:mm:ss ", ELTimeToDateTime_s(Time_s)),
" BarStatus: ", BarStatus(1),
" BarNumber: ", CurrentBar + MaxBarsBack);


end;

Plot1(XAverage(Close, 20), "EMA");
Gives

Code: Select all

15:27:37 DateTime of bar: 01-01-12 23:09:00 BarStatus: 2.00 BarNumber: 3.00
However, I still think it would be a good idea if MultiCharts reconsiders Once-Begin, since it "doesn't work as expected" (that is, from my standpoint).

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: Once..begin sometimes triggered twice

Postby arjfca » 13 Nov 2012

It is auto maximum number of bars back calculations ... Set up MaxBarsStudyWillReference manually to 20 and look on results.
The algorithm's work is looks like:
1) Set MaxBB = 0. Calculate. Detect that we need 1 BB. Break (Status Off).
2) Set MaxBB = 6. Calculate. Detect that we need *** BB. Break (Status Off).
3) Set MaxBB = *** + N. Calculate. Ok.
Hello Dru / JoshM

Strange, the SetMaxBarsBack(2); does'nt work for me. Is is worst when I use this variable.

The "Once Begin" is triggered many many when I use it. Meaning, the "Once begin" is not seen and all code is run on nearly every pass. When not set, "Once begin" is triggered twice only

Martin

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Once Begin: Executed twice... Any reason

Postby arjfca » 14 Nov 2012

Hello

Any reason why Once Begin codes lines are repeated twice?

I got some initialization code that I have put inside a

Code: Select all

Once Begin
...
...
end;
I see that user JoshM as issued a similar post about the subject and
user DRU as issued a solution by seting the

SetBarsMaxBack (2) >1 to resolve the problem

viewtopic.php?f=1&t=10592&hilit=+once+begin+#p51935

But the solution is not working in my case.

I even try to put my code inside If barnumber = 1 then ... do the initialization
Even with that condition, initialization code line are repeated

Is there a solution or MC is aware of the problem and will give a corrective later?

Martin

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Once..begin sometimes triggered twice

Postby Henry MultiСharts » 14 Nov 2012

It is auto maximum number of bars back calculations ... Set up MaxBarsStudyWillReference manually to 20 and look on results.
The algorithm's work is looks like:
1) Set MaxBB = 0. Calculate. Detect that we need 1 BB. Break (Status Off).
2) Set MaxBB = 6. Calculate. Detect that we need *** BB. Break (Status Off).
3) Set MaxBB = *** + N. Calculate. Ok.
Hello Dru / JoshM

Strange, the SetMaxBarsBack(2); does'nt work for me. Is is worst when I use this variable.

The "Once Begin" is triggered many many when I use it. Meaning, the "Once begin" is not seen and all code is run on nearly every pass. When not set, "Once begin" is triggered twice only

Martin
Hello Martin,

Probably your code requires more than two bars for calculation.
Please try to increase SetMaxBarsBack value and post your sample code here if you still have the issue.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Once Begin: Executed twice... Any reason  [SOLVED]

Postby Henry MultiСharts » 14 Nov 2012

Hello

Any reason why Once Begin codes lines are repeated twice?

I got some initialization code that I have put inside a

Code: Select all

Once Begin
...
...
end;
I see that user JoshM as issued a similar post about the subject and
user DRU as issued a solution by seting the

SetBarsMaxBack (2) >1 to resolve the problem

viewtopic.php?f=1&t=10592&hilit=+once+begin+#p51935

But the solution is not working in my case.

I even try to put my code inside If barnumber = 1 then ... do the initialization
Even with that condition, initialization code line are repeated

Is there a solution or MC is aware of the problem and will give a corrective later?

Martin

Hello Martin,

Probably your code requires more than two bars for calculation.
Please try to increase SetMaxBarsBack value and post your sample code here if you still have the issue.

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: Once Begin: Executed twice... Any reason

Postby arjfca » 14 Nov 2012

Hello Martin,

Probably your code requires more than two bars for calculation.
Please try to increase SetMaxBarsBack value and post your sample code here if you still have the issue.
Hello Henry

Result for setBarMaxBack: My code may be difficult to reproduce since I do have special function and dll include in it. The attached picture is for setBarMaxBack(51):

// setBarMaxBack not set: Two set of line code is printed

setBarMaxBack(2): Printed output line is multiple. I did not count but I suspect more than 10 set of line code is printed

setBarMaxBack(51):Printed two set of code line.

setBarMaxBack(100): Same result as with (51), two set of code line are printed.

Martin

Code: Select all



Once begin
Cbarnumber = Barnumber;
ScaleValue = ScaleValue_Calc(Barinterval);
scale = scaleString;
NameInstrument = GetSymbolName;
BarCompleted = "";
Targettime = referenceTime + delay;
Targettime = Machinetime + delay;
referenceTime = machineTime; // Saving a reference of the time

//Get the name of the Excel File
Sstop = splitstring(GVGetNamedString("StartStopMC_GV","Error1"),1); // the path of the Excel file reside on the 2' place on the string
Sbook = splitstring(GVGetNamedString("StartStopMC_GV","Error2_SBook"),2); // the path of the Excel file reside on the 2' place on the string
Ssheet = splitstring(GVGetNamedString("StartStopMC_GV","Error3_Ssheet"),3); // the path of the Excel file reside on the 2' place on the string
Print (Sbook, " ", Ssheet);

//Initilized the New EleXcel.dll
XLStarted = ELXL_Initialize ;//Initialise the ELeXcel .DLL
WorksheetReady = ELXL_WithSheet(sbook,Ssheet) ;
If XlStarted = true and WorkSheetReady = True then XLstarted = true else Xlstarted = False;
If xlstarted then print ("XlStarted with scale: ", scale) else print ("ELEXcel not started");

SbookBack = Sbook;
//Print ("_Sstop: ", Sstop, " Sbook: ", Sbook, " Ssheet: ",Ssheet);

//Send to Excel the Scale and the name of the instrument


If UseData2 then begin

InitString = NameInstrument + comma + Scale + comma + GetSymbolName data2 + comma + scale data2;
end
else begin
InitString = NameInstrument + comma + Scale + comma +comma +comma + comma;
end;

//If Sbook <> SbookBack then print ("0 ", Sbook);
// Condition0 = XLO.TextA1(sBook,sSheet,Cell_,InitString); // Capture the scale
If XlStarted then begin
Condition0 = ELXL_SetCellString( InitStringCell , Initstring ) ;

end;

//If Condition0 = False then print ("Cond0_1 ", condition0);
gowithcode=false ;
// print ("Once:", sbook, " ", ssheet);
end; //End of initialisation
Attachments
Output.png
(20.74 KiB) Downloaded 1000 times

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Once Begin: Executed twice... Any reason

Postby Henry MultiСharts » 14 Nov 2012

Martin, please provide a sample code that can be compiled by all forum members and that will reproduce the problem. If you are using custom functions-please export the study with dependent functions. That is not possible to provide any insight on the code you have posted above.

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: Once Begin: Executed twice... Any reason

Postby arjfca » 14 Nov 2012

Martin, please provide a sample code that can be compiled by all forum members and that will reproduce the problem. If you are using custom functions-please export the study with dependent functions. That is not possible to provide any insight on the code you have posted above.
OK

I think I found a solution path idea

My workspace use two Instruments. When only one is used, the code inside the Once Begin /end ; is not repeated. I will study this deeper

Martin


Return to “MultiCharts”