Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
/bin/
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>trading-backtest</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
connection.project.dir=
eclipse.preferences.version=1
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ dependencies {

compile 'org.slf4j:slf4j-api:1.7.+'
compile 'ch.qos.logback:logback-classic:1.+'

compile group: 'com.yahoofinance-api', name: 'YahooFinanceAPI', version: '3.13.0'

testCompile group: 'junit', name: 'junit', version: '4.11'
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.lst.trading.lib.util;

import org.lst.trading.lib.series.DoubleSeries;
import rx.Observable;

public interface HistoricalPriceService {
Observable<DoubleSeries> getHistoricalAdjustedPrices(String symbol);
DoubleSeries getHistoricalAdjustedPrices(String symbol);
}
69 changes: 0 additions & 69 deletions src/main/java/org/lst/trading/lib/util/yahoo/YahooFinance.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.lst.trading.lib.util.yahoo;

import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.net.SocketTimeoutException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.lst.trading.lib.series.DoubleSeries;
import org.lst.trading.lib.series.TimeSeries.Entry;
import org.lst.trading.lib.util.HistoricalPriceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import yahoofinance.Stock;
import yahoofinance.YahooFinance;
import yahoofinance.histquotes.HistoricalQuote;
import yahoofinance.histquotes.Interval;

public class YahooFinanceService implements HistoricalPriceService {

private static final Logger log = LoggerFactory.getLogger(YahooFinanceService.class);

private static final int DAYS_OF_HISTORY = 2520;

@Override
public DoubleSeries getHistoricalAdjustedPrices(String symbol) {
return getHistory(symbol, DAYS_OF_HISTORY);
}

public DoubleSeries getHistory(String symbol, int daysBack) {
DoubleSeries doubleSeries = new DoubleSeries(symbol);

try {
Calendar from = Calendar.getInstance();
from.add(Calendar.DAY_OF_MONTH, -daysBack);

Stock stock = YahooFinance.get(symbol);
//[n] is the most current, [0] is the last in history
List<HistoricalQuote> historicalQuotes = stock.getHistory(from, Interval.DAILY);

for (HistoricalQuote historicalQuote : historicalQuotes) {
BigDecimal open = historicalQuote.getOpen();
BigDecimal high = historicalQuote.getHigh();
BigDecimal low = historicalQuote.getLow();
BigDecimal close = historicalQuote.getClose();
BigDecimal adjClose = historicalQuote.getAdjClose();
Date time = historicalQuote.getDate().getTime();

if (open == null || high == null || low == null || close == null || adjClose == null) {
log.error(symbol + " " + time + " has missing data: " + open + ", " + high + ", " + low + ", " + close + ", " + adjClose);
continue;
}

doubleSeries.add(new Entry<Double>(adjClose.doubleValue(), time.toInstant()));
}
} catch (FileNotFoundException e) {
log.error("no such symbol: " + symbol);
} catch (SocketTimeoutException e) {
log.error("network error: " + symbol);
} catch (Exception e) {
e.printStackTrace();
}

return doubleSeries;
}
}
18 changes: 9 additions & 9 deletions src/main/java/org/lst/trading/main/BacktestMain.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package org.lst.trading.main;

import static java.lang.String.format;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Locale;

import org.lst.trading.lib.backtest.Backtest;
import org.lst.trading.lib.model.ClosedOrder;
import org.lst.trading.lib.model.TradingStrategy;
import org.lst.trading.lib.series.MultipleDoubleSeries;
import org.lst.trading.lib.util.Util;
import org.lst.trading.lib.util.yahoo.YahooFinance;
import org.lst.trading.lib.util.yahoo.YahooFinanceService;
import org.lst.trading.main.strategy.kalman.CointegrationTradingStrategy;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Locale;

import static java.lang.String.format;

public class BacktestMain {
public static void main(String[] args) throws URISyntaxException, IOException {
String x = "GLD";
Expand All @@ -23,8 +23,8 @@ public static void main(String[] args) throws URISyntaxException, IOException {
TradingStrategy strategy = new CointegrationTradingStrategy(x, y);

// download historical prices
YahooFinance finance = new YahooFinance();
MultipleDoubleSeries priceSeries = new MultipleDoubleSeries(finance.getHistoricalAdjustedPrices(x).toBlocking().first(), finance.getHistoricalAdjustedPrices(y).toBlocking().first());
YahooFinanceService finance = new YahooFinanceService();
MultipleDoubleSeries priceSeries = new MultipleDoubleSeries(finance.getHistoricalAdjustedPrices(x), finance.getHistoricalAdjustedPrices(y));

// initialize the backtesting engine
int deposit = 15000;
Expand Down