private bool isSwingHighTest(int bar) { double thisBarHigh = Bars.GetHigh(bar); bool nextHighBarLower = Bars.GetHigh(bar + 1) <= thisBarHigh; //Check enough previous bars if (!nextHighBarLower) { // return false; } // check previous X bars higher than this bar's low double highestPreviousPeriod = Bars.GetLow(bar); int previousBarIndex = bar - 1; int lastBarToCheckIndex = bar - SwingPeriod; for (int i = previousBarIndex; i >= lastBarToCheckIndex; i--) { // bool doubleTop = (i == previousBarIndex) && Bars.GetHigh(i).Equals(thisBarHigh); // if (!doubleTop) { if (isInsideBar(i)) { lastBarToCheckIndex--; } else { highestPreviousPeriod = Math.Max(highestPreviousPeriod, Bars.GetHigh(i)); } // } } return(thisBarHigh > highestPreviousPeriod); }
private bool isSwingLowTest(int bar) { double thisBarLow = Bars.GetLow(bar); bool nextLowBarHigher = Bars.GetLow(bar + 1) >= thisBarLow; //Check enough previous bars if (!nextLowBarHigher) { // return false; } // check previous X bars higher than this bar's low double lowestPreviousPeriod = Bars.GetHigh(bar); int previousBarIndex = bar - 1; int lastBarToCheckIndex = bar - SwingPeriod; for (int i = previousBarIndex; i >= lastBarToCheckIndex; i--) { //bool doubleBottom = (i == previousBarIndex) && Bars.GetLow(i).Equals(thisBarLow); //if (!doubleBottom) { if (isInsideBar(i)) { lastBarToCheckIndex--; } else { lowestPreviousPeriod = Math.Min(lowestPreviousPeriod, Bars.GetLow(i)); } // } } return(thisBarLow < lowestPreviousPeriod); }
public override void GetMinMaxValues(ChartControl chartControl, ref double min, ref double max) { if (Bars == null) { return; } int lastBar = Math.Min(LastBarIndexPainted, Bars.Count - 1); int firstBar = FirstBarIndexPainted; min = Double.MaxValue; max = Double.MinValue; for (int indx = firstBar; indx <= lastBar; ++indx) { if ((indx <= CurrentBar) && (indx >= 1)) { min = Math.Min(min, Bars.GetLow(indx)); max = Math.Max(max, Bars.GetHigh(indx)); } } if (max < 0) { return; } // extend the top by 1 tick, to make room for text max = max + quantizeFactor; min = min - quantizeFactor; }
/// <summary> /// Hold the indicator volume from the underlining indicator /// /// </summary> #region Methods public double GetPriceByType(int barNo, PriceSubtype priceType) { Print(CurrentBar + ":GetPriceByType=" + barNo + "," + priceType); double prc = 0; switch (priceType) { case PriceSubtype.Low: prc = Bars.GetLow(barNo); break; case PriceSubtype.High: prc = Bars.GetHigh(barNo); break; case PriceSubtype.Open: prc = Bars.GetOpen(barNo); break; case PriceSubtype.Close: prc = Bars.GetClose(barNo); break; } return(prc); }
//#region plotstuff public override void GetMinMaxValues(ChartControl chartControl, ref double min, ref double max) { if (Bars == null) { return; } int lastBar = Math.Min(LastBarIndexPainted, Bars.Count - 1); int firstBar = FirstBarIndexPainted; min = Double.MaxValue; max = Double.MinValue; var canht = ChartControl.CanvasBottom - ChartControl.CanvasTop; for (int indx = firstBar; indx <= lastBar; ++indx) { if ((indx <= CurrentBar) && (indx >= 1)) { min = Math.Min(min, Bars.GetLow(indx)); max = Math.Max(max, Bars.GetHigh(indx)); } } chartedMin = min; // store off the charted values chartedMax = max; if (max < 0) { return; } // extend the top by 1 tick, to make room for text max = max + TickSize; }
private bool isInsideBar(int barIndex) { int previousBarIndex = barIndex - 1; bool withinHigh = Bars.GetHigh(barIndex) <= Bars.GetHigh(previousBarIndex); bool withinLow = Bars.GetLow(barIndex) >= Bars.GetLow(previousBarIndex); return(withinHigh && withinLow); }
public override void OnRender(ChartControl chartControl, ChartScale chartScale, ChartBars chartBars) { Bars bars = chartBars.Bars; float lineWidth = (float)Math.Max(1, BarWidth); Vector2 point0 = new Vector2(); Vector2 point1 = new Vector2(); Vector2 point2 = new Vector2(); Vector2 point3 = new Vector2(); Vector2 point4 = new Vector2(); Vector2 point5 = new Vector2(); for (int idx = chartBars.FromIndex; idx <= chartBars.ToIndex; idx++) { SharpDX.Direct2D1.Brush overriddenBrush = chartControl.GetBarOverrideBrush(chartBars, idx); double closeValue = bars.GetClose(idx); float close = chartScale.GetYByValue(closeValue); float high = chartScale.GetYByValue(bars.GetHigh(idx)); float low = chartScale.GetYByValue(bars.GetLow(idx)); double openValue = bars.GetOpen(idx); float open = chartScale.GetYByValue(openValue); float x = chartControl.GetXByBarIndex(chartBars, idx); point0.X = point1.X = x; point0.Y = high - lineWidth * 0.5f; point1.Y = low + lineWidth * 0.5f; SharpDX.Direct2D1.Brush b = overriddenBrush ?? (closeValue >= openValue ? UpBrushDX : DownBrushDX); if (!(b is SharpDX.Direct2D1.SolidColorBrush)) { TransformBrush(b, new RectangleF(point0.X - lineWidth * 1.5f, point0.Y, lineWidth * 3, point1.Y - point0.Y)); } RenderTarget.DrawLine(point0, point1, b, lineWidth); if (!Equals(Mode, OhlcMode.HiLo)) { point2.X = x + lineWidth * 1.5f; point2.Y = close; point3.X = x; point3.Y = close; RenderTarget.DrawLine(point2, point3, b, lineWidth); if (Equals(Mode, OhlcMode.OHLC)) { point4.X = x - lineWidth * 1.5f; point4.Y = open; point5.X = x; point5.Y = open; RenderTarget.DrawLine(point4, point5, b, lineWidth); } } } }
//#region plotstuff public override void GetMinMaxValues(ChartControl chartControl, ref double min, ref double max) { if (Bars == null) { return; } int lastBar = Math.Min(LastBarIndexPainted, Bars.Count - 1); int firstBar = FirstBarIndexPainted; min = Double.MaxValue; max = Double.MinValue; var canht = ChartControl.CanvasBottom - ChartControl.CanvasTop; for (int indx = firstBar; indx <= lastBar; ++indx) { if ((indx <= CurrentBar) && (indx >= 1)) { min = Math.Min(min, Bars.GetLow(indx)); max = Math.Max(max, Bars.GetHigh(indx)); } } chartedMin = min; // store off the charted values chartedMax = max; if (max < 0) { return; } // extend the top by 1 tick, to make room for text max = max + TickSize; if (displaytotals) { // extend the bottom by enough ticks to make a reasonable area for the // data zone... var cwidth = Math.Min(18, Math.Max(ChartControl.BarSpace / 4.0, 2.0)); var cheight = 5 * 1.25 * cwidth; // assume 5:4 height/width in font, and we need 5 lines // derivation: // we need cheight pix. ticks_needed = pix_needed/pix per tick // pix per tick == numpix / numticks, so // x = pix_needed*(numticks+x)/numpix // x*numpix = pix_needed *numticks + pix_needed*x // x(numpix - pix_needed) = pix_needed*numticks // x = pix_needed*numticks / (numpix - pix_needed) var denom = canht - cheight; if (denom == 0) { denom = 1; } min = min - ((cheight * ((max - min) / TickSize)) / denom) * TickSize; } }
protected override void OnBarUpdate() { // smaFast = SMA(5); // smaSlow = SMA(20); if (CurrentBar > 1) { if (Bars.GetHigh(CurrentBar) - Bars.GetHigh(CurrentBar - 1) < 0 && direction > 0) { max.Add(Bars.GetHigh(CurrentBar - 1)); maxIndex.Add(CurrentBar - 1); upperLines.Clear(); if (min.Count > 15) { min.RemoveAt(0); } if (max.Count > 15) { max.RemoveAt(0); upperTrend(); } } if (Bars.GetLow(CurrentBar) - Bars.GetLow(CurrentBar - 1) > 0 && direction < 0) { min.Add(Bars.GetLow(CurrentBar - 1)); minIndex.Add(CurrentBar - 1); } direction = Bars.GetHigh(CurrentBar) - Bars.GetHigh(CurrentBar - 1); if (max.Count > 15) { max.RemoveAt(0); maxIndex.RemoveAt(0); } PrintTo = PrintTo.OutputTab1; Print(Bars.GetTime(CurrentBar - 1) + ": " + trendState()); if (upperLines.Count > 0) { PrintTo = PrintTo.OutputTab2; Print(Bars.GetTime(CurrentBar) + ": " + upperLines[upperLines.Count - 1].slope + " " + upperLines[upperLines.Count - 1].intercept + " " + upperLines.Count + " " + upperLines[upperLines.Count - 1].tipCount); upperTest = upperLines[upperLines.Count - 1].slope * (CurrentBar - upperLines[upperLines.Count - 1].index) + upperLines[upperLines.Count - 1].intercept; // bottomTest = bottomSlope*(CurrentBar - bottomIndex) + bottomIntercept; if (Bars.GetClose(CurrentBar) > upperTest) { // stopLoss = Math.Round((upperTest - bottomTest)/0.25); //targetProfit = stopLoss*2; SetProfitTarget(CalculationMode.Ticks, 4); SetStopLoss(CalculationMode.Ticks, 4); EnterLong(3); upperLines.RemoveAt(upperLines.Count - 1); } } } }
private void plotSwings(Dictionary <int, string> reducedPivots, string prefix) { List <int> orderedMarkers = reducedPivots.Keys.ToList(); orderedMarkers.Sort(); int objnum = 0; int previousMarker = 0; double previousValue = 0; string previous = ""; foreach (int marker in orderedMarkers) { double value = 0; if (reducedPivots[marker].Equals(MIN_MAX) && previous.Equals(MIN)) { value = Bars.GetHigh(marker); } else if (reducedPivots[marker].Equals(MIN_MAX) && previous.Equals(MAX)) { value = Bars.GetLow(marker); } else if (reducedPivots[marker].Equals(MAX)) { value = Bars.GetHigh(marker); } else { value = Bars.GetLow(marker); } if (objnum == 0) { previousMarker = marker; previousValue = value; objnum++; continue; } DateTime startTimeValue = Bars.GetTime(previousMarker); DateTime endTimeValue = Bars.GetTime(marker); Draw.Line(this, objnum.ToString() + prefix + "PivotLine", false, startTimeValue, previousValue, endTimeValue, value, Brushes.White, DashStyleHelper.Dot, 1, true); objnum++; previousValue = value; previousMarker = marker; previous = reducedPivots[marker]; } }
protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask) { if (bars.Count == 0) { AddBar(bars, open, high, low, close, TimeToBarTime(time, bars.BarsPeriod.Value), volume); } else if (time.Month <= bars.LastBarTime.Month && time.Year == bars.LastBarTime.Year || time.Year < bars.LastBarTime.Year) { if (high.ApproxCompare(bars.GetHigh(bars.Count - 1)) != 0 || low.ApproxCompare(bars.GetLow(bars.Count - 1)) != 0 || close.ApproxCompare(bars.GetClose(bars.Count - 1)) != 0 || volume > 0) { UpdateBar(bars, high, low, close, bars.LastBarTime, volume); } } else { AddBar(bars, open, high, low, close, TimeToBarTime(time, bars.BarsPeriod.Value), volume); } }
protected override void OnBarUpdate() { if (BarsInProgress != 0) { return; } double close = Close[0]; double open = Open[0]; double max = Bars.GetHigh(CurrentBar); double min = Bars.GetLow(CurrentBar); double size = Math.Abs(open - close); if (size >= BodySize && open >= min) { EnterLong(); } }
private void UpdateHiLoPrice() { String strPrint = string.Format("StopEnBarIndex={0}", StopEnBarIndex); if (StopEnBarIndex >= 0) { int barsBack = CurrentBars[0] - StopEnBarIndex; strPrint = string.Format("Hi[{0}]={1} \r\nLo[{2}]={3}", barsBack, Bars.GetHigh(StopEnBarIndex), barsBack, Bars.GetLow(StopEnBarIndex)); } //Draw.ArrowUp(this, "tag1", true, 0, Lows[0][0] - TickSize, Brushes.Red); //Draw.Diamond(this, "tag1", true, Bars.GetTime(StopEnBarIndex), Bars.GetLow(StopEnBarIndex) - TickSize, Brushes.Red); //Draw.Diamond(this, "tag1", true, 0, Low[0] - TickSize, Brushes.Red); //giChartTrader.SetStopPrice(Bars.GetLow(StopEnBarIndex).ToString()); RemoveDrawObject(tagHiLoPriceTextField); Draw.TextFixed(this, tagHiLoPriceTextField, strPrint, TextPosition.BottomRight, Brushes.Blue, new Gui.Tools.SimpleFont("Arial", 12), Brushes.Transparent, Brushes.Transparent, 100); }
void activateBearSwing(int firstIndex, int secondIndex, int thirdIndex) { //Take last 3 //Check if bear swing double firstBarValue = Bars.GetHigh(firstIndex); double secondBarValue = Bars.GetLow(secondIndex); double thirdBarValue = Bars.GetHigh(thirdIndex); bool firstIsMoreThanThird = firstBarValue > thirdBarValue; if (!firstIsMoreThanThird) { return; } bool secondLower = secondBarValue < firstBarValue && secondBarValue < thirdBarValue; if (!secondLower) { return; } if (thirdIndex > lastPatternIndex) { observePattern = OBSERVE_BEAR; observeTrigger = secondBarValue; lastPatternIndex = thirdIndex; triggerStatus = WAITING_FOR_TRIGGER; triggerRisk = (thirdBarValue - secondBarValue) * 1.2; DateTime startTimeValue = Bars.GetTime(firstIndex); DateTime endTimeValue = Bars.GetTime(secondIndex); Draw.Line(this, "ActiveSwing1", false, startTimeValue, firstBarValue, endTimeValue, secondBarValue, Brushes.Yellow, DashStyleHelper.Solid, 2, true); startTimeValue = Bars.GetTime(secondIndex); endTimeValue = Bars.GetTime(thirdIndex); Draw.Line(this, "ActiveSwing2", false, startTimeValue, secondBarValue, endTimeValue, thirdBarValue, Brushes.Yellow, DashStyleHelper.Solid, 2, true); } }
public override void OnRender(ChartControl chartControl, ChartScale chartScale, ChartBars chartBars) { Bars bars = chartBars.Bars; float barWidth = GetBarPaintWidth(BarWidthUI); Vector2 point0 = new Vector2(); Vector2 point1 = new Vector2(); RectangleF rect = new RectangleF(); for (int idx = chartBars.FromIndex; idx <= chartBars.ToIndex; idx++) { Brush overriddenBarBrush = chartControl.GetBarOverrideBrush(chartBars, idx); Brush overriddenOutlineBrush = chartControl.GetCandleOutlineOverrideBrush(chartBars, idx); double closeValue = bars.GetClose(idx); int close = chartScale.GetYByValue(closeValue); int high = chartScale.GetYByValue(bars.GetHigh(idx)); int low = chartScale.GetYByValue(bars.GetLow(idx)); double openValue = bars.GetOpen(idx); int open = chartScale.GetYByValue(openValue); int x = chartControl.GetXByBarIndex(chartBars, idx); if (Math.Abs(open - close) < 0.0000001) { // Line point0.X = x - barWidth * 0.5f; point0.Y = close; point1.X = x + barWidth * 0.5f; point1.Y = close; Brush b = overriddenOutlineBrush ?? Stroke.BrushDX; if (!(b is SolidColorBrush)) { TransformBrush(overriddenOutlineBrush ?? Stroke.BrushDX, new RectangleF(point0.X, point0.Y - Stroke.Width, barWidth, Stroke.Width)); } RenderTarget.DrawLine(point0, point1, b, Stroke.Width, Stroke.StrokeStyle); } else { // Candle rect.X = x - barWidth * 0.5f + 0.5f; rect.Y = Math.Min(close, open); rect.Width = barWidth - 1; rect.Height = Math.Max(open, close) - Math.Min(close, open); Brush brush = overriddenBarBrush ?? (closeValue >= openValue ? UpBrushDX : DownBrushDX); if (!(brush is SolidColorBrush)) { TransformBrush(brush, rect); } RenderTarget.FillRectangle(rect, brush); brush = overriddenOutlineBrush ?? Stroke.BrushDX; if (!(brush is SolidColorBrush)) { TransformBrush(brush, rect); } RenderTarget.DrawRectangle(rect, overriddenOutlineBrush ?? Stroke.BrushDX, Stroke.Width, Stroke.StrokeStyle); } Brush br = overriddenOutlineBrush ?? Stroke2.BrushDX; // High wick if (high < Math.Min(open, close)) { point0.X = x; point0.Y = high; point1.X = x; point1.Y = Math.Min(open, close); if (!(br is SolidColorBrush)) { TransformBrush(br, new RectangleF(point0.X - Stroke2.Width, point0.Y, Stroke2.Width, point1.Y - point0.Y)); } RenderTarget.DrawLine(point0, point1, br, Stroke2.Width, Stroke2.StrokeStyle); } // Low wick if (low > Math.Max(open, close)) { point0.X = x; point0.Y = low; point1.X = x; point1.Y = Math.Max(open, close); if (!(br is SolidColorBrush)) { TransformBrush(br, new RectangleF(point1.X - Stroke2.Width, point1.Y, Stroke2.Width, point0.Y - point1.Y)); } RenderTarget.DrawLine(point0, point1, br, Stroke2.Width, Stroke2.StrokeStyle); } } }
protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask) { if (SessionIterator == null) { SessionIterator = new SessionIterator(bars); } bool isNewSession = SessionIterator.IsNewSession(time, isBar); if (isNewSession) { SessionIterator.GetNextSession(time, isBar); } #region Building Bars from Base Period if (bars.Count != tmpCount) // Reset cache when bars are trimmed { if (bars.Count == 0) { tmpTime = Core.Globals.MinDate; tmpVolume = 0; tmpDayCount = 0; tmpTickCount = 0; } else { tmpTime = bars.GetTime(bars.Count - 1); tmpVolume = bars.GetVolume(bars.Count - 1); tmpTickCount = bars.TickCount; tmpDayCount = bars.DayCount; bars.LastPrice = anchorPrice = bars.GetClose(bars.Count - 1); } } switch (BarsPeriod.BaseBarsPeriodType) { case BarsPeriodType.Day: tmpTime = time.Date; if (!isBar) { tmpDayCount++; if (tmpTime < time.Date) { tmpTime = time.Date; // Make sure timestamps are ascending } } if (isBar && prevTimeD != tmpTime) { tmpDayCount++; } if (isBar && bars.Count > 0 && tmpTime == bars.LastBarTime.Date || !isBar && bars.Count > 0 && tmpTime <= bars.LastBarTime.Date || tmpDayCount < BarsPeriod.BaseBarsPeriodValue) { endOfBar = false; } else { prevTime = prevTimeD == Core.Globals.MinDate ? tmpTime : prevTimeD; prevTimeD = tmpTime; endOfBar = true; } break; case BarsPeriodType.Minute: if (tmpTime == Core.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeMinute(bars, time, isBar); } if (isBar && time <= tmpTime || !isBar && time < tmpTime) { endOfBar = false; } else { prevTime = tmpTime; tmpTime = TimeToBarTimeMinute(bars, time, isBar); endOfBar = true; } break; case BarsPeriodType.Volume: if (tmpTime == Core.Globals.MinDate) { tmpVolume = volume; endOfBar = tmpVolume >= BarsPeriod.BaseBarsPeriodValue; prevTime = tmpTime = time; if (endOfBar) { tmpVolume = 0; } break; } tmpVolume += volume; endOfBar = tmpVolume >= BarsPeriod.BaseBarsPeriodValue; if (endOfBar) { prevTime = tmpTime; tmpVolume = 0; tmpTime = time; } break; case BarsPeriodType.Month: if (tmpTime == Core.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeMonth(time, BarsPeriod.BaseBarsPeriodValue); } if (time.Month <= tmpTime.Month && time.Year == tmpTime.Year || time.Year < tmpTime.Year) { endOfBar = false; } else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeMonth(time, BarsPeriod.BaseBarsPeriodValue); } break; case BarsPeriodType.Second: if (tmpTime == Core.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeSecond(bars, time, isBar); } if (time <= tmpTime) { endOfBar = false; } else { prevTime = tmpTime; tmpTime = TimeToBarTimeSecond(bars, time, isBar); endOfBar = true; } break; case BarsPeriodType.Tick: if (tmpTime == Core.Globals.MinDate || BarsPeriod.BaseBarsPeriodValue == 1) { prevTime = tmpTime; if (prevTime == Core.Globals.MinDate) { prevTime = time; } tmpTime = time; endOfBar = BarsPeriod.BaseBarsPeriodValue == 1; break; } if (tmpTickCount < BarsPeriod.BaseBarsPeriodValue) { tmpTime = time; endOfBar = false; tmpTickCount++; } else { prevTime = tmpTime; tmpTime = time; endOfBar = true; tmpTickCount = 1; } break; case BarsPeriodType.Week: if (tmpTime == Core.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeWeek(time.Date, tmpTime.Date, BarsPeriod.BaseBarsPeriodValue); } if (time.Date <= tmpTime.Date) { endOfBar = false; } else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeWeek(time.Date, tmpTime.Date, BarsPeriod.BaseBarsPeriodValue); } break; case BarsPeriodType.Year: if (tmpTime == Core.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeYear(time, BarsPeriod.BaseBarsPeriodValue); } if (time.Year <= tmpTime.Year) { endOfBar = false; } else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeYear(time, BarsPeriod.BaseBarsPeriodValue); } break; } #endregion #region P&F logic double tickSize = bars.Instrument.MasterInstrument.TickSize; boxSize = Math.Floor(10000000.0 * BarsPeriod.Value * tickSize) / 10000000.0; reversalSize = BarsPeriod.Value2 * boxSize; if (bars.Count == 0 || IsIntraday && bars.IsResetOnNewTradingDay && isNewSession) { if (bars.Count > 0) { double lastHigh = bars.GetHigh(bars.Count - 1); double lastLow = bars.GetLow(bars.Count - 1); double lastClose = bars.GetClose(bars.Count - 1); DateTime lastTime = bars.GetTime(bars.Count - 1); bars.LastPrice = anchorPrice = lastClose; if (bars.Count == tmpCount) { CalculatePfBar(bars, lastHigh, lastLow, lastClose, prevTime == Core.Globals.MinDate ? time : prevTime, lastTime); } } AddBar(bars, close, close, close, close, tmpTime, volume); anchorPrice = close; trend = Trend.Undetermined; prevTime = tmpTime; volumeCount = 0; bars.LastPrice = close; tmpCount = bars.Count; tmpHigh = high; tmpLow = low; return; } double c = bars.GetClose(bars.Count - 1); double h = bars.GetHigh(bars.Count - 1); double l = bars.GetLow(bars.Count - 1); DateTime t = bars.GetTime(bars.Count - 1); if (endOfBar) { CalculatePfBar(bars, h, l, c, prevTime, t); volumeCount = volume; tmpHigh = high; tmpLow = low; } else { tmpHigh = high > tmpHigh ? high : tmpHigh; tmpLow = low < tmpLow ? low : tmpLow; volumeCount += volume; } bars.LastPrice = close; tmpCount = bars.Count; #endregion }
private void bearSwing(Dictionary <int, string> reducedPivots, string prefix) { List <int> orderedMarkers = reducedPivots.Keys.ToList(); orderedMarkers.Sort(); int objnum = 0; for (int markerIndex = 0; markerIndex < (orderedMarkers.Count() - 4); markerIndex++) { int firstIndex = markerIndex; int secondIndex = markerIndex + 1; int thirdIndex = markerIndex + 2; int fourthIndex = markerIndex + 3; int firstBarIndex = orderedMarkers[firstIndex]; int secondBarIndex = orderedMarkers[secondIndex]; int thirdBarIndex = orderedMarkers[thirdIndex]; int fourthBarIndex = orderedMarkers[fourthIndex]; bool firstValueIsMax = reducedPivots[firstBarIndex].Equals(MAX); if (!firstValueIsMax) { continue; } double firstBarValue = Bars.GetHigh(firstBarIndex); double secondBarValue = Bars.GetLow(secondBarIndex); double thirdBarValue = Bars.GetHigh(thirdBarIndex); double fourthBarValue = Bars.GetLow(fourthBarIndex); bool firstIsMoreThanThird = firstBarValue > thirdBarValue; if (!firstIsMoreThanThird) { continue; } bool secondLower = secondBarValue < firstBarValue && secondBarValue < thirdBarValue; if (!secondLower) { continue; } bool isSecondTriggered = false; for (int i = thirdBarIndex; i < fourthBarIndex; i++) { if (Bars.GetLow(i) < secondBarValue) { isSecondTriggered = true; } } if (isSecondTriggered) { DateTime startTimeValue = Bars.GetTime(firstBarIndex); DateTime endTimeValue = Bars.GetTime(secondBarIndex); Draw.Line(this, objnum.ToString() + prefix + "Bear", false, startTimeValue, firstBarValue, endTimeValue, secondBarValue, Brushes.Purple, DashStyleHelper.Solid, 2, true); objnum++; startTimeValue = Bars.GetTime(secondBarIndex); endTimeValue = Bars.GetTime(thirdBarIndex); Draw.Line(this, objnum.ToString() + prefix + "Bear", false, startTimeValue, secondBarValue, endTimeValue, thirdBarValue, Brushes.Purple, DashStyleHelper.Solid, 2, true); objnum++; } } }
protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask) { if (SessionIterator == null) { SessionIterator = new SessionIterator(bars); } double haClose = 0.0; double haHigh = 0.0; double haLow = 0.0; double haOpen = 0.0; switch (BarsPeriod.BaseBarsPeriodType) { case BarsPeriodType.Day: { if (bars.Count == 0) { if (isBar || bars.TradingHours.Sessions.Count == 0) { AddBar(bars, open, high, low, close, time.Date, volume); } else { SessionIterator.CalculateTradingDay(time, false); AddBar(bars, open, high, low, close, SessionIterator.ActualTradingDayExchange, volume); } } else { DateTime barTime; if (isBar) { barTime = time.Date; } else { if (bars.TradingHours.Sessions.Count > 0 && SessionIterator.IsNewSession(time, false)) { SessionIterator.CalculateTradingDay(time, false); barTime = SessionIterator.ActualTradingDayExchange; if (barTime < bars.LastBarTime.Date) { barTime = bars.LastBarTime.Date; // Make sure timestamps are ascending } } else { barTime = bars.LastBarTime.Date; // Make sure timestamps are ascending } } if (bars.DayCount < bars.BarsPeriod.BaseBarsPeriodValue || isBar && bars.Count > 0 && barTime == bars.LastBarTime.Date || !isBar && bars.Count > 0 && barTime <= bars.LastBarTime.Date) { haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, bars.GetOpen(bars.Count - 1))); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, bars.GetOpen(bars.Count - 1))); UpdateBar(bars, haHigh, haLow, haClose, barTime, volume); } else { haOpen = bars.Instrument.MasterInstrument.RoundToTickSize((bars.GetOpen(bars.Count - 1) + bars.GetClose(bars.Count - 1)) / 2.0); haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, haOpen)); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, haOpen)); AddBar(bars, haOpen, haHigh, haLow, haClose, barTime, volume); } } break; } case BarsPeriodType.Minute: { if (bars.Count == 0) { AddBar(bars, open, high, low, close, TimeToBarTimeMinute(bars, time, isBar), volume); } else if (!isBar && time < bars.LastBarTime) { haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, bars.GetOpen(bars.Count - 1))); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, bars.GetOpen(bars.Count - 1))); UpdateBar(bars, haHigh, haLow, haClose, bars.LastBarTime, volume); } else if (isBar && time <= bars.LastBarTime) { haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, bars.GetOpen(bars.Count - 1))); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, bars.GetOpen(bars.Count - 1))); UpdateBar(bars, haHigh, haLow, haClose, bars.LastBarTime, volume); } else { haOpen = bars.Instrument.MasterInstrument.RoundToTickSize((bars.GetOpen(bars.Count - 1) + bars.GetClose(bars.Count - 1)) / 2.0); haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, haOpen)); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, haOpen)); time = TimeToBarTimeMinute(bars, time, isBar); AddBar(bars, haOpen, haHigh, haLow, haClose, time, volume); } break; } case BarsPeriodType.Month: { if (bars.Count == 0) { AddBar(bars, open, high, low, close, TimeToBarTimeMonth(time, bars.BarsPeriod.BaseBarsPeriodValue), volume); } else if (time.Month <= bars.LastBarTime.Month && time.Year == bars.LastBarTime.Year || time.Year < bars.LastBarTime.Year) { if (high.ApproxCompare(bars.GetHigh(bars.Count - 1)) != 0 || low.ApproxCompare(bars.GetLow(bars.Count - 1)) != 0 || close.ApproxCompare(bars.GetClose(bars.Count - 1)) != 0 || volume > 0) { haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, bars.GetOpen(bars.Count - 1))); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, bars.GetOpen(bars.Count - 1))); UpdateBar(bars, haHigh, haLow, haClose, bars.LastBarTime, volume); } } else { haOpen = bars.Instrument.MasterInstrument.RoundToTickSize((bars.GetOpen(bars.Count - 1) + bars.GetClose(bars.Count - 1)) / 2.0); haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, haOpen)); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, haOpen)); AddBar(bars, haOpen, haHigh, haLow, haClose, TimeToBarTimeMonth(time, bars.BarsPeriod.BaseBarsPeriodValue), volume); } break; } case BarsPeriodType.Second: { if (bars.Count == 0) { DateTime barTime = TimeToBarTimeSecond(bars, time, isBar); AddBar(bars, open, high, low, close, barTime, volume); } else { if (bars.BarsPeriod.BaseBarsPeriodValue > 1 && time < bars.LastBarTime || bars.BarsPeriod.BaseBarsPeriodValue == 1 && time <= bars.LastBarTime) { haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, bars.GetOpen(bars.Count - 1))); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, bars.GetOpen(bars.Count - 1))); UpdateBar(bars, haHigh, haLow, haClose, bars.LastBarTime, volume); } else { haOpen = bars.Instrument.MasterInstrument.RoundToTickSize((bars.GetOpen(bars.Count - 1) + bars.GetClose(bars.Count - 1)) / 2.0); haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, haOpen)); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, haOpen)); time = TimeToBarTimeSecond(bars, time, isBar); AddBar(bars, haOpen, haHigh, haLow, haClose, time, volume); } } break; } case BarsPeriodType.Tick: { bool isNewSession = SessionIterator.IsNewSession(time, isBar); if (isNewSession) { SessionIterator.GetNextSession(time, isBar); } if (bars.BarsPeriod.BaseBarsPeriodValue == 1) { haOpen = haOpen.ApproxCompare(0.0) == 0 ? open : (haOpen + haClose) / 2.0; haClose = haClose.ApproxCompare(0.0) == 0 ? close : bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, haOpen)); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, haOpen)); AddBar(bars, haOpen, haHigh, haLow, haClose, time, volume); } else if (bars.Count == 0) { AddBar(bars, open, high, low, close, time, volume); } else if (bars.Count > 0 && (!isNewSession || !bars.IsResetOnNewTradingDay) && bars.BarsPeriod.BaseBarsPeriodValue > 1 && bars.TickCount < bars.BarsPeriod.BaseBarsPeriodValue) { haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, bars.GetOpen(bars.Count - 1))); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, bars.GetOpen(bars.Count - 1))); UpdateBar(bars, haHigh, haLow, haClose, time, volume); } else { haOpen = bars.Instrument.MasterInstrument.RoundToTickSize((bars.GetOpen(bars.Count - 1) + bars.GetClose(bars.Count - 1)) / 2.0); haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, haOpen)); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, haOpen)); AddBar(bars, haOpen, haHigh, haLow, haClose, time, volume); } break; } case BarsPeriodType.Volume: { if (bars.Count == 0) { while (volume > bars.BarsPeriod.BaseBarsPeriodValue) { haOpen = haOpen.ApproxCompare(0.0) == 0 ? open : (haOpen + haClose) / 2.0; haClose = haClose.ApproxCompare(0) == 0 ? close : bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, haOpen)); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, haOpen)); AddBar(bars, haOpen, haHigh, haLow, haClose, time, bars.BarsPeriod.BaseBarsPeriodValue); volume -= bars.BarsPeriod.BaseBarsPeriodValue; } if (volume > 0) { haOpen = haOpen.ApproxCompare(0.0) == 0 ? open : bars.Instrument.MasterInstrument.RoundToTickSize((haOpen + haClose) / 2.0); haClose = haClose.ApproxCompare(0.0) == 0 ? close : bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, haOpen)); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, haOpen)); AddBar(bars, haOpen, haHigh, haLow, haClose, time, volume); } } else { long volumeTmp = 0; bool isNewSession = SessionIterator.IsNewSession(time, isBar); if (!bars.IsResetOnNewTradingDay || !isNewSession) { volumeTmp = Math.Min(bars.BarsPeriod.BaseBarsPeriodValue - bars.GetVolume(bars.Count - 1), volume); if (volumeTmp > 0) { haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, bars.GetOpen(bars.Count - 1))); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, bars.GetOpen(bars.Count - 1))); UpdateBar(bars, haHigh, haLow, haClose, time, volumeTmp); } } if (isNewSession) { SessionIterator.GetNextSession(time, isBar); } volumeTmp = volume - volumeTmp; while (volumeTmp > 0) { haOpen = bars.Instrument.MasterInstrument.RoundToTickSize((bars.GetOpen(bars.Count - 1) + bars.GetClose(bars.Count - 1)) / 2.0); haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, haOpen)); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, haOpen)); AddBar(bars, haOpen, haHigh, haLow, haClose, time, Math.Min(volumeTmp, bars.BarsPeriod.BaseBarsPeriodValue)); volumeTmp -= bars.BarsPeriod.BaseBarsPeriodValue; } } break; } case BarsPeriodType.Week: { if (bars.Count == 0) { AddBar(bars, open, high, low, close, TimeToBarTimeWeek(time, time.AddDays(6 - ((int)time.DayOfWeek + 1) % 7 + (bars.BarsPeriod.BaseBarsPeriodValue - 1) * 7), bars.BarsPeriod.BaseBarsPeriodValue), volume); } else if (time.Date <= bars.LastBarTime.Date) { haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, bars.GetOpen(bars.Count - 1))); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, bars.GetOpen(bars.Count - 1))); UpdateBar(bars, haHigh, haLow, haClose, bars.LastBarTime, volume); } else { haOpen = bars.Instrument.MasterInstrument.RoundToTickSize((bars.GetOpen(bars.Count - 1) + bars.GetClose(bars.Count - 1)) / 2.0); haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, haOpen)); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, haOpen)); AddBar(bars, haOpen, haHigh, haLow, haClose, TimeToBarTimeWeek(time.Date, bars.LastBarTime.Date, bars.BarsPeriod.BaseBarsPeriodValue), volume); } break; } case BarsPeriodType.Year: { if (bars.Count == 0) { AddBar(bars, open, high, low, close, TimeToBarTimeYear(time, bars.BarsPeriod.BaseBarsPeriodValue), volume); } else { if (time.Year <= bars.LastBarTime.Year) { haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, bars.GetOpen(bars.Count - 1))); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, bars.GetOpen(bars.Count - 1))); UpdateBar(bars, haHigh, haLow, haClose, bars.LastBarTime, volume); } else { haOpen = bars.Instrument.MasterInstrument.RoundToTickSize((bars.GetOpen(bars.Count - 1) + bars.GetClose(bars.Count - 1)) / 2.0); haClose = bars.Instrument.MasterInstrument.RoundToTickSize((open + high + low + close) / 4.0); haHigh = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Max(high, haOpen)); haLow = bars.Instrument.MasterInstrument.RoundToTickSize(Math.Min(low, haOpen)); AddBar(bars, haOpen, haHigh, haLow, haClose, TimeToBarTimeYear(time.Date, bars.BarsPeriod.BaseBarsPeriodValue), volume); } } break; } } bars.LastPrice = haClose; }
protected override void OnBarUpdate() { /* When working with multiple bar series objects it is important to understand the sequential order in which the * OnBarUpdate() method is triggered. The bars will always run with the primary first followed by the secondary and * so on. * * Important: Primary bars will always execute before the secondary bar series. * If a bar is timestamped as 12:00PM on the 5min bar series, the call order between the equally timestamped 12:00PM * bar on the 1min bar series is like this: * 12:00PM 5min * 12:00PM 1min * 12:01PM 1min * 12:02PM 1min * 12:03PM 1min * 12:04PM 1min * 12:05PM 5min * 12:05PM 1min * * When the OnBarUpdate() is called from the primary bar series (2000 ticks series in this example), do the following */ if (BarsInProgress == 0) { if (CurrentBar < BarsRequiredToTrade) { return; } //Print("Current Bar time=" + Bars.GetTime(CurrentBar).ToString("HHmmss")); // if the bar elapsed time span across 12 mid night DateTime t1 = Bars.GetTime(CurrentBar - 1); DateTime t2 = Bars.GetTime(CurrentBar); if (TimeSpan.Compare(t1.TimeOfDay, t2.TimeOfDay) > 0) { Print("EOD Session"); HandleEOD(); string resetString = "-1"; byte[] resetMsg = Encoding.UTF8.GetBytes(resetString); // Send reset string of "-1" to the server int resetSent = sender.Send(resetMsg); lineNo = 0; //reset global flags currPos = Position.posFlat; profitChasingFlag = false; stopLossEncountered = false; return; } // prior Stop-Loss observed, construct the lineNo with special code before sending msg to the server - so that the server will flatten the position if (stopLossEncountered) { lineNo += 10000; } // construct the string buffer to be sent to DLNN string bufString = lineNo.ToString() + ',' + Bars.GetTime(CurrentBar - 1).ToString("HHmmss") + ',' + Bars.GetTime(CurrentBar).ToString("HHmmss") + ',' + Bars.GetOpen(CurrentBar).ToString() + ',' + Bars.GetClose(CurrentBar).ToString() + ',' + Bars.GetHigh(CurrentBar).ToString() + ',' + Bars.GetLow(CurrentBar).ToString() + ',' + Bars.GetVolume(CurrentBar).ToString() + ',' + SMA(9)[0].ToString() + ',' + SMA(20)[0].ToString() + ',' + SMA(50)[0].ToString() + ',' + MACD(12, 26, 9).Diff[0].ToString() + ',' + RSI(14, 3)[0].ToString() + ',' + Bollinger(2, 20).Lower[0].ToString() + ',' + Bollinger(2, 20).Upper[0].ToString() + ',' + CCI(20)[0].ToString() + ',' + Bars.GetHigh(CurrentBar).ToString() + ',' + Bars.GetLow(CurrentBar).ToString() + ',' + Momentum(20)[0].ToString() + ',' + DM(14).DiPlus[0].ToString() + ',' + DM(14).DiMinus[0].ToString() + ',' + VROC(25, 3)[0].ToString() + ',' + '0' + ',' + '0' + ',' + '0' + ',' + '0' + ',' + '0' + ',' + '0' + ',' + '0' + ',' + '0' + ',' + '0' + ',' + '0'; //Print("CurrentBar = " + CurrentBar + ": " + "bufString = " + bufString); byte[] msg = Encoding.UTF8.GetBytes(bufString); // Send the data through the socket. int bytesSent = sender.Send(msg); // Receive the response from the remote device. int bytesRec = sender.Receive(bytes); // prior Stop-Loss observed, hence ignore the returned signal from server and move on to the next bar, reset lineNo to next counter and reset stopLossEncountered flag if (stopLossEncountered) { lineNo -= 10000; lineNo++; stopLossEncountered = false; //svrSignal = ExtractResponse(System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length)); svrSignal = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length).Split(',')[1]; Print(Bars.GetTime(CurrentBar).ToString("yyyy-MM-ddTHH:mm:ss.ffffffK") + " Ignore Post STOP-LOSS Server response= <" + svrSignal + "> Current Bar: Open=" + Bars.GetOpen(CurrentBar) + " Close=" + Bars.GetClose(CurrentBar) + " High=" + Bars.GetHigh(CurrentBar) + " Low=" + Bars.GetLow(CurrentBar)); return; } //svrSignal = ExtractResponse(System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length)); svrSignal = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length).Split(',')[1]; Print(Bars.GetTime(CurrentBar).ToString("yyyy-MM-ddTHH:mm:ss.ffffffK") + " Server response= <" + svrSignal + "> Current Bar: Open=" + Bars.GetOpen(CurrentBar) + " Close=" + Bars.GetClose(CurrentBar) + " High=" + Bars.GetHigh(CurrentBar) + " Low=" + Bars.GetLow(CurrentBar)); //Print(Bars.GetTime(CurrentBar).ToString("yyyy-MM-ddTHH:mm:ss.ffffffK") + " Server response= <" + svrSignal + ">"); // Return signal from DLNN is not we expected, close outstanding position and restart if (bytesRec == -1) { lineNo = 0; // TODO: close current position? } else { lineNo++; } // Start processing signal after 8th signal and beyond, otherwise ignore if (lineNo >= 8) { ExecuteAITrade(svrSignal); // if position is flat, no need to do anything if (currPos == Position.posFlat) { return; } // handle stop loss or profit chasing if there is existing position and order action is either SellShort or Buy if (entryOrder != null && (entryOrder.OrderAction == OrderAction.Buy || entryOrder.OrderAction == OrderAction.SellShort) && (entryOrder.OrderState == OrderState.Filled || entryOrder.OrderState == OrderState.PartFilled)) { // if Close[0] violates soft deck, if YES handle stop loss accordingly if (ViolateSoftDeck()) { HandleSoftDeck(svrSignal); } // if profitChasingFlag is TRUE or TouchedProfitChasing then handle profit chasing if ((profitChasingFlag || TouchedProfitChasing())) { HandleProfitChasing(); } } } } // When the OnBarUpdate() is called from the secondary bar series, in our case for each tick, handle stop loss and profit chasing accordingly else { return; } }
protected override void OnCalculate() { string strReversalTradeLong = "ReversalTradeLong" + ProcessingBarIndex; string strReversalTradeShort = "ReversalTradeShort" + ProcessingBarIndex; string strTradeResultLong; string strTradeResultShort; Color colorTextBox; // 1 umkehr fallend auf steigend //-1 umkehr steigend auf fallend if (IsReversalLongTrade() == true) { ReversalTradeStartTSLong = Bars[0].Time; //TargetBarTime = GetTargetBar(Bars[0].Time); TargetBarTime = GlobalUtilities.GetTargetBar(Bars, Bars[0].Time, TimeFrame, 1); OutSeries.Set(100); Reversal2NextBar.Set(100); } else if (IsReversalShortTrade() == true) { ReversalTradeStartTSShort = Bars[0].Time; //TargetBarTime = GetTargetBar(Bars[0].Time); TargetBarTime = GlobalUtilities.GetTargetBar(Bars, Bars[0].Time, TimeFrame, 1); OutSeries.Set(-100); } else { Reversal2NextBar.Set(0); } //TradingKerze ist fertig, Zeiteinheit ist abgelaufen if (Bars[0].Time == TargetBarTime) { ReversalTradeResult = (decimal)Bars.GetClose(ProcessingBarIndex) - (decimal)Bars.GetOpen(ProcessingBarIndex); TradeCounter += 1; if (ReversalTradeStartTSLong > DateTime.MinValue) { ReversalTradeResultTotalLong = ReversalTradeResultTotalLong + ReversalTradeResult; if (ReversalTradeResult < 0) { strTradeResultLong = "Fail " + ReversalTradeResult.ToString(); colorTextBox = colFail; TradeCounterLongFail += 1; } else { strTradeResultLong = "Win " + ReversalTradeResult.ToString(); colorTextBox = colWin; TradeCounterLongWin += 1; } AddChartText(strReversalTradeLong, true, strTradeResultLong, Time[1], Bars.GetHigh(ProcessingBarIndex) + (100 * TickSize), 9, Color.Black, new Font("Arial", 9), StringAlignment.Center, Color.Black, colorTextBox, 70); } else if (ReversalTradeStartTSShort > DateTime.MinValue) { ReversalTradeResultTotalShort = ReversalTradeResultTotalShort + ReversalTradeResult; if (ReversalTradeResult < 0) { strTradeResultShort = "Win " + ReversalTradeResult.ToString(); colorTextBox = colWin; TradeCounterShortWin += 1; } else { strTradeResultShort = "Fail " + ReversalTradeResult.ToString(); colorTextBox = colFail; TradeCounterShortFail += 1; } AddChartText(strReversalTradeShort, true, strTradeResultShort, Time[1], Bars.GetHigh(ProcessingBarIndex) - (100 * TickSize), 9, Color.Black, new Font("Arial", 9), StringAlignment.Center, Color.Black, colorTextBox, 70); } //Variablen Resetten ReversalTradeStartTSLong = DateTime.MinValue; ReversalTradeStartTSShort = DateTime.MinValue; } if (IsProcessingBarIndexLast) { // Print("LongWin: " + TradeCounterLongWin + " LongFail: " + TradeCounterLongFail + " ShortWin: " + TradeCounterShortWin + " ShortFail: " + TradeCounterShortFail); // Print(Instrument.Name + "Trades: " + TradeCounter + " LongPunkte: " + ReversalTradeResultTotalLong + " ShortPunkte: " + ReversalTradeResultTotalShort); } }
protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask) { if (SessionIterator == null) { SessionIterator = new SessionIterator(bars); } #region Building Bars from Base Period if (bars.Count != tmpCount) // Reset cache when bars are trimmed { if (bars.Count == 0) { tmpTime = Core.Globals.MinDate; tmpVolume = 0; tmpDayCount = 0; tmpTickCount = 0; } else { tmpTime = bars.GetTime(bars.Count - 1); tmpVolume = bars.GetVolume(bars.Count - 1); tmpTickCount = bars.TickCount; tmpDayCount = bars.DayCount; bars.LastPrice = bars.GetClose(bars.Count - 1); anchorPrice = bars.LastPrice; } } bool isNewSession = SessionIterator.IsNewSession(time, isBar); bool isCalculateTradingDayDone = false; switch (bars.BarsPeriod.BaseBarsPeriodType) { case BarsPeriodType.Day: tmpTime = time.Date; // Will be modified for realtime only if (!isBar && time >= cacheSessionEnd /* on realtime includesEndTimeStamp is always false */) { if (isNewSession) { SessionIterator.GetNextSession(time, isBar); isCalculateTradingDayDone = true; } cacheSessionEnd = SessionIterator.ActualSessionEnd; if (tmpTime < time.Date) { tmpTime = time.Date; // Make sure timestamps are ascending } } if (prevTime != tmpTime) { tmpDayCount++; } if (tmpDayCount < bars.BarsPeriod.BaseBarsPeriodValue || isBar && bars.Count > 0 && tmpTime == bars.LastBarTime.Date || !isBar && bars.Count > 0 && tmpTime <= bars.LastBarTime.Date) { endOfBar = false; } else { prevTime = tmpTime; endOfBar = true; } break; case BarsPeriodType.Minute: if (tmpTime == Core.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeMinute(bars, time, isBar); } if (isBar && time <= tmpTime || !isBar && time < tmpTime) { endOfBar = false; } else { prevTime = tmpTime; tmpTime = TimeToBarTimeMinute(bars, time, isBar); endOfBar = true; } break; case BarsPeriodType.Volume: if (tmpTime == Core.Globals.MinDate) { tmpVolume = volume; endOfBar = tmpVolume >= bars.BarsPeriod.BaseBarsPeriodValue; prevTime = tmpTime = time; if (endOfBar) { tmpVolume = 0; } break; } tmpVolume += volume; endOfBar = tmpVolume >= bars.BarsPeriod.BaseBarsPeriodValue; if (endOfBar) { prevTime = tmpTime; tmpVolume = 0; } tmpTime = time; break; case BarsPeriodType.Tick: if (tmpTime == Core.Globals.MinDate || bars.BarsPeriod.BaseBarsPeriodValue == 1) { prevTime = tmpTime == Core.Globals.MinDate ? time : tmpTime; tmpTime = time; tmpTickCount = bars.BarsPeriod.BaseBarsPeriodValue == 1 ? 0 : 1; endOfBar = bars.BarsPeriod.BaseBarsPeriodValue == 1; break; } if (tmpTickCount < bars.BarsPeriod.BaseBarsPeriodValue) { tmpTime = time; endOfBar = false; tmpTickCount++; } else { prevTime = tmpTime; tmpTime = time; endOfBar = true; tmpTickCount = 1; } break; case BarsPeriodType.Month: if (tmpTime == Core.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeMonth(time, bars.BarsPeriod.BaseBarsPeriodValue); } if (time.Month <= tmpTime.Month && time.Year == tmpTime.Year || time.Year < tmpTime.Year) { endOfBar = false; } else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeMonth(time, bars.BarsPeriod.BaseBarsPeriodValue); } break; case BarsPeriodType.Second: if (tmpTime == Core.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeSecond(bars, time, isBar); } if (bars.BarsPeriod.BaseBarsPeriodValue > 1 && time < tmpTime || bars.BarsPeriod.BaseBarsPeriodValue == 1 && time <= tmpTime) { endOfBar = false; } else { prevTime = tmpTime; tmpTime = TimeToBarTimeSecond(bars, time, isBar); endOfBar = true; } break; case BarsPeriodType.Week: if (tmpTime == Core.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeWeek(time.Date, tmpTime.Date, bars.BarsPeriod.BaseBarsPeriodValue); } if (time.Date <= tmpTime.Date) { endOfBar = false; } else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeWeek(time.Date, tmpTime.Date, bars.BarsPeriod.BaseBarsPeriodValue); } break; case BarsPeriodType.Year: if (tmpTime == Core.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeYear(time, bars.BarsPeriod.Value); } if (time.Year <= tmpTime.Year) { endOfBar = false; } else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeYear(time, bars.BarsPeriod.Value); } break; } #endregion #region Kagi Logic reversalPoint = bars.BarsPeriod.ReversalType == ReversalType.Tick ? bars.BarsPeriod.Value * bars.Instrument.MasterInstrument.TickSize : bars.BarsPeriod.Value / 100.0 * anchorPrice; if (bars.Count == 0 || IsIntraday && (bars.BarsPeriod.BaseBarsPeriodType != BarsPeriodType.Second && bars.IsResetOnNewTradingDay && isNewSession || bars.BarsPeriod.BaseBarsPeriodType == BarsPeriodType.Second && bars.IsResetOnNewTradingDay && isNewSession)) { if (isNewSession && !isCalculateTradingDayDone) { SessionIterator.GetNextSession(tmpTime, isBar); } if (bars.Count > 0) { double lastOpen = bars.GetOpen(bars.Count - 1); double lastHigh = bars.GetHigh(bars.Count - 1); double lastLow = bars.GetLow(bars.Count - 1); double lastClose = bars.GetClose(bars.Count - 1); if (bars.Count == tmpCount) { CalculateKagiBar(bars, lastOpen, lastHigh, lastLow, lastClose, prevTime, volume); } } AddBar(bars, close, close, close, close, tmpTime, volume); anchorPrice = close; trend = Trend.Undetermined; prevTime = tmpTime; volumeCount = 0; bars.LastPrice = close; tmpCount = bars.Count; return; } double c = bars.GetClose(bars.Count - 1); double o = bars.GetOpen(bars.Count - 1); double h = bars.GetHigh(bars.Count - 1); double l = bars.GetLow(bars.Count - 1); if (endOfBar) { CalculateKagiBar(bars, o, h, l, c, prevTime, volume); } else { volumeCount += volume; } bars.LastPrice = close; tmpCount = bars.Count; #endregion }
protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { if (IsInHitTest) { return; } int lastBar = ChartBars.ToIndex; int firstBar = ChartBars.FromIndex; double highPrice = 0; double lowPrice = double.MaxValue; SharpDX.Direct2D1.Brush brushDown = BarDownBrush.ToDxBrush(RenderTarget); SharpDX.Direct2D1.Brush lineBrush = LineBrush.ToDxBrush(RenderTarget); SharpDX.Direct2D1.Brush brushUp = BarUpBrush.ToDxBrush(RenderTarget); brushDown.Opacity = (float)(Opacity / 100.0); brushUp.Opacity = (float)(Opacity / 100.0); for (int idx = firstBar; idx <= lastBar && idx >= 0; idx++) { highPrice = Math.Max(highPrice, Bars.GetHigh(idx)); lowPrice = Math.Min(lowPrice, Bars.GetLow(idx)); } int volumeBarCount = BarCount; double priceRange = highPrice - lowPrice; double priceBoxSize = priceRange / volumeBarCount; double volumeMax = 0; // Pass 1: Fill all VolumeInfo structures with appropriate data for (int i = 0; i < volumeBarCount; i++) { double priceUpper = lowPrice + priceBoxSize * (i + 1); double priceLower = lowPrice + priceBoxSize * i; double priceVolumeUp = 0; double priceVolumeDown = 0; for (int idx = firstBar; idx <= lastBar; idx++) { double checkPrice; PriceSeries series = (Inputs[0] as PriceSeries); switch (series.PriceType) { case PriceType.Open: checkPrice = Bars.GetOpen(idx); break; case PriceType.Close: checkPrice = Bars.GetClose(idx); break; case PriceType.High: checkPrice = Bars.GetHigh(idx); break; case PriceType.Low: checkPrice = Bars.GetLow(idx); break; case PriceType.Median: checkPrice = (Bars.GetHigh(idx) + Bars.GetLow(idx)) / 2; break; case PriceType.Typical: checkPrice = (Bars.GetHigh(idx) + Bars.GetLow(idx) + Bars.GetClose(idx)) / 3; break; case PriceType.Weighted: checkPrice = (Bars.GetHigh(idx) + Bars.GetLow(idx) + 2 * Bars.GetClose(idx)) / 4; break; default: checkPrice = Bars.GetClose(idx); break; } if (checkPrice >= priceLower && checkPrice < priceUpper) { if (Bars.GetOpen(idx) < Bars.GetClose(idx)) { priceVolumeUp += Bars.GetVolume(idx); } else { priceVolumeDown += Bars.GetVolume(idx); } } } volumeInfo[i].up = priceVolumeUp; volumeInfo[i].down = priceVolumeDown; volumeInfo[i].total = priceVolumeUp + priceVolumeDown; volumeMax = Math.Max(volumeMax, volumeInfo[i].total); } // Pass 2: Paint the volume bars for (int i = 0; i < Math.Min(volumeBarCount, lastBar - firstBar + 1); i++) { double priceUpper = lowPrice + priceBoxSize * (i + 1); double priceLower = lowPrice + priceBoxSize * i; int yUpper = Convert.ToInt32(chartScale.GetYByValue(priceUpper)) + BarSpacing; int yLower = Convert.ToInt32(chartScale.GetYByValue(priceLower)); int barWidthUp = (int)((chartScale.Height / 2) * (volumeInfo[i].up / volumeMax)); int barWidthDown = (int)((chartScale.Height / 2) * (volumeInfo[i].down / volumeMax)); SharpDX.RectangleF rect = new SharpDX.RectangleF(ChartPanel.X, yUpper, barWidthUp, Math.Abs(yUpper - yLower)); RenderTarget.FillRectangle(rect, brushUp); RenderTarget.DrawRectangle(rect, brushUp); SharpDX.RectangleF rect2 = new SharpDX.RectangleF(ChartPanel.X + barWidthUp, yUpper, barWidthDown, Math.Abs(yUpper - yLower)); RenderTarget.DrawRectangle(rect2, brushDown); RenderTarget.FillRectangle(rect2, brushDown); if (DrawLines) { RenderTarget.DrawLine(new SharpDX.Vector2(ChartPanel.X, yLower), new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, yLower), lineBrush); if (i == volumeBarCount - 1) { RenderTarget.DrawLine(new SharpDX.Vector2(ChartPanel.X, yUpper), new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, yUpper), lineBrush); } } } lineBrush.Dispose(); brushDown.Dispose(); brushUp.Dispose(); }
private Dictionary <int, string> removeDuplicatePivots(Dictionary <int, string> periodSwingsPivots) { Log("Remove Duplicate Pivots", NinjaTrader.Cbi.LogLevel.Information); //Remove duplicate highs / lows List <int> orderedMarkers = periodSwingsPivots.Keys.ToList(); orderedMarkers.Sort(); int previousMarker = -1; string previousMarkerType = ""; Dictionary <int, string> reducedPivots = new Dictionary <int, string>(); foreach (int marker in orderedMarkers) { if (previousMarker == -1) { previousMarker = marker; previousMarkerType = periodSwingsPivots[previousMarker]; } if (previousMarkerType.Equals(MAX) && periodSwingsPivots[marker].Equals(MAX) && Bars.GetHigh(marker) > Bars.GetHigh(previousMarker)) { reducedPivots.Remove(previousMarker); } if (previousMarkerType.Equals(MIN) && periodSwingsPivots[marker].Equals(MIN) && Bars.GetLow(marker) < Bars.GetLow(previousMarker)) { reducedPivots.Remove(previousMarker); } if (previousMarkerType.Equals(MAX) && periodSwingsPivots[marker].Equals(MAX) && Bars.GetHigh(marker) <= Bars.GetHigh(previousMarker)) { continue; } if (previousMarkerType.Equals(MIN) && periodSwingsPivots[marker].Equals(MIN) && Bars.GetLow(marker) >= Bars.GetLow(previousMarker)) { continue; } reducedPivots.Add(marker, periodSwingsPivots[marker]); previousMarker = marker; previousMarkerType = periodSwingsPivots[previousMarker]; } Log("Duplicate pivots removed", NinjaTrader.Cbi.LogLevel.Information); return(reducedPivots); }
protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask) { // method variables bool isNewSession = SessionIterator.IsNewSession(time, isBar); double tickSize = bars.Instrument.MasterInstrument.TickSize; if (SessionIterator == null) { SessionIterator = new SessionIterator(bars); } if (isNewSession) { SessionIterator.GetNextSession(time, isBar); } if (bars.Count == 0 || (bars.IsResetOnNewTradingDay && isNewSession)) { // update fields rangeMax = AddTwoDoubles(bars, bars.BarsPeriod.BaseBarsPeriodValue * tickSize, 0); rangeMin = AddTwoDoubles(bars, bars.BarsPeriod.Value * tickSize, 0); // set initial range, factoring dynamic thisRange = isDynamic ? rangeMin : rangeMax; AdjustMaxMin(bars, close, close); // add first bar AddBar(bars, thisOpen, thisOpen, thisOpen, thisOpen, time, volume); } else { // local variables double barOpen = bars.GetOpen(bars.Count - 1); double barHigh = bars.GetHigh(bars.Count - 1); double barLow = bars.GetLow(bars.Count - 1); int maxCompare = bars.Instrument.MasterInstrument.Compare(close, thisMax); int minCompare = bars.Instrument.MasterInstrument.Compare(close, thisMin); double thisClose = maxCompare > 0 ? Math.Min(close, thisMax) : minCompare < 0 ? Math.Max(close, thisMin) : close; // range exceeded; create new bar(s) if (maxCompare > 0 || minCompare < 0) { // local variables bool newBar = true; // update bias prevBias = thisBias; thisBias = close > barOpen ? 1 : close < barOpen ? -1 : 0; // close current bar; volume included for on-touch only // see this post for more info on volume calculation: http://www.ninjatrader.com/support/forum/showthread.php?p=302208#post302208 UpdateBar(bars, (maxCompare > 0 ? thisClose : barHigh), (minCompare < 0 ? thisClose : barLow), thisClose, time, 0); // add next bar and loop phantom bars, if needed do { // update thisRange for dynamic if (isDynamic) { // increment range for same bias, if range has not exceeded max if ((thisBias == prevBias || prevBias == 0) && thisRange < rangeMax) { thisRange = AddTwoDoubles(bars, thisRange, tickSize); } // increment range after trend change (will only fire once) else if (thisBias != prevBias && prevBias != 0) { thisRange = AddTwoDoubles(bars, rangeMin, tickSize); } // ensure valid range thisRange = Math.Min(thisRange, rangeMax); } // update fields AdjustMaxMin(bars, thisClose, close); thisClose = (maxCompare > 0) ? Math.Min(close, thisMax) : (minCompare < 0) ? Math.Max(close, thisMin) : close; // add new bar; include volume once (except for on-touch), then create phantom bars // see this post for more info on volume calculation: http://www.ninjatrader.com/support/forum/showthread.php?p=302208#post302208 AddBar(bars, thisOpen, (maxCompare > 0 ? thisClose : thisOpen), (minCompare < 0 ? thisClose : thisOpen), thisClose, time, (newBar ? volume : 0)); newBar = false; // update fields maxCompare = bars.Instrument.MasterInstrument.Compare(close, thisMax); minCompare = bars.Instrument.MasterInstrument.Compare(close, thisMin); }while (maxCompare > 0 || minCompare < 0); } else { UpdateBar(bars, (close > barHigh ? close : barHigh), (close < barLow ? close : barLow), close, time, volume); } } bars.LastPrice = close; }
/// <summary> /// </summary> /// <param name="bars"></param> /// <param name="open"></param> /// <param name="high"></param> /// <param name="low"></param> /// <param name="close"></param> /// <param name="time"></param> /// <param name="volume"></param> /// <param name="isRealtime"></param> public override void Add(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isRealtime) { #region Building Bars from Base Period if (bars.Count != tmpCount) // reset cache when bars are trimmed if (bars.Count == 0) { tmpTime = Cbi.Globals.MinDate; tmpVolume = 0; tmpDayCount = 0; tmpTickCount = 0; } else { tmpTime = bars.GetTime(bars.Count - 1); tmpVolume = bars.GetVolume(bars.Count - 1); tmpTickCount = bars.TickCount; tmpDayCount = bars.DayCount; bars.LastPrice = bars.GetClose(bars.Count - 1); anchorPrice = bars.LastPrice; } switch (bars.Period.BasePeriodType) { case PeriodType.Day: tmpTime = time.Date; // will be modified for realtime only if (isRealtime && time >= cacheSessionEnd /* on realtime include60 is always false */) { bars.Session.GetSessionDate(time, false, out tmpTime, out cacheSessionEnd); if (tmpTime < time.Date) tmpTime = time.Date; // make sure timestamps are ascending } if (prevTime != tmpTime) tmpDayCount++; if (tmpDayCount < bars.Period.BasePeriodValue || (!isRealtime && bars.Count > 0 && tmpTime == bars.TimeLastBar.Date) || (isRealtime && bars.Count > 0 && tmpTime <= bars.TimeLastBar.Date)) endOfBar = false; else { prevTime = tmpTime; endOfBar = true; } break; case PeriodType.Minute: if (tmpTime == Cbi.Globals.MinDate) prevTime = tmpTime = TimeToBarTimeMinute(bars, time, bars.Session.NextBeginTime, bars.Period.BasePeriodValue, isRealtime); if (!isRealtime && time <= tmpTime || isRealtime && time < tmpTime) endOfBar = false; else { prevTime = tmpTime; tmpTime = TimeToBarTimeMinute(bars, time, bars.Session.NextBeginTime, bars.Period.BasePeriodValue, isRealtime); endOfBar = true; } break; case PeriodType.Volume: if (tmpTime == Cbi.Globals.MinDate) { tmpVolume = volume; endOfBar = tmpVolume >= bars.Period.BasePeriodValue; prevTime = tmpTime = time; if (endOfBar) tmpVolume = 0; break; } tmpVolume += volume; endOfBar = tmpVolume >= bars.Period.BasePeriodValue; if (endOfBar) { prevTime = tmpTime; tmpVolume = 0; } tmpTime = time; break; case PeriodType.Tick: if (tmpTime == Cbi.Globals.MinDate || bars.Period.BasePeriodValue == 1) { prevTime = tmpTime == Cbi.Globals.MinDate ? time : tmpTime; tmpTime = time; tmpTickCount = bars.Period.BasePeriodValue == 1 ? 0 : 1; endOfBar = bars.Period.BasePeriodValue == 1; break; } if (tmpTickCount < bars.Period.BasePeriodValue) { tmpTime = time; endOfBar = false; tmpTickCount++; } else { prevTime = tmpTime; tmpTime = time; endOfBar = true; tmpTickCount = 1; } break; case PeriodType.Month: if (tmpTime == Cbi.Globals.MinDate) prevTime = tmpTime = TimeToBarTimeMonth(time, bars.Period.BasePeriodValue); if (time.Month <= tmpTime.Month && time.Year == tmpTime.Year || time.Year < tmpTime.Year) endOfBar = false; else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeMonth(time, bars.Period.BasePeriodValue); } break; case PeriodType.Second: if (tmpTime == Cbi.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeSecond(bars, time, new DateTime( bars.Session.NextBeginTime.Year, bars.Session.NextBeginTime.Month, bars.Session.NextBeginTime.Day, bars.Session.NextBeginTime.Hour, bars.Session.NextBeginTime.Minute, 0), bars.Period.BasePeriodValue); } if ((bars.Period.Value > 1 && time < tmpTime) || (bars.Period.Value == 1 && time <= tmpTime)) endOfBar = false; else { prevTime = tmpTime; tmpTime = TimeToBarTimeSecond(bars, time, bars.Session.NextBeginTime, bars.Period.BasePeriodValue); endOfBar = true; } break; case PeriodType.Week: if (tmpTime == Cbi.Globals.MinDate) prevTime = tmpTime = TimeToBarTimeWeek(time.Date, tmpTime.Date, bars.Period.BasePeriodValue); if (time.Date <= tmpTime.Date) endOfBar = false; else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeWeek(time.Date, tmpTime.Date, bars.Period.BasePeriodValue); } break; case PeriodType.Year: if (tmpTime == Cbi.Globals.MinDate) prevTime = tmpTime = TimeToBarTimeYear(time, bars.Period.Value); if (time.Year <= tmpTime.Year) endOfBar = false; else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeYear(time, bars.Period.Value); } break; } #endregion #region Kagi Logic reversalPoint = bars.Period.ReversalType == ReversalType.Tick ? bars.Period.Value * bars.Instrument.MasterInstrument.TickSize : bars.Period.Value * 0.01 * anchorPrice; if (bars.Count == 0 || (IsIntraday && ((bars.Period.BasePeriodType != PeriodType.Second && bars.IsNewSession(time, isRealtime)) || (bars.Period.BasePeriodType == PeriodType.Second && bars.IsNewSession(tmpTime, isRealtime))))) { if (bars.Count > 0) { double lastOpen = bars.GetOpen(bars.Count - 1); double lastHigh = bars.GetHigh(bars.Count - 1); double lastLow = bars.GetLow(bars.Count - 1); double lastClose = bars.GetClose(bars.Count - 1); if (bars.Count == tmpCount) CalculateKagiBar(bars, lastOpen, lastHigh, lastLow, lastClose, prevTime, volume, isRealtime); } AddBar(bars, close, close, close, close, tmpTime, volume, isRealtime); anchorPrice = close; trend = Trend.Undetermined; prevTime = tmpTime; volumeCount = 0; bars.LastPrice = close; tmpCount = bars.Count; return; } Bar bar = (Bar)bars.Get(bars.Count - 1); double c = bar.Close; double o = bar.Open; double h = bar.High; double l = bar.Low; if (endOfBar) CalculateKagiBar(bars, o, h, l, c, prevTime, volume, isRealtime); else volumeCount += volume; bars.LastPrice = close; tmpCount = bars.Count; #endregion }
protected override void OnBarUpdate() { switch (pOrT) { case PriceOrTime.Price: { MAX_HIGH = Math.Max(MAX_HIGH, High[0]); MIN_LOW = Math.Min(MIN_LOW, Low[0]); if (CurrentBar == Bars.Count - 2) { MAX_HIGH = Math.Round(MAX_HIGH * 1.05, PriceDigits); MIN_LOW = Math.Round(MIN_LOW * 0.95, PriceDigits); if (InitialPrice < MIN_LOW || InitialPrice > MAX_HIGH) { Log("InitialPrice parameter MUST be between the high price and low price on this chart", NinjaTrader.Cbi.LogLevel.Information); if (InitialPrice > MAX_HIGH) { InitialPrice = MAX_HIGH; } if (InitialPrice < MIN_LOW) { InitialPrice = MIN_LOW; } } double LevelUp = -1.0, LevelDown = -1.0; i = 1; do { if (Angle000Flag) { LevelUp = InitialPrice + Angle0[i] * TickSize * MultiplierForPriceScale; LevelDown = InitialPrice - Angle0[i] * TickSize * MultiplierForPriceScale; MakePriceLine(LevelUp, LevelDown, Color0); } if (Angle090Flag) { LevelUp = InitialPrice + Angle90[i] * TickSize * MultiplierForPriceScale; LevelDown = InitialPrice - Angle90[i] * TickSize * MultiplierForPriceScale; MakePriceLine(LevelUp, LevelDown, Color90); } if (Angle180Flag) { LevelUp = InitialPrice + Angle180[i] * TickSize * MultiplierForPriceScale; LevelDown = InitialPrice - Angle180[i] * TickSize * MultiplierForPriceScale; MakePriceLine(LevelUp, LevelDown, Color180); } if (Angle270Flag) { LevelUp = InitialPrice + Angle270[i] * TickSize * MultiplierForPriceScale; LevelDown = InitialPrice - Angle270[i] * TickSize * MultiplierForPriceScale; MakePriceLine(LevelUp, LevelDown, Color270); } i++; if (LevelUp < 0.0 || LevelDown < 0.0) { break; } if (i >= Angle0.Length) { break; } if (i >= Angle90.Length) { break; } if (i >= Angle180.Length) { break; } if (i >= Angle270.Length) { break; } if (LevelUp > MaxLevelUp) { MaxLevelUp = LevelUp; } if (LevelDown < MinLevelDown) { MinLevelDown = LevelDown; } }while (1 == 1); } break; } case PriceOrTime.Time: { if (ZuluTime.CompareTo(Time[0]) < 0 && ZuluBar < 0) { ZuluBar = CurrentBar; //set ZuluBar to time the user selected } Print("Zulubar" + ZuluBar.ToString()); if (ZuluBar > 0) { OutString = null; if (Angle000Flag) { i = Angle0[NextBar0] + ZuluBar; if (i == CurrentBar) { MakeVerticalLine(0, Color0); NextBar0++; } OutString = "Zero line coming in " + (i - CurrentBar).ToString() + " bars" + Environment.NewLine; } if (Angle090Flag) { i = Angle90[NextBar90] + ZuluBar; if (i == CurrentBar) { MakeVerticalLine(0, Color90); NextBar90++; } OutString = OutString + "90 line coming in " + (i - CurrentBar).ToString() + " bars" + Environment.NewLine; } if (Angle180Flag) { i = Angle180[NextBar180] + ZuluBar; if (i == CurrentBar) { MakeVerticalLine(0, Color180); NextBar180++; } OutString = OutString + "180 line coming in " + (i - CurrentBar).ToString() + " bars" + Environment.NewLine; } if (Angle270Flag) { i = Angle270[NextBar270] + ZuluBar; if (i == CurrentBar) { MakeVerticalLine(0, Color270); NextBar270++; } OutString = OutString + "270 line coming in " + (i - CurrentBar).ToString() + " bars"; } if (OutString.Length > 0 && DisplayCountDown) { lastBar = Math.Min(ChartControl.LastSlotPainted, Bars.Count - 1); firstBar = (lastBar - ChartControl.SlotsPainted) + 1; int i; // Find highest and lowest price points HighestPaintedPrice = double.MinValue; LowestPaintedPrice = double.MaxValue; for (i = firstBar; i <= lastBar && i >= 0; i++) { HighestPaintedPrice = Math.Max(HighestPaintedPrice, Bars.GetHigh(i)); LowestPaintedPrice = Math.Min(LowestPaintedPrice, Bars.GetLow(i)); } double Outprice = (HighestPaintedPrice + LowestPaintedPrice) / 2.0; if (Bars.GetClose(lastBar - 1) < Outprice) { Outprice = HighestPaintedPrice; } if (Bars.GetClose(lastBar - 1) >= Outprice) { Outprice = (Outprice + LowestPaintedPrice) / 2.0; } Print(OutString); NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 10) { Size = 20, Bold = false }; if (CurrentBar > 10) { Draw.Text(this, "Info", true, OutString, 10, Outprice, 0, Brushes.Gray, myFont, TextAlignment.Justify, Brushes.White, Brushes.Transparent, 50); } } } break; } } }
protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask) { double lastBarClose = bars.GetClose(bars.Count - 1); // Trae el último precio de cierre if (SessionIterator == null) { SessionIterator = new SessionIterator(bars); } offset = bars.BarsPeriod.Value * bars.Instrument.MasterInstrument.TickSize; // Periocidad * tick (Tamaño de la caja) bool isNewSession = SessionIterator.IsNewSession(time, isBar); if (isNewSession) { SessionIterator.GetNextSession(time, isBar); } // Si es una gráfica nueva o una sesión nueva y está en un nuevo día if (bars.Count == 0 || bars.IsResetOnNewTradingDay && isNewSession) { // una sesión nueva y está en un nuevo día if (bars.Count > 0) { // Close out last bar in session and set open == close DateTime lastBarTime = bars.GetTime(bars.Count - 1); // Trae la última fecha del precio de cierre long lastBarVolume = bars.GetVolume(bars.Count - 1); // Trae el último volumen de precio de cierre RemoveLastBar(bars); // Elimina la última barra AddBar(bars, lastBarClose, lastBarClose, lastBarClose, lastBarClose, lastBarTime, lastBarVolume); // Crea una nueva barra con la información obtenida } renkoHigh = close + offset; // Suma el valor de cierre y tamaño de la caja renkoLow = close - offset; // Diferencia del valor de cierre y el tamaño de la caja // ¿Hay un nuevo precio negociado? isNewSession = SessionIterator.IsNewSession(time, isBar); if (isNewSession) { SessionIterator.GetNextSession(time, isBar); // Entonces traiga el último precio } // Pinte el último precio negociado AddBar(bars, close, close, close, close, time, volume); bars.LastPrice = close; return; } double barOpen = bars.GetOpen(bars.Count - 1); // Obtiene el valor de apertura de la última barra double barHigh = bars.GetHigh(bars.Count - 1); // Obtiene el valor más alto negociado de la última barra double barLow = bars.GetLow(bars.Count - 1); // Obtiene el valor más bajo negociado de la última barra long barVolume = bars.GetVolume(bars.Count - 1); // Obtiene el volumen de la última barra DateTime barTime = bars.GetTime(bars.Count - 1); // Obtiene la fecha de la última barra // renkoHigh == 0 || renkoLow == 0 // ApproxCompare: Compares two double or float values for equality or being greater than / less than the compared to value. if (renkoHigh.ApproxCompare(0.0) == 0 || renkoLow.ApproxCompare(0.0) == 0) { if (bars.Count == 1) { renkoHigh = barOpen + offset; // suma = valor de apertura de la última barra + el tamaño de la caja renkoLow = barOpen - offset; // diferencia = valor de apertura de la última barra - el tamaño de la caja } // Penultimo valor de cierre es mayor al penultimo valor de apertura? else if (bars.GetClose(bars.Count - 2) > bars.GetOpen(bars.Count - 2)) { renkoHigh = bars.GetClose(bars.Count - 2) + offset; // Suma tamaño de la caja + el penultimo valor de cierre renkoLow = bars.GetClose(bars.Count - 2) - offset * 2; // Resta el doble del tamaño de la caja - el penultimo valor de cierre } else { renkoHigh = bars.GetClose(bars.Count - 2) + offset * 2; // Suma el doble tamaño de la caja + el penultimo valor de cierre renkoLow = bars.GetClose(bars.Count - 2) - offset; // Resta el tamaño de la caja - el penultimo valor de cierre } } bool isRail2Rail = false; // Hay cambio de tendencia hacia bajista? if (close <= (renkoHigh)) { // Elimina la barra de Update RemoveLastBar(bars); // Agrega la nueva barra con los nuevos valores renkoLow = renkoHigh - 2.0 * offset; // RenkoHigh - el doble del tamaño de la caja renkoHigh = renkoHigh + offset; // Renkohigh + el tamaño de la caja // Agrega barra alcista //AddBar(bars, _renkoLow - offset, Math.Max(_renkoLow - offset, _renkoLow), Math.Min(_renkoLow - offset, _renkoLow), _renkoLow, barTime, barVolume); //AddBar(bars, _renkoHigh + offset, Math.Max(_renkoHigh + offset, _renkoHigh), Math.Min(_renkoHigh + offset, _renkoHigh), _renkoHigh, barTime, barVolume); //AddBar(Bars bars, double open, double high, double low, double close, DateTime time, long volume) //Barra de una gráfica bajista AddBar(bars, renkoLow + offset, Math.Max(renkoLow + offset, renkoLow), Math.Min(renkoLow + offset, renkoLow), renkoLow, barTime, barVolume); isRail2Rail = true; } //bool isRail2Rail = false; // Hay cambio de tendencia hacia alcista? if (close >= (renkoLow)) { // Elimina la barra la barra de Update RemoveLastBar(bars); // Agrega la nueva barra con los nuevos valores // Original //AddBar(bars, renkoLow + offset, Math.Max(renkoLow + offset, renkoLow), Math.Min(renkoLow + offset, renkoLow), renkoLow, barTime, barVolume); // Bajista renkoHigh = renkoLow + 2.0 * offset; // RenkoLow - el doble del tamaño de la caja renkoLow = renkoLow - offset; // RenkoLow - el tamaño de la caja //AddBar(bars, _renkoHigh + offset, Math.Max(_renkoHigh + offset, _renkoHigh), Math.Min(_renkoHigh + offset, _renkoHigh), _renkoHigh, barTime, barVolume); //AddBar(bars, _renkoLow - offset, Math.Max(_renkoLow - offset, _renkoLow), Math.Min(_renkoLow - offset, _renkoLow), _renkoLow, barTime, barVolume); //Barra de gráfica alcista AddBar(bars, renkoHigh - offset, Math.Max(renkoHigh - offset, renkoHigh), Math.Min(renkoHigh - offset, renkoHigh), renkoHigh, barTime, barVolume); isRail2Rail = true; } // el precio de cierre es mayor renkohigh? // [DETECTA COMPORTAMIENTO ALCISTA] if (close.ApproxCompare(renkoHigh) >= 0) { /* if (trend != 0 && trend != 1) { * // Elimina la barra de Update * RemoveLastBar(bars); * * // Agrega la nueva barra con los nuevos valores * var _renkoLow = renkoHigh - 1.0 * offset; // RenkoHigh - el doble del tamaño de la caja * var _renkoHigh = renkoHigh + offset; // Renkohigh + el tamaño de la caja * // Agrega barra alcista * AddBar(bars, _renkoLow - offset, Math.Max(_renkoLow - offset, _renkoLow), Math.Min(_renkoLow - offset, _renkoLow), _renkoLow, barTime, barVolume); * * isRail2Rail = true; * } */ // (1) Obtiene el valor mayor entre renkoHigh y, renkoHigh - tamaño de la caja // (2) Si el valor de x es igual a y entonces retorna 0 // Si el valor de x es mayor a y entonces retorna 1 // Si el valor de x es menor a y entonces retorna -1 if (barOpen.ApproxCompare(renkoHigh - offset) != 0 || // valor de apertura de la (última barra - el tamño de la caja) es mayor o menor a barOpen? barHigh.ApproxCompare(Math.Max(renkoHigh - offset, renkoHigh)) != 0 || // Es barHigh mayor o menor a (1)? barLow.ApproxCompare(Math.Min(renkoHigh - offset, renkoHigh)) != 0) // Es barLow mayor o menor a (1)? { // No hubo cambio de tendencia if (!isRail2Rail) { // Elimina la última barra de Update RemoveLastBar(bars); } // Agrega una barra nueva con los nuevos valores // Alcista AddBar(bars, renkoHigh - offset, Math.Max(renkoHigh - offset, renkoHigh), Math.Min(renkoHigh - offset, renkoHigh), renkoHigh, barTime, barVolume); } renkoLow = renkoHigh - 2.0 * offset; // RenkoHigh - el doble del tamaño de la caja renkoHigh = renkoHigh + offset; // Renkohigh + el tamaño de la caja // ¿Hay un nuevo valor negociado? isNewSession = SessionIterator.IsNewSession(time, isBar); if (isNewSession) { SessionIterator.GetNextSession(time, isBar); // Obtiene el último valor negociado } // Agrega barras vacías para llenar el gap si el precio salta while (close.ApproxCompare(renkoHigh) >= 0) { AddBar(bars, renkoHigh - offset, Math.Max(renkoHigh - offset, renkoHigh), Math.Min(renkoHigh - offset, renkoHigh), renkoHigh, time, 0); renkoLow = renkoHigh - 2.0 * offset; renkoHigh = renkoHigh + offset; } // Agrega la barra final parcial AddBar(bars, renkoHigh - offset, Math.Max(renkoHigh - offset, close), Math.Min(renkoHigh - offset, close), close, time, volume); trend = 1; } // el precio de cierre es menor o igual a renkohigh? // El precio de cierre es menor o igual al renkolow // [DETECTA COMPORTAMIENTO BAJISTA] else if (close.ApproxCompare(renkoLow) <= 0) { /* if (trend != 0 && trend != -1) { * // Elimina la barra la barra de Update * RemoveLastBar(bars); * // Agrega la nueva barra con los nuevos valores * // Original * //AddBar(bars, renkoLow + offset, Math.Max(renkoLow + offset, renkoLow), Math.Min(renkoLow + offset, renkoLow), renkoLow, barTime, barVolume); * // Bajista * var _renkoHigh = renkoLow + 1.0 * offset; // RenkoLow - el doble del tamaño de la caja * var _renkoLow = renkoLow - offset; // RenkoLow - el tamaño de la caja * AddBar(bars, _renkoHigh + offset, Math.Max(_renkoHigh + offset, _renkoHigh), Math.Min(_renkoHigh + offset, _renkoHigh), _renkoHigh, barTime, barVolume); * isRail2Rail = true; * } */ // (1) Obtiene el valor mayor entre renkoLow y, renkoLow + tamaño de la caja // (2) Si el valor de x es igual a y entonces retorna 0 // Si el valor de x es mayor a y entonces retorna 1 // Si el valor de x es menor a y entonces retorna -1 if (barOpen.ApproxCompare(renkoLow + offset) != 0 || // Valor de apertura de (renkolow + el tamaño de la caja) es mayor o menor a barOpen? barHigh.ApproxCompare(Math.Max(renkoLow + offset, renkoLow)) != 0 || // Es barHigh mayor o menor a (1)? barLow.ApproxCompare(Math.Min(renkoLow + offset, renkoLow)) != 0) // Es barlow mayor o menor a (1)? { // TODO: Validar si la condición cambia, si si, entonces no elimine la última barra if (!isRail2Rail) { // Elimine la barra de Update RemoveLastBar(bars); } // Agrega la nueva barra con los nuevos valores // AddBar(Bars bars, double open, double high, double low, double close, DateTime time, long volume) //Bajista AddBar(bars, renkoLow + offset, Math.Max(renkoLow + offset, renkoLow), Math.Min(renkoLow + offset, renkoLow), renkoLow, barTime, barVolume); } renkoHigh = renkoLow + 2.0 * offset; // RenkoLow - el doble del tamaño de la caja renkoLow = renkoLow - offset; // RenkoLow - el tamaño de la caja // ¿Hay un nuevo valor negociado? isNewSession = SessionIterator.IsNewSession(time, isBar); if (isNewSession) { SessionIterator.GetNextSession(time, isBar); // Obtiene el último valor negociado } // Agrega barras vacías para llenar el gap si el precio salta while (close.ApproxCompare(renkoLow) <= 0) { AddBar(bars, renkoLow + offset, Math.Max(renkoLow + offset, renkoLow), Math.Min(renkoLow + offset, renkoLow), renkoLow, time, 0); renkoHigh = renkoLow + 2.0 * offset; renkoLow = renkoLow - offset; } // Agrega la barra final parcial AddBar(bars, renkoLow + offset, Math.Max(renkoLow + offset, close), Math.Min(renkoLow + offset, close), close, time, volume); trend = -1; } // El precio de cierre mayor al renkolow else { // Actualiza la barra UpdateBar(bars, close, close, close, time, volume); } // El último precio el valor de cierre bars.LastPrice = close; }
public override void OnRender(ChartControl chartControl, ChartScale chartScale, ChartBars chartBars) { Bars bars = chartBars.Bars; float chartMinX = ConvertToHorizontalPixels(chartControl, chartControl.CanvasLeft + chartControl.Properties.BarMarginRight); RectangleF rect = new RectangleF(); int toIndex = chartBars.ToIndex; if (toIndex >= 0 && toIndex < bars.Count - 1) { toIndex++; } for (int idx = chartBars.FromIndex; idx <= toIndex; idx++) { double closeValue = bars.GetClose(idx); float high = chartScale.GetYByValue(bars.GetHigh(idx)); float low = chartScale.GetYByValue(bars.GetLow(idx)); double openValue = bars.GetOpen(idx); Brush overriddenBarBrush = chartControl.GetBarOverrideBrush(chartBars, idx); Brush overriddenOutlineBrush = chartControl.GetCandleOutlineOverrideBrush(chartBars, idx); float x = chartControl.GetXByBarIndex(chartBars, idx); float boxStartPosition; if (idx == chartBars.FromIndex && (toIndex == 0 || idx == 0)) { if (toIndex == 0) { boxStartPosition = chartMinX; } else { boxStartPosition = 2 * x - chartControl.GetXByBarIndex(chartBars, idx + 1); } } else { boxStartPosition = chartControl.GetXByBarIndex(chartBars, idx - 1); } if (Math.Abs(x - boxStartPosition) < 0.2) { continue; } float width = Math.Max(2f, Math.Abs(x - boxStartPosition)); if (closeValue > openValue) { width -= Stroke.Width; rect.X = boxStartPosition; rect.Y = high; rect.Width = width; rect.Height = low - high; TransformBrush(overriddenBarBrush ?? UpBrushDX, rect); TransformBrush(overriddenOutlineBrush ?? Stroke.BrushDX, rect); RenderTarget.FillRectangle(rect, overriddenBarBrush ?? UpBrushDX); RenderTarget.DrawRectangle(rect, overriddenOutlineBrush ?? Stroke.BrushDX, Stroke.Width, Stroke.StrokeStyle); } else { width -= Stroke2.Width; rect.X = boxStartPosition; rect.Y = high; rect.Width = width; rect.Height = low - high; TransformBrush(overriddenBarBrush ?? DownBrushDX, rect); TransformBrush(overriddenOutlineBrush ?? Stroke2.BrushDX, rect); RenderTarget.FillRectangle(rect, overriddenBarBrush ?? DownBrushDX); RenderTarget.DrawRectangle(rect, overriddenOutlineBrush ?? Stroke2.BrushDX, Stroke2.Width, Stroke2.StrokeStyle); } } }
protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask) { if (SessionIterator == null) { SessionIterator = new SessionIterator(bars); } bool isNewSession = SessionIterator.IsNewSession(time, isBar); if (isNewSession) { SessionIterator.GetNextSession(time, isBar); } if (bars.Count == 0 || bars.IsResetOnNewTradingDay && isNewSession) { AddBar(bars, open, high, low, close, time, volume); } else { double barClose = bars.GetClose(bars.Count - 1); double barHigh = bars.GetHigh(bars.Count - 1); double barLow = bars.GetLow(bars.Count - 1); double tickSize = bars.Instrument.MasterInstrument.TickSize; double rangeValue = Math.Floor(10000000.0 * bars.BarsPeriod.Value * tickSize) / 10000000.0; if (close.ApproxCompare(barLow + rangeValue) > 0) { double newClose = barLow + rangeValue; // Every bar closes either with high or low if (newClose.ApproxCompare(barClose) > 0) { UpdateBar(bars, newClose, barLow, newClose, time, 0); } // If there's still a gap, fill with phantom bars double newBarOpen = newClose + tickSize; while (close.ApproxCompare(newClose) > 0) { newClose = Math.Min(close, newBarOpen + rangeValue); AddBar(bars, newBarOpen, newClose, newBarOpen, newClose, time, close.ApproxCompare(newClose) > 0 ? 0 : volume); newBarOpen = newClose + tickSize; } } else if ((barHigh - rangeValue).ApproxCompare(close) > 0) { double newClose = barHigh - rangeValue; // Every bar closes either with high or low if (barClose.ApproxCompare(newClose) > 0) { UpdateBar(bars, barHigh, newClose, newClose, time, 0); } // if there's still a gap, fill with phantom bars double newBarOpen = newClose - tickSize; while (newClose.ApproxCompare(close) > 0) { newClose = Math.Max(close, newBarOpen - rangeValue); AddBar(bars, newBarOpen, newBarOpen, newClose, newClose, time, newClose.ApproxCompare(close) > 0 ? 0 : volume); newBarOpen = newClose - tickSize; } } else { UpdateBar(bars, close > barHigh ? close : barHigh, close < barLow ? close : barLow, close, time, volume); } } bars.LastPrice = close; }
/// <summary> /// </summary> /// <param name="bars"></param> /// <param name="open"></param> /// <param name="high"></param> /// <param name="low"></param> /// <param name="close"></param> /// <param name="time"></param> /// <param name="volume"></param> /// <param name="isRealtime"></param> public override void Add(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isRealtime) { #region Building Bars from Base Period if (bars.Count != tmpCount) // reset cache when bars are trimmed if (bars.Count == 0) { tmpTime = Cbi.Globals.MinDate; tmpVolume = 0; tmpDayCount = 0; tmpTickCount = 0; } else { tmpTime = bars.GetTime(bars.Count - 1); tmpVolume = bars.GetVolume(bars.Count - 1); tmpTickCount = bars.TickCount; tmpDayCount = bars.DayCount; bars.LastPrice = anchorPrice = bars.GetClose(bars.Count - 1); } switch (bars.Period.BasePeriodType) { case PeriodType.Day: tmpTime = time.Date; if (isRealtime && time >= cacheSessionEnd) { tmpDayCount++; bars.Session.GetSessionDate(time, false, out tmpTime, out cacheSessionEnd); if (tmpTime < time.Date) tmpTime = time.Date; // make sure timestamps are ascending } if (!isRealtime && prevTimeD != tmpTime) tmpDayCount++; if ((!isRealtime && bars.Count > 0 && tmpTime == bars.TimeLastBar.Date) || (isRealtime && bars.Count > 0 && tmpTime <= bars.TimeLastBar.Date) || tmpDayCount < bars.Period.BasePeriodValue) endOfBar = false; else { prevTime = prevTimeD == Cbi.Globals.MinDate ? tmpTime : prevTimeD; prevTimeD = tmpTime; endOfBar = true; } break; case PeriodType.Minute: if (tmpTime == Cbi.Globals.MinDate) prevTime = tmpTime = TimeToBarTimeMinute(bars, time, bars.Session.NextBeginTime, bars.Period.BasePeriodValue, isRealtime); if (!isRealtime && time <= tmpTime || isRealtime && time < tmpTime) endOfBar = false; else { prevTime = tmpTime; tmpTime = TimeToBarTimeMinute(bars, time, bars.Session.NextBeginTime, bars.Period.BasePeriodValue, isRealtime); endOfBar = true; } break; case PeriodType.Volume: if (tmpTime == Cbi.Globals.MinDate) { tmpVolume = volume; endOfBar = tmpVolume >= bars.Period.BasePeriodValue; prevTime = tmpTime = time; if (endOfBar) tmpVolume = 0; break; } tmpVolume += volume; endOfBar = tmpVolume >= bars.Period.BasePeriodValue; if (endOfBar) { prevTime = tmpTime; tmpVolume = 0; tmpTime = time; } break; case PeriodType.Month: if (tmpTime == Cbi.Globals.MinDate) prevTime = tmpTime = TimeToBarTimeMonth(time, bars.Period.BasePeriodValue); if (time.Month <= tmpTime.Month && time.Year == tmpTime.Year || time.Year < tmpTime.Year) endOfBar = false; else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeMonth(time, bars.Period.BasePeriodValue); } break; case PeriodType.Second: if (tmpTime == Cbi.Globals.MinDate) { prevTime = tmpTime = TimeToBarTimeSecond(bars, time, new DateTime(bars.Session.NextBeginTime.Year, bars.Session.NextBeginTime.Month, bars.Session.NextBeginTime.Day, bars.Session.NextBeginTime.Hour, bars.Session.NextBeginTime.Minute, 0), bars.Period.BasePeriodValue); } if (time <= tmpTime) endOfBar = false; else { prevTime = tmpTime; tmpTime = TimeToBarTimeSecond(bars, time, bars.Session.NextBeginTime, bars.Period.BasePeriodValue); endOfBar = true; } break; case PeriodType.Tick: if (tmpTime == Cbi.Globals.MinDate || bars.Period.BasePeriodValue == 1) { prevTime = tmpTime = time; tmpTickCount = bars.Period.BasePeriodValue == 1 ? 0 : 1; endOfBar = bars.Period.BasePeriodValue == 1; break; } if (tmpTickCount < bars.Period.BasePeriodValue) { tmpTime = time; endOfBar = false; tmpTickCount++; } else { prevTime = tmpTime; tmpTime = time; endOfBar = true; tmpTickCount = 1; } break; case PeriodType.Week: if (tmpTime == Cbi.Globals.MinDate) prevTime = tmpTime = TimeToBarTimeWeek(time.Date, tmpTime.Date, bars.Period.BasePeriodValue); if (time.Date <= tmpTime.Date) endOfBar = false; else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeWeek(time.Date, tmpTime.Date, bars.Period.BasePeriodValue); } break; case PeriodType.Year: if (tmpTime == Cbi.Globals.MinDate) prevTime = tmpTime = TimeToBarTimeYear(time, bars.Period.Value); if (time.Year <= tmpTime.Year) endOfBar = false; else { prevTime = tmpTime; endOfBar = true; tmpTime = TimeToBarTimeYear(time, bars.Period.Value); } break; default: break; } #endregion #region P&F logic double tickSize = bars.Instrument.MasterInstrument.TickSize; boxSize = Math.Floor(10000000.0 * bars.Period.Value * tickSize) / 10000000.0; reversalSize = bars.Period.Value2 * boxSize; if (bars.Count == 0 || (IsIntraday && bars.IsNewSession(time, isRealtime))) { if (bars.Count > 0) { double lastOpen = bars.GetOpen(bars.Count - 1); double lastHigh = bars.GetHigh(bars.Count - 1); double lastLow = bars.GetLow(bars.Count - 1); double lastClose = bars.GetClose(bars.Count - 1); DateTime lastTime = bars.GetTime(bars.Count - 1); bars.LastPrice = anchorPrice = lastClose; if (bars.Count == tmpCount) CalculatePfBar(bars, lastOpen, lastHigh, lastLow, lastClose, prevTime, lastTime, isRealtime); } AddBar(bars, close, close, close, close, tmpTime, volume, isRealtime); anchorPrice = close; trend = Trend.Undetermined; prevTime = tmpTime; volumeCount = 0; bars.LastPrice = close; tmpCount = bars.Count; tmpHigh = high; tmpLow = low; return; } Bar bar = (Bar)bars.Get(bars.Count - 1); double c = bar.Close; double o = bar.Open; double h = bar.High; double l = bar.Low; DateTime t = bar.Time; if (endOfBar) { CalculatePfBar(bars, o, h, l, c, prevTime, t, isRealtime); volumeCount = volume; tmpHigh = high; tmpLow = low; } else { tmpHigh = (high > tmpHigh ? high : tmpHigh); tmpLow = (low < tmpLow ? low : tmpLow); volumeCount += volume; } bars.LastPrice = close; tmpCount = bars.Count; #endregion }
//protected override void OnBarUpdate() //{ // //TODO: Write your owner OnBarUpdate handling // //DrawArrowUp("Arrowup" + CurrentBar, true, Bars.GetTime(Count - 1), Bars.GetLow(CurrentBar) - 300 * TickSize, Color.Red); // //DrawArrowDown("Arrowdown" + CurrentBar, true, Bars.GetTime(Count - 1), Bars.GetHigh(CurrentBar) + 300 * TickSize, Color.Green); // //Occurred.Set(-1); // //Entry.Set(Close[0]); //} protected override void OnBarUpdate() { //MyGap.Set(Input[0]); if (Bars != null && Bars.Count > 0) // && TimeFrame.Periodicity == DatafeedHistoryPeriodicity.Minute // && TimeFrame.PeriodicityValue == 15) { } else { return; } if (Bars.BarsSinceSession == 0) { sessionprocessed = false; } //08.00, 08.15, 08.30, 08.45, 09.00 sind abgeschlossen -> es ist 09.15) // if(Bars.BarsSinceSession == 5) if (ToTime(Bars.GetTime(CurrentBar)) > 90000 && //größer 09.00 geht für 15M und 1Std (und 1Tag?) sessionprocessed == false) //Tag noch nicht verarbeitet { sessionprocessed = true; GapTradeLong = GapTradeShort = false; IBar GapOpenBar = Bars.Where(x => x.Time.Date == Bars[0].Time.Date).FirstOrDefault(); //liefert erster kerze des tages double GapOpen = GapOpenBar.Open; double LastDayClose = PriorDayOHLC().PriorClose[0]; double GapSize = GapOpen - LastDayClose; DateTime LastDayCloseDate = Bars.GetTime(Count - 7); DateTime LastPeriod = Time[1]; if (LastDayClose != null && Math.Abs(LastDayClose - GapOpen) > _PunkteGapMin && Math.Abs(LastDayClose - GapOpen) < _PunkteGapMax) { //Wenn Gap größer 50 und kleiner 100 existgap = true; //Gap markieren (08.00 - 09.15) string strMyRect = "MyRect" + Count; string strMyGapSize = "MyGapSize" + Count; if (LastDayClose - GapOpen < 0) //Long { //Long //DrawRectangle(strMyRect, true, LastDayCloseDate, LastDayClose, LastPeriod, HighestHighPrice(5)[0], _col_gap, _col_gap, 70); DrawText(strMyGapSize, true, Math.Round(GapSize, 1).ToString(), LastDayCloseDate, LastDayClose + 25, 9, Color.Black, new Font("Areal", 9), StringAlignment.Center, Color.Black, Color.Azure, 1); // if (LinReg(5)[0] > GapOpen) if (LinReg(Closes[0], 5)[0] > GapOpen) { //Chancenreicher SuccessTrade string strArrowUp = "ArrowUp" + Bars.GetTime(CurrentBar); DrawArrowUp(strArrowUp, true, Bars.GetTime(Count - 1), Bars.GetOpen(CurrentBar) - 300 * TickSize, Color.Green); GapTradeLong = true; Occurred.Set(1); Entry.Set(Bars.GetOpen(CurrentBar)); } } else { //Short //DrawRectangle(strMyRect, true, LastDayCloseDate, LastDayClose, LastPeriod, LowestLowPrice(5)[0], Color.Pink, Color.Pink, 70); DrawText(strMyGapSize, true, Math.Round(GapSize, 1).ToString(), LastDayCloseDate, LastDayClose - 25, 9, Color.Black, new Font("Areal", 9), StringAlignment.Center, Color.Black, Color.Azure, 1); if (LinReg(Closes[0], 5)[0] < GapOpen) { ////Chancenreicher SuccessTrade string strArrowDown = "ArrowDown" + Bars.GetTime(CurrentBar); DrawArrowDown(strArrowDown, true, Bars.GetTime(Count - 1), Bars.GetOpen(CurrentBar) + 300 * TickSize, Color.Red); GapTradeShort = true; Occurred.Set(-1); Entry.Set(Bars.GetOpen(CurrentBar)); } } if (GapTradeShort == true || GapTradeLong == true) { Print("------------------" + Time[5] + "------------------"); Print("LineReg: " + Math.Round(LinReg(5)[0]), 1); Print("Gap Open: " + GapOpen); } } else { existgap = false; } } //09.15. - 09.30 Kerze else if (Bars.BarsSinceSession == 6 && existgap == true) { //Auswertung decimal GapTradeResult; Color colorTextBox; GapTradeResult = (decimal)Bars.GetClose(CurrentBar - 1) - (decimal)Bars.GetOpen(CurrentBar - 1); if (GapTradeLong == true) { //Long GapTradeCounterLong += 1; GapTradeResultTotalLong = GapTradeResultTotalLong + GapTradeResult; string strGapeTradeLong = "GapTradeLong" + CurrentBar; string strTradeResultLong; if (GapTradeResult < 0) { Print("FAAAAAAAAAAAAAAAIIIIIIIIIIIIIILLLLLLLLLL"); GapTradeFailCounterLong += 1; strTradeResultLong = "Fail " + GapTradeResult.ToString(); colorTextBox = colFail; } else { GapTradeWinCounterLong += 1; strTradeResultLong = "Win " + GapTradeResult.ToString(); colorTextBox = colWin; } DrawText(strGapeTradeLong, true, strTradeResultLong, Time[1], Bars.GetHigh(CurrentBar - 1) + 10, 9, Color.Black, new Font("Areal", 9), StringAlignment.Center, Color.Black, colorTextBox, 70); } else if (GapTradeShort == true) { //Short GapTradeCounterShort += 1; GapTradeResultTotalShort = GapTradeResultTotalShort - GapTradeResult; string strGapeTradeShort = "GapTradeLong" + CurrentBar; string strTradeResultShort; if (GapTradeResult > 0) { Print("FAAAAAAAAAAAAAAAIIIIIIIIIIIIIILLLLLLLLLL"); GapTradeFailCounterShort += 1; strTradeResultShort = "Fail " + GapTradeResult.ToString(); colorTextBox = colFail; } else { GapTradeWinCounterShort += 1; strTradeResultShort = "Win " + GapTradeResult.ToString(); colorTextBox = colWin; } DrawText(strGapeTradeShort, true, strTradeResultShort, Time[1], Bars.GetLow(CurrentBar - 1) - 10, 9, Color.Black, new Font("Areal", 9), StringAlignment.Center, Color.Black, colorTextBox, 70); } Print("Gap Trade Result: " + GapTradeResult); } if (Count == Bars.Count - 1) { Print("Total Trades Long: " + GapTradeCounterLong); Print("Wins Long: " + GapTradeWinCounterLong); Print("Fails Long: " + GapTradeFailCounterLong); Print("Total Trades Short: " + GapTradeCounterShort); Print("Wins Short: " + GapTradeWinCounterShort); Print("Fails Short: " + GapTradeFailCounterShort); if (GapTradeCounterLong > 0) { Print("Avg Long: " + (GapTradeResultTotalLong / GapTradeCounterLong)); } if (GapTradeCounterShort > 0) { Print("Avg Short: " + (GapTradeResultTotalShort / GapTradeCounterShort)); } } }
private void plotSwingPivots(Dictionary <int, string> reducedPivots, string prefix) { List <int> orderedMarkers = reducedPivots.Keys.ToList(); orderedMarkers.Sort(); int objnum = 0; string previous = ""; foreach (int marker in orderedMarkers) { if (reducedPivots[marker].Equals(MIN_MAX) && previous.Equals(MAX)) { DateTime timeValue = Bars.GetSessionEndTime(marker); Draw.Diamond(this, objnum.ToString() + prefix + "M", false, timeValue, Bars.GetLow(marker), Brushes.Blue); objnum++; timeValue = Bars.GetSessionEndTime(marker); Draw.Diamond(this, objnum.ToString() + prefix + "M", false, timeValue, Bars.GetHigh(marker), Brushes.White); objnum++; } else if (reducedPivots[marker].Equals(MIN_MAX) && previous.Equals(MIN)) { DateTime timeValue = Bars.GetSessionEndTime(marker); Draw.Diamond(this, objnum.ToString() + prefix + "M", false, timeValue, Bars.GetHigh(marker), Brushes.White); objnum++; timeValue = Bars.GetSessionEndTime(marker); Draw.Diamond(this, objnum.ToString() + prefix + "M", false, timeValue, Bars.GetLow(marker), Brushes.Blue); objnum++; } else if (reducedPivots[marker].Equals(MAX)) { DateTime timeValue = Bars.GetSessionEndTime(marker); Draw.Diamond(this, objnum.ToString() + prefix + "M", false, timeValue, Bars.GetHigh(marker), Brushes.White); objnum++; } else { DateTime timeValue = Bars.GetSessionEndTime(marker); Draw.Diamond(this, objnum.ToString() + prefix + "M", false, timeValue, Bars.GetLow(marker), Brushes.Blue); objnum++; } previous = reducedPivots[marker]; } }