Last active
October 12, 2021 20:45
-
-
Save actSoon/a530060342bde13f9bf76062aeba33d4 to your computer and use it in GitHub Desktop.
Generates the data to use as cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| const chartStartDt = 1601524800; // 2020-10-01T04:00:00.000Z | |
| const chartEndDt = 1617249600; // moment(chartStartDt * 1000).add(6, 'months').valueOf() / 1000; | |
| function getRandomArbitrary(min, max) { | |
| return Math.floor(Math.random() * (max - min) + min); | |
| } | |
| function addMoredays(StartTickTime, timePeriod, offset = 0) { | |
| let lastTick = StartTickTime; | |
| const moreTicks = [] | |
| if(timePeriod === '6M') { | |
| lastTick = chartEndDt // got using moment(endTick).subtrack(6, "months").valueOf() | |
| } | |
| let currentTime = StartTickTime + offset; | |
| while(currentTime <= (lastTick + offset)){ | |
| moreTicks.push({ | |
| "time": currentTime, | |
| "close": getRandomArbitrary(4100, 4800), | |
| "volume": getRandomArbitrary(200011089, 559999999) | |
| }) | |
| currentTime = currentTime + 60*60*24 | |
| } | |
| return moreTicks | |
| } | |
| const writeToFile = async ({result, fileName}) => { | |
| try { | |
| await fs.promises.writeFile(fileName, JSON.stringify(result, null, 4), 'utf8'); | |
| console.log('data is written successfully in the file') | |
| } | |
| catch (err) { | |
| console.log('not able to write data in the file ') | |
| } | |
| } | |
| const pleaseGenerateChartData = async (intervalStartPoint) => { | |
| const ticks =[] | |
| // const prevOffset = (60*60*24*29 + 60*60*13 ) | |
| // const offsetBy6Days = 60*60*24*6 | |
| const modified_ticks= addMoredays(intervalStartPoint, '6M') | |
| console.log(modified_ticks.length) | |
| console.log(modified_ticks[0]) | |
| console.log(modified_ticks[modified_ticks.length - 1]) | |
| const result = { | |
| "result": { | |
| "spx:ind": { | |
| "historical": true, | |
| "ticksType": "DayTick", | |
| "ticks": modified_ticks, | |
| "low": "4000.04", | |
| "high": "5079.95", | |
| "first": modified_ticks[0].time, | |
| "last": modified_ticks[modified_ticks.length -1].time, | |
| "security": { | |
| "open": "4311.73", | |
| "prevClose": "4079.95", | |
| "ticker": "spx:ind" | |
| }, | |
| "hasVolume": true | |
| } | |
| } | |
| } | |
| await writeToFile({result, fileName: './generated_data.json'}) | |
| } | |
| pleaseGenerateChartData(chartStartDt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment