Skip to content
This repository was archived by the owner on Feb 15, 2022. It is now read-only.

Commit b0b7c92

Browse files
T-ravDeviaVir
authored andcommitted
Cherry picked master branch changes into unstable branch (#1112)
1 parent 7fde22f commit b0b7c92

File tree

3 files changed

+225
-113
lines changed

3 files changed

+225
-113
lines changed

lib/engine.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = function container (get, set, clear) {
1818
s.product_id = so.selector.product_id
1919
s.asset = so.selector.asset
2020
s.currency = so.selector.currency
21+
s.asset_capital = 0
2122

2223
if (typeof so.period_length == 'undefined')
2324
so.period_length = so.period
@@ -192,18 +193,19 @@ module.exports = function container (get, set, clear) {
192193
s.exchange.getBalance({currency: s.currency, asset: s.asset}, function (err, balance) {
193194
if (err) return cb(err)
194195
s.balance = balance
195-
if (!s.start_capital) {
196-
s.exchange.getQuote({product_id: s.product_id}, function (err, quote) {
197-
if (err) return cb(err)
196+
s.exchange.getQuote({product_id: s.product_id}, function (err, quote) {
197+
if (err) return cb(err)
198+
199+
if (!s.start_capital) {
198200
s.start_price = n(quote.ask).value()
199201
s.start_capital = n(s.balance.currency).add(n(s.balance.asset).multiply(quote.ask)).value()
200202

201203
pushMessage('Balance ' + s.exchange.name.toUpperCase(), 'sync balance ' + s.start_capital + ' ' + s.currency + '\n')
202-
203-
cb()
204-
})
205-
}
206-
else cb()
204+
}
205+
206+
s.asset_capital = n(s.balance.asset).multiply(quote.ask).value()
207+
cb()
208+
})
207209
})
208210
}
209211

@@ -435,11 +437,14 @@ module.exports = function container (get, set, clear) {
435437
if (!size) {
436438
if (so.mode === 'live' || so.mode === 'paper') {
437439
var buy_pct = so.buy_pct
438-
if(so.buy_max_amt){
439-
var buy_max_as_pct = n(so.buy_max_amt).divide(s.balance.currency).multiply(100)
440-
if(buy_max_as_pct < buy_pct){
441-
buy_pct = buy_max_as_pct
442-
}
440+
if(so.buy_max_amt){ // account for held assets as buy_max
441+
var adjusted_buy_max_amt = n(so.buy_max_amt).subtract(s.asset_capital).value()
442+
var buy_max_as_pct = n(adjusted_buy_max_amt).divide(s.balance.currency).multiply(100).value()
443+
buy_pct = buy_max_as_pct
444+
}else{ // account for held assets as %
445+
var held_pct = n(s.asset_capital).divide(s.balance.currency).multiply(100).value()
446+
var to_buy_pct = n(so.buy_pct).subtract(held_pct).value()
447+
buy_pct = to_buy_pct
443448
}
444449
if (so.order_type === 'maker') {
445450
size = n(s.balance.currency).multiply(buy_pct).divide(100).multiply(s.exchange.makerFee / 100).format('0.00000000')

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/lib/engine.test.js

Lines changed: 206 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,227 @@
11
describe("Engine", function() {
22
describe("executeSignal", function() {
3-
describe("when maker", function(){
4-
it("with buy_max_amt less than buy_pct amount should use buy_max_amt", function(){
5-
// arrange
6-
var signal_type = "buy"
7-
var currency_amount = 1
8-
var buy_pct = 50
9-
var buy_max_amt = 0.25
10-
var order_type = "maker"
11-
var buy_spy = jasmine.createSpy()
12-
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, buy_spy)
13-
// act
14-
sut.executeSignal(signal_type)
15-
// assert
16-
var expected = "2.77500000"
17-
var buyArgs = buy_spy.calls.mostRecent().args[0]
18-
expect(buyArgs.size).toBe(expected)
3+
describe("when maker in live mode", function(){
4+
describe("with buy_max set", function(){
5+
it("and no held assets should use raw buy_max_amt", function(){
6+
// arrange
7+
var signal_type = "buy"
8+
var currency_amount = 1.0
9+
var buy_pct = 50
10+
var buy_max_amt = 0.25
11+
var order_type = "maker"
12+
var held_asset = 0
13+
var buy_spy = jasmine.createSpy()
14+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
15+
// act
16+
sut.executeSignal(signal_type)
17+
// assert
18+
var expected = "2.77500000"
19+
var buyArgs = buy_spy.calls.mostRecent().args[0]
20+
expect(buyArgs.size).toBe(expected)
21+
})
22+
it("and held assets should use adjusted buy_max_amt", function(){
23+
// arrange
24+
var signal_type = "buy"
25+
var currency_amount = 3.0
26+
var buy_pct = 50
27+
var buy_max_amt = 0.25
28+
var order_type = "maker"
29+
var held_asset = 0.75
30+
var buy_spy = jasmine.createSpy()
31+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
32+
// act
33+
sut.executeSignal(signal_type)
34+
// assert
35+
var expected = "1.85925000"
36+
var buyArgs = buy_spy.calls.mostRecent().args[0]
37+
expect(buyArgs.size).toBe(expected)
38+
})
39+
it("and held assets so large adjusted buy_max_amt is below order minimum should not place order", function(){
40+
// arrange
41+
var signal_type = "buy"
42+
var currency_amount = 1.0
43+
var buy_pct = 50
44+
var buy_max_amt = 0.25
45+
var order_type = "maker"
46+
var held_asset = 2.0
47+
var buy_spy = jasmine.createSpy()
48+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
49+
// act
50+
sut.executeSignal(signal_type)
51+
// assert
52+
expect(buy_spy).not.toHaveBeenCalled()
53+
})
1954
})
20-
21-
it("with buy_max_amt more than buy_pct amount should use buy_pct", function(){
22-
// arrange
23-
var signal_type = "buy"
24-
var currency_amount = 1
25-
var buy_pct = 50
26-
var buy_max_amt = 0.75
27-
var order_type = "maker"
28-
var buy_spy = jasmine.createSpy()
29-
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, buy_spy)
30-
// act
31-
sut.executeSignal(signal_type)
32-
// assert
33-
var expected = "5.55000000"
34-
var buyArgs = buy_spy.calls.mostRecent().args[0]
35-
expect(buyArgs.size).toBe(expected)
36-
})
37-
38-
it("with buy_max_amt equals buy_pct amount should use buy_pct", function(){
39-
// arrange
40-
var signal_type = "buy"
41-
var currency_amount = 1
42-
var buy_pct = 50
43-
var buy_max_amt = 0.50
44-
var order_type = "maker"
45-
var buy_spy = jasmine.createSpy()
46-
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, buy_spy)
47-
// act
48-
sut.executeSignal(signal_type)
49-
// assert
50-
var expected = "5.55000000"
51-
var buyArgs = buy_spy.calls.mostRecent().args[0]
52-
expect(buyArgs.size).toBe(expected)
55+
describe("with no buy_max set", function(){
56+
it("and no held assets should use raw buy_pct", function(){
57+
// arrange
58+
var signal_type = "buy"
59+
var currency_amount = 1.0
60+
var buy_pct = 50
61+
var buy_max_amt = undefined
62+
var order_type = "maker"
63+
var held_asset = 0
64+
var buy_spy = jasmine.createSpy()
65+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
66+
// act
67+
sut.executeSignal(signal_type)
68+
// assert
69+
var expected = "5.55000000"
70+
var buyArgs = buy_spy.calls.mostRecent().args[0]
71+
expect(buyArgs.size).toBe(expected)
72+
})
73+
it("and held assets should use adjusted buy_pct", function(){
74+
// arrange
75+
var signal_type = "buy"
76+
var currency_amount = 1.0
77+
var buy_pct = 50
78+
var buy_max_amt = undefined
79+
var order_type = "maker"
80+
var held_asset = 0.5
81+
var buy_spy = jasmine.createSpy()
82+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
83+
// act
84+
sut.executeSignal(signal_type)
85+
// assert
86+
var expected = "4.93950000"
87+
var buyArgs = buy_spy.calls.mostRecent().args[0]
88+
expect(buyArgs.size).toBe(expected)
89+
})
90+
it("and held assets so large adjusted buy_pct is below order minimum should not place order", function(){
91+
// arrange
92+
var signal_type = "buy"
93+
var currency_amount = 1.0
94+
var buy_pct = 50
95+
var buy_max_amt = undefined
96+
var order_type = "maker"
97+
var held_asset = 5.25
98+
var buy_spy = jasmine.createSpy()
99+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
100+
// act
101+
sut.executeSignal(signal_type)
102+
// assert
103+
expect(buy_spy).not.toHaveBeenCalled()
104+
})
53105
})
54106
})
55107

56-
describe("when taker", function(){
57-
it("with buy_max_amt less than buy_pct amount should use buy_max_amt", function(){
58-
// arrange
59-
var signal_type = "buy"
60-
var currency_amount = 1
61-
var buy_pct = 50
62-
var buy_max_amt = 0.25
63-
var order_type = "taker"
64-
var buy_spy = jasmine.createSpy()
65-
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, buy_spy)
66-
// act
67-
sut.executeSignal(signal_type)
68-
// assert
69-
var expected = "2.77222222"
70-
var buyArgs = buy_spy.calls.mostRecent().args[0]
71-
expect(buyArgs.size).toBe(expected)
72-
})
73-
74-
it("with buy_max_amt more than buy_pct amount should use buy_pct", function(){
75-
// arrange
76-
var signal_type = "buy"
77-
var currency_amount = 1
78-
var buy_pct = 50
79-
var buy_max_amt = 0.75
80-
var order_type = "taker"
81-
var buy_spy = jasmine.createSpy()
82-
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, buy_spy)
83-
// act
84-
sut.executeSignal(signal_type)
85-
// assert
86-
var expected = "5.54444444"
87-
var buyArgs = buy_spy.calls.mostRecent().args[0]
88-
expect(buyArgs.size).toBe(expected)
108+
describe("when taker in live mode", function(){
109+
describe("with buy_max_amt set",function(){
110+
it("and no held assets should use raw buy_max_amt", function(){
111+
// arrange
112+
var signal_type = "buy"
113+
var currency_amount = 1
114+
var buy_pct = 50
115+
var buy_max_amt = 0.25
116+
var order_type = "taker"
117+
var held_asset = 0
118+
var buy_spy = jasmine.createSpy()
119+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
120+
// act
121+
sut.executeSignal(signal_type)
122+
// assert
123+
var expected = "2.77222222"
124+
var buyArgs = buy_spy.calls.mostRecent().args[0]
125+
expect(buyArgs.size).toBe(expected)
126+
})
127+
128+
it("and held assets should use adjusted buy_max_amt", function(){
129+
// arrange
130+
var signal_type = "buy"
131+
var currency_amount = 3.0
132+
var buy_pct = 50
133+
var buy_max_amt = 0.25
134+
var order_type = "taker"
135+
var held_asset = 0.75
136+
var buy_spy = jasmine.createSpy()
137+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
138+
// act
139+
sut.executeSignal(signal_type)
140+
// assert
141+
var expected = "1.85738888"
142+
var buyArgs = buy_spy.calls.mostRecent().args[0]
143+
expect(buyArgs.size).toBe(expected)
144+
})
145+
146+
it("and held assets so large adjusted buy_max_amt is below order minimum should not place order", function(){
147+
// arrange
148+
var signal_type = "buy"
149+
var currency_amount = 1.0
150+
var buy_pct = 50
151+
var buy_max_amt = 0.25
152+
var order_type = "taker"
153+
var held_asset = 2.0
154+
var buy_spy = jasmine.createSpy()
155+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
156+
// act
157+
sut.executeSignal(signal_type)
158+
// assert
159+
expect(buy_spy).not.toHaveBeenCalled()
160+
})
89161
})
90-
91-
it("with buy_max_amt equals buy_pct amount should use buy_pct", function(){
92-
// arrange
93-
var signal_type = "buy"
94-
var currency_amount = 1
95-
var buy_pct = 50
96-
var buy_max_amt = 0.50
97-
var order_type = "taker"
98-
var buy_spy = jasmine.createSpy()
99-
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, buy_spy)
100-
// act
101-
sut.executeSignal(signal_type)
102-
// assert
103-
var expected = "5.54444444"
104-
var buyArgs = buy_spy.calls.mostRecent().args[0]
105-
expect(buyArgs.size).toBe(expected)
162+
describe("with no buy_max_amt set",function(){
163+
it("with no buy_max_amt set and no held assets should use raw buy_pct", function(){
164+
// arrange
165+
var signal_type = "buy"
166+
var currency_amount = 1
167+
var buy_pct = 50
168+
var buy_max_amt = undefined
169+
var order_type = "taker"
170+
var held_asset = 0
171+
var buy_spy = jasmine.createSpy()
172+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
173+
// act
174+
sut.executeSignal(signal_type)
175+
// assert
176+
var expected = "5.54444444"
177+
var buyArgs = buy_spy.calls.mostRecent().args[0]
178+
expect(buyArgs.size).toBe(expected)
179+
})
180+
it("and held assets should use adjusted buy_pct", function(){
181+
// arrange
182+
var signal_type = "buy"
183+
var currency_amount = 1.0
184+
var buy_pct = 50
185+
var buy_max_amt = undefined
186+
var order_type = "taker"
187+
var held_asset = 0.5
188+
var buy_spy = jasmine.createSpy()
189+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
190+
// act
191+
sut.executeSignal(signal_type)
192+
// assert
193+
var expected = "4.93455555"
194+
var buyArgs = buy_spy.calls.mostRecent().args[0]
195+
expect(buyArgs.size).toBe(expected)
196+
})
197+
it("and held assets so large adjusted buy_pct is below order minimum should not place order", function(){
198+
// arrange
199+
var signal_type = "buy"
200+
var currency_amount = 1.0
201+
var buy_pct = 50
202+
var buy_max_amt = undefined
203+
var order_type = "taker"
204+
var held_asset = 5.25
205+
var buy_spy = jasmine.createSpy()
206+
var sut = createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy)
207+
// act
208+
sut.executeSignal(signal_type)
209+
// assert
210+
expect(buy_spy).not.toHaveBeenCalled()
211+
})
106212
})
107213
})
108214
})
109215
})
110216

111-
function createEngine(currency_amount, buy_pct, buy_max_amt, order_type, buy_spy){
217+
function createEngine(currency_amount, buy_pct, buy_max_amt, order_type, held_asset, buy_spy){
112218
var fake_asset = "test_asset"
113219
var fake_currency = "BTC"
114220
var fake_exchange = "test_exchange"
115221
var fake_project = "test_product"
116222
var fake_bid = 0.10
117223
var fake_ask = 0.11
118-
var fake_balance = { currency: currency_amount, asset:0}
224+
var fake_balance = { currency: currency_amount, asset:held_asset}
119225

120226
var fakes = {
121227
get: function() { },

0 commit comments

Comments
 (0)