I am using custom 3rd party tool for pure order management that doesn't work well on tick replay data. I want to separate indicator logic - and basically pass information when signal occurs to another indicator which in turn will be called by that 3rd party order management tool on each tick.
I am able to create add-on with static event
namespace NinjaTrader.NinjaScript.AddOns
{
public static class EventClass
{
public static event EventHandler PropChanged;
private static double TestDoubleValue;
public static double TestDoubleEvent
{
get{ return TestDoubleValue; }
set{
TestDoubleValue = value;
if(PropChanged != null) {PropChanged(value, EventArgs.Empty);
}
}
}
}
}
And I can pass and receive "Double" values:
(here is indicator below)
public class testSMA : Indicator
{
private double priorSum;
private double sum;
private double EventValue;
bool fired = false;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "TestSMA";
Name = "TestSMA";
IsOverlay = true;
IsSuspendedWhileInactive = true;
Period = 14;
AddPlot(Brushes.Goldenrod, "SMAPlot");
}
else if (State == State.Configure)
{
priorSum = 0;
sum = 0;
Print("subscribe to event");
AddOns.EventClass.PropChanged += OnPropChanged;
}
else if (State == State.Terminated)
{
if (ChartControl == null) return;
Print("Unsubscribe from event");
AddOns.EventClass.PropChanged -= OnPropChanged;
}
}
protected override void OnBarUpdate()
{
if (BarsArray[0].BarsType.IsRemoveLastBarSupported)
{
if (CurrentBar == 0)
Value[0] = Input[0];
else
{
double last = Value[1] * Math.Min(CurrentBar, Period);
if (CurrentBar >= Period)
Value[0] = (last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period);
else
Value[0] = ((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
}
}
else
{
if (IsFirstTickOfBar)
priorSum = sum;
sum = priorSum + Input[0] - (CurrentBar >= Period ? Input[Period] : 0);
Value[0] = sum / (CurrentBar < Period ? CurrentBar + 1 : Period);
}
if (State == State.Realtime)
{
if (!fired)
{
AddOns.EventClass.TestDoubleEvent = 122;
fired = true;
}
}
}
void OnPropChanged(object sender, EventArgs e)
{
EventValue = (double)sender;
Print("Event fired from within chart indicator. Prop changed to " + EventValue.ToString());
}
region Properties
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
public int Period
{ get; set; }
#endregion
}
}
Is it possible to pass class structure through event which includes side to take (long/short), limit price to place order, potential stop and target (sending class object through event)?
Also, what would be best practice if I want to pass data across multiple accounts/instruments/strategies? Will I have to code "Shared Event" for each account/instrument/strategy combination or is there a better way?
Thanks
Comment