I created a strategy with two timeframes (primary is on 1-tick, secondary on 1 Min) on the same instrument what allows me to update the value of a dynamic stop-loss (takes its value from a custom indicator) every tick.
This is needed, because the StopLoss should take the maximum value of the indicator ever reached (calculated on every tick) since a position was entered.
In Market Replay environment the strategy behaves like expected without any problems.
But when I start a backtest with the same setup, I don't get any results and the log-tab shows me following message:
Failed to call mehtod "Initialize" for strategy "AATrailProb"; "Position" property can not be accessed from within "Initialize" method.
1. What does this mean?
2. In general: Is my logic right that I should get "near to live-trading"-behaviour of my strategy when I backtest this strategy calculating indicator values on a timeframe that should represent the live-trading-primary-bar-intervall (eg 1 Min) but letting the strategy run on a one-tick-primary-timeframe when backtesting?
I'm using the Position-method for resetting purposes within the strategy:
Here is my code:
[INDENT]#region Initialize
protected override void Initialize()
{
Add(PeriodType.Minute, xminutes);
CalculateOnBarClose = true;
StopValueSCalc = new DataSeries(this);
StopValueLCalc = new DataSeries(this);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
///
#endregion
#region OnBarUpdate
protected override void OnBarUpdate()
{
if (Historical)
return;
if (CurrentBar < 10)
return;
if (BarsInProgress == 1)
{
StopValueSCalc.Set(ATRTrailingStop(10, 1, 20, 1).ATRTrailup[0]);
StopValueLCalc.Set(ATRTrailingStop(10, 1, 20, 1).ATRTraildown[0]);
ResetSLL = ATRTrailingStop(10, 1, 20, 1).ATRTraildown[0];
ResetSLS = ATRTrailingStop(10, 1, 20, 1).ATRTrailup[0];
}
if (BarsInProgress == 0)
{
#region LONG
if (Closes[0][0] > Highs[0][1])
{
EnterLong(DefaultQuantity, "ENTERLONG");
}
if (Positions[1].MarketPosition == MarketPosition.Long)
{
StopValueL = Math.Max(ResetSLL, MAX(StopValueLCalc, BarsSinceEntry())[0]);
SetStopLoss("ENTERLONG", CalculationMode.Price, StopValueL, false);
}
if (Positions[1].MarketPosition == MarketPosition.Flat)
{
SetStopLoss("ENTERLONG", CalculationMode.Price, ResetSLL, false);
}
#endregion
[/INDENT]

Comment