Skip to content

Commit

Permalink
Implemented timeseries model
Browse files Browse the repository at this point in the history
  • Loading branch information
EstDavid committed Feb 22, 2023
1 parent a826cd0 commit f0fef8b
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 43 deletions.
24 changes: 11 additions & 13 deletions client/src/helpers/priceDataCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ export const calculateArrayTimeframe = (priceObject, timeframeTo) => {

export const calculateCandlestickData = (arrayOHLC) => {
let candlestickData = [];
let timestamps = Object.keys(arrayOHLC);
for(let timestamp of timestamps) {
let priceOHLC = arrayOHLC[timestamp];
candlestickData.push([timestamp * 1000,[priceOHLC.open, priceOHLC.high, priceOHLC.low, priceOHLC.close]]);
for(let candle of arrayOHLC) {
candlestickData.push([new Date(candle._id).getTime(),[candle.open, candle.high, candle.low, candle.close]]);
}

return candlestickData;
Expand Down Expand Up @@ -148,15 +146,15 @@ export const calculateEMAFromOHLC = (arrayOHLC, nPeriods, arrayType) => {
export const getChartingData = (dataObject, arrayOHLC) => {
const chartingData = [];
let timestampsArray = [];
for(let timestamp in arrayOHLC) {
timestampsArray.push(parseInt(timestamp));
for(let valueOHLC of arrayOHLC) {
timestampsArray.push(parseInt(new Date(valueOHLC._id).getTime()));
}
timestampsArray.sort((a, b) => a - b);
let j = 0;
for(let i = 0; i < timestampsArray.length; i += 1) {
let timestamp = timestampsArray[i];
if(timestamp >= dataObject.initialTimestamp) {
chartingData.push([ timestamp * 1000, dataObject.lineData[j]]);
chartingData.push([timestamp, dataObject.lineData[j]]);
j += 1;
}
}
Expand All @@ -165,20 +163,20 @@ export const getChartingData = (dataObject, arrayOHLC) => {

const getPriceArray = (arrayOHLC, arrayType) => {
let priceArray = {};
for(let timestamp in arrayOHLC) {
let valuesOHLC = arrayOHLC[timestamp];
for(let valueOHLC of arrayOHLC) {
const timestamp = new Date(valueOHLC._id).getTime()
// Brute but effective way of getting the array...
if(arrayType === 'open') {
priceArray[timestamp] = valuesOHLC.open;
priceArray[timestamp] = valueOHLC.open;
}
if(arrayType === 'high') {
priceArray[timestamp] = valuesOHLC.high;
priceArray[timestamp] = valueOHLC.high;
}
if(arrayType === 'low') {
priceArray[timestamp] = valuesOHLC.low;
priceArray[timestamp] = valueOHLC.low;
}
if(arrayType === 'close') {
priceArray[timestamp] = valuesOHLC.close;
priceArray[timestamp] = valueOHLC.close;
}
}
return priceArray;
Expand Down
19 changes: 9 additions & 10 deletions client/src/slices/priceData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const initialState = {
loading: true,
hasErrors: false,
indicatorsLimit: 5,
viewTimeframe: timeframes['hours1'],
viewTimeframe: timeframes['days1'],
priceDataRaw: {},
maxCandlesNumber: 150,
showCTRLMouseWheel: false,
Expand All @@ -33,7 +33,7 @@ export const initialState = {
startTimestamp: undefined,
endTimestamp: undefined,
observations: {},
arrayOHLC: {},
arrayOHLC: [],
maxTimestamp: undefined,
maxObservations: 0
},
Expand Down Expand Up @@ -80,11 +80,11 @@ export const priceDataSlice = createSlice({
state.priceObject.poolFee = payload.poolFee;
state.priceObject.observationTimeframe = payload.observationTimeframe;
state.priceObject.arrayTypes = payload.arrayTypes;
state.priceObject.startTimestamp = payload.startTimestamp;
state.priceObject.endTimestamp = payload.endTimestamp;
state.priceObject.startTimestamp = new Date(payload.startTimestamp).getTime();
state.priceObject.endTimestamp = new Date(payload.endTimestamp).getTime();
state.priceObject.observations = payload.observations;
state.priceObject.arrayOHLC = payload.arrayOHLC;
state.priceObject.maxTimestamp = payload.endTimestamp;
state.priceObject.maxTimestamp = new Date(payload.endTimestamp).getTime();
state.priceObject.maxObservations = payload.maxObservations;
state.loadingPriceObject = false;
},
Expand Down Expand Up @@ -112,16 +112,14 @@ export const priceDataSlice = createSlice({
state.chartObject.symbol = state.priceObject.symbol;
state.chartObject.series[0].name = `${state.priceObject.symbol} ${state.viewTimeframe.name}`;
const availableCandles =
state.priceObject.maxObservations *
state.priceObject.observationTimeframe.seconds /
state.viewTimeframe.seconds;
state.priceObject.maxObservations;
state.chartObject.xMin =
(
state.priceObject.maxTimestamp -
(state.priceObject.maxTimestamp / 1000) -
Math.min(state.maxCandlesNumber, availableCandles * 1.2) *
state.viewTimeframe.seconds
) * 1000;
state.chartObject.xMax = state.priceObject.maxTimestamp * 1000;
state.chartObject.xMax = state.priceObject.maxTimestamp;
},
switchTimeframe: (state, {payload}) => {
state.viewTimeframe = payload;
Expand Down Expand Up @@ -234,6 +232,7 @@ export function fetchPriceData(symbol, timeframeTo) {
dispatch(setArrayOHLC());

} catch(error) {
console.log(error)
dispatch(getPriceDataFailure())
}
}
Expand Down
112 changes: 103 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,113 @@ mongoose.connect(url)
})

const Pair = require('./models/pair')
const Price = require('./models/price')

// DOWNLOAD FILE
app.get("/get-url/:timeframe/:timeframeto/:symbol", (req, res) => {

Pair.findById(req.params.symbol)
.then(pair => {
if (pair !== undefined && pair !== null) {
pair.priceData.timeframeTo.seconds = req.params.timeframeto
res.status(200).json(pair)
} else {
res.status(404).end()
const { symbol, timeframe, timeframeto } = req.params

Price.aggregate([
{
$match: {
// timestamp: { $gt: new Date("2022-12-01") },
"metadata.symbol" : symbol,
"metadata.seconds": parseInt(timeframe)
}
})
},
{
$project: {
timestamp: 1,
price: 1,
}
},
{
$group: {
_id: {
$dateFromParts: {
'year': { $year: '$timestamp' },
'month': { $month: '$timestamp' },
'day': {
$subtract: [
{ $dayOfMonth: '$timestamp'},
{ $mod: [
{ $dayOfMonth: '$timestamp'},
timeframeto / (60 * 60 * 24)
]}]
},
'hour': {
$subtract: [
{ $hour: '$timestamp'},
{ $mod: [
{ $hour: '$timestamp'},
timeframeto / (60 * 60)
]}]
},
'minute': {
$subtract: [
{ $minute: '$timestamp'},
{ $mod: [
{ $minute: '$timestamp'},
timeframeto / 60
]}]
}
}
},
open: { $first: "$price"},
low: { $min: "$price"},
high: {$max: "$price"},
close: { $last: "$price"},
}
},
{
$sort: {
_id: 1
}
}
]).then(arrayOHLC => {
console.log(arrayOHLC.length, 'candles')

Pair.findById(symbol, {priceData: 0})
.then(pair => {
if (
pair !== undefined &&
pair !== null &&
arrayOHLC.length > 0
) {
const {
symbol,
baseToken,
quoteToken,
poolAddress,
poolFee,
arrayTypes,
extraMinutesData,
} = pair

const pairObject = {
symbol,
baseToken,
quoteToken,
poolAddress,
poolFee,
arrayTypes,
extraMinutesData,
arrayOHLC,
observationTimeframe: {
name: "30 seconds",
seconds: 30,
},
startTimestamp: new Date(arrayOHLC[0]._id),
endTimestamp: new Date(arrayOHLC[arrayOHLC.length - 1]._id),
maxTimestamp: new Date(arrayOHLC[arrayOHLC.length - 1]._id),
maxObservations: arrayOHLC.length,
}
res.status(200).send(pairObject)
} else {
res.status(404).end()
}
})
})
});

// DOWNLOAD ALL FILENAMES AND CLASSIFY PAIRS ACCORDING TO QUOTE TOKEN
Expand Down
40 changes: 29 additions & 11 deletions models/pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,40 @@ const pairSchema = new mongoose.Schema({
poolFee: String,
arrayTypes: [String],
extraMinutesData: Number,
priceData: priceDataSchema,
// priceData: priceDataSchema,
})

const getEarliest = (observations) => {
const getEarliest = (earliest, observation) => {
const current = new Date(parseInt(observation._id) * 1000)
return current <= earliest ? current : earliest
}

return observations.reduce(getEarliest, new Date(parseInt(observations[0]._id) * 1000))
}

const getLatest = (observations) => {
const getLatest = (latest, observation) => {
const current = new Date(parseInt(observation._id) * 1000)
return current >= latest ? current : latest
}

return observations.reduce(getLatest, new Date(parseInt(observations[0]._id) * 1000))
}

pairSchema.set('toJSON', {
transform: (document, returnedObject) => {
const { priceData } = returnedObject
returnedObject.id = returnedObject._id.toString()
returnedObject.observationTimeframe = {
name: priceData.timeframe.name,
seconds: priceData.timeframe.seconds
}
returnedObject.observations = {}
returnedObject.arrayOHLC = priceData.arrayOHLC
returnedObject.startTimestamp = (priceData.earliest).getTime() / 1000
returnedObject.endTimestamp = (priceData.latest).getTime() / 1000
returnedObject.maxObservations = priceData.maxObservations
// returnedObject.id = returnedObject._id.toString()
// returnedObject.observationTimeframe = {
// name: priceData.timeframe.name,
// seconds: priceData.timeframe.seconds
// }
// returnedObject.observations = {}
// returnedObject.arrayOHLC = priceData.arrayOHLC
// returnedObject.startTimestamp = (priceData.earliest).getTime() / 1000
// returnedObject.endTimestamp = (priceData.latest).getTime() / 1000
// returnedObject.maxObservations = priceData.maxObservations
delete returnedObject.priceData
delete returnedObject._id
delete returnedObject.__v
Expand Down

0 comments on commit f0fef8b

Please sign in to comment.