/// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (CurrentBar == 1) { Print("Date,open,high,low,close,sessionLow,sessionHigh"); } if (Bars.BarsSinceSession == 100000000) { double value = StdDev(High, 100)[0]; Print(Time + " The current StdDev value is " + value.ToString()); // Time Series Forecast function displays the statistical trend of a // security's price over a specified time period based on linear regression analysis. value = TSF(6, 20)[0]; Print(Time + " The current TSF value is " + value.ToString()); double median = (GetMedian(Open, 9)); Print(Time + " The median of the last 10 open prices is: " + GetMedian(Open, 9).ToString()); DrawVerticalLine(CurrentBar + "line", 0, Color.Blue); } if (Bars.FirstBarOfSession) { double open = Bars.GetDayBar(tradingDaysBack).Open; double high = Bars.GetDayBar(tradingDaysBack).High; double low = Bars.GetDayBar(tradingDaysBack).Low; double close = Bars.GetDayBar(tradingDaysBack).Close; Print(Bars.GetDayBar(tradingDaysBack).Time + "," + open + "," + high + "," + low + "," + close + "," + sessionLow + "," + sessionHigh); } if (ToTime(Time[0]) == ToTime(hour, minute, 0)) { sessionHigh = High[HighestBar(High, Bars.BarsSinceSession - 1)]; sessionLow = Low[LowestBar(Low, Bars.BarsSinceSession - 1)]; } //NetVolDoublerAlert nd = new NetVolDoublerAlert(); }
protected override void OnBarUpdate() { //BK add **************************** if (Bars.FirstBarOfSession && FirstTickOfBar) { sessioncount = sessioncount + 1; } if (sessioncount < 8) { return; // Need 7 days of bars } if (Bars.FirstBarOfSession) { //Build Array for 7 days back data for (int i = 1; i < 8; i++) { // Get Day data for previous days 1-7 if (Bars.GetDayBar(i) != null) { d_high[i] = Bars.GetDayBar(i).High; d_low[i] = Bars.GetDayBar(i).Low; } } } //******************************************************* // Get 7 Day High and Low //******************************************************* d_high_7 = 0; d_low_7 = 999; for (int i = 1; i < 8; i++) // { if (d_high[i] > d_high_7) { d_high_7 = d_high[i]; } if (d_low[i] < d_low_7) { d_low_7 = d_low[i]; } } if (CurrentDayOHL().CurrentHigh[0] > d_high_7) { d_high_7 = CurrentDayOHL().CurrentHigh[0]; } if (CurrentDayOHL().CurrentLow[0] < d_low_7) { d_low_7 = CurrentDayOHL().CurrentLow[0]; } //************************************* ma.Set(EMA(SMA(Median, period), smooth)[0]); if (CurrentBar > 0 && Volume[0] == 0 || Volume[1] == 0) { return; } //************** // Long Orders //************** if (ToTime(Time[0]) > starttime && ToTime(Time[0]) < endtime && Position.MarketPosition != MarketPosition.Long && ma[0] > ma[entrydisplacement] && ma[1] <= ma[entrydisplacement + 1] // BK Add && CurrentDayOHL().CurrentLow[0] > d_low_7) { EnterLong(numcontracts, "Long"); if (Close[0] < Open[0]) { // SetStopLoss("Long",CalculationMode.Price,Close[0]-((stoploss-4)*TickSize),false); stoppricelong = Close[0] - ((stoploss - 4) * TickSize); } else { // SetStopLoss("Long",CalculationMode.Price,Close[0]-(stoploss*TickSize),false); stoppricelong = Close[0] - (stoploss * TickSize); } entrypricelong = Close[0]; entrybarlong = CurrentBar; } //***************** // Short Orders //***************** else if ((direction == 0 || direction == -1) && ToTime(Time[0]) > starttime && ToTime(Time[0]) < endtime && Position.MarketPosition != MarketPosition.Short && ma[0] < ma[entrydisplacement] && ma[1] >= ma[entrydisplacement + 1] // BK Add && CurrentDayOHL().CurrentHigh[0] < d_high_7) { EnterShort(numcontracts, "Short"); if (Close[0] > Open[0]) { // SetStopLoss("Short",CalculationMode.Price,Close[0]+((stoploss-4)*TickSize),false); stoppriceshort = Close[0] + ((stoploss - 4) * TickSize); } else { // SetStopLoss("Short",CalculationMode.Price,Close[0]+(stoploss*TickSize),false); stoppriceshort = Close[0] + (stoploss * TickSize); } entrypriceshort = Close[0]; entrybarshort = CurrentBar; } //****************** // Long Exits //****************** else if (Position.MarketPosition == MarketPosition.Long) { DrawLine("entry", true, CurrentBar - entrybarlong + 3, entrypricelong, 0, entrypricelong, Color.Black, DashStyle.Solid, 1); DrawLine("stop", true, CurrentBar - entrybarlong + 3, stoppricelong, 0, stoppricelong, Color.Black, DashStyle.Solid, 1); DrawTextFixed("positionlong", "Current PNL: " + RoundPrice((Close[0] - entrypricelong) / TickSize), TextPosition.Center, Color.White, textFont, Color.Black, Color.Black, 10); if (ma[0] < ma[entrydisplacement] || Close[0] <= stoppricelong) { ExitLong("Long"); // DrawTriangleUp("long"+CurrentBar,true,0,Channel(43).Lower[1],Color.Black); } } //**************** // Short Exits //**************** else if (Position.MarketPosition == MarketPosition.Short) { DrawLine("entry", true, CurrentBar - entrybarshort + 3, entrypriceshort, 0, entrypriceshort, Color.Black, DashStyle.Solid, 1); DrawLine("stop", true, CurrentBar - entrybarshort + 3, stoppriceshort, 0, stoppriceshort, Color.Black, DashStyle.Solid, 1); DrawTextFixed("positionshort", "Current PNL: " + RoundPrice((entrypriceshort - Close[0]) / TickSize), TextPosition.Center, Color.White, textFont, Color.Black, Color.Black, 10); if (ma[0] > ma[entrydisplacement] || Close[0] >= stoppriceshort) { ExitShort("Short"); } } //************************** // Remove Drawing Objects //************************** if (Position.MarketPosition != MarketPosition.Long) { RemoveDrawObject("positionlong"); } if (Position.MarketPosition != MarketPosition.Short) { RemoveDrawObject("positionshort"); } if (Position.MarketPosition == MarketPosition.Flat) { RemoveDrawObject("entry"); RemoveDrawObject("stop"); } //*********************************** // Plot PnL/ Stats ... on Chart //*********************************** // if ((int) Performance.AllTrades.TradesPerformance.Currency.CumProfit != previousPNL) // Print(Time[0] + " " + Performance.LongTrades.TradesPerformance.Currency.CumProfit.ToString("0") + " + " + Performance.ShortTrades.TradesPerformance.Currency.CumProfit.ToString("0") + " = " + Performance.AllTrades.TradesPerformance.Currency.CumProfit.ToString("0")); DrawTextFixed("pnl", "All: " + Performance.AllTrades.TradesCount + " W:" + (Performance.AllTrades.WinningTrades.Count) + " L: " + (Performance.AllTrades.LosingTrades.Count) + " PNL: $" + (int)Performance.AllTrades.TradesPerformance.Currency.CumProfit + "\nLongs: " + Performance.LongTrades.TradesCount + " W: " + (Performance.LongTrades.WinningTrades.Count) + " L: " + (Performance.LongTrades.LosingTrades.Count) + " PNL: $" + (int)Performance.LongTrades.TradesPerformance.Currency.CumProfit + "\nShorts: " + Performance.ShortTrades.TradesCount + " W: " + (Performance.ShortTrades.WinningTrades.Count) + " L: " + (Performance.ShortTrades.LosingTrades.Count) + " PNL: $" + (int)Performance.ShortTrades.TradesPerformance.Currency.CumProfit + "\nAccount: " + this.Account.Name + "\nAccountBalance: " + this.GetAccountValue(AccountItem.CashValue).ToString("0") , TextPosition.TopLeft, Color.White, textFont, Color.Black, Color.Black, 10); previousPNL = (int)Performance.AllTrades.TradesPerformance.Currency.CumProfit; }
/// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { //string kObj = "OnBarUpdate"; if (Bars == null) { return; } IBDataSeries ib30; IBDataSeries ib60; IBDataSeries ibEOD; #region IBPrior if (Bars.BarsSinceSession == 0) { recordIB30 = true; recordIBEOD = true; recordIB60 = true; ib30 = new IBDataSeries() { StartBarSinceSession = Bars.BarsSinceSession, TradeDate = Bars.GetTradingDayFromLocal(Time[0]).ToShortDateString(), PriorDayOpen = Bars.GetDayBar(1).Open, PriorDayHigh = Bars.GetDayBar(1).High, PriorDayLow = Bars.GetDayBar(1).Low, PriorDayClose = Bars.GetDayBar(1).Close, PriorVolume = Bars.GetDayBar(1).Volume, CurrDayOpen = Open[0], IBOpen = Open[0] }; ib60 = new IBDataSeries() { StartBarSinceSession = Bars.BarsSinceSession, TradeDate = Bars.GetTradingDayFromLocal(Time[0]).ToShortDateString(), PriorDayOpen = Bars.GetDayBar(1).Open, PriorDayHigh = Bars.GetDayBar(1).High, PriorDayLow = Bars.GetDayBar(1).Low, PriorDayClose = Bars.GetDayBar(1).Close, PriorVolume = Bars.GetDayBar(1).Volume, CurrDayOpen = Open[0], IBOpen = Open[0] }; ibEOD = new IBDataSeries() { StartBarSinceSession = Bars.BarsSinceSession, TradeDate = Bars.GetTradingDayFromLocal(Time[0]).ToShortDateString(), PriorDayOpen = Bars.GetDayBar(1).Open, PriorDayHigh = Bars.GetDayBar(1).High, PriorDayLow = Bars.GetDayBar(1).Low, PriorDayClose = Bars.GetDayBar(1).Close, PriorVolume = Bars.GetDayBar(1).Volume, CurrDayOpen = Open[0], IBOpen = Open[0] }; dIB30.Add(idxIB30, ib30); dIB60.Add(idxIB60, ib60); dIBEOD.Add(idxIBEOD, ibEOD); idxIB30++; idxIB60++; idxIBEOD++; //kLog(kObj, "PRIOR", String.Format(" {0} prior. O {2} H {3} L {4} C {5} V {6} R {7}", // ib.StartBarSinceSession, // ib.TradeDate, // ib.PriorDayOpen.ToString("0.00"), // ib.PriorDayHigh.ToString("0.00"), // ib.PriorDayLow.ToString("0.00"), // ib.PriorDayClose.ToString("0.00"), // ib.PriorVolume.ToString("0"), // (ib.PriorDayClose - ib.PriorDayOpen).ToString("0.00") // )); } #endregion #region IB30 if (ToTime(Time[0]) >= ib30Time && recordIB30 == true) { double ibHigh = 0; double ibLow = 9999; int emaCrossCount = 0; int ibBullCount = 0; int ibBearCount = 0; int ibDojiCount = 0; double ibVolume = 0; string name = "IB30"; for (int i = 0; i < Bars.BarsSinceSession; i++) { ibHigh = Math.Max(ibHigh, High[i]); ibLow = Math.Min(ibLow, Low[i]); ibVolume = ibVolume + Volume[i]; if (Close[i] - Open[i] > 0) { ibBullCount++; } else if (Close[i] - Open[i] < 0) { ibBearCount++; } else if (Close[i] - Open[i] == 0) { ibDojiCount++; } if ((CrossAbove(EMA(emaPrimaryPeriod), EMA(emaReferencePeriod), i)) || (CrossBelow(EMA(emaPrimaryPeriod), EMA(emaReferencePeriod), i))) { emaCrossCount++; } } if (dIB30.Count > 0) { dIB30[dIB30.Count - 1].IBClose = Close[0]; dIB30[dIB30.Count - 1].IBEndBar = Bars.BarsSinceSession; dIB30[dIB30.Count - 1].IBLow = ibLow; dIB30[dIB30.Count - 1].IBVolume = ibVolume; dIB30[dIB30.Count - 1].IBHigh = ibHigh; dIB30[dIB30.Count - 1].IBBullCount = ibBullCount; dIB30[dIB30.Count - 1].IBBearCount = ibBearCount; dIB30[dIB30.Count - 1].IBDojiCount = ibDojiCount; dIB30[dIB30.Count - 1].IBEmaPrimaryAtClose = EMA(emaPrimaryPeriod)[0]; dIB30[dIB30.Count - 1].IBEmaReferenceAtClose = EMA(emaReferencePeriod)[0]; dIB30[dIB30.Count - 1].IBEmaCrossCount = emaCrossCount; string insertIB30 = String.Format("Insert into algoIB values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}')", dIB30[dIB30.Count - 1].TradeDate.ToString() + "-" + name, dIB30[dIB30.Count - 1].TradeDate, name, dIB30[dIB30.Count - 1].IBOpen, dIB30[dIB30.Count - 1].IBHigh, dIB30[dIB30.Count - 1].IBLow, dIB30[dIB30.Count - 1].IBClose, dIB30[dIB30.Count - 1].IBVolume, dIB30[dIB30.Count - 1].StartBarSinceSession, dIB30[dIB30.Count - 1].IBEndBar, dIB30[dIB30.Count - 1].IBBullCount, dIB30[dIB30.Count - 1].IBBearCount, dIB30[dIB30.Count - 1].IBDojiCount, dIB30[dIB30.Count - 1].IBEmaCrossCount, dIB30[dIB30.Count - 1].IBEmaPrimaryAtClose.ToString("0.00"), dIB30[dIB30.Count - 1].IBEmaReferenceAtClose.ToString("0.00") ); Print(insertIB30); MySqlCommand insertIBQuery30 = new MySqlCommand(insertIB30, dbConn); insertIBQuery30.ExecuteNonQuery(); recordIB30 = false; // to prevent double prints } } #endregion #region IB60 if (ToTime(Time[0]) >= ib60Time && recordIB60 == true) { double ibHigh = 0; double ibLow = 9999; int emaCrossCount = 0; int ibBullCount = 0; int ibBearCount = 0; int ibDojiCount = 0; double ibVolume = 0; string name = "IB60"; for (int i = 0; i < Bars.BarsSinceSession; i++) { ibHigh = Math.Max(ibHigh, High[i]); ibLow = Math.Min(ibLow, Low[i]); ibVolume = ibVolume + Volume[i]; if (Close[i] - Open[i] > 0) { ibBullCount++; } else if (Close[i] - Open[i] < 0) { ibBearCount++; } else if (Close[i] - Open[i] == 0) { ibDojiCount++; } if ((CrossAbove(EMA(emaPrimaryPeriod), EMA(emaReferencePeriod), i)) || (CrossBelow(EMA(emaPrimaryPeriod), EMA(emaReferencePeriod), i))) { emaCrossCount++; } } if (dIB60.Count > 0) { dIB60[dIB60.Count - 1].IBClose = Close[0]; dIB60[dIB60.Count - 1].IBEndBar = Bars.BarsSinceSession; dIB60[dIB60.Count - 1].IBLow = ibLow; dIB60[dIB60.Count - 1].IBVolume = ibVolume; dIB60[dIB60.Count - 1].IBHigh = ibHigh; dIB60[dIB60.Count - 1].IBBullCount = ibBullCount; dIB60[dIB60.Count - 1].IBBearCount = ibBearCount; dIB60[dIB60.Count - 1].IBDojiCount = ibDojiCount; dIB60[dIB60.Count - 1].IBEmaPrimaryAtClose = EMA(emaPrimaryPeriod)[0]; dIB60[dIB60.Count - 1].IBEmaReferenceAtClose = EMA(emaReferencePeriod)[0]; dIB60[dIB60.Count - 1].IBEmaCrossCount = emaCrossCount; string insertIB60 = String.Format("Insert into algoIB values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}')", dIB60[dIB60.Count - 1].TradeDate.ToString() + "-" + name, dIB60[dIB60.Count - 1].TradeDate, name, dIB60[dIB60.Count - 1].IBOpen, dIB60[dIB60.Count - 1].IBHigh, dIB60[dIB60.Count - 1].IBLow, dIB60[dIB60.Count - 1].IBClose, dIB60[dIB60.Count - 1].IBVolume, dIB60[dIB60.Count - 1].StartBarSinceSession, dIB60[dIB60.Count - 1].IBEndBar, dIB60[dIB60.Count - 1].IBBullCount, dIB60[dIB60.Count - 1].IBBearCount, dIB60[dIB60.Count - 1].IBDojiCount, dIB60[dIB60.Count - 1].IBEmaCrossCount, dIB60[dIB60.Count - 1].IBEmaPrimaryAtClose.ToString("0.00"), dIB60[dIB60.Count - 1].IBEmaReferenceAtClose.ToString("0.00") ); Print(insertIB60); try { MySqlCommand insertIBQuery60 = new MySqlCommand(insertIB60, dbConn); insertIBQuery60.ExecuteNonQuery(); } catch (Exception ex) { Print(ex.ToString()); } recordIB60 = false; // to prevent double prints } } #endregion #region IBEOD if (ToTime(Time[0]) >= ibEndTime && recordIBEOD == true) { double ibHigh = 0; double ibLow = 9999; int emaCrossCount = 0; int ibBullCount = 0; int ibBearCount = 0; int ibDojiCount = 0; double ibVolume = 0; string name = "IBEOD"; for (int i = 0; i < Bars.BarsSinceSession; i++) { ibHigh = Math.Max(ibHigh, High[i]); ibLow = Math.Min(ibLow, Low[i]); ibVolume = ibVolume + Volume[i]; if (Close[i] - Open[i] > 0) { ibBullCount++; } else if (Close[i] - Open[i] < 0) { ibBearCount++; } else if (Close[i] - Open[i] == 0) { ibDojiCount++; } if ((CrossAbove(EMA(emaPrimaryPeriod), EMA(emaReferencePeriod), i)) || (CrossBelow(EMA(emaPrimaryPeriod), EMA(emaReferencePeriod), i))) { emaCrossCount++; } } if (dIBEOD.Count > 0) { dIBEOD[dIBEOD.Count - 1].IBClose = Close[0]; dIBEOD[dIBEOD.Count - 1].IBEndBar = Bars.BarsSinceSession; dIBEOD[dIBEOD.Count - 1].IBLow = ibLow; dIBEOD[dIBEOD.Count - 1].IBVolume = ibVolume; dIBEOD[dIBEOD.Count - 1].IBHigh = ibHigh; dIBEOD[dIBEOD.Count - 1].IBBullCount = ibBullCount; dIBEOD[dIBEOD.Count - 1].IBBearCount = ibBearCount; dIBEOD[dIBEOD.Count - 1].IBDojiCount = ibDojiCount; dIBEOD[dIBEOD.Count - 1].IBEmaPrimaryAtClose = EMA(emaPrimaryPeriod)[0]; dIBEOD[dIBEOD.Count - 1].IBEmaReferenceAtClose = EMA(emaReferencePeriod)[0]; dIBEOD[dIBEOD.Count - 1].IBEmaCrossCount = emaCrossCount; string insertIBEOD = String.Format("Insert into algoIB values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}')", dIBEOD[dIBEOD.Count - 1].TradeDate.ToString() + "-" + name, dIBEOD[dIBEOD.Count - 1].TradeDate, name, dIBEOD[dIBEOD.Count - 1].IBOpen, dIBEOD[dIBEOD.Count - 1].IBHigh, dIBEOD[dIBEOD.Count - 1].IBLow, dIBEOD[dIBEOD.Count - 1].IBClose, dIBEOD[dIBEOD.Count - 1].IBVolume, dIBEOD[dIBEOD.Count - 1].StartBarSinceSession, dIBEOD[dIBEOD.Count - 1].IBEndBar, dIBEOD[dIBEOD.Count - 1].IBBullCount, dIBEOD[dIBEOD.Count - 1].IBBearCount, dIBEOD[dIBEOD.Count - 1].IBDojiCount, dIBEOD[dIBEOD.Count - 1].IBEmaCrossCount, dIBEOD[dIBEOD.Count - 1].IBEmaPrimaryAtClose.ToString("0.00"), dIBEOD[dIBEOD.Count - 1].IBEmaReferenceAtClose.ToString("0.00") ); Print(insertIBEOD); MySqlCommand insertIBQuery = new MySqlCommand(insertIBEOD, dbConn); insertIBQuery.ExecuteNonQuery(); recordIBEOD = false; // to prevent double prints } } #endregion }
protected override void OnBarUpdate() { if (Bars.IsFirstBarOfSession) { Print(string.Format("Bar number {0} was the first bar processed of the session at {1}.", CurrentBar, Time[0])); } GIFibonacci fib = GIFibonacci(1); Print(CurrentBar + ":BarsInProgress, Bars.GetDayBar(1)=" + BarsInProgress + "," + Bars.GetDayBar(1)); if (BarsInProgress != 0) { return; } if (Bars.GetDayBar(1) != null) { // Print(string.Format( "{0}: day[1] Hi={1}, Lo={2}, Close={3}, day[0] high={4}, low={5}, open={6}", // CurrentBar, Bars.GetDayBar(1).High, Bars.GetDayBar(1).Low, Bars.GetDayBar(1).Close, // CurrentDayOHL().CurrentHigh[0], CurrentDayOHL().CurrentLow[0], CurrentDayOHL().CurrentOpen[0])); if (ShowLastdayHL) { LastDaySpt[0] = Bars.GetDayBar(1) == null? double.MinValue:Bars.GetDayBar(1).Low; //Values[4][0] = Bars.GetDayBar(1).Low; LastDayRst[0] = Bars.GetDayBar(1) == null? double.MaxValue:Bars.GetDayBar(1).High; //Values[5][0] = Bars.GetDayBar(1).High; CheckLastDayHLEvent(); } if (ShowLastdayClose) { LastDayClose[0] = Bars.GetDayBar(1).Close; } if (ShowTodayOpen) { if (IsStartTimeBar(GetTimeByHM(TM_OpenStartH, TM_OpenStartM, true), ToTime(Time[0]), ToTime(Time[1]))) { today_open = Open[0]; } if (today_open > 0) { TodayOpen[0] = today_open; //CurrentDayOHL().CurrentOpen[0]; } } if (ShowOvernightHL) { if (GetOvernightHigh() > 0) { OverNightRst[0] = overnight_hi; } if (GetOvernightLow() > 0) { OverNightSpt[0] = overnight_lo; } } getHLByTimeRange.Update(); //Print(CurrentBar + ":" + ShowOpenHL + "," + getHLByTimeRange.SnRRange + "," + getHLByTimeRange.SnRRanges); if (ShowOpenHL && getHLByTimeRange.SnRRange != null) { OpenRst[0] = getHLByTimeRange.SnRRange.Resistance.SnRPrice; OpenSpt[0] = getHLByTimeRange.SnRRange.Support.SnRPrice; } } //double Rst = GIGetHighLowByTimeRange(High, 8, 30, 9, 30).HighestHigh[0]; //double Spt = GIGetHighLowByTimeRange(Low, 8, 30, 9, 30).LowestLow[0]; // Go long if we have three up bars on all bars objects //if (Close[0] > Open[0] && Closes[1][0] > Opens[1][0] && Closes[2][0] > Opens[2][0]) // if(CurrentBar > 10*BarsRequiredToPlot && Bars.IsFirstBarOfSession) // Print(CurrentBar + ":[" + High[0] + "," + Low[0] + "]--[" + Highs[1][1] + "," + Lows[1][1] + "]"); }