public override void Log(LogWriter logWriter, DBInputOutput.DBWriter dbWriter = null, int assetid = -1) { DateTime dTime = ServerTime.GetRealTime(); if (dbWriter != null) { dbWriter.InsertOrderLog(dTime, OrderId, "Failed", Cookie, Reason, assetid); } logWriter.WriteLine(dTime.ToString(DateTimeFormat) + " | Order failed. Cookie: {0}; OrderId: {1}; Reason: {2}", Cookie, OrderId, Reason); }
public override void Log(LogWriter logWriter, DBInputOutput.DBWriter dbWriter = null, int assetid = -1) { DateTime dTime = ServerTime.GetRealTime(); if (dbWriter != null) { dbWriter.InsertOrderLog(dTime, OrderId, "CancelFailed", 0, "", assetid); } logWriter.WriteLine(dTime.ToString(DateTimeFormat) + " | Order cancel failed. OrderId: " + OrderId); }
public override void Log(LogWriter logWriter, DBInputOutput.DBWriter dbWriter = null, int assetid = -1) { DateTime dTime = ServerTime.GetRealTime(); if (dbWriter != null) { dbWriter.InsertPosition(dTime, Symbol, assetid, Amount, Planned, AvgPrice); } logWriter.WriteLine(dTime.ToString(DateTimeFormat) + " | Update position. Symbol: {0}; Amount: {1}; Planned: {2}", Symbol, Amount, Planned); }
public override void Log(LogWriter logWriter, DBInputOutput.DBWriter dbWriter = null, int assetid = -1) { DateTime dTime = ServerTime.GetRealTime(); if (dbWriter != null) { dbWriter.InsertOrderLog(dTime, OrderId, "UpdateOrder", Cookie, "", assetid, (int)State, (int)Action, (int)Type, Price, Amount, Stop, Filled); } logWriter.WriteLine(dTime.ToString(DateTimeFormat) + " | Update order. Symbol: {0}; State: {1}; Action: {2}; Type: {3}; Price: {4}; Amount: {5}; Stop: {6}; Filled: {7}; " + "Datetime: {8}; OrderId: {9}; Cookie: {10}", Symbol, State, Action, Type, Price, Amount, Stop, Filled, Datetime, OrderId, Cookie); }
private void HandleInformTimer(object sender, ElapsedEventArgs e) { //dbWriter.InsertGeneral("Listening", "Listener"); dbWriter.InsertSts(ServerTime.GetRealTime(), "Listener", "Listening"); if (IsEndOfWork()) { //dbWriter.InsertGeneral("Work Ended", "Listener"); dbWriter.InsertSts(Stocks.ServerTime.GetRealTime(), "Listener", "Work Ended"); DisconnectStockServer(); Environment.Exit(0); } }
protected override List <Order> PreparePlaceOrders() { WriteToLogDB("PreparePlaceOrders", "Started"); Bar bar = DatabaseReader.SelectLastPrice(Symbol); double lastPrice = bar.Close; double buyPrice = RoundToStep(lastPrice - 0.02); double sellPrice = RoundToStep(lastPrice + 0.02); int buyVol = 0; int sellVol = 0; if (CurrentState.Position == 0) { buyVol = ContractsToTrade; sellVol = ContractsToTrade; } else if (CurrentState.Position < 0) { buyVol = ContractsToTrade; sellVol = ContractsToTrade - Math.Abs(CurrentState.Position); } else { buyVol = ContractsToTrade - Math.Abs(CurrentState.Position); sellVol = ContractsToTrade; } if (buyPrice >= sellPrice || (buyPrice <= 0 && buyVol != 0) || (sellPrice <= 0 && sellVol != 0)) { throw new SmartException(ExceptionImportanceLevel.HIGH, "PreparePlaceOrders", "ContrTrendStrat", "buyPrice = " + buyPrice + ", sellPrice = " + sellPrice); } WriteToLogDB("PreparePlaceOrders", "Buy: Price = " + buyPrice + ", Volume = " + buyVol + "; Sell: Price = " + sellPrice + ", Volume = " + sellVol); List <Order> placeOrders = new List <Order>(); DateTime dTime = ServerTime.GetRealTime(); DatabaseWriter.InsertDecision(dTime, Symbol, ActionEnum.BUY, buyPrice, buyVol, 0); DatabaseWriter.InsertDecision(dTime, Symbol, ActionEnum.SELL, sellPrice, sellVol, 0); if (buyVol > 0) { placeOrders.Add(new Order(Symbol, GenerateCookie(), "", buyVol, 0, buyPrice, 0, ActionEnum.BUY, OrderTypeEnum.LIMIT)); } if (sellVol > 0) { placeOrders.Add(new Order(Symbol, GenerateCookie(), "", sellVol, 0, sellPrice, 0, ActionEnum.SELL, OrderTypeEnum.LIMIT)); } WriteToLogDB("PreparePlaceOrders", "Finished"); return(placeOrders); }
public Listener() { Collector = new DataCollector(); dbWriter = new DBInputOutput.DBWriter(); dbReader = new DBInputOutput.DBReader(); dbWriter.InsertSts(ServerTime.GetRealTime(), "Listener", "Work Started"); Collector.BarsCollected += BarsCollectedHandler; InformTimer = new Timer(InformTimerInterval); InformTimer.AutoReset = true; InformTimer.Elapsed += HandleInformTimer; InformTimer.Start(); dbWriter.InsertSts(ServerTime.GetRealTime(), "Listener", "Listening"); DayOffs = dbReader.SelectDayOffs(ServerTime.GetRealTime()); bool isDayOff = IsDayOff(ServerTime.GetRealTime()); if (isDayOff || IsEndOfWork()) { Environment.Exit(0); } WasConnected = false; }
protected override List <Order> PreparePlaceOrders() { WriteToLogDB("PreparePlaceOrders", "Started"); if (Bars.Count < 3) { throw new SmartException(ExceptionImportanceLevel.MEDIUM, "PreparePlaceOrders", "TrendStrat", Symbol + ": Bars are empty or has less than three elements!"); } if (AmountOfVolatEstim() > AmountOfSkipBars()) { throw new SmartException(ExceptionImportanceLevel.MEDIUM, "PreparePlaceOrders", "TrendStrat", Symbol + ": AmountOfVolatEstim > AmountOfSkipBars"); } double[] closes = Bars.Select(bar => bar.Close).ToArray(); double[] ys = new double[Bars.Count - 1]; for (int i = 0; i < Bars.Count - 1; i++) { ys[i] = closes[i + 1] / closes[i] - 1; } ys = ys.Skip(Bars.Count - 1 - AmountOfVolatEstim()).ToArray(); double sdev = CalcVolat(ys); double alpha = AlphaSpread() * Math.Sqrt(AmountOfSkipBars()) * sdev; //double lastPrice = Bars.Last().Close; double lastPrice = 0.0; try { Bar bar = DatabaseReader.SelectLastPrice(Symbol); lastPrice = bar.Close; } catch (Exception e) { lastPrice = Bars.Last().Close; } double buyStopPrice = RoundToStep(lastPrice * (1 + alpha)); double sellStopPrice = RoundToStep(lastPrice * (1 - alpha)); int buyVol = 0; int sellVol = 0; if (CurrentState.Position == 0) { buyVol = ContractsToTrade; sellVol = ContractsToTrade; } else if (CurrentState.Position < 0) { buyVol = ContractsToTrade; sellVol = ContractsToTrade - Math.Abs(CurrentState.Position); } else { buyVol = ContractsToTrade - Math.Abs(CurrentState.Position); sellVol = ContractsToTrade; } double buyPrice = buyStopPrice + Slip(); double sellPrice = sellStopPrice - Slip(); if (buyStopPrice <= sellStopPrice || (buyStopPrice <= 0 && buyVol != 0) || (sellStopPrice <= 0 && sellVol != 0)) { throw new SmartException(ExceptionImportanceLevel.HIGH, "PreparePlaceOrders", "TrendStrat", "buyPrice = " + buyStopPrice + ", sellPrice = " + sellStopPrice); } WriteToLogDB("PreparePlaceOrders", "Buy: Price = " + buyStopPrice + ", Volume = " + buyVol + "; Sell: Price = " + sellStopPrice + ", Volume = " + sellVol); List <Order> placeOrders = new List <Order>(); DateTime dTime = ServerTime.GetRealTime(); DatabaseWriter.InsertDecision(dTime, Symbol, ActionEnum.BUY, buyPrice, buyVol, buyStopPrice); DatabaseWriter.InsertDecision(dTime, Symbol, ActionEnum.SELL, sellPrice, sellVol, sellStopPrice); if (buyVol > 0) { placeOrders.Add(new Order(Symbol, GenerateCookie(), "", buyVol, 0, buyPrice, buyStopPrice, ActionEnum.BUY, OrderTypeEnum.STOP)); } if (sellVol > 0) { placeOrders.Add(new Order(Symbol, GenerateCookie(), "", sellVol, 0, sellPrice, sellStopPrice, ActionEnum.SELL, OrderTypeEnum.STOP)); } WriteToLogDB("PreparePlaceOrders", "Finished"); return(placeOrders); }
public bool IsNowEveningTradeTime() { DateTime dTime = ServerTime.GetRealTime(); return((dTime.Hour >= 19 && dTime.Hour < 23) || (dTime.Hour == 23 && dTime.Minute < 50)); }
public bool IsNowDayTradeTime() { DateTime dTime = ServerTime.GetRealTime(); return((dTime.Hour >= 10 && dTime.Hour < 18) || (dTime.Hour == 18 && dTime.Minute < 45)); }
public bool IsNowTradeTime() { DateTime dTime = ServerTime.GetRealTime(); return(IsNowDayTradeTime() || IsNowEveningTradeTime()); }
public bool IsEveningPause() { DateTime dTime = ServerTime.GetRealTime(); return(dTime.Hour == 18 && dTime.Minute >= 45 || (dTime.Hour == 19 && dTime.Minute == 0)); }
/// <summary> /// Проверяет, является ли текущий момент времени концом торгового дня /// </summary> /// <returns>Возвращает true, если торговый день завершен</returns> private bool IsEndOfWork() { DateTime dTime = ServerTime.GetRealTime(); return(dTime.Hour < 9 || (dTime.Hour == 23 && dTime.Minute >= 50)); }