I want to create a function that is repeatedly called by a signal on the same bar, but nonetheless only outputs data once per bar. For example, if the signal calls the function 9 times on a bar, it needs to do a certain action only once, and not nine times.
This is my example signal:
Code: Select all
[IntrabarOrderGeneration = true];
once cleardebug;
if (BarStatus(1) = 2) then begin
MyFunction(200, "Only on bar close tick");
end;
MyFunction(200, "Every tick");
So this is my example function:
Code: Select all
Inputs:
BarsToPrint(NumericSimple),
TextToPrint(StringSimple);
Variables:
printText(""),
IntraBarPersist PrevBarPrinted(0),
startBar(Symbol_Length - BarsToPrint),
bar_number(0);
bar_number = CurrentBar + MaxBarsBack;
if (bar_number < startBar) then
#return;
// Only on bar close and only if bar_number is different from PrevBarPrinted
if (BarStatus(1) = 2) and (bar_number <> PrevBarPrinted) then begin
printText = Text("BarClose: '", TextToPrint,
"' bar_number: ", NumToStr(bar_number, 0),
", prevBarPrinted: ", NumToStr(PrevBarPrinted, 0));
PrevBarPrinted = bar_number;
end else begin
printText = Text(Spaces(3), TextToPrint);
end;
Print(printText);
Code: Select all
BarClose: 'Only on bar close tick' bar_number: 5289, prevBarPrinted: 5288
BarClose: 'Every tick' bar_number: 5289, prevBarPrinted: 5288
Every tick
Every tick
Every tick
Every tick
In other words, the `bar_number: 5289, prevBarPrinted: 5288` output is only allowed to be printed once per bar. After all, if `PrevBarPrinted` is assigned the value of `bar_number`, this can only be true once (and not twice) per bar.
So the problem is likely to be located here:
Code: Select all
if (BarStatus(1) = 2) and (bar_number <> PrevBarPrinted) then begin
...
PrevBarPrinted = bar_number;
end
Code: Select all
BarClose: 'Only on bar close tick' bar_number: 5289, prevBarPrinted: 5288
Every tick
Every tick
Every tick
Every tick
Notes:
* Using `IntraBarPersist` or not did not affect this behaviour.
* I'm using MultiCharts64 Version 8.8 Release (Build 8967).
* The function is a Numeric Simple function.