OnInitialize Method
When overridden in a derived class, the OnInitialize method gives the strategy implementation a chance to initialize the strategy-level variables and other important states of the Strategy. This method is called once during the active lifetime of the strategy instance and is akin to the constructor of a class, which is called once to initialize the state of your object.
If the initialization fails, the strategy instance will be in a bad state and will never transition into a Start running state. In such cases, you need to throw an Exception with a reason text to let the trader know the reason for the failure of your Strategy Instance Initialization. Ideally, the OnInitialize method should not fail.
Typical implementation logic placed in the OnInitialize method based on your strategy requirements could include:
protected override void OnInitialize()
{
_ivInfo = base.GetIVInfo(_ivInstrument);
if (_ivInfo.InstrumentName.ToUpper().StartsWith("BANKNIFTY"))
{
_strikeGap = 100;
_instrumentFutUnderlineName = "BANKNIFTY-I";
_bbProfitMargin = 100;
}
else if (_ivInfo.InstrumentName.ToUpper().StartsWith("FINNIFTY"))
{
_strikeGap = 100;
_instrumentFutUnderlineName = "FINNIFTY-I";
_bbProfitMargin = 50;
}
else if (_ivInfo.InstrumentName.ToUpper().StartsWith("NIFTY"))
{
_strikeGap = 100;
_instrumentFutUnderlineName = "NIFTY-I";
_bbProfitMargin = 50;
}
else
throw new Exception("Underline not supported.");
_timeDataSeries = LoadHistoricalData(instrumentFutUnderlineName);
_superTrendIndicatorFast = QX.FinLib.Data.IndicatorRegistrationManager.Instance.GetIndicator("Supertrend", _ timeDataSeries);
_superTrendIndicatorFast.SetFieldValue("LengthPeriod", 10);
_superTrendIndicatorFast.SetFieldValue("Multiplier", 1);
// Bind a callback method that is called when 5 Minutes realtime Bar will be periodically completed_timeDataSeries.OnBarDataCompletedEvent += TimeDataSeriesFutures_OnBarDataCompletedEvent;
}
IVObjects Getter Override Method
This method is mandatory to implement, and it must return the list of all static IVObjects defined in your strategy. If the strategy does not define any IVObject, this method returns an empty list.
The IVObject represents an abstraction of an instrument or market. During the instance creation of the strategy, the static IVObject defined is bound with a specific market, such as the NIFTY29MAR24FUT Futures contract of NSEFO or ESH4 of CME. Blitz provides over 20 Exchange connectivity adapters, and IVObjects can be bound to any configured market. Any new market can be developed and plugged into the system in a very short time frame.
public class MainStrategy : StrategyBase
{
private IVObject _ivFuturesLeg = new IVObject("FuturesIV", "Futures Instrument", true, InstrumentType.Futures, MarketDataType.All, OrderEventType.All);
private IVObject _ivCallLeg = new IVObject("CallIV", "Call Instrument", true, InstrumentType.Options, MarketDataType.All, OrderEventType.All);
private IVObject _ivPutLeg = new IVObject("PutIV", "Put Instrument", true, InstrumentType.Options, MarketDataType.All, OrderEventType.All);
private IVInfo _ivInfoFutures, _ivInfoCallOptions, _ivInfoPutOptions;
public override IVObject[] IVObjects
{
get
{
List<IVObject> _ivObjectArray = new List<IVObject>();
_ivObjectArray.Add(_ivFuturesLeg);
_ivObjectArray.Add(_ivCallLeg);
_ivObjectArray.Add(_ivPutLeg);
return _ivObjectArray.ToArray();
}
}
protected override void OnInitialize()
{
_ivInfoFutures = base.GetIVInfo(_ivFuturesLeg);
_ ivInfoCallOptions = base.GetIVInfo(_ivCallLeg);
_ ivInfoPutOptions = base.GetIVInfo(_ivPutLeg);
}
}
BuildVersion Getter Override Method
The Strategy provides an override method to assign a unique version number to your strategy. This allows you to increment the versioning whenever a new change is released in the production state of the strategy. The version number is displayed in logs and the trading dashboard.
The version is represented by four values: Major, Minor, Build, and Revision.
public override Version BuildVersion
{
get
{
return new Version(1, 05, 09, 09);
}
}
OnStart Override Method
The Strategy Instance maintains two major state: Stopped or Started and it toggle between these two state. Once the Strategy is initialized, it is by default in the Stopped state. Under Stopped state, the strategy will receive real-time market events and can perform any computation but will ignore any order-routing-related. The order routing is only possible if Strategy State is Started.
The method OnStart is automatically called when the Strategy instance is explicitly started from the trading dashboard. This method enables the Strategy to capture a Started event and perform any necessary business logic to validate if the Strategy is in a suitable state to execute an order-routing mechanism. The method must return true if all states are satisfactory or return false along with the appropriate reason for the start failure.
If it return false from Strategy instance code, the attempt to start the Strategy will fail and User dashboard will displayed the reason of Start Strategy failure.
protected override bool OnStart(out string errorString)
{
errorString = "";
return true;
}
OnStopping Override Method
The method is called when the user explicitly stops the Strategy instance from the trading dashboard. Additionally, this method is automatically invoked when the Blitz system detects the rejection of an order in a looping state, preventing the continuous pumping of orders towards the exchange. Within this method, Strategists can perform cleaning tasks, such as canceling all open orders or squaring off any open positions.
protected override void OnStopping()
{
foreach (IOrderCommandEx orderCommandEx in _orderExecutionMAP.Values)
{
if (orderCommandEx.IVInfo.Statistics.NetPosition != 0)
{
orderCommandEx.UserText = "Strategy Stopped.";
orderCommandEx.GoFlat();
}
}
TraceLogInfo("Strategy Stopped.");
}
In the above code snippet, an advanced execution concept of Blitz is utilized, which allows for the squaring off of all open positions across any market with a simple call. We will delve into the order state management and execution functionality of Blitz in later sections.
Furthermore, the Strategy developer can also explicitly stop the strategy with an API call from their Strategy code.
if (isStrategyStoppedTimeBreached)
base.Stop("The startegy is stopped due to EOD time reached.");
The following code snippet checks the Strategy mode and may return before evaluating core trading logic if the Strategy is not in the started state:
if(!this.StrategyRunningMode == StrategyMode.Started)
{
return;
}