Skip to content

Instantly share code, notes, and snippets.

@franciskim
Last active December 9, 2017 23:11
Show Gist options
  • Save franciskim/8bd05c26d8aa41e074b95c212b648b59 to your computer and use it in GitHub Desktop.
Save franciskim/8bd05c26d8aa41e074b95c212b648b59 to your computer and use it in GitHub Desktop.
const YahooFinanceAPI = require('yahoo-finance-data')
// fill in Yahoo Finance API keys here
const api = new YahooFinanceAPI({
key: null,
secret: null
})
const talib = require('talib')
console.log("TALib Version: " + talib.version)
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(null) // fill in SendGrid API key here
// list of symbols you want to track
let symbols = [
{symbol: 'CL=F', dontNotifyUntil: 0},
{symbol: 'ES=F', dontNotifyUntil: 0},
{symbol: 'BTCUSD=X', dontNotifyUntil: 0},
{symbol: 'AUDUSD=X', dontNotifyUntil: 0},
{symbol: '^AXJO', dontNotifyUntil: 0},
{symbol: '^N225', dontNotifyUntil: 0},
{symbol: 'GC=F', dontNotifyUntil: 0},
{symbol: 'NQ=F', dontNotifyUntil: 0},
{symbol: 'FB', dontNotifyUntil: 0},
{symbol: 'AAPL', dontNotifyUntil: 0},
{symbol: 'NFLX', dontNotifyUntil: 0},
{symbol: 'GOOG', dontNotifyUntil: 0},
{symbol: 'TSLA', dontNotifyUntil: 0},
{symbol: 'ETHUSD=X', dontNotifyUntil: 0},
{symbol: 'DX-Y.NYB', dontNotifyUntil: 0}
]
const interval = 30000
const step1 = (symbol, i) => {
console.log(`Running Step 1 on ${symbol}`)
api
.getHistoricalData(symbol, '1d', 'max')
.then(data => {
let high = data.chart.result[0].indicators.quote[0].high.filter(n => n)
let low = data.chart.result[0].indicators.quote[0].low.filter(n => n)
console.log(high.length, low.length)
talib.execute({
name: "SAR",
high: high,
low: low,
startIdx: 0,
endIdx: high.length - 1,
optInAcceleration: 0.02,
optInMaximum: 0.2
}, function (error, result) {
if (error) console.error(error)
console.log("1D Results:")
console.log(result.result.outReal[result.result.outReal.length - 1])
console.log('H: ' + high[high.length - 1])
console.log('L: ' + low[low.length - 1])
if (result.result.outReal[result.result.outReal.length - 1] > high[high.length - 1]) {
console.log(`Step 1 short signal on ${symbol}`)
step2(symbol, i)
}
})
})
.catch(err => console.log(err))
}
const step2 = (symbol, i) =>
api
.getIntradayChartData(symbol, '1h', true)
.then(data => {
let high = data.chart.result[0].indicators.quote[0].high.filter(n => n)
let low = data.chart.result[0].indicators.quote[0].low.filter(n => n)
console.log(high.length, low.length)
talib.execute({
name: "SAR",
high: high,
low: low,
startIdx: 0,
endIdx: high.length - 1,
optInAcceleration: 0.02,
optInMaximum: 0.2
}, function (error, result) {
if (error) console.error(error)
console.log("1h Results:")
console.log(result.result.outReal[result.result.outReal.length - 1])
console.log('H: ' + high[high.length - 1])
console.log('L: ' + low[low.length - 1])
if (result.result.outReal[result.result.outReal.length - 1] > high[high.length - 1]) {
console.log(`Step 2 short signal on ${symbol}`)
step3(symbol, i)
}
})
})
.catch(err => console.log(err))
const step3 = (symbol, i) =>
api
.getIntradayChartData(symbol, '15m', true)
.then(data => {
let high = data.chart.result[0].indicators.quote[0].high.filter(n => n)
let low = data.chart.result[0].indicators.quote[0].low.filter(n => n)
console.log(high.length, low.length)
talib.execute({
name: "SAR",
high: high,
low: low,
startIdx: 0,
endIdx: high.length - 1,
optInAcceleration: 0.02,
optInMaximum: 0.2
}, function (error, result) {
if (error) console.error(error)
console.log("1h Results:")
console.log(result.result.outReal[result.result.outReal.length - 1])
console.log('H: ' + high[high.length - 1])
console.log('L: ' + low[low.length - 1])
if (result.result.outReal[result.result.outReal.length - 1] > high[high.length - 1]) {
console.log(`15HD rule says short ${symbol}!!!!!!!!`)
if (new Date() * 1 > symbols[i].dontNotifyUntil) {
console.log(`Current timestamp: ${new Date() * 1}`)
console.log(`Do not notify until: ${symbols[i].dontNotifyUntil}`)
console.log(`Sending notification!`)
const msg = {
to: 'hello@franciskim.co',
from: 'hello@franciskim.co',
subject: `Short ${symbol}`,
text: `15HD rule says short ${symbol}`
}
sgMail.send(msg)
if (new Date().getDay() !== 6 && new Date().getDay() !== 0) {
symbols[i].dontNotifyUntil = new Date() * 1 + 4 * 60 * 60 * 1000 // do not notify for 4 hours
}
else symbols[i].dontNotifyUntil = new Date() * 1 + 24 * 60 * 60 * 1000 // do not notify for 24 hours on weekend
}
else {
console.log(`Current timestamp: ${new Date() * 1}`)
console.log(`Do not notify until: ${symbols[i].dontNotifyUntil}`)
console.log(`Skipping notification`)
}
}
})
})
.catch(err => console.log(err))
let i = -1
setInterval(() => {
i++
if (i > symbols.length - 1) i = 0 // loop
step1(symbols[i].symbol, i)
}, interval)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment