private static T GetAttribute <T>(KLineSize klineSize) where T : Attribute { var type = klineSize.GetType(); FieldInfo field = type.GetField(Enum.GetName(type, klineSize)); T attr = Attribute.GetCustomAttribute(field, typeof(T)) as T; return(attr); }
public static string GetKLineListNamePrefix(KLineSize kLineSize) { DescriptionAttribute attr = GetAttribute <DescriptionAttribute>(kLineSize); if (attr == null) { return(string.Empty); } return(attr.Description); }
public static DateTime GetKLineTime(DateTime quoteTime, KLineSize klineSize, ProdDef prodDef = null) { if (klineSize == KLineSize.Day) { return(Products.GetLocalDateRegardingSessionRefreshTime(quoteTime, prodDef)); } PeriodAttribute attr = GetAttribute <PeriodAttribute>(klineSize); int period = attr.Period; return(DateTimes.GetPeriodStartTime(quoteTime, period)); }
private static void UpdateKLine(List <Quote> quotes, IRedisTypedClient <KLine> redisKLineClient, ProdDef prodDef, DateTime dtAyondoNow, KLineSize kLineSize) { var list = redisKLineClient.Lists[KLines.GetKLineListNamePrefix(kLineSize) + prodDef.Id]; if (quotes.Count == 0) //no quotes received, then should just fill the non-changing candle { if (kLineSize != KLineSize.Day) //no need to fill the non-changing candle for day kline { if (list.Count != 0) { var last = list[list.Count - 1]; var klineTime = KLines.GetKLineTime(dtAyondoNow, kLineSize, prodDef); if (prodDef.QuoteType == enmQuoteType.Closed) { klineTime = KLines.GetKLineTime(prodDef.LastClose.Value, kLineSize); } if (klineTime > last.T) //a new candle? { //fill the non-changing candle list.Add(new KLine() { T = klineTime, O = last.C, C = last.C, H = last.C, L = last.C, }); } } } } else { var orderedQuotes = quotes.OrderBy(o => o.Time).ToList(); var firstQuote = orderedQuotes.First(); var lastQuote = orderedQuotes.Last(); var klineTime1 = KLines.GetKLineTime(firstQuote.Time, kLineSize, prodDef); var klineTime2 = KLines.GetKLineTime(lastQuote.Time, kLineSize, prodDef); //var list = redisKLineClient.Lists[KLines.GetKLineListNamePrefix(kLineSize) + prodDef.Id]; if (klineTime1 != klineTime2 && kLineSize != KLineSize.Day) //quotes range more than 1 candle { var list1 = orderedQuotes.Where(o => o.Time < klineTime2).ToList(); var list2 = orderedQuotes.Where(o => o.Time >= klineTime2).ToList(); var k1 = new KLine() { T = klineTime1, O = Quotes.GetLastPrice(list1.First()), C = Quotes.GetLastPrice(list1.Last()), H = list1.Max(o => Quotes.GetLastPrice(o)), L = list1.Min(o => Quotes.GetLastPrice(o)), }; var k2 = new KLine() { T = klineTime2, O = Quotes.GetLastPrice(list2.First()), C = Quotes.GetLastPrice(list2.Last()), H = list2.Max(o => Quotes.GetLastPrice(o)), L = list2.Min(o => Quotes.GetLastPrice(o)), }; if (list.Count == 0) { list.Add(k1); list.Add(k2); } else { var last = list[list.Count - 1]; if (last.T < klineTime1) //2 new candles to append { list.Add(k1); list.Add(k2); } else if (last.T == klineTime1) //update last 1, append 1 new { list[list.Count - 1] = new KLine() { T = last.T, O = last.O, C = k1.C, H = Math.Max(last.H, k1.H), L = Math.Min(last.L, k1.L), }; list.Add(k2); } else { //should not be here } } } else //quotes range within 1 candle { var k = new KLine() { T = klineTime1, O = Quotes.GetLastPrice(firstQuote), C = Quotes.GetLastPrice(lastQuote), H = orderedQuotes.Max(o => Quotes.GetLastPrice(o)), L = orderedQuotes.Min(o => Quotes.GetLastPrice(o)), }; if (list.Count == 0) { list.Add(k); } else { var last = list[list.Count - 1]; if (last.T < k.T) //append 1 new { list.Add(k); } else if (last.T == k.T) //update last 1 { list[list.Count - 1] = new KLine() { T = last.T, O = last.O, C = k.C, H = Math.Max(last.H, k.H), L = Math.Min(last.L, k.L), }; } else { //should not be here } } } } //clear history/prevent data increasing for good var clearWhenSize = KLines.GetClearWhenSize(kLineSize); var clearToSize = KLines.GetClearToSize(kLineSize); if (list.Count > clearWhenSize) //data count at most possible size (in x days ) { YJYGlobal.LogLine("KLine " + kLineSize + " " + prodDef.Id + " Clearing data from " + list.Count + " to " + clearToSize); var klines = list.GetAll(); var newKLines = klines.Skip(klines.Count - clearToSize); list.RemoveAll(); list.AddRange(newKLines); } }
public static int GetClearToSize(KLineSize klineSize) { ClearToAttribute attr = GetAttribute <ClearToAttribute>(klineSize); return(attr.Size); }