Example #1
0
        //Constructor
        public PivotPointBar(Bars bars, int period, bool pivotPointHigh, bool tradeable, string description)
            : base(bars, description)
        {
            _bars      = bars;
            _ppHigh    = pivotPointHigh;
            _period    = period;
            _tradeable = tradeable;

            FirstValidValue += _period;
            if (bars.Count < _period)
            {
                return;
            }

            for (int bar = _period; bar < bars.Count; bar++)
            {
                this[bar] = -1d;        // returns -1 until a Valid Pivot Point is found
            }

            for (int bar = _period; bar < bars.Count; bar++)
            {
                if (_tradeable)
                {
                    if (_ppHigh)
                    {
                        if (bars.High[bar] >= Highest.Series(bars.High, _period)[bar - 1])
                        {
                            _lastPP = bar;
                        }
                    }
                    else if (bars.Low[bar] <= Lowest.Series(bars.Low, _period)[bar - 1])
                    {
                        _lastPP = bar;
                    }
                }
                else
                {
                    int periodFwd = _period;
                    if (bar + period >= bars.Count)
                    {
                        periodFwd = bars.Count - bar - 1;
                    }
                    if (_ppHigh)
                    {
                        if (bars.High[bar] >= Highest.Series(bars.High, _period)[bar - 1] &&
                            bars.High[bar] >= Highest.Value(bar + periodFwd, bars.High, periodFwd))     // use Value method since periodFwd is variable at the end
                        {
                            _lastPP = bar;
                        }
                    }
                    else if (bars.Low[bar] <= Lowest.Series(bars.Low, _period)[bar - 1] &&
                             bars.Low[bar] <= Lowest.Value(bar + periodFwd, bars.Low, periodFwd))
                    {
                        _lastPP = bar;
                    }
                }
                this[bar] = _lastPP;
            }
        }
Example #2
0
        public override void CalculatePartialValue()
        {
            if (_bars.Count < _period || _bars.Low.PartialValue == Double.NaN || _bars.High.PartialValue == Double.NaN)
            {
                PartialValue = Double.NaN;
                return;
            }

            int bar = _bars.Count;          // bar - 1 is last bar number (prior to PartialBar)

            if (_tradeable)
            {
                if (_ppHigh)
                {
                    if (_bars.High.PartialValue >= Highest.Series(_bars.High, _period)[bar - 1])
                    {
                        _lastPP = bar;
                    }
                }
                else if (_bars.Low.PartialValue <= Lowest.Series(_bars.Low, _period)[bar - 1])
                {
                    _lastPP = bar;
                }
            }
            else
            {
                int periodFwd = _period;
                if (bar + _period >= _bars.Count)
                {
                    periodFwd = _bars.Count - bar - 1;
                }
                if (_ppHigh)
                {
                    if (_bars.High.PartialValue >= Highest.Series(_bars.High, _period)[bar - 1] &&
                        _bars.High.PartialValue >= Highest.Value(bar + periodFwd, _bars.High, periodFwd))     // use Value method since periodFwd is variable at the end
                    {
                        _lastPP = bar;
                    }
                }
                else if (_bars.Low.PartialValue <= Lowest.Series(_bars.Low, _period)[bar - 1] &&
                         _bars.Low.PartialValue <= Lowest.Value(bar + periodFwd, _bars.Low, periodFwd))
                {
                    _lastPP = bar;
                }
            }
            this.PartialValue = _lastPP;
        }
    protected override void Execute()
    {
        const string sep         = ",";
        const string fmt         = "0.00########";
        string       dateFormat  = "yyyyMMdd";
        string       workingDir  = @"Z:\Win10D";
        int          barsInAYear = 250;

        ClearDebug();

        string today = DateTime.Now.ToString("yyyy-MM-dd");
        string path  = Path.Combine(workingDir, "Data", today);

        PrintDebug(String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
                                 "Symbol", "HighPrice", "LowPrice", "PriceDiffPct", "Weeks", "PriceDiffPctWk"));

        for (int ds = 0; ds < DataSetSymbols.Count; ds++)
        {
            string symbol = DataSetSymbols[ds];
            Bars   bars   = GetExternalSymbol(symbol, false);

            PrintStatusBar("Processing: " + ds + " / " + DataSetSymbols.Count + " : " + bars.Symbol + "     ");

            if (bars.Count & gt; barsInAYear)
            {
                double highPrice = Highest.Value(bars.Count - 1, bars.High, barsInAYear);
                double highBar   = HighestBar.Value(bars.Count - 1, bars.High, barsInAYear);
                double lowPrice  = Lowest.Value(bars.Count - 1, bars.Low, barsInAYear);
                double lowBar    = LowestBar.Value(bars.Count - 1, bars.Low, barsInAYear);
                if (highBar & gt; lowBar)
                {
                    double priceDiffPct   = (highPrice - lowPrice) / lowPrice * 100;
                    double weeks          = Math.Ceiling((highBar - lowBar) / 5.0);
                    double priceDiffPctWk = priceDiffPct / weeks;
                    PrintDebug(String.Format("{0}\t{1:f}\t{2:f}\t{3:f}\t{4}\t{5:f}",
                                             symbol, highPrice, lowPrice, priceDiffPct, weeks, priceDiffPctWk));
                }
            }

            Bars.Cache.Clear();
        }
        //PrintStatusBar("Complete!");
    }
