public Signal DiscardSignal(int signalId) { var trades = context.Trades .Where(x => x.SignalId == signalId) .Where(x => x.TradeSteps.All(t => t.Type != TradeStepType.Hide)) .FirstOrDefault(); if (trades != null) return null; var signal = context.Signals.Include(x => x.SignalSteps).ThenInclude(t => t.PriceEntry) .Where(x => x.SignalSteps.All(t => t.SignalStepType != SignalStepType.Cancel)) .FirstOrDefault(x => x.Id == signalId); var lastEntry = context.PriceEntries .Where(p => p.TimeIntervalId == signal.SignalSteps.First().PriceEntry.TimeIntervalId) .Where(p => p.BrokerInstrumentId == signal.BrokerInstrumentId) .OrderByDescending(p => p.TimeStamp) .FirstOrDefault(); var newStep = new SignalStep { SignalId = signalId, SignalStepType = SignalStepType.Cancel, Reason = "Manuell abgebrochen", PriceEntry = lastEntry }; context.SignalSteps.Add(newStep); return signal; }
public void AddSignalStep(SignalStep step) { var signal = context.Signals .Include(s => s.Trades).ThenInclude(x => x.TradeSteps) .Include(s => s.Trades).ThenInclude(x => x.Orders) .FirstOrDefault(s => s.Id == step.SignalId); if (signal.Trades.Count > 0) { foreach (var trade in signal.Trades) { var lastStep = trade.TradeSteps.OrderByDescending(x => x.Created).FirstOrDefault(); if(lastStep.Type == TradeStepType.Placed || lastStep.Type == TradeStepType.Prepped || lastStep.Type == TradeStepType.PrepCancel || lastStep.Type == TradeStepType.PrepChange){ if(step.SignalStepType == SignalStepType.Change) trade.SignalState = SignalState.Changed; if(step.SignalStepType == SignalStepType.Cancel) trade.SignalState = SignalState.Cancelled; } if (lastStep.Type == TradeStepType.Placed && step.SignalStepType == SignalStepType.Change) { // updateTradeFromSignal(trade, signal, false); } } } }
public void ProcessSignals() { // offne signale fuer dieses BI ueber signaltransactions fuer BI DateTimeOffset? lastTimeStamp = null; var timeIntervals = context.PriceEntries.Select(x => x.TimeInterval).Distinct(); foreach (var ti in timeIntervals) { var biSignals = context.Signals .Include(y => y.SignalSteps).ThenInclude(s => s.PriceEntry) .Include(y => y.Trades).ThenInclude(z => z.TradeSteps) .Where(x => x.TimeIntervalId == ti.Id).ToList(); var openSignals = biSignals.Where(x => x.SignalSteps.All(t => t.SignalStepType != SignalStepType.Cancel)).ToList(); //var temptt = biSignals.SelectMany(x => x.SignalSteps).ToList(); // lastTimeStamp= temptt.Max(y => y.PriceEntry.TimeStamp); var allPrices = context.PriceEntries.Where(x => x.TimeIntervalId == ti.Id).ToList(); var count = allPrices.Select(x => x.TimeStamp).Distinct().Count(); //var allIds = context.BrokerInstruments.Where(x => x.Id == 179).Select(x => x.Id).ToList(); var allIds = context.BrokerInstruments.Select(x => x.Id).ToList(); var pfullAll = GetAllWithIndicatorAndScreener(timeInterval: ti.Id, count: count).ToList(); //var pfullAll = GetAllWithIndicatorAndScreener(timeInterval: ti.Id, count: count,brokerInstrumentId:104).ToList(); foreach (var brokerInstrumentId in allIds) { try { lastTimeStamp = biSignals.Where(x => x.BrokerInstrumentId == brokerInstrumentId) .SelectMany(x => x.SignalSteps).Max(y => y.PriceEntry.TimeStamp); } catch { lastTimeStamp = DateTimeOffset.FromUnixTimeSeconds(0); } var prices = allPrices.Where(x => x.BrokerInstrumentId == brokerInstrumentId) .Where(x => x.TimeStamp > lastTimeStamp).OrderBy(x => x.TimeStamp); var pfull = pfullAll.Where(x => x.p.BrokerInstrumentId == brokerInstrumentId); foreach (var price in prices) { bool pendingComplete = false; //ICER bestehende // ICER ermitteln ob neues signal da ist. var p = pfull.SkipWhile(x => x.p.TimeStamp != price.TimeStamp).Take(2).ToList(); if (p.Count != 2) continue; var i0 = p[0].scr.FirstOrDefault(x => x.ScreenerEntryType.Name == "ICER").Bg; var i1 = p[1].scr.FirstOrDefault(x => x.ScreenerEntryType.Name == "ICER").Bg; var w0 = p[0].scr.FirstOrDefault(x => x.ScreenerEntryType.Name == "Wolke").Bg; var w1 = p[1].scr.FirstOrDefault(x => x.ScreenerEntryType.Name == "Wolke").Bg; var s0 = p[0].scr.FirstOrDefault(x => x.ScreenerEntryType.Name == "Setter").Bg; var welle0 = p[0].scr.FirstOrDefault(x => x.ScreenerEntryType.Name == "Welle").Bg; var b10 = p[0].scr.FirstOrDefault(x => x.ScreenerEntryType.Name == "B10").Bg; var name = context.BrokerInstruments.Where(b => b.Id == brokerInstrumentId) .Include(b => b.BrokerSymbol).ThenInclude(n => n.InstrumentName).FirstOrDefault() .BrokerSymbol.InstrumentName.Name; TradeDirection? newDirection = null; // ICER //////////////////////////////////////////////////////// newDirection = null; if ((i0 == CellColor.Green && i1 != CellColor.Green && w0 == CellColor.Red) || (i0 == CellColor.Green && w0 == CellColor.Red && w1 != CellColor.Red)) newDirection = TradeDirection.Short; if ((i0 == CellColor.Red && i1 != CellColor.Red && w0 == CellColor.Green) || (i0 == CellColor.Red && w0 == CellColor.Green && w1 != CellColor.Green)) newDirection = TradeDirection.Long; var r = prices.OrderByDescending(x => x.TimeStamp).Take(13).Average(x => x.High - x.Low); var openICERs = openSignals.Where(x => x.BrokerInstrumentId == brokerInstrumentId && x.SignalType == SignalType.uxIcer); if (openICERs.Count() > 1) { throw (new Exception("too many open signals, something in the code is wrong")); } // ist schon ein laufendes signal da?? // ist schon ein laufendes signal da?? var openIcerSignal = openICERs.FirstOrDefault(x => x.SignalType == SignalType.uxIcer); if (openIcerSignal != null) { var lastSignalStep = openIcerSignal.SignalSteps.OrderByDescending(x => x.Updated).First(); // ist auch ein neues da? dann laufendes abbrechen if (newDirection != null) { var signalStepCancel = new SignalStep { SignalId = openIcerSignal.Id, SignalStepType = SignalStepType.Cancel, PriceEntry = price, Reason = "neues Signal" }; context.SignalSteps.Add(signalStepCancel); AddSignalStep(signalStepCancel); pendingComplete = true; } else // kein neues da { // neues hoch oder tief if ((openIcerSignal.TradeDirection == TradeDirection.Long && lastSignalStep.Sl > price.Low) || (openIcerSignal.TradeDirection == TradeDirection.Short && lastSignalStep.Sl < price.High)) { // icer nicht mehr farbig dann abbrechen if ((openIcerSignal.TradeDirection == TradeDirection.Long && i0 != CellColor.Red) || (openIcerSignal.TradeDirection == TradeDirection.Short && i0 != CellColor.Green)) { var signalStepCancel = new SignalStep { SignalId = openIcerSignal.Id, SignalStepType = SignalStepType.Cancel, PriceEntry = price, Reason = "Neues H/T bei ICER=Schwarz" }; context.SignalSteps.Add(signalStepCancel); AddSignalStep(signalStepCancel); } else if (w0 == (openIcerSignal.TradeDirection == TradeDirection.Long ? CellColor.Red : CellColor.Green)) { var reason = "Wolke Farbwechsel"; var signalStepCancel = new SignalStep { SignalId = openIcerSignal.Id, SignalStepType = SignalStepType.Cancel, PriceEntry = price, Reason = reason }; context.SignalSteps.Add(signalStepCancel); AddSignalStep(signalStepCancel); pendingComplete = true; } else { // sonst werte neu berechnen, neuer signalstep mit change var entry = openIcerSignal.TradeDirection == TradeDirection.Long ? price.Low + r : price.High -r; var signalStep = new SignalStep { Entry = entry, Sl = openIcerSignal.TradeDirection == TradeDirection.Long ? price.Low : price.High, Tp = openIcerSignal.TradeDirection == TradeDirection.Long ? price.Low + (decimal)3.47 * r : price.High -(decimal) 3.47 * r, EntryType = EntryType.StopLimit, ExitType = ExitType.Icer1, SlType = StopLossType.Icer1, PriceEntry = price, SignalStepType = SignalStepType.Change, SignalId = openIcerSignal.Id, Reason = "Neues H/T Anpassung der Marken" }; context.SignalSteps.Add(signalStep); AddSignalStep(signalStep); } pendingComplete = true; } else { // kein neues H/T checken ob screener noch gut string reason = null; if (i0 == (openIcerSignal.TradeDirection == TradeDirection.Long ? CellColor.Green : CellColor.Red)) reason = "ICER Farbwechsel"; if (w0 == (openIcerSignal.TradeDirection == TradeDirection.Long ? CellColor.Red : CellColor.Green)) reason = "Wolke Farbwechsel"; if (reason != null) { var signalStepCancel = new SignalStep { SignalId = openIcerSignal.Id, SignalStepType = SignalStepType.Cancel, PriceEntry = price, Reason = reason }; context.SignalSteps.Add(signalStepCancel); AddSignalStep(signalStepCancel); pendingComplete = true; } } } } //if (pendingComplete) return; // neue ICER signale in DB erstellen if (newDirection != null) { logger.LogInformation("NewSignal Name: {0}, TS: {1}, Direction: {2}", name, p[0].p.TimeStamp, newDirection); var signal = new Signal { SignalType = SignalType.uxIcer, TradeDirection = (TradeDirection)newDirection, BrokerInstrumentId = brokerInstrumentId, TimeIntervalId = ti.Id }; var signalStep = new SignalStep { Entry = newDirection == TradeDirection.Long ? price.Low + r : price.High - r, Sl = newDirection == TradeDirection.Long ? price.Low : price.High, Tp = newDirection == TradeDirection.Long ? price.Low + (decimal)3.47 * r : price.High - (decimal)3.47 * r, EntryType = EntryType.StopLimit, ExitType = ExitType.Icer1, PriceEntry = price, SignalStepType = SignalStepType.New, Signal = signal, Reason = "Signalstart" }; context.Signals.Add(signal); context.SignalSteps.Add(signalStep); pendingComplete = true; } // uNL //////////////////////////////////////////////////////// newDirection = null; if(b10 == CellColor.Green && welle0 == CellColor.Red && s0 == CellColor.Red && p[0].ind.FirstOrDefault(y => y.Type == IndicatorDataType.OrigWolke_SpanA).Data >p[0].p.High && p[0].ind.FirstOrDefault(y => y.Type == IndicatorDataType.OrigWolke_SpanB).Data >p[0].p.High) newDirection = TradeDirection.Long; var openUNLs = openSignals.Where(x => x.BrokerInstrumentId == brokerInstrumentId && x.SignalType == SignalType.uNL); if (openUNLs.Count() > 1) { throw (new Exception("too many open uNL signals, something in the code is wrong")); } var openuNLSignal = openUNLs.FirstOrDefault(x => x.SignalType == SignalType.uNL); if (openuNLSignal != null) { var lastSignalStep = openuNLSignal.SignalSteps.OrderByDescending(x => x.Updated).First(); // ist auch ein neues da? dann laufendes abbrechen if (newDirection != null) { var signalStepCancel = new SignalStep { SignalId = openuNLSignal.Id, SignalStepType = SignalStepType.Cancel, PriceEntry = price, Reason = "neues Signal" }; context.SignalSteps.Add(signalStepCancel); AddSignalStep(signalStepCancel); pendingComplete = true; } else // kein neues da { // neues hoch oder tief if ((openuNLSignal.TradeDirection == TradeDirection.Long && lastSignalStep.Sl > price.Low)) { var signalStepCancel = new SignalStep { SignalId = openuNLSignal.Id, SignalStepType = SignalStepType.Cancel, PriceEntry = price, Reason = "Neues Tief unter SL" }; context.SignalSteps.Add(signalStepCancel); AddSignalStep(signalStepCancel); } } } if (newDirection != null) { logger.LogInformation("NewSignal uNL Name: {0}, TS: {1}, Direction: {2}", name, p[0].p.TimeStamp, newDirection); var signal = new Signal { SignalType = SignalType.uNL, TradeDirection = (TradeDirection)newDirection, BrokerInstrumentId = brokerInstrumentId, TimeIntervalId = ti.Id }; var signalStep = new SignalStep { Entry = price.High, Sl = price.Low, Tp = price.High + 10 * (price.High - price.Low), EntryType = EntryType.StopLimit, ExitType = ExitType.Icer1, PriceEntry = price, SignalStepType = SignalStepType.New, Signal = signal, Reason = "Signalstart" }; context.Signals.Add(signal); context.SignalSteps.Add(signalStep); pendingComplete = true; } if (pendingComplete) { //openSignals = context.Signals.Where(x => x.BrokerInstrumentId == brokerInstrumentId && x.TimeIntervalId == ti.Id) // .Where(x => x.SignalSteps.All(s => s.SignalStepType != SignalStepType.Cancel)).ToList(); pendingComplete = false; break; } // } } } }