Skip to content

Commit 3cd57ec

Browse files
author
aleem ahmed
committed
.
1 parent 590b5b9 commit 3cd57ec

File tree

5 files changed

+135
-106
lines changed

5 files changed

+135
-106
lines changed

client/Algo.js

+118-97
Original file line numberDiff line numberDiff line change
@@ -9,114 +9,135 @@ const CBPublicClient = require('./coinbase/CBPublicClient')
99
const MathFunctions = require('./MathFunctions')
1010

1111

12-
class Algo {
13-
// [ALGO] Average //
14-
static async algo(product_id, tradeAmount) {
15-
// If no params passed set Default
16-
if (!product_id) product_id = 'ETH-USD'
17-
18-
// [INIT] //
19-
const timeFrames = [60, 300, 1800, 3600, 43200, 86400]
20-
const grossProfitMarginPct = 0.02
21-
const minimumInterval = 0.002 // smallest interval we’ll trade at
22-
const moderateInterval = 0.004 // moderate interval we’ll trade at
23-
const maximumInterval = 0.008 // largest interval we’ll trade at
24-
25-
26-
// [INIT] To Be Determined //
27-
let timeFramePriceAvgs = []
28-
let currentPrice = {}
29-
let myOrders = []
30-
let grossProfitMarginPrice = 0
31-
let count = 0
32-
let currentInterval = 0
33-
34-
// [GET] Average(s) //
35-
for (let i = 0; i < timeFrames.length; i++) {
36-
try {
37-
timeFramePriceAvgs[i] = await MathFunctions.getAverage(
38-
product_id,
39-
timeFrames[i]
40-
)
41-
}
42-
catch(e) { console.log(`Caught Error --> ${e}`) }
43-
}
44-
45-
// [GET] currentPrice // [GET] myOrders //
12+
// [ALGO] Average //
13+
async function algo(product_id, tradeAmount) {
14+
// [SET-PRODUCT-ID] If no params passed set Default
15+
if (!product_id) product_id = 'ETH-USD'
16+
17+
18+
// [INIT-CONST] //
19+
const timeFrames = [60, 300, 1800, 3600, 43200, 86400]
20+
const grossProfitMarginPct = 0.02
21+
const minimumInterval = 0.002 // smallest interval we’ll trade at
22+
const moderateInterval = 0.004 // moderate interval we’ll trade at
23+
const maximumInterval = 0.008 // largest interval we’ll trade at
24+
25+
26+
// [INIT] To Be Determined //
27+
let timeFramePriceAvgs = []
28+
let currentPrice = {}
29+
let myOrders = []
30+
let myFills = []
31+
let currentSellPrice = null
32+
let count = 0
33+
let currentInterval = 0
34+
let alreadyBought = false
35+
36+
37+
// [GET] Average(s) //
38+
for (let i = 0; i < timeFrames.length; i++) {
4639
try {
47-
currentPrice = await CBPublicClient.t_getProductTicker(product_id)
48-
myOrders = await CBAuthClient.t_getOrders()
40+
timeFramePriceAvgs[i] = await MathFunctions.getAverage(
41+
product_id,
42+
timeFrames[i]
43+
)
4944
}
5045
catch(e) { console.log(`Caught Error --> ${e}`) }
46+
}
5147

5248

53-
// [ARITHMETIC] grossProfitMarginPrice //
54-
grossProfitMarginPrice = currentPrice.price * (1 + grossProfitMarginPct)
49+
// [GET] currentPrice // [GET] myOrders //
50+
try {
51+
currentPrice = await CBPublicClient.t_getProductTicker(product_id)
52+
console.log('currentPrice:', currentPrice.price)
5553

54+
myOrders = await CBAuthClient.t_getOrders()
55+
//console.log('myOrders:', myOrders)
5656

57-
// [ARITHMETIC] Determine Count //
58-
timeFramePriceAvgs.forEach(timeFramePriceAvg => {
59-
if (timeFramePriceAvg.average > currentPrice.price) {
60-
count = count + 1
61-
}
62-
})
63-
64-
// [ARITHMETIC] Determine Interval //
65-
if (count <= 1) { currentInterval = maximumInterval }
66-
else if (count <= 4) { currentInterval = moderateInterval }
67-
else if (count <= 6) { currentInterval = minimumInterval }
68-
else { console.log(`Error Count Is: ${count}`) }
69-
70-
let something = false
71-
myOrders.forEach(myOrder => {
72-
if (myOrder.product_id == product_id) {
73-
// [INIT] //
74-
const currentSellPrice = currentPrice.price * (1 + grossProfitMarginPct)
75-
const topPriceRange = currentSellPrice * (1 + currentInterval)
76-
const bottomPriceRange = (currentSellPrice * (currentInterval - 1) * -1)
77-
78-
// [LOG] //
79-
console.log('grossProfitMarginPct:', grossProfitMarginPct)
80-
console.log('currentSellPrice:', currentSellPrice)
81-
console.log('topPriceRange:', topPriceRange)
82-
console.log('bottomPriceRange:', bottomPriceRange)
83-
console.log('myOrder.price:', myOrder.price)
84-
85-
// if ANY of the orders are within the range
86-
if (
87-
myOrder.price <= topPriceRange &&
88-
myOrder.price >= bottomPriceRange
89-
) { something = true }
90-
else { something = false }
91-
}
92-
})
93-
94-
if (something == true) {
95-
// Try to execute the trade
96-
console.log(`Will buy @ ${currentPrice.price}`)
97-
98-
try {
99-
/*
100-
await CBAuthClient.t_placeOrder(
101-
'Buy',
102-
null,
103-
null,
104-
product_id,
105-
tradeAmount
106-
)
107-
*/
57+
myFills = await CBAuthClient.t_getFills(product_id)
58+
//console.log('myFills:', myFills)
59+
}
60+
catch(e) { console.log(`Caught Error --> ${e}`) }
61+
62+
63+
// [ARITHMETIC] currentSellPrice //
64+
currentSellPrice = currentPrice.price * (1 + grossProfitMarginPct)
65+
console.log('currentSellPrice:', currentSellPrice)
66+
67+
68+
// [ARITHMETIC] Determine Count //
69+
timeFramePriceAvgs.forEach(timeFramePriceAvg => {
70+
if (timeFramePriceAvg.average > currentPrice.price) {
71+
count = count + 1
72+
}
73+
74+
console.log(
75+
'timeframe:', timeFramePriceAvg.timeFrame,
76+
'average:', timeFramePriceAvg.average
77+
)
78+
})
79+
80+
81+
// [COUNT][ARITHMETIC] Determine Interval //
82+
if (count <= 1) { currentInterval = maximumInterval }
83+
else if (count <= 4) { currentInterval = moderateInterval }
84+
else if (count <= 6) { currentInterval = minimumInterval }
85+
else { console.log(`Error Count Is: ${count}`) }
86+
console.log('count:', count)
87+
88+
89+
90+
// ??? [DETERMIN] if we already bought //
91+
myOrders.forEach(myOrder => {
92+
if (myOrder.product_id == product_id) {
93+
// [INIT] //
94+
const topPriceRange = currentSellPrice * (1 + currentInterval)
95+
const bottomPriceRange = (currentSellPrice * (currentInterval - 1) * -1)
96+
97+
// if ANY of the orders are within the range set alreadyBought
98+
// bottom <= myO <= top
99+
if (myOrder.price >= bottomPriceRange && myOrder.price <= topPriceRange) {
100+
alreadyBought = true
108101
}
109-
catch (e) { console.log(`Trade Execution Caught Error --> ${e}`) }
102+
else { alreadyBought = false }
103+
104+
// [LOG] //
105+
console.log('topPriceRange:', topPriceRange)
106+
console.log('bottomPriceRange:', bottomPriceRange)
107+
console.log('myOrder.price:', myOrder.price)
110108
}
111-
else { console.log(`Will NOT buy @ ${currentPrice.price}`) }
109+
})
110+
112111

113-
// [LOG] //
114-
//console.log('timeFramePriceAvgs:', timeFramePriceAvgs)
115-
//console.log('currentPrice:', currentPrice.price)
116-
//console.log('myOrders:', myOrders)
117-
//console.log('count:', count)
112+
// [] //
113+
if (alreadyBought == false) {
114+
// Try to execute the trade
115+
console.log(`Will buy @ ${currentPrice.price}`)
116+
117+
/*
118+
try {
119+
await CBAuthClient.t_placeOrder(
120+
'Buy',
121+
null,
122+
null,
123+
product_id,
124+
tradeAmount
125+
)
126+
}
127+
catch (e) { console.log(`Trade Execution Caught Error --> ${e}`) }
128+
*/
118129
}
130+
else { console.log(`Will NOT buy @ ${currentPrice.price}`) }
131+
132+
// [LOG] //
133+
//console.log('timeFramePriceAvgs:', timeFramePriceAvgs)
134+
//console.log('currentPrice:', currentPrice.price)
135+
//console.log('myOrders:', myOrders)
136+
//console.log('count:', count)
119137
}
120138

121139
// [EXPORT] //
122-
module.exports = Algo
140+
// [EXPORT] //
141+
module.exports = {
142+
algo,
143+
}

client/coinbase/CBAuthClient.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class CBAuthClient {
3939
product_id: product_id,
4040
}
4141

42-
authedClient.buy(buyParams)
42+
try { return await authedClient.buy(buyParams) }
43+
catch (e) { console.log(`"t_buy" Caught Error --> ${e}`) }
4344
}
4445

4546
// [SELL] //
@@ -50,7 +51,8 @@ class CBAuthClient {
5051
product_id: product_id,
5152
}
5253

53-
authedClient.sell(buyParams)
54+
try { return await authedClient.sell(buyParams) }
55+
catch (e) { console.log(`"t_buy" Caught Error --> ${e}`) }
5456
}
5557

5658

@@ -64,14 +66,22 @@ class CBAuthClient {
6466
}
6567

6668
try { return await authedClient.placeOrder(params) }
67-
catch(e) { console.log(`Caught Error --> ${e}`) }
69+
catch(e) { console.log(`"t_placeOrder" Caught Error --> ${e}`) }
6870
}
6971

7072

7173
// [GET-ORDERS] //
7274
static async t_getOrders() {
7375
try { return await authedClient.getOrders() }
74-
catch(e) { console.log(`Caught Error --> ${e}`) }
76+
catch(e) { console.log(`"t_getOrders" Caught Error --> ${e}`) }
77+
}
78+
79+
// [GET-FILLS]
80+
static async t_getFills(product_id) {
81+
const params = { product_id: product_id }
82+
83+
try { return await authedClient.getFills(params) }
84+
catch(e) { console.log(`"t_getFills" Caught Error --> ${e}`) }
7585
}
7686
}
7787

client/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const Algo = require('./Algo')
99

1010

1111

12-
1312
// [TRADING-BOT-START] //
1413
tradeAmount = 50
1514
Algo.algo(null, tradeAmount)

client/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"dependencies": {
1212
"coinbase-pro": "^0.9.0",
1313
"dotenv": "^8.2.0",
14-
"mongodb": "^3.5.9",
15-
"prompt-sync": "^4.2.0"
14+
"mongodb": "^3.5.9"
1615
},
1716
"devDependencies": {
1817
"nodemon": "^2.0.4"

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"description": "automated coinbase trader",
55
"main": "index.js",
66
"scripts": {
7-
"client": "nodemon index"
7+
"dev": "nodemon server"
88
},
99
"author": "Aleem Ahmed, Gregory P. Kahanec",
1010
"license": "MIT",
1111
"dependencies": {
1212
"coinbase-pro": "^0.9.0",
1313
"dotenv": "^8.2.0",
14-
"mongodb": "^3.5.9"
14+
"mongoose": "^5.9.26"
1515
},
1616
"devDependencies": {
1717
"nodemon": "^2.0.4"

0 commit comments

Comments
 (0)