-
Notifications
You must be signed in to change notification settings - Fork 11
/
sungkhumrsi4.coffee
352 lines (243 loc) · 11.6 KB
/
sungkhumrsi4.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
###########################################################
# #
# Sungkhum StochRSI/MACD Bot 4.0 2h #
# BTC: 1Pu3a4kNEPYiszh8xFv3x7JqWrpbDs28XK #
# #
# Using Thanasis full working framework #
# Thanasis BTC: 1CRSH4LGGRWVWqgwfquky2Rk5eJK6gsyus #
# #
###########################################################
class Init
########################################
#### define basic constants ############
@init_context: (context) ->
context.fee = 0.2
context.lag = 1
context.period = 20
context.FastPeriod = 10
context.SlowPeriod = 21
context.MAType = 3
context.SignalPeriod = 9
context.fastK_period = 3
context.slowK_period = 14
context.slowK_MAType = 3
context.slowD_period = 14
context.slowD_MAType = 3
context.fastD_period = 3
context.fastD_MAType = 3
context.mystochK = 100
context.myzero = 0
###### Serialized constants - don't touch bellow ###########
context.price_buy = 0
context.price_sell = 0
context.first_price = 0
context.number_of_orders = 0
context.have_fiat = null
context.have_coins = null
context.commisions_paid = 0
context.price_of_last_order = null
context.last_action_was = null
context.prevrsi_d = [10, 10, 10, 10]
context.prevrsi_k = [30, 30, 30, 30]
context.prevmacd = [0, 0, 0, 0]
context.sellcount = 1
context.selltrue = false
context.buytrue = false
context.KrsiZero = false
#################################################################################
class functions
########################################
#### basic functions #############
@diff: (x, y) ->
(( x - y ) / (( x + y ) / 2)) * 100
@percent: (x,y) ->
(( x - y ) / y) * 100
@norm_growth: (a,x) ->
1 - Math.exp( - a * x )
########################################
####### indicatots #####################
@macd: (data, lag, FastPeriod,SlowPeriod,SignalPeriod) ->
results = talib.MACD
inReal: data
startIdx: 0
endIdx: data.length - lag
optInFastPeriod: FastPeriod
optInSlowPeriod: SlowPeriod
optInSignalPeriod: SignalPeriod
result =
macd: _.last(results.outMACD)
signal: _.last(results.outMACDSignal)
histogram: _.last(results.outMACDHist)
result
@stochrsi: (data, lag, period, fastK_period,fastD_period,fastD_MAType) ->
results = talib.STOCHRSI
inReal: data
startIdx: 0
endIdx: data.length - lag
optInTimePeriod: period
optInFastK_Period: fastK_period
optInFastD_Period: fastD_period
optInFastD_MAType: fastD_MAType
result =
K: _.last(results.outFastK)
D: _.last(results.outFastD)
result
#################################################################################
init: (context) ->
Init.init_context(context)
#################################################################################
serialize: (context)->
price_of_last_order = context.price_of_last_order
context.price_buy = context.price_buy
context.price_sell = context.price_sell
number_of_orders = context.number_of_orders
have_fiat = context.have_fiat
have_coins = context.have_coins
first_price = context.first_price
first_price_found = context.first_price_found
first_capital = context.first_capital
commisions_paid = context.commisions_paid
last_action_was = context.last_action_was
#################################################################################
handle: (context, data)->
########################################
####### define instrument ##############
instrument = data.instruments[0]
price = instrument.close[instrument.close.length - 1]
open = instrument.open[instrument.open.length - 1]
high = instrument.high[instrument.high.length - 1]
low = instrument.low[instrument.low.length - 1]
close = instrument.close[instrument.close.length - 1]
volume = instrument.volumes[instrument.volumes.length - 1]
price_lag = instrument.close[instrument.close.length - context.lag]
open_lag = instrument.open[instrument.open.length - context.lag]
high_lag = instrument.high[instrument.high.length - context.lag]
low_lag = instrument.low[instrument.low.length - context.lag]
close_lag = instrument.close[instrument.close.length - context.lag]
volume_lag = instrument.volumes[instrument.volumes.length - context.lag]
########################################
##### calculate capital ################
unless context.first_price_found
context.first_price = price
fiat = portfolio.positions[instrument.curr()].amount
coins = portfolio.positions[instrument.asset()].amount
context.first_capital = fiat + coins * price
last_action_was = null
maximum = Math.max(fiat, coins * price)
if maximum = fiat
context.have_fiat = true
context.have_coins = false
else
context.have_fiat = false
context.have_coins = true
context.first_price_found = true
fiat = portfolio.positions[instrument.curr()].amount
coins = portfolio.positions[instrument.asset()].amount
capital = fiat + coins * price
########################################
##### define basic functions ###########
#high_low_percent = functions.percent(high, low)
########################################
##### define indicator functions #######
macd = functions.macd(instrument.close, context.lag, context.FastPeriod,context.SlowPeriod,context.SignalPeriod)
stochrsi = functions.stochrsi(instrument.close,context.lag,context.period,context.fastK_period,context.fastD_period,context.fastD_MAType)
###########################################
###### debug indicators ################
#debug "#{macd.macd} #{macd.signal} #{macd.histogram}"
#warn "#{stochrsi.K}#{stochrsi.D} "
########################################
###### Get Ready signals ################
context.mystochK = stochrsi.K
########################################
###### More Ready signals ################
if context.mystochK < 1
context.KrsiZero = true
########################################
###### Store Past signals ################
context.prevrsi_d.push(stochrsi.D)
context.prevrsi_d.shift()
context.prevrsi_k.push(stochrsi.K)
context.prevrsi_k.shift()
context.prevmacd.push(macd.macd)
context.prevmacd.shift()
########################################
###### buy or sell Strategy #########
if (context.prevrsi_d[2] < context.prevrsi_k[2] and stochrsi.D > stochrsi.K)
context.selltrue = true
context.sellcount = (context.sellcount + 1)
context.buytrue = false
#warn "SELL is TRUE"
if context.KrsiZero and context.prevrsi_d[2] > context.prevrsi_k[2] and stochrsi.D < stochrsi.K
context.buytrue = true
context.selltrue = false
context.sellcount = 1
#warn "BUY is TRUE"
if context.buytrue and (macd.macd - context.prevmacd[2]) > 1 and stochrsi.D > 50 and stochrsi.D < stochrsi.K ## you must write here your own condition to buy
###### don't touch bellow #####
if context.have_fiat
buy instrument
context.number_of_orders = context.number_of_orders + 1
context.price_buy = price
context.commisions_paid = context.commisions_paid + context.fee * capital / 100
context.price_of_last_order = context.price_buy
context.last_action_was = "buy"
context.have_coins = true
context.have_fiat = false
context.KrsiZero = false
context.selltrue = false
context.buytrue = false
context.sellcount = 1
#warn "MACD for BUY >1: "+(macd.macd - context.prevmacd[2])
if context.selltrue and context.prevmacd[2] > macd.macd and context.sellcount < 6 and stochrsi.D > stochrsi.K ##and (context.prevmacd[2] - macd.macd) > 0.4 you must write here your own condition to sell
###### don't touch bellow #####
if context.have_coins
sell instrument
context.number_of_orders = context.number_of_orders + 1
context.price_sell = price
context.commisions_paid = context.commisions_paid + context.fee * capital / 100
context.price_of_last_order = context.price_sell
context.last_action_was = "sell"
context.have_coins = false
context.have_fiat = true
context.KrsiZero = false
context.buytrue = false
context.selltrue = false
context.sellcount = 1
#warn "MACD for sell > 0.5: "+(context.prevmacd[2] - macd.macd)
#########################################
###### recalculate capital ############
fiat = portfolio.positions[instrument.curr()].amount
coins = portfolio.positions[instrument.asset()].amount
capital = fiat + coins * price
if context.have_fiat
efficiency_of_last_trade = -functions.percent(price, context.price_of_last_order)
else
efficiency_of_last_trade = functions.percent(price, context.price_of_last_order)
percent_buy_and_hold = Math.round(100 * functions.percent(price, context.first_price))/100
percent_bot = Math.round(100 * functions.percent(capital, context.first_capital))/100
efficiency_of_last_trade = Math.round(100 * efficiency_of_last_trade)/100
context.commisions_paid = Math.round(100 * context.commisions_paid)/100
context.first_capital = Math.round(100 * context.first_capital)/100
capital = Math.round(100 * capital)/100
############################################
###### debug stats ######################
#warn "###########################################################"
#warn "price now: #{price}"
debug "last action was '#{context.last_action_was}' at the price of: #{context.price_of_last_order}"
debug "smartness of the bot from the last trade: #{efficiency_of_last_trade} %"
debug "total number of buy/sell orders: #{context.number_of_orders}"
debug "total commisions paid by now: #{context.commisions_paid}"
#warn "start capital: #{context.first_capital}"
#warn "capital now: #{capital}"
#warn "total buy and hold efficiency: #{percent_buy_and_hold} %"
#warn "total bot efficiency: #{percent_bot} % "
#warn "MACD Didn't BUY >1: "+(macd.macd - context.prevmacd[2])
#warn "MACD Didn't SELL >0.5 "+(context.prevmacd[2] - macd.macd)
#warn " MACD: "+macd.macd+" "
#warn "MACD Prev: "+context.prevmacd[2]
#warn "StochK: "+stochrsi.K+" "
#warn " StochK Prev:"+context.prevrsi_k[2]
#warn " StochD: "+stochrsi.D+" "
#warn "StochD Prev: "+context.prevrsi_d[2]
#warn "MACD "+macd.macd+" StochK "+stochrsi.K+" StochD: "+stochrsi.D+" "+price
#warn "###########################################################"