Example #1
0
        public void Add(DateTime TradeDate, decimal Price)
        {
            try
            {
                var trade = new BBandSignal()
                {
                    Price = Price, CloseDate = TradeDate
                };

                if (this.Values.Count >= 19)
                {
                    decimal sumOfPrices = trade.Price;
                    this.Values.GetRange(this.Values.Count - 19, 19).ForEach(i => sumOfPrices += i.Price);

                    //Middle Band 20-Day SMA
                    trade.SMA = Math.Round((double)(sumOfPrices / 20), 4);

                    //Standard Deviation formula
                    //SD = Sqrt((∑∣x−μ∣²)/N)
                    double aux = Math.Pow(Math.Abs((double)Price - trade.SMA), 2);
                    this.Values.GetRange(this.Values.Count - 19, 19).ForEach(i => aux += Math.Pow(((double)i.Price - trade.SMA), 2));
                    trade.SD = Math.Round(Math.Sqrt(aux / 20), 4);

                    trade.UpperBandSMA = Math.Round(trade.SMA + trade.SD * 2, 2);

                    trade.LowerBandSMA = Math.Round(trade.SMA - trade.SD * 2, 2);

                    trade.BandWidth = Math.Round(trade.UpperBandSMA - trade.LowerBandSMA, 2);

                    //calc trend based on the inclination
                    decimal y1, y2;
                    decimal x1, x2;

                    x2 = this.Values.Count;
                    x1 = x2 - 19;

                    y2 = this.Values[this.Values.Count - 1].Price;
                    y1 = this.Values[this.Values.Count - 19].Price;

                    trade.Inclination = (y2 - y1) / (x2 - x1);

                    trade.UpTrend = trade.Inclination > 0;

                    var pos = (trade.Price - (decimal)trade.LowerBandSMA) / (decimal)(trade.UpperBandSMA - trade.LowerBandSMA);
                    trade.Position = Math.Round(pos * 100, 4);
                }

                Values.Add(trade);
                IndicatorReady?.Invoke(this, trade);
            }
            catch (Exception ex)
            {
                if (this.OnError != null)
                {
                    OnError(this, ex.Message);
                }
            }
        }
Example #2
0
        private static void tickerHndl_BBand_IndicatorReady(object sender, BBandSignal e)
        {
            Console.ForegroundColor = ConsoleColor.Gray;

            if (!e.UpTrend && (e.Position <= (decimal)0.8))
            {
                //bearishing
                Console.ForegroundColor = ConsoleColor.Red;
            }

            if (e.UpTrend && (e.Position >= (decimal)0.2))
            {
                //bullishing
                Console.ForegroundColor = ConsoleColor.Green;
            }

            Console.WriteLine($"{e.CloseDate.ToString("HH:mm:ss")} Last Trade: {e.Price} - Upper Band: {e.UpperBandSMA} - Middle Band: {e.SMA} - Lower Band: {e.LowerBandSMA} - Bandwidth: {e.BandWidth} - Position: {e.Position}");
        }
Example #3
0
 private void _bband_IndicatorReady(object sender, BBandSignal e)
 {
     BBandReady(this, e);
 }