Full Day Strategy Plugin 2

This is the first plugin to implement the real time mode. The main goal with the design is to share the same decision engine between the historical and the real time systems to exercise it with historical data before using it with real money.

This is the internal structure. It has been designed as symetrical as possible on purpose as both subsystems do essentially the same process:

As you can see there are three groups of elements:

  1. The shared elements are the ones that are used in historical and in real-time modes, as has been already explained the benefit of sharing the same elements is that they can be tested with historical data before going into production.
  2. The historical elements are the ones used only while feeding the engine with historical data.
  3. The real-time elements are the ones used only while feeding the engine with real-time data.

The core of all the plugin is the DecissionEngine class. This class implements the algorithm of the plugin and is the one that based on the data received from the feeder evaluates it and generates, if appropriate, buy or sell orders.

The flow of the shared elements is the following one: the DataFeeder filters the data loaded by the DataLoader and pass it to the DecissionEngine, it evaluates the data and generates when required orders that are managed by the OperationManager. The operation manager pass the orders to an IOrderExecutor and this one, depending on the mode pass the orders to the market of simulates their execution based on the values present on the next quotation.

Decision Engine

The decission engine is basically a state machine that evolves following the next flow diagram (simplified):

One of the main things the engine needs to know is when a market opens because this engine relies on the concept of opening price. When we are on historical mode is pretty easy to find this information because the Engine is processing day by day and only needs to inform the decision engine that a new day has started. On real-time is harder to find this information as the one that knows it is the StockEngine and currently it's not publishing this information. Also we're resetting all the group information  on each start up of the real-time acquisition engine so if we just publish the market open we will notify about one open for each engine start.

To avoid this problem we have added a variable holding the date of the last start to the StockEngine. If the starts are done on the same day we're just continuing with the data we have, if not we're discarding all the data and starting again.

Now we still need a way to know when a market has been open, or what is the same, when a group has been initialized. We don't want to add an Initialized event to the StockGroup class because event is not directly related with the group but with the engine or the market.

Another source of complexity is the fact that each day the stock can change (if we are tracking futures) as they change when the expiry date ends. The DataLoader must check on each market open which one is the current stock with the desired name as the name is the identifier of the stock.

Initially we made the DecissionEngine need to know when a market was open (to track the opening value) and when was closed (to enforce the closing of operations open if any). To simplify the solution we will make the DecissionEngine to work only with the signal of opening a market and we will rely on the forced close time to ensure positions are closed. We already have this information because on each market open the StockGroupItems of a StockGroup are reset.

About the DataLoader, it contains the list of groups to look for the stock with the name being processed. One of the problems is that the list of groups can be already filled or filled after the creation of the DataLoader (depending on the activation of the realtime engine). As all we have is the stock name need a way to find out the group where the stock belongs, we will put as a precondition that the group never will change in run-time. To overcome this limitation we will turn the groups list to a ReadWriteObservableCollection, this way we can be sure that we can process the collection without changes.

Generic Classes

Historical Classes

Realtime Classes

  • DataLoader: this class is in charge of passing the data that comes from the real-time engine to the DecissionEngine feeder. It's main job is finding out on which group the desired stock resides and attach a handler to the list of Quotations it has. Once the group is found the loader has to detect when the group is initialized again (happens on each market start) to register for the new instance of the StockGroupItem that will contain the quotations of that day). One of the complexities of the class is that we don't know when we start the loader if the groups will be initialized or not, that makes us search inside all the groups for the required stock, if it's not found then we attach to detect when new items are added and search thru the added items. Once the group is found the search is not required anymore and all the search handlers are removed. The class does not support stocks changing from one group to another one.

This is the sequence executed by the loader:

  •  

Last edited Feb 18, 2013 at 8:57 AM by somos, version 16

Comments

No comments yet.