Orders and trades loading |
At the start of the strategy you may need to load the previously matched orders and trades (for example, when the algorithm has been restarted during the trading session or orders and trades transferred through the night). To do this:
The following example shows the loading of all trades in the strategy:
To load the previous state of the Strategy, you must override StrategyProcessNewOrders(IEnumerableOrder). All IConnectorOrders and IConnectorStopOrders will be received by this method from the StrategyOnStarted. And you should to filter them:
private bool _isOrdersLoaded; private bool _isStopOrdersLoaded; protected override IEnumerable<Order> ProcessNewOrders(IEnumerable<Order> newOrders, bool isStopOrders) { // check if the orders was already loaded if ((!isStopOrders && _isOrdersLoaded) || (isStopOrders && _isStopOrdersLoaded)) return base.ProcessNewOrders(newOrders, isStopOrders); return Filter(newOrders); }
To implement orders filtering, you must determine the filter criterion. For example, if to save all registered during the strategy work orders in the file, you can create the filter by the transaction number OrderTransactionId. If such number is in the file, so the order was registered through this strategy:
private IEnumerable<Order> Filter(IEnumerable<Order> orders) { // to get the identifiers from text file var transactions = File.ReadAllLines("orders_{0}.txt".Put(Name)).Select(l => l.To<long>()).ToArray(); // finding our orders return orders.Where(o => transactions.Contains(o.TransactionId)); }
A record of orders transaction numbers registering through the strategy can be accomplished by overriding the StrategyRegisterOrder(Order) method:
protected override void RegisterOrder(Order order) { // registering the order base.RegisterOrder(order); // and saving order's transaction id File.AppendAllLines("orders_{0}.txt".Put(Name), new[]{ order.TransactionId.ToString() }); }
Once orders are loaded in the strategy, all of their matched trades will be also loaded. This will be done automatically.