-
Notifications
You must be signed in to change notification settings - Fork 60
/
trader.coffee
164 lines (156 loc) · 5.21 KB
/
trader.coffee
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
_ = require 'underscore'
logger = require 'winston'
vm = require 'vm'
Fiber = require 'fibers'
inspect = require('./utils').inspect
Instrument = require './instrument'
talib = require './talib_sync'
class Trader
constructor: (@name,@config,@account,@script)->
@sandbox =
_:_
talib: talib
portfolio:
positions: {}
debug: (message)->
logger.verbose message
info: (message)->
logger.info message
warn: (message)->
logger.info message
buy: (instrument,amount,cb)=>
@trade
asset: instrument.asset()
curr: instrument.curr()
platform: instrument.platform
type: 'buy'
amount: amount
,cb
sell: (instrument,amount,cb)=>
@trade
asset: instrument.asset()
curr: instrument.curr()
platform: instrument.platform
type: 'sell'
amount: amount
,cb
sendEmail: (text)->
logger.verbose 'Sending e-mail '+text
# @TODO add send email functionality
@script = vm.runInNewContext @script, @sandbox, @name
_.extend @sandbox,@script
platformCls = require('./platforms/'+config.platform)
platform = new platformCls()
try
platform.init config.platforms[config.platform],config.instrument,@account
catch e
logger.error e.message
process.exit 1
@data = {}
instrument = new Instrument(platform,@config.instrument)
@data[config.instrument] = instrument
@data.instruments = [instrument]
@context = {}
@sandbox.init @context
updateTicker: (platform,cb)->
platform.getTicker (err,ticker)=>
if err?
logger.error err
else
logger.verbose "updateTicker: #{inspect(ticker)}"
@ticker = ticker
cb()
updatePortfolio: (positions,platform,cb)->
platform.getPositions positions,(err, result)=>
if err?
logger.error err
else
logger.verbose "updatePortfolio: #{inspect(result)}"
for curr,amount of result
@sandbox.portfolio.positions[curr] =
amount:amount
cb()
calcPositions: (pair)->
asset = pair[0]
curr = pair[1]
amount = @sandbox.portfolio.positions[asset].amount
result = "#{amount} #{asset.toUpperCase()} "
if @ticker?
result += "(#{amount*@ticker.sell} #{curr.toUpperCase()}) "
cash = @sandbox.portfolio.positions[curr].amount
result += "#{cash} #{curr.toUpperCase()}"
result
trade: (order,cb)->
platform = order.platform
switch order.type
when 'buy'
order.price = @ticker.buy
order.maxAmount = order.amount or @sandbox.portfolio.positions[order.curr].amount / order.price
break
when 'sell'
order.price = @ticker.sell
order.maxAmount = order.amount or @sandbox.portfolio.positions[order.asset].amount
break
platform.trade order, (err,orderId)=>
if err?
logger.info err
return
self = @
if orderId
orderStr = "##{orderId} "
else
orderStr = ''
orderCb = ->
self.updatePortfolio [order.asset,order.curr], order.platform,(err)=>
unless err?
balance = self.calcPositions [order.asset,order.curr]
message = "#{order.type.toUpperCase()} order #{orderStr}traded. Balance: #{balance}"
self.sandbox.info message
if cb?
cb()
if orderId
switch order.type
when 'buy'
amount = order.amount or @sandbox.portfolio.positions[order.curr].amount / order.price
logger.info "BUY order ##{orderId} amount: #{amount} #{order.asset.toUpperCase()} @ #{order.price}"
break
when 'sell'
amount = order.amount or @sandbox.portfolio.positions[order.asset].amount
logger.info "SELL order ##{orderId} amount: #{amount} #{order.asset.toUpperCase()} @ #{order.price}"
break
setTimeout =>
platform.isOrderActive orderId,(err,active)=>
if err?
logger.error err
if active
logger.info "Canceling order ##{orderId} as it was inactive for #{@config.check_order_interval} seconds."
platform.cancelOrder orderId, (err)=>
if err?
logger.error err
else
logger.info "Creating new order.."
@updateTicker platform,=>
@updatePortfolio [order.asset,order.curr], order.platform,=>
@trade order, cb
else
orderCb()
,@config.check_order_interval*1000
else
orderCb()
init: (bars)->
instrument = @data[@config.instrument]
for bar in bars
instrument.update bar
@updatePortfolio instrument.pair,instrument.platform, =>
balance = @calcPositions instrument.pair
logger.info "Trader initialized successfully. Starting balance: #{balance}"
handle: (bar)->
instrument = @data[bar.instrument]
instrument.update bar
@data.at = bar.at
@updateTicker instrument.platform, =>
@updatePortfolio instrument.pair,instrument.platform, =>
Fiber =>
@sandbox.handle @context, @data
.run()
module.exports = Trader