I write up a simple Strategy with this output. For an ordinary guy like me, presuming
these prints are sequential as they are, the first 3 lines are a little strange. The SetDefault
is called in the 49806078 instance, apparently before its constructor is called which,
to most programmers would seem a bit odd; but maybe Reflection is doing things, who knows?
OrderEntryStrategy SetDefaults.49806078
OrderEntryStrategy SetDefaults END.
Constructor count: 1 for 49806078
OrderEntryStrategy Terminated.49806078
OrderEntryStrategy Terminated END.49806078
OrderEntryStrategy SetDefaults.14672690
OrderEntryStrategy SetDefaults END.
Constructor count: 2 for 14672690
OrderEntryStrategy SetDefaults.28031203
OrderEntryStrategy SetDefaults END.
Constructor count: 3 for 28031203
OrderEntryStrategy SetDefaults.13155542
OrderEntryStrategy SetDefaults END.
Constructor count: 4 for 13155542
OrderEntryStrategy Terminated.13155542
OrderEntryStrategy Terminated END.13155542
OrderEntryStrategy Terminated.14672690
OrderEntryStrategy Terminated END.14672690
OrderEntryStrategy SetDefaults.62063080
OrderEntryStrategy SetDefaults END.
Constructor count: 5 for 62063080
OrderEntryStrategy Configure.62063080
OrderEntryStrategy Configure END.62063080
OrderEntryStrategy Terminated.62063080
OrderEntryStrategy Terminated END.62063080
OrderEntryStrategy Terminated.28031203
OrderEntryStrategy Terminated END.28031203
So, when this Strategy was added to a chart, that was the
output. And presumably the strategy was activated.
I'll edit this to post the code; and may be able to attach a zipped
project, but here's the code in text: [will edit]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
namespace NinjaTrader.NinjaScript.Strategies {
public class MyStrategy : Strategy {
static int constructorCallCount = 0; // class static
static void myPrint(Strategy strategy, string msg) {
strategy.Print(msg);
}
// constructor
public MyStrategy() {
++constructorCallCount;
myPrint(this, "Constructor count: " + constructorCallCount + " for " + GetHashCode());
}
protected override void OnStateChange() {
try {
if (State == State.SetDefaults) {
myPrint(this, "OrderEntryStrategy SetDefaults." + this.GetHashCode());
myPrint(this, "OrderEntryStrategy SetDefaults END.");
} else if (State == State.Configure) {
myPrint(this, "OrderEntryStrategy Configure." + this.GetHashCode());
myPrint(this, "OrderEntryStrategy Configure END." + this.GetHashCode());
} else if (State == State.Active) {
myPrint(this, "OrderEntryStrategy Active." + this.GetHashCode());
myPrint(this, "OrderEntryStrategy Active END." + this.GetHashCode());
} else if (State == State.DataLoaded) {
myPrint(this, "OrderEntryStrategy DataLoaded." + this.GetHashCode());
myPrint(this, "OrderEntryStrategy DataLoaded END." + this.GetHashCode());
} else if (State == State.Historical) {
myPrint(this, "OrderEntryStrategy Historical." + this.GetHashCode());
myPrint(this, "OrderEntryStrategy Historical END." + this.GetHashCode());
} else if (State == State.Transition) {
myPrint(this, "OrderEntryStrategy Transition." + this.GetHashCode());
myPrint(this, "OrderEntryStrategy Transition END." + this.GetHashCode());
} else if (State == State.Realtime) {
myPrint(this, "OrderEntryStrategy Realtime." + this.GetHashCode());
myPrint(this, "OrderEntryStrategy Realtime END.");
} else if (State == State.Terminated) {
// should release ALL resources
myPrint(this, "OrderEntryStrategy Terminated." + this.GetHashCode());
myPrint(this,"OrderEntryStrategy Terminated END." + this.GetHashCode());
} else {
myPrint(this, "### Unhandled State: " + State);
}
} // end try
catch (Exception ex) {
myPrint(this, "Exception in OnStateChange state: " + State.ToString());
}
}
}
}
Sorry don't know how to prettyprint in the forum.
If we track the Object HashCodes as individual "clones", and also take note of the
number of times the constructor is invoked, this is a bit confusing.
It's possible that actions taken in States might influence the lifecycle evolution, of course,
in particular in the SetDefaults state.
Is it normal to have this lifecycle evolution on activation?
Comment