Beispiel #1
0
        public async void TestHeikinAshi()
        {
            TickerBLL           bll   = new TickerBLL(this.s3_bucket_name, this.tempTickerFolder);
            List <TickerEntity> tList = await bll.GetDailyTickerEntityList("ORG", 20191001, 20191101);

            double[] o = new double[tList.Count];
            double[] h = new double[tList.Count];
            double[] l = new double[tList.Count];
            double[] c = new double[tList.Count];

            double?[] oo = new double?[tList.Count];
            double?[] oh = new double?[tList.Count];
            double?[] ol = new double?[tList.Count];
            double?[] oc = new double?[tList.Count];

            var i = 0;

            foreach (var t in tList)
            {
                o[i] = t.O;
                h[i] = t.H;
                l[i] = t.L;
                c[i] = t.C;

                i++;
            }

            Result res = HeikinAshi.Calculate(o, c, h, l, oo, oc, oh, ol);

            Console.WriteLine(ObjectHelper.ToJson(oo));
        }
Beispiel #2
0
        public static Candle HA_M15_Candles(Instrument instrument)
        {
            Candle        haPreviounsCandle = null;
            Candle        haCurrentCandle   = null;
            List <Candle> haCandles         = new List <Candle>();
            List <Candle> candles           = OANDA.Data.Prices.GetCandles(instrument.Name, 10, "M15");

            for (int i = 0; i < candles.Count; i++)
            {
                if (i == 0)
                {
                    haPreviounsCandle = HeikinAshi.GeneratePrevious(candles[i]);
                    haCandles.Add(haPreviounsCandle);
                }
                else
                {
                    haCurrentCandle = HeikinAshi.Generate(haPreviounsCandle, candles[i]);

                    haCandles.Add(haCurrentCandle);
                    haPreviounsCandle = haCurrentCandle;
                }
            }


            return(haCandles.LastOrDefault());
        }
        private List <Candle> HA_D_Candles(Instrument instrument)
        {
            Candle        haPreviounsCandle = null;
            Candle        haCurrentCandle   = null;
            List <Candle> haCandles         = new List <Candle>();

            List <Candle> candles = OANDA.Data.Prices.GetCandles(instrument.Name, new DateTime(2020, 01, 01).AddDays(-50), "D");


            for (int i = 0; i < candles.Count; i++)
            {
                if (i == 0)
                {
                    haPreviounsCandle = HeikinAshi.GeneratePrevious(candles[i]);
                    haCandles.Add(haPreviounsCandle);
                }

                else
                {
                    haCurrentCandle = HeikinAshi.Generate(haPreviounsCandle, candles[i]);

                    haCandles.Add(haCurrentCandle);
                    haPreviounsCandle = haCurrentCandle;
                }
            }


            return(haCandles);
        }
Beispiel #4
0
        public static List <Candle> HA_D_Candles(Instrument instrument, out InstrumentDetails instrumentDetails)
        {
            instrumentDetails = new InstrumentDetails();
            Candle        haPreviounsCandle = null;
            Candle        haCurrentCandle   = null;
            List <Candle> haCandles         = new List <Candle>();
            int           emaPeriod         = 21;
            List <Candle> candles           = OANDA.Data.Prices.GetCandles(instrument.Name, emaPeriod, "D");

            instrumentDetails.Current = candles.LastOrDefault().Close;
            EMA ema = new EMA(emaPeriod);
            List <MyTrade.Core.Model.Candle> wcandles = MyTrade.Core.SqliteDataAccess.WeekyCandles.LoadCandles(instrument.Name);
            PivotPoints pps  = new PivotPoints();
            PivotPoint  wpps = pps.Get(wcandles[wcandles.Count - 2], instrumentDetails.Current);

            instrumentDetails.W_PivotPoints = wpps;
            List <MyTrade.Core.Model.Candle> mcandles = MyTrade.Core.SqliteDataAccess.MonthlyCandles.LoadCandles(instrument.Name);
            PivotPoint mpps = pps.Get(mcandles[mcandles.Count - 2], instrumentDetails.Current);

            instrumentDetails.M_PivotPoints = mpps;
            instrumentDetails.TimeFrame     = Core.Constants.TimeFrame.DAILY;
            for (int i = 0; i < candles.Count; i++)
            {
                instrumentDetails.Max = Math.Max(instrumentDetails.Max, candles[i].High);

                if (i == 0)
                {
                    instrumentDetails.Min = candles[i].Low;
                    instrumentDetails.Min = Math.Min(candles[i].Low, instrumentDetails.Min);
                    haPreviounsCandle     = HeikinAshi.GeneratePrevious(candles[i]);
                    haCandles.Add(haPreviounsCandle);
                }

                else
                {
                    haCurrentCandle = HeikinAshi.Generate(haPreviounsCandle, candles[i]);

                    haCandles.Add(haCurrentCandle);
                    haPreviounsCandle = haCurrentCandle;
                }


                if (i == (candles.Count - 1))
                {
                    instrumentDetails.EMAs = new List <Core.Model.Indicators.EMA>();
                    ema.AddDataPoint(candles[i].Close);
                    instrumentDetails.EMAs.Add(new Core.Model.Indicators.EMA()
                    {
                        Period = 21, Value = ema.Average
                    });
                }
                else
                {
                    ema.AddDataPoint(candles[i].Close);
                }
            }


            return(haCandles);
        }
Beispiel #5
0
        public override void ComparesAgainstExternalDataAfterReset()
        {
            var indicator = new HeikinAshi();

            for (var i = 1; i <= 2; i++)
            {
                TestHelper.TestIndicator(indicator, TestFileName, "HA_Open",
                                         (ind, expected) => ((double)((HeikinAshi)ind).Open.Current.Price).Should()
                                         .BeApproximately(expected, 1e-3));
                indicator.Reset();
                TestHelper.TestIndicator(indicator, TestFileName, "HA_High",
                                         (ind, expected) => ((double)((HeikinAshi)ind).High.Current.Price).Should()
                                         .BeApproximately(expected, 1e-3));
                indicator.Reset();
                TestHelper.TestIndicator(indicator, TestFileName, "HA_Low",
                                         (ind, expected) => ((double)((HeikinAshi)ind).Low.Current.Price).Should()
                                         .BeApproximately(expected, 1e-3));
                indicator.Reset();
                TestHelper.TestIndicator(indicator, TestFileName, "HA_Close",
                                         (ind, expected) => ((double)((HeikinAshi)ind).Close.Current.Price).Should()
                                         .BeApproximately(expected, 1e-3));
                indicator.Reset();
                TestHelper.TestIndicator(indicator, TestFileName, "Volume",
                                         (ind, expected) => ((double)((HeikinAshi)ind).Volume.Current.Price).Should()
                                         .BeApproximately(expected, 1e-3));
                indicator.Reset();
            }
        }
        public static Candle HA_W_Candles(Instrument instrument)
        {
            Candle        haPreviounsCandle = null;
            Candle        haCurrentCandle   = null;
            List <Candle> haCandles         = new List <Candle>();
            List <Candle> candles           = MyTrade.Core.SqliteDataAccess.WeekyCandles.LoadCandles(instrument.Name);

            for (int i = 0; i < candles.Count; i++)
            {
                if (i == 0)
                {
                    haPreviounsCandle = HeikinAshi.GeneratePrevious(candles[i]);
                    haCandles.Add(haPreviounsCandle);
                }

                else
                {
                    haCurrentCandle = HeikinAshi.Generate(haPreviounsCandle, candles[i]);

                    haCandles.Add(haCurrentCandle);
                    haPreviounsCandle = haCurrentCandle;
                }
            }


            return(haCandles.LastOrDefault());
        }
