public void TestForwardCodeIndeier() { string code = "RB1710"; int start = 20170601; int endDate = 20170603; IDataPackage_Code dataPackage = DataCenter.Default.DataPackageFactory.CreateDataPackage_Code(code, start, endDate); ForwardReferedPeriods referedPeriods = new ForwardReferedPeriods(); referedPeriods.UsedKLinePeriods.Add(KLinePeriod.KLinePeriod_1Minute); referedPeriods.UsedKLinePeriods.Add(KLinePeriod.KLinePeriod_5Minute); referedPeriods.UsedKLinePeriods.Add(KLinePeriod.KLinePeriod_15Minute); referedPeriods.UsedKLinePeriods.Add(KLinePeriod.KLinePeriod_1Hour); referedPeriods.UsedKLinePeriods.Add(KLinePeriod.KLinePeriod_1Day); referedPeriods.UseTickData = true; referedPeriods.UseTimeLineData = true; printLine.Clear(); DataForForward_Code data1 = new DataForForward_Code(dataPackage, referedPeriods); DataForForward_CodeIndeier indeier = new DataForForward_CodeIndeier(data1); int len = data1.GetMainKLineData().Length; for (int i = 0; i < len; i++) { IKLineBar bar = data1.GetMainKLineData().GetBar(i); Print(bar, indeier.GetFinishedBarsRelativeToMainKLine(i), data1); } AssertUtils.AssertEqual_List("forwardcodeindeier", GetType(), printLine); }
public static KLineBar GetKLineBar(IKLineBar klineBar, ITickData tickData, int startIndex, int endIndex) { KLineBar bar = new KLineBar(); ITickBar endTickBar = tickData.GetBar(endIndex); bar.Time = endTickBar.Time; bar.Start = klineBar.Start; float high = klineBar.High; float low = klineBar.Low; float money = klineBar.Money; int mount = klineBar.Mount; for (int i = startIndex; i <= endIndex; i++) { ITickBar tickBar = tickData.GetBar(i); if (high < tickBar.Price) { high = tickBar.Price; } if (low > tickBar.Price) { low = tickBar.Price; } money += tickBar.Mount * tickBar.Price; mount += tickBar.Mount; } bar.High = high; bar.Low = low; bar.End = endTickBar.Price; bar.Money = money; bar.Mount = mount; bar.Hold = endTickBar.Hold; return(bar); }
private void DrawMount(Graphics g, IKLineBar chart, int index, PriceGraphicMapping priceMapping, float blockWidth, float blockPadding) { Rectangle rectangle = priceMapping.DrawRect; bool isRed = chart.End > chart.Start; //Brush b = new SolidBrush(isRed ? Color.Red : Color.LightSeaGreen); Brush b = isRed ? this.colorConfig.Brush_CandleBlockUp : this.colorConfig.Brush_CandleBlockDown; Pen p = new Pen(b); double XMiddle = priceMapping.CalcX(index); double YTop = priceMapping.CalcY(chart.Mount); double YBottom = rectangle.Bottom; double halfBlockWidth = (blockWidth - blockPadding) / 2; float XLeft = (float)(XMiddle - halfBlockWidth); float XRight = (float)(XMiddle + halfBlockWidth); if (isRed) { g.DrawRectangle(p, (int)XLeft, (int)YTop, (int)halfBlockWidth * 2, (int)(YBottom - YTop)); } else { g.FillRectangle(b, (int)XLeft, (int)YTop, (int)halfBlockWidth * 2, (int)(YBottom - YTop)); } }
public void AddValue(int barPos, IKLineBar klineBar, double value) { data[barPos] = value; if (klineBar.High > value && klineBar.Low < value) { MaCrossPoint point = new MaCrossPoint(barPos, klineBar, value); recentCrossPoints.Add(point); } }
private void ForwardKLineData(IKLineData_RealTime klineData) { IKLineBar nextMainBar = mainKLineData.GetBar(mainKLineData.BarPos + 1); int currentBarPos = klineData.BarPos; int nextBarPos = currentBarPos + 1; if (nextBarPos >= klineData.Length) { ForwardBar_CurrentPeriod(klineData, nextMainBar); } else { double nextTime = klineData.Arr_Time[nextBarPos]; double nextMainTime = mainKLineData.Arr_Time[mainKLineData.BarPos + 1]; if (nextMainTime >= nextTime) { ForwardBar_NextPeriod(klineData, nextMainBar); } else { ForwardBar_CurrentPeriod(klineData, nextMainBar); } /* * 20171021 ww * 处理回测时非主K线要onbar时时间不对,如下5分钟线 * * 1分钟:20170531.21,3102,3106,3095,3101,135598,0,3992164 * 1分钟:20170531.2101,3102,3103,3096,3096,58762,0,4008012 * 1分钟:20170531.2102,3095,3102,3094,3100,35524,0,4013054 * 1分钟:20170531.2103,3101,3101,3093,3094,35276,0,4023900 * 1分钟:20170531.2104,3094,3102,3094,3101,34660,0,4029194 * 5分钟:20170531.2104,3102,3106,3093,3101,299820,0,402919 * * 正确5分钟线 * 5分钟:20170531.21,3102,3106,3093,3101,299820,0,4029194 */ if (klineData.Period.Equals(KLinePeriod.KLinePeriod_1Day)) { if (mainKLineData.IsDayEnd(mainKLineData.BarPos + 1)) { klineData.ResetCurrentBar(); } } else if (mainKLineData.BarPos < mainKLineData.Length - 2) { double nextMainTime2 = mainKLineData.Arr_Time[mainKLineData.BarPos + 2]; if (nextMainTime2 >= nextTime) { klineData.ResetCurrentBar(); return; } } } }
public static KLineBar CopyFrom(IKLineBar otherKlineBar) { KLineBar klineBar = new KLineBar(); klineBar.code = otherKlineBar.Code; klineBar.start = otherKlineBar.Start; klineBar.high = otherKlineBar.High; klineBar.low = otherKlineBar.Low; klineBar.end = otherKlineBar.End; klineBar.mount = otherKlineBar.Mount; klineBar.money = otherKlineBar.Money; klineBar.hold = otherKlineBar.Hold; return(klineBar); }
private KLineBar GetKLineBar(IKLineBar klineBar, ITickBar tickBar) { KLineBar bar = new KLineBar(); bar.Time = tickBar.Time; bar.Start = klineBar.Start; bar.High = tickBar.Price > klineBar.High ? tickBar.Price : klineBar.High; bar.Low = tickBar.Price < klineBar.Low ? tickBar.Price : klineBar.Low; bar.End = tickBar.Price; bar.Money = klineBar.Money + tickBar.Mount * tickBar.Price; bar.Mount = klineBar.Mount + tickBar.Mount; bar.Hold = tickBar.Hold; return(bar); }
public void ChangeTime(double time) { if (currentTime == time) { return; } //比如 20150531140500 //1分钟线可以正常显示 //日线需要将 int date = DaySplitter.GetTimeDate(time, dataCache_Code.GetOpenDateReader()); if (currentDate != date) { //TODO 将以前生成的klineChartBuilder_1Minute cache下来 this.currentDataCache_CodeDate = dataCache_Code.GetCache_CodeDate(date); this.klineChartBuilder_1Minute = new RealTimeDataBuilder_DayData(currentDataCache_CodeDate, time); this.currentDate = date; } int index = klineData.IndexOfTime(time); double t = klineData.Arr_Time[index]; IKLineData todayMinuteKLineData = klineChartBuilder_1Minute.MinuteKlineData; KLineBar currentMinuteChart = klineChartBuilder_1Minute.GetCurrentChart(); //TODO 这里最多只支持到日线,两日线或以上不支持 int startIndex; if (t == (int)t) { startIndex = 0; } else { startIndex = todayMinuteKLineData.IndexOfTime(t); } int endIndex = todayMinuteKLineData.IndexOfTime(currentMinuteChart.Time); IKLineBar currentChart = todayMinuteKLineData.GetAggrKLineBar(startIndex, endIndex - 1); KLineBarMerge.MergeTo((KLineBar)currentChart, currentMinuteChart); this.currentChart = currentChart; this.currentTime = time; }
public void ChangeCurrentBar(IKLineBar chart, int barPos) { this.BarPos = barPos; if (chart == null) { list_Time.ClearTmpValue(); list_Start.ClearTmpValue(); list_High.ClearTmpValue(); list_Low.ClearTmpValue(); list_End.ClearTmpValue(); list_Mount.ClearTmpValue(); list_Money.ClearTmpValue(); list_Hold.ClearTmpValue(); list_Height.ClearTmpValue(); list_HeightPercent.ClearTmpValue(); list_BlockHigh.ClearTmpValue(); list_BlockLow.ClearTmpValue(); list_BlockHeight.ClearTmpValue(); list_BlockHeightPercent.ClearTmpValue(); list_UpPercent.ClearTmpValue(); } else { list_Time.SetTmpValue(barPos, chart.Time); list_Start.SetTmpValue(barPos, chart.Start); list_High.SetTmpValue(barPos, chart.High); list_Low.SetTmpValue(barPos, chart.Low); list_End.SetTmpValue(barPos, chart.End); list_Mount.SetTmpValue(barPos, chart.Mount); list_Money.SetTmpValue(barPos, chart.Money); list_Hold.SetTmpValue(barPos, chart.Hold); list_Height.SetTmpValue(barPos, chart.Height); list_HeightPercent.SetTmpValue(barPos, chart.HeightPercent); list_BlockHigh.SetTmpValue(barPos, chart.BlockHigh); list_BlockLow.SetTmpValue(barPos, chart.BlockLow); list_BlockHeight.SetTmpValue(barPos, chart.BlockHeight); list_BlockHeightPercent.SetTmpValue(barPos, chart.BlockHeightPercent); float upPercent = barPos == 0 ? (float)NumberUtils.percent(chart.End, chart.Start) : (float)NumberUtils.percent(chart.End, klineData.Arr_End[barPos - 1]); list_UpPercent.SetTmpValue(barPos, upPercent); } }
private void Loop(int startBarPos, int endBarPos) { double start = klineData.GetBar(startBarPos).End; double high = 0; double low = double.MaxValue; for (int i = startBarPos; i <= endBarPos; i++) { IKLineBar bar = klineData.GetBar(i); if (bar.High > high) { high = bar.High; } if (bar.Low < low) { low = bar.Low; } } }
private void ForwardBar_CurrentPeriod(IKLineData_RealTime klineData, IKLineBar klineBar) { double time = klineBar.Time; IKLineBar oldbar = klineData.GetBar(klineData.BarPos); KLineBar bar = new KLineBar(); bar.Time = klineBar.Time; bar.Start = oldbar.Start; bar.High = klineBar.High > oldbar.High ? klineBar.High : oldbar.High; bar.Low = klineBar.Low < oldbar.Low ? klineBar.Low : oldbar.Low; bar.End = klineBar.End; bar.Mount = oldbar.Mount + klineBar.Mount; bar.Money = oldbar.Money + klineBar.Money; bar.Hold = klineBar.Hold; klineData.ChangeCurrentBar(bar); }
//private KLineDataIndeier klineDataIndeier; public void ChangeTime(double time) { if (this.time == time) { return; } if (!IsInCurrentKLineData(time)) { Change(code, time, period); return; } RefreshChartBuilder(time); this.time = time; this.currentKLineIndex = TimeIndeierUtils.IndexOfTime_KLine(klineData, time); this.chartBuilder.ChangeTime(time); IKLineBar chart = this.chartBuilder.GetCurrentChart(); klineData.SetRealTimeData(chart, currentKLineIndex); }
private void Print(IKLineBar bar, IList <KLineBarPos> posList, DataForForward_Code data1) { if (posList == null) { return; } StringBuilder sb = new StringBuilder(); sb.Append(bar.Time + ":"); //Console.Write(bar.Time + ":"); for (int i = 0; i < posList.Count; i++) { KLineBarPos periodBar = posList[i]; //Console.Write(periodBar + "," + data1.GetKLineData(periodBar.KLinePeriod).GetBar(periodBar.BarPos).Time + "|"); sb.Append(periodBar + "," + data1.GetKLineData(periodBar.KLinePeriod).GetBar(periodBar.BarPos).Time + "|"); //Assert.AreEqual(bar.Time, data1.GetKLineData(posList[i].KLinePeriod).Time); } //Console.WriteLine(); printLine.Add(sb.ToString()); }
private void DrawCandle(Graphics g, IKLineBar chart, int index, PriceGraphicMapping priceMapping, float blockWidth, float blockPadding) { bool isRed = chart.End > chart.Start; Brush b = isRed ? this.ColorConfig.Brush_CandleBlockUp : this.ColorConfig.Brush_CandleBlockDown; if (chart.End.Equals(chart.Start)) { b = this.ColorConfig.Brush_CandleFlat; } Pen p = new Pen(b); float XMiddle = priceMapping.CalcX(index); float YTop = priceMapping.CalcY(chart.High); float YBottom = priceMapping.CalcY(chart.Low); float YBlockTop = (float)priceMapping.CalcY(isRed ? chart.End : chart.Start); float YBlockBottom = (float)priceMapping.CalcY(isRed ? chart.Start : chart.End); //画上影线和下影线 g.DrawLine(p, new PointF(XMiddle, YTop), new PointF(XMiddle, YBlockTop)); g.DrawLine(p, new PointF(XMiddle, YBottom), new PointF(XMiddle, YBlockBottom)); float halfBlockWidth = (blockWidth - blockPadding) / 2; float XLeft = XMiddle - halfBlockWidth; float XRight = XMiddle + halfBlockWidth; //画block if (chart.End == chart.Start) { g.DrawLine(p, XLeft, YBlockBottom, XRight, YBlockBottom); } else if (isRed) { g.DrawRectangle(p, XLeft, YBlockTop, halfBlockWidth * 2, YBlockBottom - YBlockTop); } else { g.FillRectangle(b, XLeft, YBlockTop, halfBlockWidth * 2, YBlockBottom - YBlockTop); } }
public override void OnBar(object sender, IStrategyOnBarArgument currentData) { if (isOpen) { return; } IStrategyOnBarInfo barInfo = currentData.MainBar; IKLineBar currentBar = barInfo.KLineBar; IKLineBar lastBar = barInfo.KLineData.GetBar(barInfo.BarPos - 1); //多头吞噬 if (currentBar.BlockHigh > lastBar.BlockHigh && currentBar.BlockLow < lastBar.BlockLow && currentBar.isRed() && currentBar.BlockHeight >= 20) { openPrice = currentBar.End; StrategyHelper.Trader.Open(currentData.Code, data.market.OrderSide.Buy, openPrice, 10); isOpen = true; strategyResult.AddRow(currentBar.Code, barInfo.KLineBar.Time, new object[] { currentBar.BlockHeight, lastBar.BlockHeight }); IStrategyDrawer_PriceRect drawer = StrategyHelper.Drawer.GetDrawer_KLine(barInfo.KLinePeriod); drawer.DrawPoint(new graphic.shape.PriceShape_Point(barInfo.BarPos, openPrice)); } }
private void ForwardKLineData(IKLineData_RealTime klineData) { IKLineBar nextMainBar = mainKLineData.GetBar(mainKLineData.BarPos + 1); int currentBarPos = klineData.BarPos; int nextBarPos = currentBarPos + 1; if (nextBarPos >= klineData.Length) { ForwardBar_CurrentPeriod(klineData, nextMainBar); } else { double nextTime = klineData.Arr_Time[nextBarPos]; double nextMainTime = mainKLineData.Arr_Time[mainKLineData.BarPos + 1]; if (nextMainTime >= nextTime) { ForwardBar_NextPeriod(klineData, nextMainBar); } else { ForwardBar_CurrentPeriod(klineData, nextMainBar); } } }
private void DrawMount(Graphics g, IKLineBar chart, int index) { Rectangle rectangle = DisplayRect; bool isRed = chart.End > chart.Start; Brush b = new SolidBrush(isRed ? Color.Red : Color.LightSeaGreen); Pen p = new Pen(b); double XMiddle = priceMapping.CalcX(index); double YTop = priceMapping.CalcY(chart.Mount); double YBottom = rectangle.Bottom; double halfBlockWidth = (BlockWidth - BlockPadding) / 2; float XLeft = (float)(XMiddle - halfBlockWidth); float XRight = (float)(XMiddle + halfBlockWidth); if (isRed) { g.DrawRectangle(p, (int)XLeft, (int)YTop, (int)halfBlockWidth * 2, (int)(YBottom - YTop)); } else { g.FillRectangle(b, (int)XLeft, (int)YTop, (int)halfBlockWidth * 2, (int)(YBottom - YTop)); } }
private void ForwardBar_NextPeriod(IKLineData_RealTime klineData, IKLineBar klineBar) { klineData.BarPos++; klineData.ChangeCurrentBar(klineBar); }
public MaCrossPoint(int barPos, IKLineBar klineBar, double maPrice) { this.barPos = barPos; this.klineBar = klineBar; this.maPrice = maPrice; }
/// <summary> /// 修改当前chart, /// </summary> /// <param name="chart"></param> public void ChangeCurrentBar(IKLineBar chart) { ChangeCurrentBar(chart, BarPos); }
private void ForwardBar_NextPeriod(KLineData_RealTime klineData, IKLineBar klineBar) { klineData.BarPos++; klineData.SetRealTimeData(klineBar); }
private static KLineBar GetKLineBar(IKLineBar klineBar, ITickBar tickBar) { return(KLineUtils.GetKLineBar(klineBar, tickBar)); }
public KLineBarInfo(IKLineBar klineBar, int barPos) { this.KLineBar = klineBar; this.BarPos = barPos; }