public void GetValueWhenTimeFrameIs12() { ROCIndicator roc = new ROCIndicator(closePrice, 12); // Incomplete time frame Assert.AreEqual(roc.GetValue(0), 0); Assert.AreEqual(roc.GetValue(1), 1.104997885972909670836475700M); Assert.AreEqual(roc.GetValue(2), -0.3319067800062832325511282200M); Assert.AreEqual(roc.GetValue(3), 0.9647568597236645188392859600M); // Complete time frame decimal[] results13to20 = new decimal[] { -3.8487968152883542004858188200M, -4.848880483410522847021487700M, -4.5206433873122946493699022900M, -6.3438915406709033405279671600M, -7.8592301293062817282691957500M, -6.2083414610806652852563276800M, -4.3130817313541793405523786300M, -3.2434109184301613709644012800M }; for (int i = 0; i < results13to20.Length; i++) { Assert.AreEqual(roc.GetValue(i + 12), results13to20[i]); } }
public static ROCIndicator Create(double eMA9Day, double eMA12Day, double rOCOnEMA9Day, Quote market, TriggerDetail triggerDetail) { var macd = new ROCIndicator() { ROC14Days = eMA9Day, EMA9Day = eMA12Day, ROCOnEMA9Day = rOCOnEMA9Day, Quote = market, TriggerDetail = triggerDetail }; return(macd); }
public void Calculate_ROCIndicators() { var nifty = new Stock("nifty", ExchangeTypeEnum.NSE, "nifty 50"); _rocLists = new ROCLists(); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 1), 0, 0, 7954.9, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 2), 0, 0, 7931.35, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 3), 0, 0, 7864.15, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 4), 0, 0, 7781.9, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 7), 0, 0, 7765.4, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 8), 0, 0, 7701.7, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 9), 0, 0, 7612.5, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 10), 0, 0, 7683.3, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 11), 0, 0, 7610.45, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 14), 0, 0, 7650.05, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 15), 0, 0, 7700.9, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 16), 0, 0, 7750.9, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 17), 0, 0, 7844.35, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 18), 0, 0, 7761.95, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 21), 0, 0, 7834.45, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 22), 0, 0, 7786.1, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 23), 0, 0, 7865.95, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 24), 0, 0, 7861.05, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 28), 0, 0, 7925.15, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 29), 0, 0, 7928.95, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 30), 0, 0, 7896.25, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2015, 12, 31), 0, 0, 7946.35, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2016, 01, 01), 0, 0, 7963.2, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2016, 01, 04), 0, 0, 7791.3, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2016, 01, 04), 0, 0, 7784.65, 0, nifty), null)); _rocLists.Add(ROCIndicator.Create(0, 0, 0, new Quote(new DateTime(2016, 01, 06), 0, 0, 7741, 0, nifty), null)); var stringBuilder = new StringBuilder(); _rocLists.Foreach((item) => { if (item.TriggerDetail != null) { stringBuilder.Append($"{item.Quote.Today},{item.Quote.Close}, {item.ROC14Days}, {item.EMA9Day},{item.ROCOnEMA9Day}, {item.TriggerDetail.TradeType}, {item.TriggerDetail.TriggeredPrice},{item.TriggerDetail.IsTriggered},{System.Environment.NewLine}"); } }); File.WriteAllText(@"RocCalculation1.csv", stringBuilder.ToString()); }
public static void Main(string[] args) { /// <summary> /// Getting time series /// </summary> TimeSeries series = CsvTradesLoader.loadBitstampSeries(); /// <summary> /// Creating indicators /// </summary> // Close price ClosePriceIndicator closePrice = new ClosePriceIndicator(series); // Typical price TypicalPriceIndicator typicalPrice = new TypicalPriceIndicator(series); // Price variation PriceVariationIndicator priceVariation = new PriceVariationIndicator(series); // Simple moving averages SMAIndicator shortSma = new SMAIndicator(closePrice, 8); SMAIndicator longSma = new SMAIndicator(closePrice, 20); // Exponential moving averages EMAIndicator shortEma = new EMAIndicator(closePrice, 8); EMAIndicator longEma = new EMAIndicator(closePrice, 20); // Percentage price oscillator PPOIndicator ppo = new PPOIndicator(closePrice, 12, 26); // Rate of change ROCIndicator roc = new ROCIndicator(closePrice, 100); // Relative strength index RSIIndicator rsi = new RSIIndicator(closePrice, 14); // Williams %R WilliamsRIndicator williamsR = new WilliamsRIndicator(series, 20); // Average true range AverageTrueRangeIndicator atr = new AverageTrueRangeIndicator(series, 20); // Standard deviation StandardDeviationIndicator sd = new StandardDeviationIndicator(closePrice, 14); /// <summary> /// Building header /// </summary> StringBuilder sb = new StringBuilder("timestamp,close,typical,variation,sma8,sma20,ema8,ema20,ppo,roc,rsi,williamsr,atr,sd\n"); /// <summary> /// Adding indicators values /// </summary> //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int nbTicks = series.getTickCount(); int nbTicks = series.TickCount; for (int i = 0; i < nbTicks; i++) { sb.Append(series.getTick(i).EndTime.Millis / 1000d).Append(',').Append(closePrice.getValue(i)).Append(',').Append(typicalPrice.getValue(i)).Append(',').Append(priceVariation.getValue(i)).Append(',').Append(shortSma.getValue(i)).Append(',').Append(longSma.getValue(i)).Append(',').Append(shortEma.getValue(i)).Append(',').Append(longEma.getValue(i)).Append(',').Append(ppo.getValue(i)).Append(',').Append(roc.getValue(i)).Append(',').Append(rsi.getValue(i)).Append(',').Append(williamsR.getValue(i)).Append(',').Append(atr.getValue(i)).Append(',').Append(sd.getValue(i)).Append('\n'); } /// <summary> /// Writing CSV file /// </summary> System.IO.StreamWriter writer = null; try { writer = new System.IO.StreamWriter("indicators.csv"); writer.Write(sb.ToString()); } catch (IOException ioe) { //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method: Logger.getLogger(typeof(IndicatorsToCsv).FullName).log(Level.SEVERE, "Unable to write CSV file", ioe); } finally { try { if (writer != null) { writer.Close(); } } catch (IOException) { } } }