From 34ff5ec68fc774e670078e731804232605ef8c59 Mon Sep 17 00:00:00 2001 From: Joe Tretter Date: Mon, 9 Mar 2020 10:49:19 -0500 Subject: [PATCH] First functioning version with server calls. --- js/masterFilter.js | 56 ++++++++++++++++++++----------- js/optiTrainer.js | 33 ++++++++++++++---- server/index.js | 72 +++++++++++++++++++++++++++++++++++++--- server/package-lock.json | 59 ++++++++++++++++++++++++++++++++ server/package.json | 5 ++- 5 files changed, 194 insertions(+), 31 deletions(-) diff --git a/js/masterFilter.js b/js/masterFilter.js index 6bf39ce..2111c88 100644 --- a/js/masterFilter.js +++ b/js/masterFilter.js @@ -29,18 +29,26 @@ let beforeShowDay=(theDate)=>{ let isoDate=theDate.toISOString().substr(0,10); console.log("BeforeShow",isoDate,theDate); - if (!filterContext.availableDates.dates[isoDate]) { + let isDateFound=false; + for (let i=0;i{ - $.ajax({ - url:"https://netsquat.ddns.kawomi.com/wc/raw/tosym" + $.get({ + url:"http://localhost:3000/getSymbols" }).done(response=>{ - let allSymbols=JSON.parse(response); + let allSymbols=response; console.log(allSymbols); - refreshSymbolList(allSymbols.symbols); + refreshSymbolList(allSymbols); }); } @@ -54,8 +62,8 @@ let symbolChanged=_=>{ $("#datePicker").datepicker({beforeShowDay:beforeShowDay}) .on('changeDate', (e)=>{ console.log("DatePicker changed date",e.date); - filterContext.selectedDate=e.date.toISOString().substr(0,10); - showTimesForDay(filterContext.selectedDate); + filterContext.theDate=e.date.toISOString().substr(0,10); + showTimesForDay(filterContext.theDate); }); }); } @@ -68,29 +76,37 @@ let timeChanged=()=>{ let requestOptionChain=()=>{ $.ajax({ - url:"https://netsquat.ddns.kawomi.com/wc/raw/spy" + url:"http://localhost:3000/getOptionChain?Date=" + filterContext.theDate + "&Symbol=" + filterContext.theSymbol + "&Time="+filterContext.selectedTime }).done(response=>{ - dta=JSON.parse(response); + dta=JSON.parse(response[0].Data); console.log("received",dta); displayOptionChain(); }); } -let showTimesForDay=(theDate)=>{ - let lbTime=$("#lbTime"); - lbTime.empty(); - allTimes = filterContext.availableDates.dates[theDate]; - for (let i=0;i${theTime}`)) - } +let showTimesForDay=()=>{ + return $.ajax({ + url:"http://localhost:3000/getTimes?Date=" + filterContext.theDate + "&Symbol=" + filterContext.theSymbol + }).done(response=>{ + let availableTimes=response; + console.log(availableTimes); + let lbTime=$("#lbTime"); + lbTime.empty(); + allTimes = availableTimes; + for (let i=0;i${theTime}`)) + } + + }); + } let getAvailDatesForSymbol=(theSymbol)=>{ return $.ajax({ - url:"https://netsquat.ddns.kawomi.com/wc/raw/availDates" + url:"http://localhost:3000/getDates" }).done(response=>{ - let availableDates=JSON.parse(response); + let availableDates=response; console.log(availableDates); filterContext.availableDates=availableDates; }); @@ -100,7 +116,7 @@ let refreshSymbolList=(allSymbols)=>{ let lbSymbol=$("#lbSymbol"); lbSymbol.empty(); for (let i=0;i${theSymbol}`)) } diff --git a/js/optiTrainer.js b/js/optiTrainer.js index c73c576..6c31ec1 100644 --- a/js/optiTrainer.js +++ b/js/optiTrainer.js @@ -1,7 +1,11 @@ var costAmount=0; +var allCombinations; $(document).ready(_=>{ dta=fakeDta; + allCombinations=$.get("http://localhost:3000/getCombinations", + data=>{allCombinations=data;}, + "json"); // displayOptionChain(); createOrderFrame(); createMasterFilterFrame(); @@ -18,12 +22,20 @@ let createOrderFrame=()=>{ window.document.getElementById('app').append(theFrameDiv); theFrameDiv.innerHTML=` + +
Gain $0
`; } +let clearOrder=()=>{ + costAmount=0; + $("#listOrderItems").empty(); + showCostAmount(); +} + let addToOrder=(openType,optionData)=>{ optionData=JSON.parse(optionData.replace(/'/g,'"')); console.log("AddToOrder",openType,optionData); @@ -31,11 +43,14 @@ let addToOrder=(openType,optionData)=>{ let price=(openType.substr(0,1)==="b")?optionData.ask:optionData.bid; costAmount+=(price*100) * ((openType.substr(0,1)==="b")?-1:1); - $("#lbCostAmount").text(costAmount.toLocaleString('en',{minimumFractionDigits: 2, maximumFractionDigits: 2})); - + showCostAmount(); $("#listOrderItems").append(`
  • ${openType} | ${optionData.symbol} | ${price*100}
  • `); } +let showCostAmount=_=>{ + $("#lbCostAmount").text(costAmount.toLocaleString('en',{minimumFractionDigits: 2, maximumFractionDigits: 2})); +} + let dta={}; let datesOut=(dateInp)=>{ @@ -56,10 +71,13 @@ let getCallArtifact=(callData,allColumns)=>{ `; } let strCallJSON=JSON.stringify(callData).replace(/"/g,"\\'"); + + bidFrm=Number(callData.bid).toLocaleString('en',{minimumFractionDigits: 2, maximumFractionDigits: 2}); + askFrm=Number(callData.ask).toLocaleString('en',{minimumFractionDigits: 2, maximumFractionDigits: 2}); artifact+=` - ${callData.bid} - ${callData.ask} + ${bidFrm} + ${askFrm} `; return artifact; } @@ -78,9 +96,12 @@ let getPutArtifact=(putData,allColumns)=>{ let strPutJSON=JSON.stringify(putData).replace(/"/g,"\\'"); + bidFrm=Number(putData.bid).toLocaleString('en',{minimumFractionDigits: 2, maximumFractionDigits: 2}); + askFrm=Number(putData.ask).toLocaleString('en',{minimumFractionDigits: 2, maximumFractionDigits: 2}); + artifact+=` - ${putData.bid} - ${putData.ask} + ${bidFrm} + ${askFrm} `; return artifact; } diff --git a/server/index.js b/server/index.js index 8bf34d2..6a0193b 100644 --- a/server/index.js +++ b/server/index.js @@ -1,12 +1,22 @@ const micro = require('micro'); +const queryString = require('querystring'); const Database = require('better-sqlite3'); module.exports = (req, res) => { console.log(req.method, req.url); + res.setHeader("Access-Control-Allow-Origin","*"); + //console.log(res); if (req.method === "GET") { console.log("Get Request coming in"); - if (req.url === "/dates") { - console.log("Request for Dates"); + let qsAttr; + let func=req.url; + if (req.url.indexOf("?") >=0){ + func=req.url.substring(0,req.url.indexOf("?")) + qsAttr=queryString.parse(req.url.substring(req.url.indexOf("?")+1)); + } + console.log(func,qsAttr); + if (func === "/getCombinations") { + console.log("Request for combinations"); const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log }); const stmt = db.prepare("SELECT distinct Symbol,date(TimeStamp,'localtime') Date ,time(TimeStamp,'localtime') Time FROM OptionQuotes"); let allDates = stmt.all(); @@ -14,8 +24,62 @@ module.exports = (req, res) => { db.close(); micro.send(res,200,allDates); } - } else { - res.end('Welcome to Micro'); + if (func === "/getSymbols") { + console.log("Request for Symbols"); + const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log }); + const stmt = db.prepare("SELECT distinct Symbol FROM OptionQuotes order by 1"); + let allDates = stmt.all(); + //console.log(JSON.stringify(allDates)); + db.close(); + micro.send(res,200,allDates); + } + if (func === "/getDates") { + console.log("Request for Dates"); + const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log }); + const stmt = db.prepare("SELECT distinct date(TimeStamp,'localtime') Date FROM OptionQuotes order by 1"); + let allDates = stmt.all(); + //console.log(JSON.stringify(allDates)); + db.close(); + micro.send(res,200,allDates); + } + if (func === "/getTimes") { + console.log("Request for Times"); + const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log }); + let sql = "SELECT distinct time(TimeStamp,'localtime') Time FROM OptionQuotes where 1=1" + if (qsAttr.Symbol) { + sql +=" and Symbol='" + qsAttr.Symbol+ "'"; + } + if (qsAttr.Date){ + sql +=" and date(TimeStamp,'localtime')='" + qsAttr.Date+ "'"; + } + sql+=" order by 1"; + const stmt = db.prepare(sql); + let allDates = stmt.all(); + //console.log(JSON.stringify(allDates)); + db.close(); + micro.send(res,200,allDates); + } + if (func === "/getOptionChain") { + console.log("Request for OptionChain"); + const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log }); + let sql = "SELECT Data FROM OptionQuotes where 1=1" + if (qsAttr.Symbol) { + sql +=" and Symbol='" + qsAttr.Symbol+ "'" + } + if (qsAttr.Date){ + sql +=" and date(TimeStamp,'localtime')='" + qsAttr.Date+ "'"; + } + if (qsAttr.Time){ + sql +=" and time(TimeStamp,'localtime')='" + qsAttr.Time+ "'"; + } + + const stmt = db.prepare(sql); + let allDates = stmt.all(); + //console.log(JSON.stringify(allDates)); + db.close(); + micro.send(res,200,allDates); + } + } } diff --git a/server/package-lock.json b/server/package-lock.json index 6285b5c..1f433c4 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -38,6 +38,11 @@ "pirates": "^3.0.2" } }, + "babel-regenerator-runtime": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/babel-regenerator-runtime/-/babel-regenerator-runtime-6.5.0.tgz", + "integrity": "sha1-DkHNHJ+ARCRm8BXHSf/4upj44RA=" + }, "babylon": { "version": "6.18.0", "resolved": "https://registry.npmjs.org/babylon/-/babylon-6.18.0.tgz", @@ -256,6 +261,11 @@ "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" }, + "isstream": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", + "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" + }, "magic-string": { "version": "0.22.5", "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.22.5.tgz", @@ -264,6 +274,11 @@ "vlq": "^0.2.2" } }, + "media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" + }, "micro": { "version": "9.3.4", "resolved": "https://registry.npmjs.org/micro/-/micro-9.3.4.tgz", @@ -275,6 +290,45 @@ "raw-body": "2.3.2" } }, + "micro-core": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/micro-core/-/micro-core-0.4.0.tgz", + "integrity": "sha1-8NnHCAGthl/jpu/wHPGHdU56WTM=", + "requires": { + "babel-regenerator-runtime": "6.5.0", + "isstream": "0.1.2", + "media-typer": "0.3.0", + "minimist": "1.2.0", + "raw-body": "2.1.6" + }, + "dependencies": { + "bytes": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-2.3.0.tgz", + "integrity": "sha1-1baAoWW2IBc5rLYRVCqrwtjOsHA=" + }, + "iconv-lite": { + "version": "0.4.13", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.13.tgz", + "integrity": "sha1-H4irpKsLFQjoMSrMOTRfNumS4vI=" + }, + "raw-body": { + "version": "2.1.6", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.1.6.tgz", + "integrity": "sha1-nAUHN/4HztbZSk/QnGG2rYdNMQ8=", + "requires": { + "bytes": "2.3.0", + "iconv-lite": "0.4.13", + "unpipe": "1.0.0" + } + } + } + }, + "micro-cors": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/micro-cors/-/micro-cors-0.1.1.tgz", + "integrity": "sha512-6WqIahA5sbQR1Gjexp1VuWGFDKbZZleJb/gy1khNGk18a6iN1FdTcr3Q8twaxkV5H94RjxIBjirYbWCehpMBFw==" + }, "mimic-response": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-2.1.0.tgz", @@ -413,6 +467,11 @@ "once": "^1.3.1" } }, + "querystring": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", + "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" + }, "raw-body": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.3.2.tgz", diff --git a/server/package.json b/server/package.json index e2936aa..fc5178b 100644 --- a/server/package.json +++ b/server/package.json @@ -11,6 +11,9 @@ "dependencies": { "async-to-gen": "^1.4.0", "better-sqlite3": "^6.0.1", - "micro": "^9.3.4" + "micro": "^9.3.4", + "micro-core": "^0.4.0", + "micro-cors": "^0.1.1", + "querystring": "^0.2.0" } }