Beispiel #7
0
        /// <summary>
        /// Runs the primary calculation for trading
        /// </summary>
        /// <param name="candles">List of candles. Everything will be checking based off of the last candle.</param>
        /// <returns>Does Math Stuff.</returns>
        public static OrderType RunCalculation(IList <Candle> candles)
        {
            var latestHaCandle   = new HeikinAshi(candles[2].CandleBody, candles[1].CandleBody);
            var previousHaCandle = new HeikinAshi(candles[1].CandleBody, candles[0].CandleBody);

            if (Helpers.CheckHaCandleAnimalType(latestHaCandle) == AnimalType.Bear && Helpers.CheckHaCandleAnimalType(previousHaCandle) == AnimalType.Bear)
            {
                if (Math.Abs(latestHaCandle.HA_Open - latestHaCandle.HA_Close) > Math.Abs(previousHaCandle.HA_Open - previousHaCandle.HA_Close))
                {
                    if (Math.Abs(latestHaCandle.HA_Open - latestHaCandle.HA_High) < 0.09)
                    {
                        return(OrderType.Buy);
                    }
                }
            }

            if (Helpers.CheckHaCandleAnimalType(latestHaCandle) == AnimalType.Bull && Helpers.CheckHaCandleAnimalType(previousHaCandle) == AnimalType.Bull)
            {
                if (Math.Abs(latestHaCandle.HA_Open - latestHaCandle.HA_Close) > Math.Abs(previousHaCandle.HA_Open - previousHaCandle.HA_Close))
                {
                    if (Math.Abs(latestHaCandle.HA_Close - latestHaCandle.HA_Low) < 0.09)
                    {
                        return(OrderType.Sell);
                    }
                }
            }

            return(OrderType.NoAction);
        }
        public async Task<IndHeikinEntity[]> GetHeikin(string code, int start = 0, int end = 0, string type = "day")
        {
            TickerEntity[] tickers = await base.getTickerEntityArray(code, start, end, type);
            List<IndHeikinEntity> outList = new List<IndHeikinEntity>();

            int len = tickers.Length;

            double[] open = tickers.Select(t => (double)t.O).ToArray();
            double[] close = tickers.Select(t => (double)t.C).ToArray();
            double[] high = tickers.Select(t => (double)t.H).ToArray();
            double[] low = tickers.Select(t => (double)t.L).ToArray();

            double?[] outOpen = new double?[len];
            double?[] outClose = new double?[len];
            double?[] outHigh = new double?[len];
            double?[] outLow = new double?[len];

            HeikinAshi.Calculate(open, close, high, low, outOpen, outClose, outHigh, outLow);

            for (int i = 0; i < len; i++)
            {
                outList.Add(new IndHeikinEntity
                {
                    T = tickers[i].T,
                    P = tickers[i].P,
                    Open = outOpen[i],
                    Close = outClose[i],
                    High = outHigh[i],
                    Low = outLow[i]
                });
            }

            return outList.Where(r => (start == 0 || r.P >= start) && (end == 0 || r.P <= end)).ToArray();
        }
Beispiel #9
0
        public void TestCalculate()
        {
            TickerBLL tbll = new TickerBLL(_unit);

            List <Ticker> tList = tbll.GetTickerListByShareDB(1585, 20160401, 21100000);

            double[] o = new double[tList.Count];
            double[] h = new double[tList.Count];
            double[] l = new double[tList.Count];
            double[] c = new double[tList.Count];

            double?[] oo = new double?[tList.Count];
            double?[] oh = new double?[tList.Count];
            double?[] ol = new double?[tList.Count];
            double?[] oc = new double?[tList.Count];

            var i = 0;

            foreach (var t in tList)
            {
                o[i] = t.Open;
                h[i] = t.High;
                l[i] = t.Low;
                c[i] = t.Close;

                i++;
            }

            Result res = HeikinAshi.Calculate(o, c, h, l, oo, oc, oh, ol);
        }
Beispiel #10
0
        /// <summary>
        /// Converts Heikin Ashi into CandleBody.
        /// </summary>
        /// <param name="haCandle">The Heikin Ashi candlestick that you want to convert into a CandleBody object</param>
        /// <returns>Returns CandleBody object</returns>
        public static CandleBody ConvertHaCandle(HeikinAshi haCandle)
        {
            var candle = new CandleBody
            {
                OpenPrice  = haCandle.HA_Open,
                HighPrice  = haCandle.HA_High,
                LowPrice   = haCandle.HA_Low,
                ClosePrice = haCandle.HA_Close
            };

            return(candle);
        }