Example #4
0
        //===========================================================================================================
        // Show Trades
        //-----------------------------------------------------------------------------------------------------------
        protected void showTrades(BarHistory bars, string tradeFile)
        {
            ClearAllAnnotations();

            if (bars.Symbol == this.settings.SP500 || bars.Symbol == this.settings.NASDAQ || bars.Symbol == "1EQUITY")
            {
                showTradesOnIndexChart(bars, tradeFile);
                return;
            }

            string[] lines = System.IO.File.ReadAllLines(tradeFile);
            foreach (string line in lines)
            {
                // 1/2/18,3/12/18,ABMD,B1,105,closed,191.28,290.53,"20,084","30,506","10,421",52%,50,5.19%
                string[] columns = line.Split(',');
                //PrintDebug(line);
                var symbol = columns[2];
                if (symbol == bars.Symbol)
                {
                    var label = columns[3];

                    // Buy
                    var buyDateStr = columns[0];
                    if (buyDateStr != "")
                    {
                        DateTime d      = DateTime.Parse(buyDateStr);
                        int      buyBar = bars.IndexOf(d, true);
                        if (buyBar > 0)
                        {
                            double buyPrice = Double.Parse(columns[6]);
                            DrawBarAnnotation2(label, buyBar, true);
                            int nextBar = buyBar + 1;
                            if (buyBar == bars.Count + bars.ExtendedBars - 1)
                            {
                                nextBar = buyBar;                                                                            // Make sure the index is within the limit
                            }
                            DrawLine(buyBar - 1, buyPrice, nextBar, buyPrice, Color.Orange, 3);

                            // If it is B1, then draw the pivot point and 25% target
                            if (label == "B1")
                            {
                                var pivotPrice  = Highest.Value(buyBar, bars.High, 26 * 5);
                                var pivotBar    = (int)bars.High.GetHighestBar(buyBar, 26 * 5);
                                var targetPrice = pivotPrice * 1.25;
                                DrawLine(pivotBar, pivotPrice, bars.Count - 1, pivotPrice, Color.Orange, 1);
                                DrawLine(pivotBar, targetPrice, bars.Count - 1, targetPrice, Color.Orange, 1);

                                // Find breakout day
                                var breakoutBar = -1;
                                for (int i = pivotBar + 1; i < bars.Count - 1; i++)
                                {
                                    if (bars.High[i] > pivotPrice)
                                    {
                                        breakoutBar = i;
                                        break;
                                    }
                                }
                                if (breakoutBar != -1)
                                {
                                    SetBackgroundColor(bars, breakoutBar, Color.FromArgb(15, Color.Blue));
                                    var timeGoalBar = breakoutBar + 40;
                                    if (timeGoalBar < bars.Count)
                                    {
                                        SetBackgroundColor(bars, timeGoalBar, Color.FromArgb(15, Color.Blue));
                                    }
                                }
                            }
                        }
                    }

                    // Sell
                    var sellDateStr = columns[1];
                    if (sellDateStr != "")
                    {
                        DateTime d       = DateTime.Parse(sellDateStr);
                        int      sellBar = bars.IndexOf(d, true);
                        WriteToDebugLog("sellDateStr = " + sellDateStr);
                        WriteToDebugLog("d = " + d);
                        WriteToDebugLog("sellBar = " + sellBar);
                        if (sellBar > 0)
                        {
                            double sellPrice = Double.Parse(columns[7]);
                            DrawBarAnnotation2(label, sellBar, false);
                            int nextBar = sellBar + 1;
                            if (sellBar == bars.Count + bars.ExtendedBars - 1)
                            {
                                nextBar = sellBar;                                                                             // Make sure the index is within the limit
                            }
                            DrawLine(sellBar - 1, sellPrice, nextBar, sellPrice, Color.Brown, 3);
                        }
                    }
                }
            }

            DrawAllAnnotationsAtEnd(Color.FromArgb(255, 211, 84, 0), Color.Brown, 14);
        }
