Skip to content

Commit

Permalink
Trying toget backtesting to work
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelgrosner committed Jun 7, 2015
1 parent 5cf4ae5 commit 7df944a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"knockout": "^3.2.0",
"line-by-line": "^0.1.3",
"lodash": "^2.4.1",
"minimist": "^1.1.1",
"moment": "^2.8.3",
"mongodb": "^1.4.22",
"node-uuid": "^1.4.2",
Expand Down
16 changes: 12 additions & 4 deletions src/service/backtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import _ = require('lodash');
import fs = require("fs");
import mongo = require("mongodb");
import Persister = require("./persister");
import Q = require("q");

var shortId = require("shortid");
var SortedArray = require("collections/sorted-array");
Expand All @@ -30,14 +31,14 @@ class Timed {
}

export class BacktestTimeProvider implements Utils.IBacktestingTimeProvider {
constructor(private _internalTime : Moment) { }
constructor(private _internalTime : Moment, private _endTime : Moment) { }

utcNow = () => this._internalTime;

private _immediates = new Array<() => void>();
setImmediate = (action: () => void) => this._immediates.push(action);

private _timeouts = new SortedArray(null, null, (a : Timed, b : Timed) => a.time.diff(b.time));
private _timeouts : Timed[] = [];
setTimeout = (action: () => void, time: Duration) => {
this.setAction(action, time, TimedType.Timeout);
};
Expand All @@ -47,15 +48,22 @@ export class BacktestTimeProvider implements Utils.IBacktestingTimeProvider {
};

private setAction = (action: () => void, time: Duration, type : TimedType) => {
this._timeouts.push(new Timed(action, this._internalTime.clone().add(time), type, time));
var dueTime = this._internalTime.clone().add(time);

if (dueTime.diff(this.utcNow())) {
return;
}

this._timeouts.push(new Timed(action, dueTime, type, time));
this._timeouts.sort((a, b) => a.time.diff(b.time));
};

scrollTimeTo = (time : Moment) => {
while (this._immediates.length > 0) {
this._immediates.pop()();
}

while (this._timeouts.length > 0 && this._timeouts.min().time.diff(time) > 0) {
while (this._timeouts.length > 0 && _.first(this._timeouts).time.diff(time) < 0) {
var evt : Timed = this._timeouts.shift();
this._internalTime = evt.time;
evt.action();
Expand Down
26 changes: 19 additions & 7 deletions src/service/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import express = require('express');
import util = require('util');
import moment = require("moment");
import fs = require("fs");
import minimist = require("minimist");

import HitBtc = require("./gateways/hitbtc");
import OkCoin = require("./gateways/okcoin");
Expand Down Expand Up @@ -36,27 +37,38 @@ import PositionManagement = require("./position-management");
import Statistics = require("./statistics");
import Backtest = require("./backtest");

var cmdParams = minimist(process.argv.slice(2));

var config = new Config.ConfigProvider(cmdParams);

["uncaughtException", "exit", "SIGINT", "SIGTERM"].forEach(reason => {
process.on(reason, (e?) => {
Utils.errorLog(util.format("Terminating!", reason, e, (typeof e !== "undefined" ? e.stack : undefined)));

if (config.inBacktestMode) process.exit(1);
});
});

var mainLog = Utils.log("tribeca:main");
var messagingLog = Utils.log("tribeca:messaging");

mainLog(util.inspect(cmdParams));

var pair = new Models.CurrencyPair(Models.Currency.BTC, Models.Currency.USD);
var orderCache = new Broker.OrderStateCache();
var config = new Config.ConfigProvider();

if (config.inBacktestMode) {
var inputData : Array<Models.Market | Models.MarketTrade> = JSON.parse(fs.readFileSync("inputData.json", 'utf8'));
// EXCHANGE=null TRIBECA_MODE=dev node main.js backtest --mdFile=/Users/grosner/inputData.json --paramFile=/Users/grosner/parameters.json
var inputData : Array<Models.Market | Models.MarketTrade> = JSON.parse(fs.readFileSync(cmdParams['mdFile'], 'utf8'));
_.forEach(inputData, d => d.time = moment(d.time));
inputData = _.sortBy(inputData, d => d.time);
var parameters : Backtest.BacktestParameters = JSON.parse(fs.readFileSync("backtestParameters.json", 'utf8'));
var timeProvider : Utils.ITimeProvider = new Backtest.BacktestTimeProvider(_.first(inputData).time);

var parameters : Backtest.BacktestParameters = JSON.parse(fs.readFileSync(cmdParams['paramFile'], 'utf8'));

var timeProvider : Utils.ITimeProvider = new Backtest.BacktestTimeProvider(_.first(inputData).time, _.last(inputData).time);
var exchange = Models.Exchange.Null;
var gw = new Backtest.BacktestGateway(inputData, parameters.startingBasePosition, parameters.startingQuotePosition, <Backtest.BacktestTimeProvider>timeProvider);
var gateway = new Backtest.BacktestExchange(gw);
var gateway : Interfaces.CombinedGateway = new Backtest.BacktestExchange(gw);

var getPublisher = <T>(topic: string, persister: Persister.ILoadAll<T> = null): Messaging.IPublish<T> => {
return new Messaging.NullPublisher<T>();
Expand All @@ -67,7 +79,7 @@ if (config.inBacktestMode) {
var getPersister = <T>(collectionName: string) : Persister.ILoadAllByExchangeAndPair<T> => new Backtest.BacktestPersister<T>();
var getMarketTradePersister = () => getPersister<Models.MarketTrade>("mt");

var getRepository = <T>(defValue: T, collectionName: string) : Persister.ILoadLatest<T> => new Backtest.BacktestPersister<T>();
var getRepository = <T>(defValue: T, collectionName: string) : Persister.ILoadLatest<T> => new Backtest.BacktestPersister<T>([defValue]);

var startingActive : Models.SerializedQuotesActive = new Models.SerializedQuotesActive(true, timeProvider.utcNow());
var startingParameters : Models.QuotingParameters = parameters.quotingParameters;
Expand Down Expand Up @@ -126,7 +138,7 @@ else {

var getReceiver = <T>(topic: string) : Messaging.IReceive<T> => new Messaging.Receiver<T>(topic, io, messagingLog);

var db = config.inBacktestMode ? null : Persister.loadDb();
var db = Persister.loadDb();

var getPersister = <T>(collectionName: string) : Persister.ILoadAllByExchangeAndPair<T> =>
new Persister.BasicPersister<T>(db, collectionName);
Expand Down
3 changes: 3 additions & 0 deletions tsd.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
},
"compression/compression.d.ts": {
"commit": "b3324f69165309a3ea2bd02fd10e97841bf07c83"
},
"minimist/minimist.d.ts": {
"commit": "2a5858c16563634453533009242b8e0b18521370"
}
}
}

0 comments on commit 7df944a

Please sign in to comment.