-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
115 lines (99 loc) · 5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const { getEnvValue, getTradeSettings, onHttps, setTradeSettings } = require("../Firebase");
const { saveBacktestSettingsToBuckets } = require("../Firebase/storage")
const { getBacktestResults, recordOptimalTradeSettingResult } = require("../Farnsworth")
const { makeInternalRequest } = require("../helpers/fetch")
const { round } = require("../helpers/numbers")
const { log, logError } = require("../helpers/log")
const { internalHeaderAuthKey, functionsEndpoint } = require("../constants");
exports.backtesetingSetProductionOptimalTradeSettings = () => {
return onHttps(async (req, res) => {
let internal = req.get("Internal-Auth");
if (internal && internal === internalHeaderAuthKey) {
let currency = req.body.currency
let exchange = req.body.exchange
let pairedAsset = req.body.pairedAsset
try {
let mostOptimal = await _getBacktestOptimalTradeSettings(currency, exchange, pairedAsset)
if (!mostOptimal.settings) {
throw Error(`Settings not found is mostOptimal object: ${mostOptimal}`)
}
let body = { currency, exchange, pairedAsset, mostOptimal }
let setOptimalProdEndpoint = functionsEndpoint + "/fenderSetOptimalSettings"
await makeInternalRequest(setOptimalProdEndpoint, body)
res.json(mostOptimal);
} catch (e) {
logError({ title: `Error with Fender setOptimalTradeSettings`, message: e.message, details: e.stack });
res.sendStatus(404);
}
} else {
res.sendStatus(404);
}
})
}
exports.setOptimalTradeSettingsProd = () => {
return onHttps(async (req, res) => {
let internal = req.get("Internal-Auth");
if (internal && internal === internalHeaderAuthKey) {
let currency = req.body.currency
let exchange = req.body.exchange
let pairedAsset = req.body.pairedAsset
let mostOptimal = req.body.mostOptimal
try {
let savedOptimalProd = await _setOptimalTradeSettings(mostOptimal, currency, exchange, pairedAsset)
res.json(savedOptimalProd);
} catch (e) {
logError({ title: `Error with Fender setOptimalTradeSettings`, message: e.message, details: e.stack });
res.sendStatus(404);
}
} else {
res.sendStatus(404);
}
})
}
exports.getOptimalTradeSettings = () => {
return onHttps(async (req, res) => {
let internal = req.get("Internal-Auth");
if (internal && internal === internalHeaderAuthKey) {
let currency = req.body.currency
let exchange = req.body.exchange
let pairedAsset = req.body.pairedAsset
try {
let isProd = getEnvValue === "prod" ? true : false
let result = await getTradeSettings(isProd, currency, exchange, pairedAsset)
res.json(result);
} catch (e) {
logError({ title: `Error with Fender getOptimalTradeSettings`, message: e.message, details: e.stack });
res.sendStatus(404);
}
} else {
res.sendStatus(404);
}
})
}
const _getBacktestOptimalTradeSettings = async (currency, exchange, pairedAsset) => {
let allResults = await getBacktestResults(currency, pairedAsset, exchange)
let mostOptimal = allResults.reduce((prev, current) => {
if (Number(current.alpha) >= Number(prev.alpha)) {
return current
} else {
return prev
}
}, allResults[0])
log({
title: `🤖 Fender update ${currency}-${pairedAsset} ${exchange} trade settings`,
message: `Most optimal with *alpha: ${round(mostOptimal.alpha)} %*, return on WC: ${round(mostOptimal.percentReturnWC)} %, # trades: ${mostOptimal.numberOfTrades}, ID: ${mostOptimal.id}`,
details: JSON.stringify(mostOptimal.settings),
overrideQuietMode: true,
postToSlack: true
})
await saveBacktestSettingsToBuckets(exchange, currency, pairedAsset, allResults)
return mostOptimal
}
const _setOptimalTradeSettings = async (mostOptimal, currency, exchange, pairedAsset) => {
let isProd = getEnvValue === "prod" ? true : false
await setTradeSettings(isProd, currency, exchange, pairedAsset, mostOptimal.settings).catch(e => { throw Error(`Error setting trade settings for ${exchange}-${pairedAsset}: ${e.message}`)})
// For now we set it manually... need to work out a solution for converting USD <-> EUR prices for backup entries
await setTradeSettings(isProd, currency, "coinbaseEUR", "EUR", mostOptimal.settings).catch(e => { throw Error(`Error setting trade settings for ${exchange}-${pairedAsset}: ${e.message}`) })
await recordOptimalTradeSettingResult(mostOptimal).catch(e => { throw Error(`Error recording most optimal trade settings result: ${JSON.stringify(mostOptimal)}: ${e.message}`) })
return mostOptimal
}