Example #5
0
        //===========================================================================================================
        // RS Line
        // indexUpFactor - typically 1.02 to 1.2
        // rsDownFactor - typically 0.60 to 0.85
        //-----------------------------------------------------------------------------------------------------------
        protected void drawIndexAndRsLines(BarHistory bars, double indexUpFactor, double rsDownFactor)
        {
            StartIndex = 200;
            BarHistory SPX = GetHistory(bars, this.settings.SP500);
            BarHistory NDX = GetHistory(bars, this.settings.NASDAQ);

            //
            // Last 5 Bars Gains, weekly, monthly, quaterly gains
            DrawHeaderText(String.Format("{0,-15} {1,6} {2,6} {3,6} {4,7} {5,6} {6,11} {7,6} {8,6}",
                                         "Gains", "-5D", "-4D", "-3D", "-2D", "D", "W", "M", "Q"), Color.Brown, 14);
            BarHistory[] barsArr = new BarHistory[] { bars, SPX, NDX };
            string[]     names   = new string[3] {
                "INST", "SP5X", "NDX"
            };

            // Following indexes are from the end (that is why named prime)
            int[] startIdxPrime = new int[8] {
                -6, -5, -4, -3, -2, -6, -23, -67
            };
            int[] endIdxPrime = new int[8] {
                -5, -4, -3, -2, -1, -1, -1, -1
            };
            for (int i = 0; i < 3; i++)
            {
                var      currBars = barsArr[i];
                object[] args     = new object[9];
                args[0] = names[i];
                for (int j = 0; j < 8; j++)
                {
                    var barsRequired = (-1 * startIdxPrime[j]) + 1;
                    var startIdx     = currBars.Count + startIdxPrime[j];
                    var endIdx       = currBars.Count + endIdxPrime[j];
                    if (currBars.Count > barsRequired)
                    {
                        args[j + 1] = 100.0 * (currBars.Close[endIdx] - currBars.Close[startIdx]) / currBars.Close[startIdx];
                    }
                    else
                    {
                        args[j + 1] = 0.0;
                    }
                }
                DrawHeaderText(String.Format("{0,-15} {1,7:0.0} {2,7:0.0} {3,7:0.0} {4,7:0.0} {5,7:0.0} {6,12:0.0} {7,7:0.0} {8,7:0.0}",
                                             args), Color.Brown, 14);
            }

            // Index Line
            double highRefPrice = Highest.Value(lastBarIdx, bars.High, 200);
            //TimeSeries SPXNormalized = highRefPrice * indexUpFactor * SPX.Close / SPX.Close[lastBarIdx];
            //PlotTimeSeriesLine(SPXNormalized, "SPXNormalized", "Price", Color.Black, 1);

            var        factor        = highRefPrice * indexUpFactor / SPX.Close[lastBarIdx];
            BarHistory SPXScaledUp   = new BarHistory(SPX);
            BarHistory SPXScaledDown = new BarHistory(SPX);

            for (int i = 0; i < SPX.Count; i++)
            {
                if (SPX.Close[i] >= SPX.Open[i])
                {
                    SPXScaledUp.Add(SPX.DateTimes[i],
                                    SPX.Open[i] * factor,
                                    SPX.High[i] * factor,
                                    SPX.Low[i] * factor,
                                    SPX.Close[i] * factor,
                                    SPX.Volume[i]);
                    SPXScaledDown.Add(SPX.DateTimes[i], float.NaN, float.NaN, float.NaN, float.NaN, float.NaN);
                }
                else
                {
                    SPXScaledUp.Add(SPX.DateTimes[i], float.NaN, float.NaN, float.NaN, float.NaN, float.NaN);
                    SPXScaledDown.Add(SPX.DateTimes[i],
                                      SPX.Open[i] * factor,
                                      SPX.High[i] * factor,
                                      SPX.Low[i] * factor,
                                      SPX.Close[i] * factor,
                                      SPX.Volume[i]);
                }
            }
            PlotBarHistoryStyle(SPXScaledUp, "Price", "bars", this.settings.upColor, false);
            PlotBarHistoryStyle(SPXScaledDown, "Price", "bars", this.settings.downColor, false);

            // RS Line
            TimeSeries RS  = bars.Close / SPX.Close;
            TimeSeries RS2 = bars.Close[lastBarIdx] * rsDownFactor * RS / RS[lastBarIdx];

            //PlotTimeSeriesLine(RS, "RS", "RS", Color.Blue, 1);
            PlotTimeSeriesLine(RS2, "RS2", "Price", Color.Blue, 1);
        }