Maltured StochRSI/MACD/MFI LTC BTC-e 118% Mod4c7 15-30


SUBMITTED BY: Guest

DATE: Sept. 19, 2014, 12:22 p.m.

FORMAT: Text only

SIZE: 19.3 kB

HITS: 865

  1. ###########################################################
  2. # #
  3. # Maltured LTC BTC-e 118% Mod4c7 15-30 #
  4. # BTC: 1J44iFpW3AsyuZ7SRPsDG7HCbPTKg7MwY1 #
  5. # ow.ly/BFxyo #
  6. # #
  7. ###########################################################
  8. ###########################################################
  9. # #
  10. # Based on Sungkhum StochRSI/MACD/MFI Bot 1.3 30 min #
  11. # BTC: 1Pu3a4kNEPYiszh8xFv3x7JqWrpbDs28XK #
  12. # #
  13. # Using Thanasis full working framework and Emergency bot #
  14. # Thanasis BTC: 1CRSH4LGGRWVWqgwfquky2Rk5eJK6gsyus #
  15. # #
  16. ###########################################################
  17. class Init
  18. ########################################
  19. #### define basic constants ############
  20. @init_context: (context) ->
  21. ###### Serialized constants - don't touch bellow ###########
  22. context.price_buy = 0
  23. context.price_sell = 0
  24. context.first_price = 0
  25. context.number_of_orders = 0
  26. context.have_fiat = null
  27. context.have_coins = null
  28. context.commisions_paid = 0
  29. context.price_of_last_order = null
  30. context.last_action_was = null
  31. context.prevrsi_d = [10, 10, 10, 10]
  32. context.prevrsi_k = [30, 30, 30, 30]
  33. context.prevmacd = [0, 0, 0, 0]
  34. context.sellcount = 1
  35. context.selltrue = false
  36. context.buytrue = false
  37. context.KrsiZero = false
  38. context.last_time = [0, 0]
  39. #################################################################################
  40. class functions
  41. ########################################
  42. #### basic functions #############
  43. @diff: (x, y) ->
  44. (( x - y ) / (( x + y ) / 2)) * 100
  45. @percent: (x,y) ->
  46. (( x - y ) / y) * 100
  47. @norm_growth: (a,x) ->
  48. 1 - Math.exp( - a * x )
  49. ########################################
  50. ####### indicatots #####################
  51. @mfi: (high, low, close, volume,lag, period) ->
  52. results = talib.MFI
  53. high: high
  54. low: low
  55. close: close
  56. volume: volume
  57. startIdx: 0
  58. endIdx: high.length - lag
  59. optInTimePeriod: period
  60. _.last(results)
  61. @macd: (data, lag, FastPeriod,SlowPeriod,SignalPeriod) ->
  62. results = talib.MACD
  63. inReal: data
  64. startIdx: 0
  65. endIdx: data.length - lag
  66. optInFastPeriod: FastPeriod
  67. optInSlowPeriod: SlowPeriod
  68. optInSignalPeriod: SignalPeriod
  69. result =
  70. macd: _.last(results.outMACD)
  71. signal: _.last(results.outMACDSignal)
  72. histogram: _.last(results.outMACDHist)
  73. result
  74. @stochrsi: (data, lag, period, fastK_period,fastD_period,fastD_MAType) ->
  75. results = talib.STOCHRSI
  76. inReal: data
  77. startIdx: 0
  78. endIdx: data.length - lag
  79. optInTimePeriod: period
  80. optInFastK_Period: fastK_period
  81. optInFastD_Period: fastD_period
  82. optInFastD_MAType: fastD_MAType
  83. result =
  84. K: _.last(results.outFastK)
  85. D: _.last(results.outFastD)
  86. result
  87. #################################################################################
  88. init: (context) ->
  89. context.fee = 0.2
  90. context.lag = 20
  91. context.period = 57
  92. context.FastPeriod = 54
  93. context.SlowPeriod = 143
  94. context.MAType = 7
  95. context.SignalPeriod = 132
  96. context.fastK_period = 71
  97. context.slowK_period = 233
  98. context.slowK_MAType = 10
  99. context.slowD_period = 219
  100. context.slowD_MAType = 11
  101. context.fastD_period = 249
  102. context.fastD_MAType = 1
  103. context.mystochK = 248
  104. context.myzero = 0
  105. context.mfi_HIGH = false
  106. context.mfi_LOW = false
  107. context.percent_emergency_sell = 62
  108. context.percent_recovery_buy = 19
  109. context.percent_emergency_buy = 51
  110. context.percent_recovery_sell = 31
  111. context.lagemergency = 30
  112. Init.init_context(context)
  113. #################################################################################
  114. serialize: (context)->
  115. price_of_last_order = context.price_of_last_order
  116. context.price_buy = context.price_buy
  117. context.price_sell = context.price_sell
  118. number_of_orders = context.number_of_orders
  119. have_fiat = context.have_fiat
  120. have_coins = context.have_coins
  121. first_price = context.first_price
  122. first_price_found = context.first_price_found
  123. first_capital = context.first_capital
  124. commisions_paid = context.commisions_paid
  125. last_action_was = context.last_action_was
  126. #################################################################################
  127. handle: (context, data)->
  128. ########################################
  129. ####### define instrument ##############
  130. instrument = data.instruments[0]
  131. price = instrument.close[instrument.close.length - 1]
  132. open = instrument.open[instrument.open.length - 1]
  133. high = instrument.high[instrument.high.length - 1]
  134. low = instrument.low[instrument.low.length - 1]
  135. close = instrument.close[instrument.close.length - 1]
  136. volume = instrument.volumes[instrument.volumes.length - 1]
  137. price_lag = instrument.close[instrument.close.length - context.lag]
  138. open_lag = instrument.open[instrument.open.length - context.lag]
  139. high_lag = instrument.high[instrument.high.length - context.lag]
  140. low_lag = instrument.low[instrument.low.length - context.lag]
  141. close_lag = instrument.close[instrument.close.length - context.lag]
  142. volume_lag = instrument.volumes[instrument.volumes.length - context.lag]
  143. emergency_price_lag = instrument.close[instrument.close.length - context.lagemergency]
  144. emergency_open_lag = instrument.open[instrument.open.length - context.lagemergency]
  145. emergency_high_lag = instrument.high[instrument.high.length - context.lagemergency]
  146. emergency_low_lag = instrument.low[instrument.low.length - context.lagemergency]
  147. emergency_close_lag = instrument.close[instrument.close.length - context.lagemergency]
  148. emergency_volume_lag = instrument.volumes[instrument.volumes.length - context.lagemergency]
  149. #period_in_minutes = (instrument.ticks[2].at - instrument.ticks[1].at) / 60000
  150. ########################################
  151. ##### calculate capital ################
  152. unless context.first_price_found
  153. context.first_price = price
  154. fiat = portfolio.positions[instrument.curr()].amount
  155. coins = portfolio.positions[instrument.asset()].amount
  156. context.first_capital = fiat + coins * price
  157. last_action_was = null
  158. maximum = Math.max(fiat, coins * price)
  159. if maximum = fiat
  160. context.have_fiat = true
  161. context.have_coins = false
  162. else
  163. context.have_fiat = false
  164. context.have_coins = true
  165. context.first_price_found = true
  166. fiat = portfolio.positions[instrument.curr()].amount
  167. coins = portfolio.positions[instrument.asset()].amount
  168. capital = fiat + coins * price
  169. ########################################
  170. ##### define basic functions ###########
  171. #high_low_percent = functions.percent(high, low)
  172. percent_price_change = functions.percent(price, emergency_price_lag)
  173. ########################################
  174. ##### define indicator functions #######
  175. mfi = functions.mfi(instrument.high, instrument.low, instrument.close, instrument.volumes, context.lag,context.period)
  176. macd = functions.macd(instrument.close, context.lag, context.FastPeriod,context.SlowPeriod,context.SignalPeriod)
  177. stochrsi = functions.stochrsi(instrument.close,context.lag,context.period,context.fastK_period,context.fastD_period,context.fastD_MAType)
  178. ###########################################
  179. ###### debug indicators ################
  180. #debug "#{macd.macd} #{macd.signal} #{macd.histogram}"
  181. #warn "#{stochrsi.K}#{stochrsi.D} "
  182. ########################################
  183. ###### plot signals ################
  184. ########################################
  185. ###### plot signals ################
  186. bottom = 750
  187. plot
  188. macd: (macd.macd * 15) + 850
  189. "80%": bottom + 80
  190. "20%": bottom + 20
  191. K: stochrsi.K + bottom
  192. D: stochrsi.D + bottom
  193. mfi: (mfi * 2) + 600
  194. ##################################################
  195. ###### emergency main calculations ###############
  196. percent_emergency_sell2 = context.percent_emergency_sell + (context.percent_recovery_buy - context.percent_emergency_sell) * (1 / 2)
  197. percent_emergency_buy2 = context.percent_emergency_buy + (context.percent_recovery_sell - context.percent_emergency_buy) * (1 / 2)
  198. price_emergency_sell = emergency_price_lag * (1 - context.percent_emergency_sell / 100)
  199. price_emergency_sell2 = emergency_price_lag * (1 - percent_emergency_sell2 / 100)
  200. price_recovery_buy = emergency_price_lag * (1 - context.percent_recovery_buy / 100)
  201. price_emergency_buy = emergency_price_lag * (1 + context.percent_emergency_buy / 100)
  202. price_emergency_buy2 = emergency_price_lag * (1 + percent_emergency_buy2 / 100)
  203. price_recovery_sell = emergency_price_lag * (1 + context.percent_recovery_sell / 100)
  204. emergency_buy_signal = off
  205. emergency_sell_signal = off
  206. if (price_emergency_sell2 < price < price_emergency_sell) or price > price_recovery_sell
  207. emergency_buy_signal = off
  208. emergency_sell_signal = on
  209. if (price_emergency_buy < price < price_emergency_buy2) or price < price_recovery_buy
  210. emergency_buy_signal = on
  211. emergency_sell_signal = off
  212. ##################################################
  213. ###### Emergency buy or sell orders ##########
  214. if emergency_buy_signal
  215. if context.have_fiat
  216. buy instrument
  217. context.number_of_orders = context.number_of_orders + 1
  218. context.price_buy = price
  219. context.commisions_paid = context.commisions_paid + instrument.fee * capital / 100
  220. context.price_of_last_order = context.price_buy
  221. context.last_action_was = "buy"
  222. context.have_coins = true
  223. context.have_fiat = false
  224. warn "Emergency Buy"
  225. if emergency_sell_signal
  226. if context.have_coins
  227. sell instrument
  228. context.number_of_orders = context.number_of_orders + 1
  229. context.price_sell = price
  230. context.commisions_paid = context.commisions_paid + instrument.fee * capital / 100
  231. context.price_of_last_order = context.price_sell
  232. context.last_action_was = "sell"
  233. context.have_coins = false
  234. context.have_fiat = true
  235. warn "Emergency Sell"
  236. ########################################
  237. ###### Get Ready signals ################
  238. context.mystochK = stochrsi.K
  239. ########################################
  240. ###### More Ready signals ################
  241. if context.mystochK < 1
  242. context.KrsiZero = true
  243. ########################################
  244. ###### Get Past data ################
  245. context.prevrsi_d.push(stochrsi.D)
  246. context.prevrsi_d.shift()
  247. context.prevrsi_k.push(stochrsi.K)
  248. context.prevrsi_k.shift()
  249. context.prevmacd.push(macd.macd)
  250. context.prevmacd.shift()
  251. macdprev1 = functions.macd(instrument.close, 2, context.FastPeriod,context.SlowPeriod,context.SignalPeriod)
  252. macdprev0 = functions.macd(instrument.close, 0, context.FastPeriod,context.SlowPeriod,context.SignalPeriod)
  253. stochrsiprev1 = functions.stochrsi(instrument.close,2,context.period,context.fastK_period,context.fastD_period,context.fastD_MAType)
  254. ########################################
  255. ###### buy or sell Strategy #########
  256. if mfi > 70
  257. context.mfi_HIGH = true
  258. context.mfi_LOW = false
  259. #warn "MFI HIGH"
  260. if mfi < 30
  261. context.mfi_LOW = true
  262. context.mfi_HIGH = false
  263. #warn "MFI LOW"
  264. if stochrsiprev1.D < stochrsiprev1.K and stochrsi.D > stochrsi.K and context.mfi_HIGH
  265. context.selltrue = true
  266. context.sellcount = (context.sellcount + 1)
  267. context.buytrue = false
  268. context.mfi_HIGH = false
  269. context.mfi_LOW = false
  270. #warn "SELL is TRUE"
  271. if context.KrsiZero and stochrsiprev1.D > stochrsiprev1.K and stochrsi.D < stochrsi.K and context.mfi_LOW
  272. context.buytrue = true
  273. context.selltrue = false
  274. context.mfi_LOW = false
  275. context.mfi_HIGH = false
  276. context.sellcount = 1
  277. #warn "BUY is TRUE"
  278. if stochrsi.D > stochrsi.K
  279. context.buytrue = false
  280. if context.buytrue and (macd.macd - macdprev1.macd) > 1 and stochrsi.D > 50 and stochrsi.D < stochrsi.K ## you must write here your own condition to buy
  281. ###### don't touch bellow #####
  282. if context.have_fiat
  283. buy instrument
  284. context.number_of_orders = context.number_of_orders + 1
  285. context.price_buy = price
  286. context.commisions_paid = context.commisions_paid + context.fee * capital / 100
  287. context.price_of_last_order = context.price_buy
  288. context.last_action_was = "buy"
  289. context.have_coins = true
  290. context.have_fiat = false
  291. context.KrsiZero = false
  292. context.selltrue = false
  293. context.buytrue = false
  294. context.sellcount = 1
  295. #warn "MACD for BUY >1: "+(macd.macd - context.prevmacd[2])
  296. if context.selltrue and macdprev1.macd > macd.macd and context.sellcount < 6 and stochrsi.D > stochrsi.K ##and (macdprev1.macd - macd.macd) > 0.4 you must write here your own condition to sell
  297. ###### don't touch bellow #####
  298. if context.have_coins
  299. sell instrument
  300. context.number_of_orders = context.number_of_orders + 1
  301. context.price_sell = price
  302. context.commisions_paid = context.commisions_paid + context.fee * capital / 100
  303. context.price_of_last_order = context.price_sell
  304. context.last_action_was = "sell"
  305. context.have_coins = false
  306. context.have_fiat = true
  307. context.KrsiZero = false
  308. context.buytrue = false
  309. context.selltrue = false
  310. context.sellcount = 1
  311. #warn "MACD for sell > 0.5: "+(context.prevmacd[2] - macd.macd)
  312. #########################################
  313. ###### recalculate capital ############
  314. fiat = portfolio.positions[instrument.curr()].amount
  315. coins = portfolio.positions[instrument.asset()].amount
  316. capital = fiat + coins * price
  317. if context.have_fiat
  318. efficiency_of_last_trade = -functions.percent(price, context.price_of_last_order)
  319. else
  320. efficiency_of_last_trade = functions.percent(price, context.price_of_last_order)
  321. percent_buy_and_hold = Math.round(100 * functions.percent(price, context.first_price))/100
  322. percent_bot = Math.round(100 * functions.percent(capital, context.first_capital))/100
  323. efficiency_of_last_trade = Math.round(100 * efficiency_of_last_trade)/100
  324. context.commisions_paid = Math.round(100 * context.commisions_paid)/100
  325. context.first_capital = Math.round(100 * context.first_capital)/100
  326. capital = Math.round(100 * capital)/100
  327. ############################################
  328. ###### debug stats ######################
  329. #warn "###########################################################"
  330. #warn "price now: #{price}"
  331. #warn "last action was '#{context.last_action_was}' at the price of: #{context.price_of_last_order}"
  332. #debug "smartness of the bot from the last trade: #{efficiency_of_last_trade} %"
  333. #debug "total number of buy/sell orders: #{context.number_of_orders}"
  334. #debug "total commisions paid by now: #{context.commisions_paid}"
  335. #warn "start capital: #{context.first_capital}"
  336. #warn "capital now: #{capital}"
  337. #warn "total buy and hold efficiency: #{percent_buy_and_hold} %"
  338. #warn "total bot efficiency: #{percent_bot} % "
  339. #warn "MACD Didn't BUY >1: "+(macd.macd - macdprev1.macd)
  340. #warn "MACD Didn't SELL >0.5 "+(macdprev1.macd - macd.macd)
  341. #warn " MACD: "+macd.macd+" "
  342. #warn "MACD Prev: "+macdprev1.macd
  343. #warn "StochK: "+stochrsi.K+" "
  344. #warn " StochK Prev:"+context.prevrsi_k[2]
  345. #warn " StochD: "+stochrsi.D+" "
  346. #warn "StochD Prev: "+context.prevrsi_d[2]
  347. #warn "MACD "+macd.macd+" StochK "+stochrsi.K+" StochD: "+stochrsi.D+" "+price
  348. #warn "MFI: "+mfi+" MFI HIGH="+context.mfi_HIGH+" MFI LOW="+context.mfi_LOW
  349. #warn "###########################################################"
  350. #warn "MACD at 0: "+macdprev0.macd
  351. #warn "LAST Date: "+new Date(data.at)+" Curr DATE: "+new Date(data.at)
  352. #warn "StochK Prev: "+stochrsiprev1.K
  353. #warn "MACD: "+macd.macd+" StochD: "+stochrsi.D+" StochK: "+stochrsi.K+" MFI: "+mfi

comments powered by Disqus