Skip to content

Instantly share code, notes, and snippets.

@biggestT
Last active November 2, 2018 11:18
Show Gist options
  • Save biggestT/8fb5dceeae767320bce02b0afbedd898 to your computer and use it in GitHub Desktop.
Save biggestT/8fb5dceeae767320bce02b0afbedd898 to your computer and use it in GitHub Desktop.
const { always, tap, pipeP, invoker, map, constructN, path, pipe, prop,
applySpec, assoc, converge, pick, values, mean } = require('ramda')
const { igAuthenticatedClientP } = require('./ig')
const { pOf, convergeP, applySpecP } = require('ramdap')
// remove the spread (difference between ask/bid)
const getPrice = attr => pipe(
prop(attr),
pick(['ask', 'bid']),
values,
mean
)
const getTime = pipe(
prop('snapshotTime'),
constructN(1, Date)
)
const igRes2Quotes = pipe(
prop('prices'),
map(
applySpec({
date: getTime,
open: getPrice('openPrice'),
close: getPrice('closePrice'),
high: getPrice('highPrice'),
low: getPrice('lowPrice'),
created_at: constructN(0, Date)
})
)
)
const transform = converge(
map, [
pipe(
prop('serviceId'),
assoc('service_id')
),
pipe(
prop('igRes'),
igRes2Quotes
)
]
)
const sid2epic = {
'cfd_OMX30-20SEK-HOUR': 'IX.D.OMX.IFM.IP',
'cfd_HANGSENG-10HK-HOUR': 'IX.D.HANGSENG.IFM.IP',
'cfd_NIKKEI-1USD-HOUR': 'IX.D.NIKKEI.IFM.IP',
'cfd_ITALY40-1EUR-HOUR': 'IX.D.MIB.IFM.IP'
}
// { igClientId, igUname, iPwd, igIsDemo, serviceId, from, to } => Promise(IgRes)
const reqP = convergeP(
invoker(2, 'get'), [
({ serviceId, from, to }) => `prices/${sid2epic[serviceId]}/HOUR/${from}%2000%3A00%3A00/${to}%2000%3A00%3A00`,
always(2),
igAuthenticatedClientP
])
// { igClientId, igUname, iPwd, igIsDemo, serviceId, from, to } => Promise(Quotes[])
const fetchQuotesP = pipeP(
applySpecP({
igRes: reqP,
serviceId: prop('serviceId')
}),
tap(pipe(path(['igRes', 'allowance']), console.log)),
transform,
pOf
)
module.exports = {
fetchQuotesP
}
const IG = require('ig-api')
// igConfigs -> IGAPIClient
const igAuthenticatedClientP = ({ igClientId, igIsDemo, igUname, igPwd }) => {
const ig = new IG(igClientId, igIsDemo)
return new Promise((resolve, reject) => {
ig.login(igUname, igPwd)
.then(() => resolve(ig))
.catch(reject)
})
}
module.exports = {
igAuthenticatedClientP
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment