how to use RecalcLastBarAfter  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
HPF
Posts: 47
Joined: 02 Apr 2013
Has thanked: 11 times
Been thanked: 7 times

how to use RecalcLastBarAfter

Postby HPF » 08 Aug 2013

Hi,

I have a question on how to properly use RecalcLastBarAfter. We have MultiCharts.NET 8.7 build 7410.

I have a study on a daily chart and would like CalcBar() to be called on today's bar at least once every 10 seconds even if there are no ticks in the meantime. When I use (with intra bar order generation) the following study

Code: Select all

namespace PowerLanguage.Strategy
{
public class Test : SignalObject
{
public Test (object _ctx) : base (_ctx) {}

protected override void Create() {}

protected override void StartCalc() {}

protected override void CalcBar()
{
if (Bars.LastBarOnChart)
{
Output.WriteLine ("last bar "+Bars.Time[0].ToString ("yyyy-MM-dd HH:mm:ss tt")+" received at "+DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss"));
}
else
Output.WriteLine ("other bar "+Bars.Time[0].ToString ("yyyy-MM-dd HH:mm:ss tt")+" received at "+DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss"));

ExecControl.RecalcLastBarAfter (new TimeSpan (0,0,10));
}
}
}
I get the last bar updated whenever the symbol receives a tick, but not every 10 seconds when there is no tick.

Am I using RecalcLastBarAfter properly? Is there any option that I forgot to activate for this to work? Any kind of help is most appreciated.

Once I have this for a study, I would also like to get it work for an indicator. The reason is that I have an indicator that depends on two instruments, and I do not know which one(s) receive the next tick. I therefore want to recalculate the indicator at least every 10 seconds because its value might change when instrument 2 receives a tick, even if instrument 1 does not.

HPF

Fabrice
Posts: 182
Joined: 14 Jun 2011
Has thanked: 44 times
Been thanked: 30 times

Re: how to use RecalcLastBarAfter

Postby Fabrice » 11 Aug 2013

I had the same puzzle. This is because

Code: Select all

ExecControl.RecalcLastBarAfter (new TimeSpan (0,0,10));
does not recalculate, contrary to what we could expect. It "only" calls the OnRecalcLastBarAfterEvent() method. Add this method to your code and it should work :

Code: Select all

protected override void OnRecalcLastBarAfterEvent() {
ExecControl.Recalculate();
}
Regards.

PS: I do not know if it works on a signal. I have only used this on indicators.

HPF
Posts: 47
Joined: 02 Apr 2013
Has thanked: 11 times
Been thanked: 7 times

Re: how to use RecalcLastBarAfter

Postby HPF » 12 Aug 2013

Thank you! It works. I just tried it with an indicator.

HPF

HPF
Posts: 47
Joined: 02 Apr 2013
Has thanked: 11 times
Been thanked: 7 times

Re: how to use RecalcLastBarAfter

Postby HPF » 13 Aug 2013

One further question. When I call ExecControl.Recalculate(), the entire indicator is recalculated, starting with StartCalc() and then calling CalcBar() for every bar of my chart.

It would be sufficient if MutliCharts called CalcBar() on the most recent bar only. Is there any way of doing this? I.e. to trigger after a fixed period of of time the same behaviour that happens when a new tick is received, just without changing o,h,l,c of the most recent bar, but simply repeating it.

Thanks for your help.

HPF

User avatar
Alex MultiCharts
Posts: 194
Joined: 09 Aug 2013
Has thanked: 43 times
Been thanked: 77 times

Re: how to use RecalcLastBarAfter  [SOLVED]

Postby Alex MultiCharts » 14 Aug 2013

Please find the script sample in the attached file, you can do it the way it is done there.
Attachments
Test_RecalcLastbarAfterExample.pln
(1.22 KiB) Downloaded 781 times

HPF
Posts: 47
Joined: 02 Apr 2013
Has thanked: 11 times
Been thanked: 7 times

Re: how to use RecalcLastBarAfter

Postby HPF » 15 Aug 2013

Very nice! Does exactly what I need, thanks!

The key to why this works is apparently that I can call CalcBar() myself as often as I want, and in the absence of any new ticks, its context (Bars.xyz) remains that of the last tick.

HPF


Return to “MultiCharts .NET”