Beispiel #11
0
        public override void ComparesAgainstExternalDataAfterReset()
        {
            var indicator = new HeikinAshi();

            for (var i = 1; i <= 2; i++)
            {
                TestHelper.TestIndicator(indicator, TestFileName, "HA_Open", (ind, expected) => Assert.AreEqual(expected, (double)((HeikinAshi)ind).Open.Current.Value, 1e-3));
                indicator.Reset();
                TestHelper.TestIndicator(indicator, TestFileName, "HA_High", (ind, expected) => Assert.AreEqual(expected, (double)((HeikinAshi)ind).High.Current.Value, 1e-3));
                indicator.Reset();
                TestHelper.TestIndicator(indicator, TestFileName, "HA_Low", (ind, expected) => Assert.AreEqual(expected, (double)((HeikinAshi)ind).Low.Current.Value, 1e-3));
                indicator.Reset();
                TestHelper.TestIndicator(indicator, TestFileName, "HA_Close", (ind, expected) => Assert.AreEqual(expected, (double)((HeikinAshi)ind).Close.Current.Value, 1e-3));
                indicator.Reset();
            }
        }
Beispiel #12
0
        /// <summary>
        /// Checks to see if Candle Stick is Bearish or Bullish
        /// </summary>
        /// <param name="haCandle">The candlestick that you want to check.</param>
        /// <returns>Returns the animal type of the Heikin Ashi candle</returns>
        public static AnimalType CheckHaCandleAnimalType(HeikinAshi haCandle)
        {
            if (haCandle.HA_Close > haCandle.HA_Open) //White Candle
            {
                if (haCandle.HA_Close > (haCandle.HA_High + haCandle.HA_Low) / 2)
                {
                    return(AnimalType.Bull);
                }
            }

            if (haCandle.HA_Open > haCandle.HA_Close) //Black Candle
            {
                if (haCandle.HA_Close < (haCandle.HA_High + haCandle.HA_Low) / 2)
                {
                    return(AnimalType.Bear);
                }
            }

            return(AnimalType.NoAnimal);
        }
Beispiel #13
0
        private void BuildHekinAshiCandle()
        {
            if (Candles.Count == 0)
            {
                return;
            }
            var candle     = Candles.Last();
            var heikinAshi = new Candle();

            if (HeikinAshi.Count == 0)
            {
                heikinAshi.Open  = (candle.Open + candle.Close) / 2;
                heikinAshi.Close = (candle.Open + candle.Close + candle.High + candle.Low) / 4.0m;
                heikinAshi.High  = candle.High;
                heikinAshi.Low   = candle.Low;
            }
            else
            {
                var lastHeikinAshi = HeikinAshi.Last();
                heikinAshi.Open  = (lastHeikinAshi.Open + lastHeikinAshi.Close) / 2;
                heikinAshi.Close = (candle.Open + candle.Close + candle.High + candle.Low) / 4.0m;
                heikinAshi.High  = Math.Max(Math.Max(heikinAshi.Open, heikinAshi.Close), candle.High);
                heikinAshi.Low   = Math.Min(Math.Min(heikinAshi.Open, heikinAshi.Close), candle.Low);
            }
            ulong netVolume = candle.Volume;

            if (HeikinAshi.Count > 2)
            {
                netVolume = candle.Volume - HeikinAshi.ElementAt(HeikinAshi.Count - 2).Volume;
            }
            heikinAshi.CandleVolume = netVolume;
            heikinAshi.Volume       = candle.Volume;
            heikinAshi.TimeStamp    = candle.TimeStamp;

            heikinAshi.Index = (uint)HeikinAshi.Count;
            this.heikinAshi.Add(heikinAshi);
        }
