/// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { // Ignore bar update events for the supplementary Bars object added above if (BarsInProgress == 0) { return; } /* * if (ToTime(Time[0]) < ToTime(3, 00, 0)) * return; * * if (ToTime(Time[0]) >= ToTime(8, 00, 0)) * return; */ ic = IchiCloud(BarsArray[1], periodFast, periodMedium, periodSlow); ema = EMA(BarsArray[1], 13); //zz = ZigZag(BarsArray[1], DeviationType.Points, deviationValue, useHighLow); swing = Swing(BarsArray[1], strength); //SetHigherHighsAndLowerLowers(1); if (Position.MarketPosition == MarketPosition.Long) { // check for sell trigger on a down bar if (Close[0] < Open[0]) { sellTrigger = Math.Max(sellTrigger, Low[0] - 1 * TickSize); //DrawDot(CurrentBar + "sell", false, 0, ic[0], Color.Pink); } else { double stop = Math.Max(Position.AvgPrice - stopLoss * TickSize, sellTrigger); DrawDot(CurrentBar + "stop", false, 0, stop, Color.Pink); SetStopLoss(CalculationMode.Price, stop); } if (sellTrigger < (Position.AvgPrice - stopLoss * TickSize)) { //SetStopLoss(CalculationMode.Price, sellTrigger); //DrawDot(CurrentBar + "ouch", false, 0, sellTrigger, Color.Yellow); } } //hh = High[swing.SwingHighBar(0, 1, 1000)] > High[swing.SwingHighBar(0, 2, 1000)]; //ll = Low[swing.SwingLowBar(swing.SwingHighBar(0, 1, 1000), 1, 1000)] SetSwingHighAndLows(); if ( //!newHighPending High[swing.SwingHighBar(0, 1, 500)] > High[swing.SwingHighBar(0, 2, 500)] && Low[swing.SwingLowBar(0, 1, 500)] > Low[swing.SwingLowBar(0, 2, 500)] //&& Low[swing.SwingLowBar(swing.SwingHighBar(0, 1, 500), 1, 500)] < Low[0] && High[0] > High[swing.SwingHighBar(20, 1, 500)] && (High[0] - stopLoss * TickSize < ic[0] || High[0] - stopLoss * TickSize < ema[0]) && Close[0] > ic[0] //&& hl ) { BackColor = Color.Beige; //DrawDot(CurrentBar + "ic", false, 0, ic[0], Color.Pink); if (isValidLongTrigger()) { if (Position.MarketPosition == MarketPosition.Flat) { EnterLongStop(High[0] + 1 * TickSize); // set to some real high value sellTrigger = 0; SetStopLoss(CalculationMode.Price, High[0] + 1 * TickSize - stopLoss * TickSize); DrawDot(CurrentBar + "estop", false, 0, High[0] + 1 * TickSize - stopLoss * TickSize, Color.Blue); } } } }
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 == null || CurrentBar < BarsRequiredToTrade) { return; } sellEntryBar = false; buyEntryBar = false; //Last swing high is higher than the most recent swing high, which means market is trending down int swingHighBar = swing.SwingHighBar(0, 1, 2 + 1); if (swingHighBar == 2) { double swingHighPrice = High[swingHighBar]; if (swingHighPrice < lastHighPrice && isUp == 0 && lastHighBar > -1) { isUp = -1; } else if (swingHighPrice > lastHighPrice && isUp == -1 && lastHighBar > -1) { isUp = 0; } lastHighBar = CurrentBar - swingHighBar; lastHighPrice = swingHighPrice; } //Last swing low is lower than the most recent swing low, which means market is trending up int swingLowBar = swing.SwingLowBar(0, 1, 2 + 1); if (swingLowBar == 2) { double swingLowPrice = Low[swingLowBar]; if (swingLowPrice > lastLowPrice && isUp == 0 && lastLowBar > -1) { isUp = 1; } else if (swingLowPrice < lastLowPrice && isUp == 1 && lastLowBar > -1) { isUp = 0; } lastLowBar = CurrentBar - swingLowBar; lastLowPrice = swingLowPrice; } if (Close[0] > lastHighPrice && isUp == -1) { isUp = 1; } if (Close[0] < lastLowPrice && isUp == 1) { isUp = -1; } } else if (BarsInProgress == 1) //Runs every single tick { if (CurrentBars[0] == null || CurrentBars[0] < BarsRequiredToTrade) { return; } //doesn't run if no bars has formed yet if (lastCurrentBar != CurrentBars[0]) { lastHigh = High[0]; lastLow = Low[0]; } lastClose = Close[0]; if (High[0] > lastHigh) { lastHigh = High[0]; } if (Low[0] < lastLow) { lastLow = Low[0]; } lastCurrentBar = CurrentBars[0]; if (Position.MarketPosition == MarketPosition.Flat) //runs if currently, there are no open positions { SetStopLoss(CalculationMode.Ticks, 12); Stoplossstorer = 1; if (ToTime(Time[0]) >= 73000 && ToTime(Time[0]) < 153000) //allow entering trades during market open { if (isUp == 1 && (CCI(Closes[0], 14)[0] <= 100 && CCIPredictor(this, 14) >= 100 || CCI(Closes[0], 14)[0] <= -100 && CCIPredictor(this, 14) >= -100)) { EnterLong(1); } if (isUp == -1 && (CCI(Closes[0], 14)[0] >= -100 && CCIPredictor(this, 14) <= -100 || CCI(Closes[0], 14)[0] >= 100 && CCIPredictor(this, 14) <= 100)) { EnterShort(1); } } } //trailing stoploss for collecting profit and securing gains else if (Position.MarketPosition == MarketPosition.Long) { while (Close[0] >= Position.AveragePrice + Stoplossstorer * 2) { SetStopLoss(CalculationMode.Price, Position.AveragePrice + 2 * (Stoplossstorer - 1)); Stoplossstorer++; } } else if (Position.MarketPosition == MarketPosition.Short) { while (Close[0] <= Position.AveragePrice - Stoplossstorer * 2) { SetStopLoss(CalculationMode.Price, Position.AveragePrice - 2 * (Stoplossstorer - 1)); Stoplossstorer++; } } } else { return; } }
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; } }