4.6.6 Calculation per Bar

From MultiCharts
Jump to navigation Jump to search

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)]:
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:

  • Bars.OpenNextBar() or Bars.Open[-1],
  • 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:
[CalcAtOpenNextBar(true)]
public class TestSignal : SignalObject {

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

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:

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
}
BB

If in the signal it is necessary to react at every price change within the bar, then Intrabar Order Generation (IOG) mode can be enabled for the signal with [IOGMode(IOGMode.Enabled)] the attribute or through the Format Signal dialog window at the Properties tab:

For the signal in IOG mode, the CalcBar() method will be called upon for every tick received for the symbol on the chart in real-time or at Open, High, Low, Close of each bar at historic calculation. If Bar Magnifier mode is enabled for the strategy, then CalcBar() will be called upon for each detailed tick. In IOG mode limits for the filling of orders can be specified for the signal:

  • Limit each order command in this signal to one entry and one exit per bar, the strategy can fill each signal order once per bar.
  • Limit this signal to one entry and one exit per bar, the strategy can fill only one entry order and one exit from this signal on the bar.
  • Allow unlimited entries and exits per bar, the strategy can execute each signal order unlimited number of times on the bar.

Information on whether one or several strategy signals are calculated in IOG mode, can be obtained through the Environment.IOGEnabled property (true – IOG mode is enabled).