Call of AverageTrueRange restarts the strategy  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
Posts: 6
Joined: 16 Jan 2014
Has thanked: 3 times

Call of AverageTrueRange restarts the strategy

Postby uniquepito » 10 Jul 2014

I've noticed strange behavior when calling AverageTrueRange during CalcBar() - it restarts the strategy and StartCalc() is called twice. This causes that strategy does not work properly since I'm initailizating some values in StartCalc().
Use any currency pair on LMAX - Forex.
Subchart #1 interval is 1 hour, with range 60 days back.
Subchart #2 interval is 1 day, with range 60 days back.

Heres's the code:

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;

namespace PowerLanguage.Strategy {
public class TestOnly : SignalObject {
public TestOnly(object _ctx):base(_ctx){}
private IOrderMarket buy_order;
protected override void Create() {
// create variable objects, function objects, order objects etc.
buy_order = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, EOrderAction.Buy));

protected override void StartCalc() {
// assign inputs
// here I'm initializating some properties which are reinitialized when calling AverageTrueRange
Output.WriteLine("Start calc CALL!");
protected override void CalcBar(){
if (!Environment.IsAutoTradingMode || !Environment.IsRealTimeCalc)
Output.WriteLine("CalcBar CALL!");
if (StrategyInfo.MarketPosition == 0)
// double atr_day = AvgTrueRange.AverageTrueRange(this, ATR_LENGTH,0,DAY_CHART_ID);
Output.WriteLine("Computing ATR...");
double atr_day = this.AverageTrueRange(ATR_LENGTH,0,DAY_CHART_ID);
Output.WriteLine("ATR value: {0}", atr_day);
When I start automated trading output looks like:

Start calc CALL!
Start calc CALL!
CalcBar CALL!
Computing ATR...
Start calc CALL!
CalcBar CALL!
Computing ATR...
ATR value: 0,431759999999999
CalcBar CALL!
Computing ATR...
ATR value: 0,431759999999999
CalcBar CALL!
CalcBar CALL!
CalcBar CALL!
CalcBar CALL!

See the Start calc CALL after first computation? I believe StartCalc() shouldn't be called once the Automated Trading is started and CalcBar() is triggered.
Could anybody tell me if I'm doing something wrong or should I report it as bug?
Thank you.

User avatar
Henry MultiСharts
Posts: 9089
Joined: 25 Aug 2011
Has thanked: 1258 times
Been thanked: 2923 times

Re: Call of AverageTrueRange restarts the strategy  [SOLVED]

Postby Henry MultiСharts » 11 Jul 2014

Hello uniquepito,

That is expected behavior. On initial calculation MultiCharts .NET does not know how many data series your study requires for calculation. By default it is considered that a single (main) data series is required, even if there are multiple data series on the chart. New data series are added into the calculation once such reference is reached in the code, this triggers study recalculation.
This is exactly what happens in your case - MultiCharts .NET reaches the reference to additional data stream only when AT is turned on, the study is calculated on real time data and AverageTrueRange calculation starts.
If you force a call of second data series in StartCalc then there will be no recalculation.

Return to “MultiCharts .NET”