Run code Once Per new bar
Run code Once Per new bar
04 Mar 2024, 12:25
I am failing to understand why nothing I do can make code only run once , and then wait for a new bar to run again . I have searched this forum for an answer but found no answer .
Here are two methods I found on this forum and they both don't work .
[Indicator(AccessRights = AccessRights.None)]
public class Testing1 : Indicator
{
[Parameter(DefaultValue = "Hello world!")]
public string Message { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
int lastIndex;
protected override void Initialize()
{
lastIndex = -1;
}
public override void Calculate(int index)
{
if (index != lastIndex)
{
OnBar( index);
lastIndex = index;
}
}
private void OnBar(int index)
{
Print(" print only once at a new bar " + index);
}
}
[Indicator(AccessRights = AccessRights.None)]
public class Testing2 : Indicator
{
[Parameter(DefaultValue = "Hello world!")]
public string Message { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
protected override void Initialize()
{
}
public override void Calculate(int index)
{
if (!IsLastBar)
Print(" print only once at a new bar at the moment" + index);
}
}
These both produce these results in the log :
Cleary running at every tick instead of the once per bar . Here its running on the 1 chart meaning its supposed to print once and then print again after an Hour
Replies
mihlali700
05 Mar 2024, 07:06
RE: Run code Once Per new bar
firemyst said:
It's not running on every tick; it's running every bar. The index you're printing confirms it.
When you first load a chart in cTrader, it loads previous bars, and runs through the calculations on those, and that's exactly what's happening here, unless it gets to the latest bar.
Sort the log by the date/time DESCENDING instead of ASCENDING and you'll see with the most recent data it only executes once per bar.
So its going through the entire index once per bar ?
What if I just want data from the latest index and only starting from that index ? Like I want to print the value of the indicator but only for the latest bar
@mihlali700
firemyst
05 Mar 2024, 07:54
RE: RE: Run code Once Per new bar
mihlali700 said:
firemyst said:
It's not running on every tick; it's running every bar. The index you're printing confirms it.
When you first load a chart in cTrader, it loads previous bars, and runs through the calculations on those, and that's exactly what's happening here, unless it gets to the latest bar.
Sort the log by the date/time DESCENDING instead of ASCENDING and you'll see with the most recent data it only executes once per bar.
So its going through the entire index once per bar ?
What if I just want data from the latest index and only starting from that index ? Like I want to print the value of the indicator but only for the latest bar
No, it's not going through all the indexes once per bar. As I said, when you first start cTrader, it loads up all the previous bars because it has to. Otherwise,e there's no way to display previous chart data is there?
If you only want data from the latest index going forward, then in your Initialize method, get the latest index:
int latestIndex = Bars.OpenTimes.Count - 1
Then in your calculate method check if index ≥ latestIndex before doing anything.
@firemyst
firemyst
05 Mar 2024, 03:11 ( Updated at: 05 Mar 2024, 06:44 )
It's not running on every tick; it's running every bar. The index you're printing confirms it.
When you first load a chart in cTrader, it loads previous bars, and runs through the calculations on those, and that's exactly what's happening here, unless it gets to the latest bar.
Sort the log by the date/time DESCENDING instead of ASCENDING and you'll see with the most recent data it only executes once per bar.
@firemyst