public async void TestMACD() { TickerBLL bll = new TickerBLL(this.s3_bucket_name, this.tempTickerFolder); List <TickerEntity> tList = await bll.GetDailyTickerEntityList("ORG", 20181001, 20191101); double[] inputData = new double[tList.Count]; var i = 0; foreach (var t in tList) { inputData[i] = t.C; i++; } double?[] m = new double?[tList.Count]; double?[] s = new double?[tList.Count]; double?[] h = new double?[tList.Count]; Result res = MACD.Calculate(inputData, 26, 12, 9, m, s, h); var tArray = tList.ToArray(); for (int j = 0; j < tList.Count; j++) { Console.WriteLine($"{tArray[j].T} {tArray[j].P} O: {tArray[j].O} H: {tArray[j].H} L: {tArray[j].L} C: {tArray[j].C} M: {m[j]} S: {s[j]} H: {h[j]} "); } }
public void IndicatorsShouldNotChangeTheSourceOhlcList() { // read ohlc list from csv file List <Ohlc> ohlcList = ReadCsvFile(csvPath); // create a deep clone of the list List <Ohlc> ohlcListDeepClone = ohlcList.DeepClone(); // run MACD on the ohlcList MACD macd = new MACD(true); macd.Load(ohlcList); MACDSerie serie = macd.Calculate(); // check if the ohlcList is still the same as its deep copy int i = 0; foreach (var item in ohlcList) { Assert.Equal(item.AdjClose, ohlcListDeepClone[i].AdjClose); Assert.Equal(item.Close, ohlcListDeepClone[i].Close); Assert.Equal(item.Date, ohlcListDeepClone[i].Date); Assert.Equal(item.High, ohlcListDeepClone[i].High); Assert.Equal(item.Low, ohlcListDeepClone[i].Low); Assert.Equal(item.Open, ohlcListDeepClone[i].Open); Assert.Equal(item.Volume, ohlcListDeepClone[i].Volume); i++; } }
public async Task<IndMACDEntity[]> GetMACD(string code, int slow = 26, int fast = 12, int signal = 9, int start = 0, int end = 0, string type = "day") { TickerEntity[] tickers = await base.getTickerEntityArray(code, start, end, type); List<IndMACDEntity> outList = new List<IndMACDEntity>(); int len = tickers.Length; double[] close = tickers.Select(t => (double)t.C).ToArray(); double?[] outMACD = new double?[len]; double?[] outSignal = new double?[len]; double?[] outHist = new double?[len]; MACD.Calculate(close, slow, fast, signal, outMACD, outSignal, outHist); for (int i = 0; i < len; i++) { outList.Add(new IndMACDEntity { T = tickers[i].T, P = tickers[i].P, MACD = outMACD[i], Signal = outSignal[i], Hist = outHist[i] }); } return outList.Where(r => (start == 0 || r.P >= start) && (end == 0 || r.P <= end)).ToArray(); }
public override List <IndicatorResult> Calculate(IIndicatorEntity indicator, List <QuotesModel> quotes) { Validate(indicator, quotes); var macdParams = ExtractMacdParams(indicator.Params); return(_calculator.Calculate(quotes, macdParams)); }
private static MACDSerie GetMacdSeries(MACD macd, string stockName) { string downloadFolderName = ConfigurationManager.AppSettings["downloadLocation"]; macd.Load(downloadFolderName + stockName + ".ax.csv"); MACDSerie serie = macd.Calculate(); //ExcelUtilities.WriteMacdhistogramDataToExcel(serie, stockName); return(serie); }
public void MACD() { //MACD macd = new MACD(); MACD macd = new MACD(true); macd.Load(OhlcList); MACDSerie serie = macd.Calculate(); Assert.IsNotNull(serie); Assert.IsTrue(serie.Signal.Count > 0); Assert.IsTrue(serie.MACDLine.Count > 0); Assert.IsTrue(serie.MACDHistogram.Count > 0); }
public void MACD() { //MACD macd = new MACD(); MACD macd = new MACD(true); macd.Load(Directory.GetCurrentDirectory() + "\\table.csv"); MACDSerie serie = macd.Calculate(); Assert.NotNull(serie); Assert.True(serie.Signal.Count > 0); Assert.True(serie.MACDLine.Count > 0); Assert.True(serie.MACDHistogram.Count > 0); }
public void TestCalculate() { TickerBLL tbll = new TickerBLL(_unit); List <Ticker> tList = tbll.GetTickerListByShareDB(1585, 0, 21100000); double[] inputData = new double[tList.Count]; var i = 0; foreach (var t in tList) { inputData[i] = t.Close; i++; } double?[] m = new double?[tList.Count]; double?[] s = new double?[tList.Count]; double?[] h = new double?[tList.Count]; Result res = MACD.Calculate(inputData, 26, 12, 9, m, s, h); }
public void MACD() { //File.Delete(@"MacdHData.xlsx"); //MACD macd = new MACD(); MACD macd = new MACD(false); //https://au.finance.yahoo.com/quote/VAS.AX/ //https://au.finance.yahoo.com/quote/STW.AX/ //https://au.finance.yahoo.com/quote/APT.AX //MACDSerie serie = GenerateMacdHistogramData(macd, "vas"); q q //macd = new MACD(false); //serie = GenerateMacdHistogramData(macd, "stw"); //macd = new MACD(false); //serie = GenerateMacdHistogramData(macd, "apt"); macd.Load(Directory.GetCurrentDirectory() + "\\" + "table" + ".csv"); MACDSerie serie = macd.Calculate(); Assert.IsNotNull(serie); Assert.IsTrue(serie.Signal.Count > 0); Assert.IsTrue(serie.MACDLine.Count > 0); Assert.IsTrue(serie.MACDHistogramDataList.Count > 0); //serie.MACDHistogram.Select((x,i) => new {i, value = x.GetValueOrDefault(),Consider = x != null && x.Value < 0 && x.Value >-1 }).Where(x => x.Consider) //serie.MACDHistogram.ElementAt(i-1) if its negative and > current negative value between 0 and -1 , good time to buy }
public override void Calculate(IDataSet dataSet) { DataSet = dataSet; // Reading the parameters var basePrice = (BasePrice)IndParam.ListParam[2].Index; var referencePeriod = (int)IndParam.NumParam[3].Value; int previous = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation // --------------------------------------------------------- var macd = new MACD(); macd.Initialize(SlotType); macd.IndParam.ListParam[1].Index = IndParam.ListParam[1].Index; macd.IndParam.ListParam[2].Index = IndParam.ListParam[2].Index; macd.IndParam.ListParam[3].Index = 0; macd.IndParam.NumParam[0].Value = IndParam.NumParam[0].Value; macd.IndParam.NumParam[1].Value = IndParam.NumParam[1].Value; macd.IndParam.NumParam[2].Value = IndParam.NumParam[2].Value; macd.IndParam.CheckParam[0].Checked = IndParam.CheckParam[0].Checked; macd.Calculate(DataSet); double[] indicatorMa = MovingAverage(referencePeriod, previous, MAMethod.Simple, macd.Component[0].Value); double[] marketMa = MovingAverage(referencePeriod, previous, MAMethod.Simple, Price(basePrice)); // ---------------------------------------------------------- int firstBar = macd.Component[0].FirstBar + referencePeriod + 2; var cd = new double[Bars]; if (IndParam.ListParam[0].Text == "Convergence") { for (int bar = firstBar; bar < Bars; bar++) { cd[bar] = IsConvergence(indicatorMa, marketMa, bar); } } else if (IndParam.ListParam[0].Text == "Divergence") { for (int bar = firstBar; bar < Bars; bar++) { cd[bar] = IsDivergence(indicatorMa, marketMa, bar); } } // Saving the components Component = new IndicatorComp[4]; Component[0] = new IndicatorComp { CompName = "MACD Histogram", DataType = IndComponentType.IndicatorValue, ChartType = IndChartType.Line, ChartColor = Color.RoyalBlue, FirstBar = firstBar, Value = macd.Component[0].Value }; Component[1] = new IndicatorComp { CompName = "MACD MA", DataType = IndComponentType.IndicatorValue, ChartType = IndChartType.Line, ChartColor = Color.Red, FirstBar = firstBar, Value = indicatorMa }; Component[2] = new IndicatorComp { ChartType = IndChartType.NoChart, FirstBar = firstBar, Value = cd }; Component[3] = new IndicatorComp { ChartType = IndChartType.NoChart, FirstBar = firstBar, Value = cd }; // Sets the Component's type if (SlotType == SlotTypes.OpenFilter) { Component[2].DataType = IndComponentType.AllowOpenLong; Component[2].CompName = "Is long entry allowed"; Component[3].DataType = IndComponentType.AllowOpenShort; Component[3].CompName = "Is short entry allowed"; } else if (SlotType == SlotTypes.CloseFilter) { Component[2].DataType = IndComponentType.ForceCloseLong; Component[2].CompName = "Close out long position"; Component[3].DataType = IndComponentType.ForceCloseShort; Component[3].CompName = "Close out short position"; } }
public Form1() { InitializeComponent(); chart1.Series.Clear(); Int32 timeStampStart = (Int32)(DateTime.UtcNow.AddMonths(-12).Subtract(new DateTime(1970, 1, 1))).TotalSeconds; Int32 timeStampNow = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; String yahooAPI = $"https://query1.finance.yahoo.com/v7/finance/download/BTC-EUR?period1={timeStampStart}&period2={timeStampNow}&interval=1d&events=history"; using (var client = new WebClient()) { client.DownloadFile(yahooAPI, "fileLOG"); } String csvFile = @"fileLOG"; List <Ohlc> ohlcList = new List <Ohlc>(); StreamReader r = new StreamReader(csvFile); String file = r.ReadToEnd(); String[] pricesClose = file.Split(new String[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); chart1.Series.Add("Price").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastPoint; // chart1.Series["Price"].Color = Color.Black; int j = 0; List <DateTime> dates = new List <DateTime>(); foreach (var s in pricesClose) { String[] pricesOscillation = s.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); try { int index = 0; DateTime date = DateTime.Parse(pricesOscillation[index++]); double open = double.Parse(pricesOscillation[index++]); double high = double.Parse(pricesOscillation[index++]); double low = double.Parse(pricesOscillation[index++]); double close = double.Parse(pricesOscillation[index++]); double adjClose = double.Parse(pricesOscillation[index++]); double volume = double.Parse(pricesOscillation[index++]); Ohlc newOhlc = new Ohlc(); newOhlc.Open = open; newOhlc.High = high; newOhlc.Low = low; newOhlc.Close = close; newOhlc.AdjClose = adjClose; newOhlc.Volume = volume; ohlcList.Add(newOhlc); dates.Add(date); chart1.Series["Price"].Points.AddXY(date, close); } catch (Exception err) { } } BollingerBand bollingerBand = new BollingerBand(); bollingerBand.Load(ohlcList); BollingerBandSerie serie = bollingerBand.Calculate(); MACD macd = new MACD(); macd.Load(ohlcList); MACDSerie macdserie = macd.Calculate(); EMA ema = new EMA(); ema.Load(ohlcList); SingleDoubleSerie singleDoubleSerie = ema.Calculate(); RSI rsi = new RSI(14); rsi.Load(ohlcList); RSISerie serieRSI = rsi.Calculate(); chart1.Series.Add("bUP").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; chart1.Series.Add("bDOWN").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; chart1.Series.Add("bMIDDLE").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; chart1.Series.Add("RSI").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; chart1.Series.Add("EMA").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; chart1.Series.Add("MACD").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; chart1.Series.Add("MACDHistogram").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; for (int i = 0; i < serie.BandWidth.Count; i++) { if (macdserie.MACDLine[i] != null) { chart1.Series["MACD"].Points.AddXY(dates[i], macdserie.MACDLine[i]); } if (macdserie.MACDHistogram[i] != null) { chart1.Series["MACDHistogram"].Points.AddXY(dates[i], macdserie.MACDHistogram[i]); } if (singleDoubleSerie.Values[i] != null) { chart1.Series["EMA"].Points.AddXY(dates[i], singleDoubleSerie.Values[i]); } if (serie.UpperBand[i] != null) { chart1.Series["bUP"].Points.AddXY(dates[i], serie.UpperBand[i]); } if (serie.MidBand[i] != null) { chart1.Series["bMIDDLE"].Points.AddXY(dates[i], serie.MidBand[i]); } if (serie.LowerBand[i] != null) { chart1.Series["bDOWN"].Points.AddXY(dates[i], serie.LowerBand[i]); } if (serieRSI.RSI[i] != null) { chart1.Series["RSI"].Points.AddXY(dates[i], serieRSI.RSI[i]); } } }