How to implement “open next bar” in MC.NET?  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
simonred
Posts: 1
Joined: 17 Apr 2013
Has thanked: 1 time

How to implement “open next bar” in MC.NET?

Postby simonred » 17 Apr 2013

Hello all,
I would like to port my EL code to MC.net, But one EL statement like this:
Buy at next bar Open of next bar + 15 Stop;

What would the equivalent code be in PowerLanguage .NET?
m_ShortOrder.Send(Bars.Open[-1]) can work?

I saw on MC Blog describing MC.NET 8.5 new feature:
Added new calculation methods that are equivalent to “date next bar”, “time next bar” or “open next bar” in PowerLanguage. More info here viewtopic.php?f=19&t=11506&p=56689#p56689.
Updated Trendline_LE and Trendline_SE signals according to new “next bar” capabilities.

We can use "Bars.Open[-1]" to get the open next bar ?

Thanks

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

Re: How to implement “open next bar” in MC.NET?  [SOLVED]

Postby Henry MultiСharts » 17 Apr 2013

Hello simonred,

Historic calculation per bar can be executed in one of the two modes:

1. Calculation upon Close of each bar and filling of the generated orders collection on the next bar. This is the default mode.

2) Calculation upon Open of each bar in the context of the previous bar and filling of the generated orders collection on this bar. This mode is enabled by the signal class attribute [CalcAtOpenNextBarAttribute(true)]:

Code: Select all

namespace PowerLanguage.Strategy {
[CalcAtOpenNextBarAttribute(true)]
public class Test : SignalObject {
public Test(object _ctx):base(_ctx){}
private IOrderMarket buy_order;
……………….
}
}
Open and Time values for the next bar become available through the following functions:

Code: Select all

Bars.OpenNextBar()or Bars.Open[-1], and Bars.TimeNextBar() or Bars.Time[-1].
But the calculation is still executed in the context of the current bar close (for example, Bars.Close[0] – will still return Close of the current bar, despite the calculation being executed at the Open of the next bar).

Example:

Code: Select all

[CalcAtOpenNextBar(true)]
public class TestSignal : SignalObject {

Now, Open of the opened bar is available using the index [-1] or Bars.OpenNextBar():

Code: Select all

double o_next_bar = Bars.Open[-1]; or double o_next_bar = Bars.OpenNextBar();
Using index [0] the information upon the closed bar is still available.

Now, the following trading strategy can be implemented: to buy if opened with the gap down and sell if opened with the gap up:

Code: Select all

protected override void CalcBar(){
double o_next_bar = Bars.Open[-1];//Open of the opened bar
double h = Bars.High[0];//High of the closed calculation bar
double l = Bars.Low[0]; //Low of the closed calculation bar
if (o_next_bar > h) sellshort_order.Send();//Gap up - sell
if (o_next_bar < l) buy_order.Send();//Gap down – buy
}


Return to “MultiCharts .NET”