public PersistItem getItem(string exchangeOrderId) { PersistItem result = null; items.TryGetValue(exchangeOrderId, out result); return(result); }
public void save(Order order) { if (items.ContainsKey(order.exchangeOrderId)) { return; } PersistItem item = new PersistItem(DateTime.Now, order.exchangeOrderId, order.orderId, order.symbol, order.ordStatus.ToString(), order.account); items.TryAdd(order.exchangeOrderId, item); processor.add(item); }
public void load() { items.Clear(); Directory.CreateDirectory(dir); string fileName = dir + "\\" + file; if (File.Exists(fileName)) { string line; long count = 0; System.IO.StreamReader file = new System.IO.StreamReader(fileName); while ((line = file.ReadLine()) != null) { ++count; try { PersistItem item = PersistItem.deserialize(line); if (null == item) { logger.Error("persist data error in line " + count); continue; } else { if (item.time.Date == DateTime.Today) { items[item.exchangeOrderId] = item; } } } catch (Exception e) { logger.Error("persist data error in line " + count); logger.Error(e); logger.Error(e.StackTrace); } } // end while file.Close(); } FileStream fs = new FileStream(fileName, FileMode.Create); stream = new System.IO.StreamWriter(fs); // write it back so we can get rid of yesterday's order records foreach (var item in items) { stream.WriteLine(item.Value.serialize()); } stream.Flush(); }
private void process(PersistItem item) { stream.WriteLine(item.serialize()); stream.Flush(); }
void onExecutionReport(OrderMessage.IExecutionReportMessage msg, OrderConnection.ExecDupEnum possDup) { Utils.printExecutionReport(msg, possDup); Order existingOrder; if (null != msg.OrderID && msg.OrderID != "") { if (this.recovering) // during recovery there won't be an order in the cache { recoveryCount++; Order order = new Order(this.id, msg.Symbol, "unknown", msg.Price, msg.OrderQty, FieldConverter.convert(msg.Side), FieldConverter.convert(msg.OrderType), "recovered"); if (updateOrder(order, msg)) { orders[order.exchangeOrderId] = order; PersistItem item = persistence.getItem(order.exchangeOrderId); if (null == item) // log error here !!! { logger.Error("Error: Cant find order in peristence: " + order.exchangeOrderId); return; } // This is the only reason we need persistence! order.symbol = item.symbol; order.orderId = item.orderId; order.account = item.account; logger.Info("Recovery add order: " + order); } } else { if (orders.TryGetValue(msg.OrderID, out existingOrder)) { if (updateOrder(existingOrder, msg)) { persistence.save(existingOrder); this.onOrder(existingOrder); } } else if (msg.NID != 0 && pendings.TryGetValue(msg.NID, out existingOrder)) { if (updateOrder(existingOrder, msg)) { pendings.TryRemove(msg.NID, out existingOrder); orders[msg.OrderID] = existingOrder; persistence.save(existingOrder); this.onOrder(existingOrder); } } else { logger.Info("Cant find corresponding order in cache"); } } } else { logger.Info("ExecutionReport OrderId is null or empty"); } }