Created
March 21, 2017 12:35
-
-
Save mniewrzal/734e03012cba295afd3608a1d65a8354 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.breakcoder.jogun.trader; | |
import java.io.IOException; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.List; | |
import com.breakcoder.jogun.client.market.base.MarketClientException; | |
import com.breakcoder.jogun.client.market.poloniex.PoloniexClient; | |
import com.breakcoder.jogun.client.market.poloniex.entities.ChartCandle; | |
import com.breakcoder.jogun.client.market.poloniex.entities.Period; | |
import com.google.gson.JsonSyntaxException; | |
public class Test { | |
public static void main(String[] args) { | |
PoloniexClient client = new PoloniexClient(); | |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd"); | |
List<ChartCandle> data = null; | |
try { | |
data = client.getChartData("BTC_LTC", dateFormat.parse("2017.03.01"), new Date(), Period.DAY); | |
} catch (JsonSyntaxException | MarketClientException | IOException | ParseException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
// List<Tick> ticks = new ArrayList<>(); | |
// for (ChartCandle candle : data) { | |
// //System.out.println(new Date(candle.getDate())); | |
// ticks.add(new Tick(new DateTime(candle.getDate()), candle.getOpen(), candle.getHigh(), candle.getLow(), | |
// candle.getClose(), candle.getVolume())); | |
// } | |
// | |
// TimeSeries series = new TimeSeries(ticks); | |
// | |
// /** | |
// * Creating indicators | |
// */ | |
// // Close price | |
// ClosePriceIndicator closePrice = new ClosePriceIndicator(series); | |
// // Typical price | |
// TypicalPriceIndicator typicalPrice = new TypicalPriceIndicator(series); | |
// // Price variation | |
// PriceVariationIndicator priceVariation = new PriceVariationIndicator(series); | |
// | |
// final int nbTicks = series.getTickCount(); | |
// Indicator<Decimal> rsiIndicator = new RSIIndicator(closePrice, 14); | |
// System.out.println(rsiIndicator.getTimeSeries().getTickCount()); | |
// // for (int i = 0; i < nbTicks; i++) { | |
// System.out.println("First: " + rsiIndicator.getValue(nbTicks - 1)); | |
// // } | |
try { | |
calculate(data); | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public static void calculate(List<ChartCandle> data) throws Exception { | |
int periodLength = 14; | |
int lastBar = data.size() - 1; | |
int firstBar = lastBar - periodLength + 1; | |
if (firstBar < 0) { | |
String msg = "Quote history length " + data.size() + " is insufficient to calculate the indicator."; | |
throw new Exception(msg); | |
} | |
double aveGain = 0, aveLoss = 0; | |
for (int bar = firstBar + 1; bar <= lastBar; bar++) { | |
double change = data.get(bar).getClose() - data.get(bar - 1).getClose(); | |
if (change >= 0) { | |
aveGain += change; | |
} else { | |
aveLoss += change; | |
} | |
} | |
double rs = aveGain / Math.abs(aveLoss); | |
double rsi = 100 - 100 / (1 + rs); | |
System.out.println("RSI: " + rsi); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment