protected override void OnBarUpdate() //Runs each time a new bar is formed { if (BarsInProgress == 0) //Runs everytime a new 2000 Ticks Bar is formed { if (CurrentBar < 0 || CurrentBar == null) { return; } //Last swing high is higher than the most recent swing high, means market is trending down int swingHighBar = swing.SwingHighBar(0, 1, Strength + 1); if (swingHighBar != -1) //swing point is found within the lookBackPeriod { double swingHighPrice = High[swingHighBar]; if (swingHighPrice < lastHighPrice && lastHighBar > -1) { if (trendBreak) { CurrentTrend = new TrendChannels(lastHighBar, lastHighPrice, CurrentBar - swingHighBar, swingHighPrice, lastLowBar, lastLowPrice) { IsHigh = false }; trendChannel.Enqueue(CurrentTrend); trendBreak = false; } } lastHighBar = CurrentBar - swingHighBar; lastHighPrice = swingHighPrice; } //Last swing low is lower than the most recent swing low, means market is trending up int swingLowBar = swing.SwingLowBar(0, 1, Strength + 1); if (swingLowBar != -1) //swing point is found within the lookBackPeriod { double swingLowPrice = Low[swingLowBar]; if (swingLowPrice > lastLowPrice && lastLowBar > -1) { if (trendBreak) { CurrentTrend = new TrendChannels(lastLowBar, lastLowPrice, CurrentBar - swingLowBar, swingLowPrice, lastHighBar, lastHighPrice) { IsHigh = true }; trendChannel.Enqueue(CurrentTrend); trendBreak = false; } } lastLowBar = CurrentBar - swingLowBar; lastLowPrice = swingLowPrice; } if (!trendBreak) { //top and bottom of the trend channel double highRay = CurrentTrend.ParallelPrice + (CurrentBar - CurrentTrend.ParallelBar) * CurrentTrend.Slope; double lowRay = CurrentTrend.EndPrice + (CurrentBar - CurrentTrend.EndBar) * CurrentTrend.Slope; //if price breaks the trend channel, new trend channel is forming if (Close[0] > highRay + 0.5 * TickSize || Close[0] < lowRay - 0.5 * TickSize) //0.5*TickSize for filtering false breakout { Draw.Text(this, "broken" + CurrentBar, "broken", 0, High[0], Brushes.Black); trendBreak = true; CurrentTrend = null; } } } else { return; } }
protected override void OnBarUpdate() //anytime a bar closes, so every 2000 ticks, this method get called { if (BarsInProgress == 0) //Runs everytime a new 2000 Ticks Bar is formed { if (CurrentBar == null || CurrentBar < BarsRequiredToTrade) { return; } //Last swing high is higher than the most recent swing high, means market is trending down int swingHighBar = swing.SwingHighBar(0, 1, Strength + 1); if (swingHighBar != -1) //swing point is found within the lookBackPeriod { double swingHighPrice = High[swingHighBar]; if (swingHighPrice < lastHighPrice && lastHighBar > -1) { if (trendBreak) { CurrentTrend = new TrendChannels(lastHighBar, lastHighPrice, CurrentBar - swingHighBar, swingHighPrice, lastLowBar, lastLowPrice) { IsHigh = false }; trendChannel.Enqueue(CurrentTrend); trendBreak = false; } } lastHighBar = CurrentBar - swingHighBar; lastHighPrice = swingHighPrice; } //Last swing low is lower than the most recent swing low, means market is trending up int swingLowBar = swing.SwingLowBar(0, 1, Strength + 1); if (swingLowBar != -1) //swing point is found within the lookBackPeriod { double swingLowPrice = Low[swingLowBar]; if (swingLowPrice > lastLowPrice && lastLowBar > -1) { if (trendBreak) { CurrentTrend = new TrendChannels(lastLowBar, lastLowPrice, CurrentBar - swingLowBar, swingLowPrice, lastHighBar, lastHighPrice) { IsHigh = true }; trendChannel.Enqueue(CurrentTrend); trendBreak = false; } } lastLowBar = CurrentBar - swingLowBar; lastLowPrice = swingLowPrice; } if (!trendBreak) { //top and bottom of the trend channel double highRay = CurrentTrend.ParallelPrice + (CurrentBar - CurrentTrend.ParallelBar) * CurrentTrend.Slope; double lowRay = CurrentTrend.EndPrice + (CurrentBar - CurrentTrend.EndBar) * CurrentTrend.Slope; //if price breaks the trend channel, new trend channel is forming if (Close[0] > highRay + 0.5 * TickSize || Close[0] < lowRay - 0.5 * TickSize) //0.5*TickSize for filtering false breakout { Draw.Text(this, "broken" + CurrentBar, "broken", 0, High[0], Brushes.Black); trendBreak = true; CurrentTrend = null; } } if (buy_leg == null) { buy_leg = new buy_2lpb(this, High[0]); sell_leg = new sell_2lpb(this, Low[0]); } buy_leg.Setup(ref buy_leg); sell_leg.Setup(ref sell_leg); sellEntryBar = false; buyEntryBar = false; if (enter_buy) { IntraBar(this, true); } if (enter_sell) { IntraBar(this, false); } } else if (BarsInProgress == 1) //Runs within each 5 tick bar { if (ToTime(Time[0]) >= 73000 && ToTime(Time[0]) < 153000) //allow entering trades during market open { if (enter_buy && (Close[0] >= (buy_leg.legs[3] + 0.25)) && buyEntryBar && CurrentTrend.IsHigh) { enter_buy = false; EnterLong(1, "My Buy Entry"); } if (enter_sell && (Close[0] <= (sell_leg.legs[3] - 0.25)) && sellEntryBar && CurrentTrend.IsHigh) { enter_sell = false; EnterShort(1, "My Sell Entry"); } } } else { return; } }