added save and retrieve STATE

This commit is contained in:
artin 2023-07-26 11:27:22 +00:00
parent c71679b850
commit d204e94330

View File

@ -77,6 +77,21 @@ function processTP() {
}
}
async function saveState() {
await client.query(`insert into osapi_state (account_state,prices, state_time) values ($1, now())`, [STATE, PRICE]);
}
async function loadState() {
let res = await client.query(`select account_state from osapi_state order by state_time desc limit 1`);
if (result.rows.length !== 0) {
STATE = res['rows'][0];
}
}
loadState()
async function getTransactionsAll(account, acct_id, api_key) {
for (const rg of [...Array(32).keys()]) {
try {
@ -518,12 +533,12 @@ app.get("/order/:instrument/:quantity", async (req, res) => {
if (req.params.instrument.includes("JPY")) {
delta = 0.12;
}
if (typeof(STATE[account]) === 'undefined') {
STATE[account] = {};
}
if (typeof(STATE[account][req.params.instrument]) === 'undefined') {
STATE[account][req.params.instrument] = {};
}
if (typeof(STATE[account]) === 'undefined') {
STATE[account] = {};
}
if (typeof(STATE[account][req.params.instrument]) === 'undefined') {
STATE[account][req.params.instrument] = {};
}
STATE[account][req.params.instrument]["base"] =
response["data"]["orderFillTransaction"]["price"] + delta;
STATE[account][req.params.instrument]["TP"] = false;
@ -532,10 +547,10 @@ app.get("/order/:instrument/:quantity", async (req, res) => {
response["data"]["orderFillTransaction"]["tradeOpened"]["tradeID"];
if (req.params.quantity > 0) {
STATE[account][req.params.instrument]["watch"] = "ask";
STATE[account][req.params.instrument]["trigger"] = (response["data"]["orderFillTransaction"]["price"] + delta) * 1.05;
STATE[account][req.params.instrument]["trigger"] = (response["data"]["orderFillTransaction"]["price"] + delta) * 1.05;
} else {
STATE[account][req.params.instrument]["watch"] = "bid";
STATE[account][req.params.instrument]["trigger"] = (response["data"]["orderFillTransaction"]["price"] + delta) * 0.95;
STATE[account][req.params.instrument]["trigger"] = (response["data"]["orderFillTransaction"]["price"] + delta) * 0.95;
}
}
}
@ -549,13 +564,12 @@ app.listen(port, host, () => {
console.log(`osapi started`);
});
const response = await fetch(
"https://stream-fxpractice.oanda.com/v3/accounts/101-001-8005237-001/pricing/stream?instruments=GBP_CAD%2CNZD_CAD%2CEUR_CHF%2CEUR_CAD%2CNZD_CHF%2CCHF_JPY%2CUSD_CHF%2CAUD_JPY%2CEUR_USD%2CNZD_USD%2CUSD_JPY%2CGBP_AUD%2CEUR_AUD%2CCAD_JPY%2CEUR_GBP%2CAUD_CAD%2CEUR_JPY%2CAUD_CHF%2CCAD_CHF%2CGBP_JPY%2CUSD_CAD%2CNZD_JPY%2CUSD_SGD%2CAUD_USD%2CGBP_CHF%2CAUD_NZD%2CGBP_USD",
{
"https://stream-fxpractice.oanda.com/v3/accounts/101-001-8005237-001/pricing/stream?instruments=GBP_CAD%2CNZD_CAD%2CEUR_CHF%2CEUR_CAD%2CNZD_CHF%2CCHF_JPY%2CUSD_CHF%2CAUD_JPY%2CEUR_USD%2CNZD_USD%2CUSD_JPY%2CGBP_AUD%2CEUR_AUD%2CCAD_JPY%2CEUR_GBP%2CAUD_CAD%2CEUR_JPY%2CAUD_CHF%2CCAD_CHF%2CGBP_JPY%2CUSD_CAD%2CNZD_JPY%2CUSD_SGD%2CAUD_USD%2CGBP_CHF%2CAUD_NZD%2CGBP_USD", {
method: "GET",
headers: {
Authorization:
"Bearer e88218d201bd344c2dc3c469f8f8d1f3-e77504680a17f51f0baecf9dababa40b",
Authorization: "Bearer e88218d201bd344c2dc3c469f8f8d1f3-e77504680a17f51f0baecf9dababa40b",
},
}
);
@ -581,10 +595,11 @@ try {
bid: x["bids"][0]["price"],
};
}
} catch (e) { }
} catch (e) {}
}
} catch (err) {
console.error(err.stack);
}
setTimeout(processTP, 1500);
setTimeout(saveState, 60000);