public VWAP(Bars bars, string description) : base(bars, description) { Helper.CompatibilityCheck(); base.FirstValidValue = 1; //Can't work if data isn't intraday if (!bars.IsIntraday) { return; } double x = 0; double MVol = 0; //First, compute the typical price for the intraday period. This is the average of the high, low and close {(H+L+C)/3)}. AveragePrice ap = AveragePrice.Series(bars); for (int bar = FirstValidValue; bar < bars.Count; bar++) { // daily initialization if (bars.IntradayBarNumber(bar) == 0) { x = 0; MVol = 0; //20180413 fix for not taking the intraday bar 0 value, replacing it with the prior day VWAP closing value //if (bar > 0) // base[bar] = base[bar - 1]; } //else //20180413 fix for not taking the intraday bar 0 value, replacing it with the prior day VWAP closing value { //Second, multiply the typical price by the period's volume. //Third, create a running total of these values. This is also known as a cumulative total. x += ap[bar] * bars.Volume[bar]; //Fourth, create a running total of volume (cumulative volume). MVol += bars.Volume[bar]; //Fifth, divide the running total of price-volume by the running total of volume. base[bar] = x / MVol; } // Old version (prior to 2012.04) /*x += ap[bar] * bars.Volume[bar]; * MVol += bars.Volume[bar]; * if (MVol == 0) * base[bar] = ap[bar]; * else * base[bar] = x / MVol;*/ } }
public SmartMoneyIndex(Bars bars, string description) : base(bars, description) { Helper.CompatibilityCheck(); base.FirstValidValue = 1; double am = 0, pm = 0, tc = 0, yc = 0, op = 0; if (!bars.IsIntraday) { return; } for (int bar = FirstValidValue; bar < bars.Count; bar++) { if (bars.IntradayBarNumber(bar) == 0) { yc = bars.Close[bar - 1]; } if (GetTime(bars, bar) == 1000) { am = bars.Close[bar]; } if (GetTime(bars, bar) == 1500) { pm = bars.Close[bar]; } if (GetTime(bars, bar) == 0930) { op = bars.Open[bar]; } if (bars.IsLastBarOfDay(bar)) { tc = bars.Close[bar]; //base[bar] = (tc - pm) - (op - am) + base[bar]; base[bar] = base[bar] - (op - am) + (tc - pm); } else { base[bar] = base[bar - 1]; } } }
public LastHour(Bars bars, string description) : base(bars, description) { base.FirstValidValue = 1; double am = 0; double pm = 0; double tc = 0; double yc = 0; if (!bars.IsIntraday) { return; } for (int bar = FirstValidValue; bar < bars.Count; bar++) { if (bars.IntradayBarNumber(bar) == 0) { yc = bars.Close[bar - 1]; } if (GetTime(bars, bar) == 1030) { am = bars.Close[bar]; } if (GetTime(bars, bar) == 1500) { pm = bars.Close[bar]; } if (bars.IsLastBarOfDay(bar)) { tc = bars.Close[bar]; base[bar] = (tc - pm) - (am - yc); } else { base[bar] = base[bar - 1]; } } }
public CIV(Bars bars, int startTime, int stopTime, string description) : base(bars, description) { base.FirstValidValue = 0; double cv = 0d; for (int bar = 0; bar < bars.Count; bar++) { if (bars.IntradayBarNumber(bar) == 0) { cv = 0d; } int time = bars.Date[bar].Hour * 100 + bars.Date[bar].Minute; if (time > startTime && time <= stopTime) { cv += bars.Volume[bar]; } this[bar] = cv; } }
protected override void Execute() { SMA sma10 = SMA.Series(Close, 10); EMA ema30 = EMA.Series(Close, 30, EMACalculation.Modern); WilliamsR wlm = WilliamsR.Series(Bars, 3); BBandLower LB = BBandLower.Series(Close, 29, 2.5); BBandUpper UB = BBandUpper.Series(Close, 29, 2.5); SolidBrush shadowBrush = new SolidBrush(Color.FromArgb(10, Color.FromArgb(102, 0, 0, 255))); PlotSeriesFillBand(PricePane, LB, UB, Color.FromArgb(255, 176, 196, 222), shadowBrush, LineStyle.Solid, 2); DateTime nextReportDate = new DateTime(2010, 1, 1); FundamentalItem fi = GetFundamentalItem(Bars.Count - 1, Bars.Symbol, "earnings per share"); bool crossOver = false; bool pullback = false; bool crossUnder = false; bool pullup = false; for (int bar = 30; bar < Bars.Count; bar++) { SetContext("SPY", true); bool signal = sma10[bar] > ema30[bar]; bool goLong = false; bool goShort = false; if (signal && wlm[bar] < 20) { goLong = true; } if (!signal && wlm[bar] > 80) { crossOver = false; goShort = true; } RestoreContext(); //goLong = true; if (!IsLastPositionActive) { if (goLong) { if (CrossOver(bar, sma10, ema30)) { crossOver = true; pullback = false; crossUnder = false; pullup = false; continue; } if (crossOver && Close[bar] < sma10[bar] && pullback == false) { pullback = true; } if (pullback && Close[bar] > sma10[bar]) { // DrawCircle( PricePane, 10, bar, High[bar], Color.Green, Color.DarkGreen, WealthLab.LineStyle.Solid, 2, true ); // crossOver = false; pullback = false; BuyAtMarket(bar + 1, "SMA 10 is above EMA 30 and pull back is done "); } } else if (goShort) { if (CrossUnder(bar, sma10, ema30)) { crossUnder = true; pullup = false; crossOver = false; pullback = false; continue; } if (crossUnder && Close[bar] > sma10[bar] && pullup == false) { pullup = true; } if (pullup && Close[bar] < sma10[bar]) { // DrawCircle( PricePane, 10, bar, High[bar], Color.Green, Color.DarkGreen, WealthLab.LineStyle.Solid, 2, true ); // crossOver = false; pullup = false; ShortAtMarket(bar + 1, "SMA 10 is below EMA 30 and pull back is done "); } // DrawCircle( PricePane, 10, bar, High[bar], Color.Blue, Color.Black, WealthLab.LineStyle.Solid, 2, true ); } } else { if (LastActivePosition.PositionType == PositionType.Long) { double b = LB[bar]; if ( LastActivePosition.EntryPrice > Close[bar] && Close[bar] < b && bar >= LastActivePosition.EntryBar ) { SellAtMarket(bar + 1, LastActivePosition, "stop lose"); crossOver = false; pullback = false; continue; } if (Bars.IntradayBarNumber(bar) == 0 && EarningsDate.InWindow(this, bar, "earnings per share", 365, 0)) { //DrawLabel(PricePane, "Next Earnings: " + Date[bar].ToString()); PrintDebug(Date[bar].ToString()); } if (Date[bar].Day > (LastActivePosition.EntryDate.Day + 4) && Bars.IsLastBarOfDay(bar)) { SellAtMarket(bar + 1, LastActivePosition, "Close on 5th day"); crossOver = false; pullback = false; continue; } else if (CrossUnder(bar, sma10, ema30)) { SellAtMarket(bar + 1, LastActivePosition, "Take profit"); crossOver = false; pullback = false; continue; } } else if (LastActivePosition.PositionType == PositionType.Short) { double b = UB[bar]; if ( LastActivePosition.EntryPrice < Close[bar] && Close[bar] > b && bar >= LastActivePosition.EntryBar ) { CoverAtMarket(bar + 1, LastActivePosition, "Short stop lose"); crossUnder = false; pullup = false; continue; } if (Date[bar].Day > (LastActivePosition.EntryDate.Day + 4) && Bars.IsLastBarOfDay(bar)) { CoverAtMarket(bar + 1, LastActivePosition, "Close on 5th day"); crossUnder = false; pullup = false; continue; } else if (CrossOver(bar, sma10, ema30)) { CoverAtMarket(bar + 1, LastActivePosition, "Take profit"); crossUnder = false; pullup = false; continue; } } } } //Pushed indicator ChartPane statements ChartPane paneWilliamsR1 = CreatePane(40, true, true); //Pushed indicator PlotSeries statements PlotSeries(PricePane, sma10, Color.DarkGreen, LineStyle.Solid, 2); PlotSeries(PricePane, ema30, Color.Blue, LineStyle.Solid, 2); PlotSeries(paneWilliamsR1, wlm, Color.OliveDrab, LineStyle.Solid, 2); }
protected override void Execute() { int level; signleToSell = false; priceAtCross = 0; firstUp = false; firstDown = false; priceAtCrossDown = 0; rocAtCrossDown = 0; topPrice = 0; lastTop = 0; breakPrice = 0; rocPrice = 0; //Create and plot period RSI RSI rsi20 = RSI.Series(Close, rsiPeriod.ValueInt); ChartPane rsiPane = CreatePane(50, true, true); PlotSeries(rsiPane, rsi20, Color.Navy, LineStyle.Solid, 3); DrawHorzLine(rsiPane, oversold.Value, Color.Red, LineStyle.Dotted, 2); DrawHorzLine(rsiPane, overbought.Value, Color.Green, LineStyle.Dotted, 2); ChartPane paneROC1 = CreatePane(40, true, true); PlotSeries(paneROC1, ROC.Series(Close, rocPeriod.ValueInt), Color.FromArgb(255, 112, 128, 144), LineStyle.Dotted, 2); DrawHorzLine(paneROC1, 0, Color.Red, LineStyle.Solid, 2); int period = Math.Max(rsiPeriod.ValueInt, rocPeriod.ValueInt); if (initPosition) { // init starting state to force buy into the market priceAtCrossDown = Close[period + 1]; rocAtCrossDown = ROC.Value(period + 1, Close, rocPeriod.ValueInt); firstDown = true; } double High60 = 0; double Low60 = 0; int n = Bars.BarInterval; int num = (60 / n) - 1; // the first hour bar int firstIntradayBar = -1; //Trading system main loop for (int bar = period + 1; bar < Bars.Count; bar++) { if (Bars.IntradayBarNumber(bar) == 0) { firstIntradayBar = bar; } if (Bars.IntradayBarNumber(bar) <= num) // highlight the first hour { SetBarColor(bar, Color.Silver); } if (Bars.IntradayBarNumber(bar) == num) // get the highest high and the lowest low after first hour { High60 = Highest.Series(Bars.High, num + 1)[bar]; Low60 = Lowest.Series(Bars.Low, num + 1)[bar]; if (firstIntradayBar > -1) { DrawLine(PricePane, bar, High60, firstIntradayBar, High60, Color.Blue, LineStyle.Dashed, 1); DrawLine(PricePane, bar, Low60, firstIntradayBar, Low60, Color.Red, LineStyle.Dashed, 1); } } if (Close[bar] > topPrice) { topPrice = Close[bar]; } level = oversold.ValueInt; if (CrossUnder(bar, rsi20, level)) { if (!firstDown) { // first time to go below // set cross happen and record the targit price firstDown = true; priceAtCrossDown = Close[bar]; rocAtCrossDown = ROC.Value(bar, Close, rocPeriod.ValueInt); } } // play trayling int addToPosition = 0; bool rsiOk = false; bool priceOK = false; if (firstDown) { // ROC must improve by delta double delta = Math.Abs(rocAtCrossDown * 0.2); double newrocSMA = ROC.Value(bar, Close, rocPeriod.ValueInt); if (bar == Bars.Count - 1) { rocPrice = rocAtCrossDown + delta; } if (newrocSMA <= (rocAtCrossDown + delta)) { if (newrocSMA < rocAtCrossDown) { rocAtCrossDown = newrocSMA; } } else { rsiOk = true; } double riseV = priceRise.ValueInt; delta = priceAtCrossDown * (riseV / 1000.0 + 0.00017); if (bar == Bars.Count - 1) { breakPrice = priceAtCrossDown + delta; } if (Close[bar] < (priceAtCrossDown + delta)) { // DrawLabel(PricePane, "ready to buy, price is not rising: " + Close[bar].ToString() + " less than " + (priceAtCrossDown + delta).ToString()); if (Close[bar] < priceAtCrossDown) { priceAtCrossDown = Close[bar]; } } else { priceOK = true; } if (priceOK && rsiOk) { addToPosition++; firstDown = false; } } // you can have only one active position foreach (Position pos in Positions) { if (pos.Active && pos.PositionType == PositionType.Long) { addToPosition = 0; break; } } if (addToPosition > 0) { // OverSold // Close all shorts foreach (Position pos in Positions) { if (pos.Active && pos.PositionType == PositionType.Short) { CoverAtMarket(bar + 1, pos); } } // DrawLabel(PricePane, "buy at bar = " + bar.ToString()); Position p = BuyAtMarket(bar + 1); firstUp = false; } level = overbought.ValueInt; int ClosedTrades = 0; signleToSell = false; if (CrossOver(bar, rsi20, overbought.ValueInt)) { if (!firstUp) { // first time to go above // set cross happen and record the targit price firstUp = true; priceAtCross = Close[bar]; } } if (firstUp) { double riseV = priceRise.ValueInt; double delta = priceAtCross * (riseV / 1000 + 0.00017); priceOK = true; if (Close[bar] >= (priceAtCross - delta)) { if (Close[bar] > priceAtCross) { priceAtCross = Close[bar]; } } else { priceOK = false; } // keep as long ROC over zero if (ROC.Value(bar, Close, rocPeriod.ValueInt) <= 0 && !priceOK) { signleToSell = true; firstUp = false; } } // sync real account with strategy // if (Bars.Date[bar] == syncDate) // { // signleToSell = true; // close postions // } // Over Bought // wait until price either move up or stopped out if (signleToSell) { firstUp = false; //DrawLabel(PricePane, ActivePositions.Count.ToString()); foreach (Position pos in Positions) { if (pos.Active && pos.PositionType == PositionType.Long) { SellAtMarket(bar + 1, pos); ClosedTrades++; } } signleToSell = false; if (ClosedTrades > 0) { // Short only after sell long position if (!trend) { ShortAtMarket(bar + 1); } } } // sell on % lose foreach (Position pos in Positions) { if (pos.Active && pos.PositionType == PositionType.Long && pos.EntryPrice > (Close[bar] + pos.EntryPrice * (0.01 + stopLose.ValueInt * 0.001)) && bar >= pos.EntryBar ) { SellAtMarket(bar + 1, pos, "stop lose"); signleToSell = false; firstUp = false; firstDown = false; continue; } if (pos.Active && pos.PositionType == PositionType.Short && Close[bar] > (pos.EntryPrice + pos.EntryPrice * (0.01 + stopLose.ValueInt * 0.001)) && bar >= pos.EntryBar ) { CoverAtMarket(bar + 1, pos, "stop lose Short"); } } } double currentPrice = Close[Bars.Count - 1]; DrawLabel(paneROC1, "Top: " + topPrice.ToString(), Color.Red); DrawLabel(paneROC1, "Current Price: " + currentPrice.ToString()); DrawLabel(paneROC1, "Goal : " + " At +5% " + (currentPrice * 1.05).ToString(), Color.BlueViolet); DrawLabel(paneROC1, "Goal : " + " At +8% " + (currentPrice * 1.08).ToString(), Color.MediumSpringGreen); DrawLabel(paneROC1, "Goal : " + " At +10% " + (currentPrice * 1.10).ToString(), Color.DarkOliveGreen); DrawLabel(PricePane, "Drop : " + " At -5% " + (topPrice * 0.95).ToString(), Color.DarkGreen); DrawLabel(PricePane, "Correction : " + " At -8% " + (topPrice * 0.92).ToString(), Color.DarkBlue); DrawLabel(PricePane, "Correction : " + " At -10% " + (topPrice * 0.90).ToString(), Color.Red); DrawLabel(PricePane, "Bear market: " + " At -20% " + (topPrice * 0.80).ToString(), Color.DarkRed); if (breakPrice > 0) { DrawLabel(PricePane, "Break above Price: " + breakPrice.ToString(), Color.DarkGoldenrod); } if (rocPrice > 0) { DrawLabel(PricePane, "Break above Momuntem: " + rocPrice.ToString(), Color.DarkGoldenrod); } DrawHorzLine(PricePane, currentPrice * 1.05, Color.BlueViolet, LineStyle.Solid, 6); DrawHorzLine(PricePane, currentPrice * 1.08, Color.MediumSpringGreen, LineStyle.Solid, 3); DrawHorzLine(PricePane, currentPrice * 1.10, Color.DarkOliveGreen, LineStyle.Solid, 3); DrawHorzLine(PricePane, topPrice * 0.95, Color.DarkGreen, LineStyle.Solid, 3); DrawHorzLine(PricePane, topPrice * 0.92, Color.DarkBlue, LineStyle.Solid, 3); DrawHorzLine(PricePane, topPrice * 0.90, Color.Red, LineStyle.Solid, 3); DrawHorzLine(PricePane, topPrice * 0.80, Color.DarkRed, LineStyle.Solid, 3); if (breakPrice > 0) { DrawHorzLine(PricePane, breakPrice, Color.DarkGoldenrod, LineStyle.Solid, 4); } }