Skip to content

Commit

Permalink
Load realtime prices in paper trade mode from nasdaq page
Browse files Browse the repository at this point in the history
  • Loading branch information
junajan committed Dec 23, 2017
1 parent 23ac73e commit 3b67131
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 46 deletions.
8 changes: 4 additions & 4 deletions backtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ Log.info("Running Backtest");
config = {
tickers: tickers,
// from: "2015-01-01",
from: '2008-01-01',
// to: '2016-01-01',
// to: '2016-10-10',
to: moment().format('YYYY-MM-DD'),
// from: '2008-01-01',
from: '2017-01-01',
to: '2017-10-10',
// to: moment().format('YYYY-MM-DD'),
capital: 10000 * 2,
monthlyAdd: 0,
mailLog: false,
Expand Down
1 change: 0 additions & 1 deletion config/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module.exports = function(app, config) {
app.use(cookieParser());
app.use(session({
secret: 'This was really big kind of stuff',
saveUninitialized: true,
resave: false,
store: new FileStore({
logFn: _.noop,
Expand Down
3 changes: 0 additions & 3 deletions modules/Connector/YahooMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ var Mock = function (config, app) {

Log = app.Log || LogMockup;

// realtime data mockup URL
var realtimeUrl = 'http://download.finance.yahoo.com/d/quotes.csv?f=sl1&s=';

Log.info("Starting mockup API with config:", config);

var getNextOrderId = function () {
Expand Down
47 changes: 12 additions & 35 deletions modules/Connector/_yahoo.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,28 @@
var request = require("request");
var _ = require("lodash");
var csv = require("fast-csv");
var NasdaqFinance = require("nasdaq-finance").default;

var Yahoo = function() {
var self = this;

// realtime data mockup URL
var realtimeUrl = 'http://download.finance.yahoo.com/d/quotes.csv?f=sl1&s=';

/**
* Trasnform data from array to object
* @param data Array of pairs [ticker, price]
* @return Object {ticker: price, ...}
*/
var transformPriceData = function (data) {
var out = {};
data.forEach(function(item) {
out[item[0]] = item[1];
});

return out;
};
const client = new NasdaqFinance({
tickerConcurrency: 3
})

/**
* Get prices for given tickers (if they are streamed)
* @param tickers List of tickers to load
* @param done Function(err, {ticker: price, ...}
*/
self.getPrices = function (tickers, done) {
if(_.isArray(tickers))
tickers = tickers.join(',');

console.log("Load market prices for these tickers", tickers);
request(realtimeUrl+tickers, function (error, response, body) {
if(error || response.statusCode !== 200)
return done(error || {statusCode: response.statusCode});

var buffer = [];
csv.fromString(body)
.on("data", function(data) {
buffer.push(data);
})
.once("error", done)
.on("end", function() {
done(null, transformPriceData(buffer));
});
});
if(_.isString(tickers))
tickers = tickers.split(',')

console.log("Load market prices for these tickers", tickers.join(','));

return client.getPrice(tickers, true)
.then(prices => done(null, prices))
.catch(err => done(err, null))
};

return this;
Expand Down
3 changes: 2 additions & 1 deletion modules/Loader/AlphaVantage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ function AlphaVantage(API_KEY) {
return { ticker: ticker, price: prices[dates[0]]['4. close'] }
})
.catch(function(err) {
return null;

console.error(
`Error when downloading realtime prices for ${ticker}`,
err, response.body
)
return Promise.resolve(null)
})

}, { concurrency: 1 })
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"strip-ansi": "^3.0.1",
"syncprompt": "^1.0.1",
"throttle-function": "^0.1.0",
"yahoo-finance": "0.3.0"
"yahoo-finance": "0.3.5"
},
"devDependencies": {
"eslint": "^3.4.0",
Expand Down
2 changes: 1 addition & 1 deletion singleRun.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var Robot;
require(config.dirWeb+'Routes')(app);

if(!PROCESS_LOAD_DELAY || PROCESS_LOAD_DELAY < 1000) {
Log.error("Delay can't be smaller than 30'000ms - value: "+PROCESS_LOAD_DELAY)
Log.error("Delay can't be smaller than 1'000ms - value: "+PROCESS_LOAD_DELAY)
process.exit(1)
}

Expand Down

0 comments on commit 3b67131

Please sign in to comment.