Beispiel #14
0
        public static Model.Candle Generate(Model.Candle HApreviousCandel, Model.Candle currentCandel)
        {
            Model.Candle HAcandel = new Model.Candle();
            HAcandel.Time  = currentCandel.Time;
            HAcandel.Open  = HeikinAshi.OpenValue(HApreviousCandel.Open, HApreviousCandel.Close);
            HAcandel.Close = HeikinAshi.CloseValue(currentCandel.Open, currentCandel.Close, currentCandel.Hight, currentCandel.Low);
            double low  = HeikinAshi.MinValue(currentCandel.Low, HAcandel.Open, HAcandel.Close);
            double high = HeikinAshi.MaxValue(currentCandel.Hight, HAcandel.Open, HAcandel.Close);

            HAcandel.OriginalColor = currentCandel.OriginalColor;
            if (HAcandel.Open < HAcandel.Close)
            {
                HAcandel.Low   = low;
                HAcandel.Hight = high;
                HAcandel.Color = Constants.CandelColour.GREEN;
            }
            else
            {
                HAcandel.Low   = high;
                HAcandel.Hight = low;
                HAcandel.Color = Constants.CandelColour.RED;
            }
            return(HAcandel);
        }
Beispiel #15
0
        internal override void Paint(object drawingContext)
        {
            //if (_painted) return;

            Style ps = null;

            if (_priceStyleType != _chartPanel._chartX._priceStyle || _seriesTypeType != _seriesType)
            {
                if (_chartPanel._chartX._priceStyle != PriceStyleEnum.psStandard)
                {
                    switch (_chartPanel._chartX._priceStyle)
                    {
                    case PriceStyleEnum.psKagi:
                        ps = new Kagi(this);
                        break;

                    case PriceStyleEnum.psCandleVolume:
                    case PriceStyleEnum.psEquiVolume:
                    case PriceStyleEnum.psEquiVolumeShadow:
                        ps = new EquiVolume(this);
                        break;

                    case PriceStyleEnum.psPointAndFigure:
                        ps = new PointAndFigure(this);
                        break;

                    case PriceStyleEnum.psRenko:
                        ps = new Renko(this);
                        break;

                    case PriceStyleEnum.psThreeLineBreak:
                        ps = new ThreeLineBreak(this);
                        break;

                    case PriceStyleEnum.psHeikinAshi:
                        ps = new HeikinAshi(this);
                        break;
                    }
                }
                else
                {
                    switch (_seriesType)
                    {
                    case SeriesTypeEnum.stCandleChart:
                        ps = new Candles(this);
                        break;

                    case SeriesTypeEnum.stStockBarChartHLC:
                    case SeriesTypeEnum.stStockBarChart:
                        ps = new PriceStyles.Stock(this);
                        break;

                    case SeriesTypeEnum.stLineChart:
                        ps = new Linear(this);
                        break;
                    }
                }
                if (_priceStyle != null)
                {
                    _priceStyle.RemovePaint();
                }
            }

            if (_darvasBoxes != null)
            {
                _darvasBoxes.RemovePaint();
            }

            if (_chartPanel._chartX._priceStyle == PriceStyleEnum.psStandard || _chartPanel._chartX._priceStyle == PriceStyleEnum.psHeikinAshi)
            {
                if (_darvasBoxes == null)
                {
                    _darvasBoxes = new DarvasBoxes(this);
                }

                _darvasBoxes.SetSeriesStock(this);
                if (_chartPanel._chartX._darwasBoxes)
                {
                    _darvasBoxes.Paint();
                }
            }

            if (_priceStyle != null || ps != null)
            {
                (ps ?? _priceStyle).SetStockSeries(this);
                Style psToPaint = ps ?? _priceStyle;
                bool  res;
                if (psToPaint is Candles && drawingContext != null)
                {
                    res = psToPaint.Paint(drawingContext);
                }
                else
                {
                    res = psToPaint.Paint();
                }
                //if (!(ps ?? _priceStyle).Paint()) return;
                if (!res)
                {
                    return;
                }
            }

            if (Selected)
            {
                ShowSelection();
            }

            if (ps == null)
            {
                return;
            }

            _priceStyle     = ps;
            _priceStyleType = _chartPanel._chartX._priceStyle;
            _seriesTypeType = _seriesType;
        }
Beispiel #16
0
 private void OnHeikinAshi(Candle candle)
 {
     HeikinAshi.Add(candle);
     OnPropertyChanged(nameof(FilteredHeikinAshi));
 }