A little update.
I tried incorporating an extension method (thanks Dru for the code and RiverTrader for the explanation of it), but that didn't work due to a null reference because the 'test' object isn't instantiated before its called:
Code: Select all
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using PowerLanguage.Strategy;
namespace PowerLanguage
{
public static class Extensions
{
public static int NumOfTrades(this IPortfolioPerformance _this)
{
return _this.TotalTrades;
}
}
}
namespace PowerLanguage.Indicator
{
[SameAsSymbol(false)]
public class MyTestIndicator : IndicatorObject
{
public MyTestIndicator(object _ctx) : base(_ctx) { }
private VariableSeries<Int32> numTrades;
private IPlotObject Plot1;
private IPortfolioPerformance test;
protected override void Create()
{
numTrades = new VariableSeries<Int32>(this);
Plot1 = AddPlot(new PlotAttributes("NumTrades", 0, Color.Green, Color.Empty, 1, (int)EPlotStyles.Solid, true));
}
protected override void StartCalc() { }
protected override void CalcBar()
{
numTrades.Value = test.NumOfTrades();
Plot1.Set(0, numTrades.Value);
}
}
}
I've also tried using properties, so not passing a StrategyObject to an IndicatorObject but letting the StrategyObject encapsulate the TotalTrades property, but this gave the same null reference problem. This because of the NumberOfTrades property being called on a StrategyObject that hasn't been initialized yet:
Code: Select all
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using PowerLanguage.Strategy;
namespace PowerLanguage.Indicator
{
[SameAsSymbol(false)]
public class MyTestIndicator : IndicatorObject
{
public MyTestIndicator(object _ctx) : base(_ctx) { }
private VariableSeries<Int32> numTrades;
private IPlotObject Plot1;
private StrategyObject stratObj;
protected override void Create()
{
numTrades = new VariableSeries<Int32>(this);
Plot1 = AddPlot(new PlotAttributes("NumTrades", 0, Color.Green, Color.Empty, 1, (int)EPlotStyles.Solid, true));
}
protected override void StartCalc() { }
protected override void CalcBar()
{
numTrades.Value = stratObj.NumberOfTrades;
Plot1.Set(0, numTrades.Value);
}
}
// StrategyObject class
public class StrategyObject : SignalObject
{
private StrategyObject stratObj;
public StrategyObject(object _ctx) : base(_ctx) { }
public int NumberOfTrades
{
get
{
if (stratObj == null)
MakeObject();
return stratObj.Portfolio.TotalTrades;
}
}
protected override void CalcBar() { }
private StrategyObject MakeObject()
{
stratObj = new StrategyObject(this);
return stratObj;
}
}
}
The obvious solution would be: instantiate the StrategyObject before calling it. So far I haven't found a way to do that, since both objects (MyTestIndicator and StrategyObject) are derived from different base classes and the 'this' keyword will then return an incompatible instance to it.
I've also started a thread on a general C# forum on this problem, but so far no solution has come out of it.
Anyway, I know it's possible since MC Support said so, now I just have to figure out
how. Anyone has an idea I could try? Even a speculation would be great.