@@ -9,114 +9,135 @@ const CBPublicClient = require('./coinbase/CBPublicClient')
9
9
const MathFunctions = require ( './MathFunctions' )
10
10
11
11
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 ++ ) {
46
39
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
+ )
49
44
}
50
45
catch ( e ) { console . log ( `Caught Error --> ${ e } ` ) }
46
+ }
51
47
52
48
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 )
55
53
54
+ myOrders = await CBAuthClient . t_getOrders ( )
55
+ //console.log('myOrders:', myOrders)
56
56
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
108
101
}
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 )
110
108
}
111
- else { console . log ( `Will NOT buy @ ${ currentPrice . price } ` ) }
109
+ } )
110
+
112
111
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
+ */
118
129
}
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)
119
137
}
120
138
121
139
// [EXPORT] //
122
- module . exports = Algo
140
+ // [EXPORT] //
141
+ module . exports = {
142
+ algo,
143
+ }
0 commit comments