Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. class functions
  2. @align: (key, value, length = 0, char = '\u00A0') ->
  3. key = '' + key
  4. value = '' + value
  5. char = '' + char
  6. space = Math.ceil (length - (key.length + value.length)) / char.length
  7. _.times(space, -> key += char)
  8. key + value
  9. @stats: (id, current_price, current_assets, current_currency, initial_price, initial_assets, initial_currency, p_curr_gain, ticks, period, lastAction) ->
  10. start_assets = (initial_currency/initial_price) + initial_assets
  11. start_currency = (initial_assets * initial_price) + initial_currency
  12. assets = (current_currency/current_price) + current_assets
  13. currency = (current_assets * current_price) + current_currency
  14.  
  15. pairs = id.toUpperCase().split '_'
  16. info [functions.align('|Uber Snail Bot v3.6', '' ,25) + ' |']
  17. debug [functions.align('|Day', (ticks * period/1440).toFixed(0), 25) + ' |' + functions.align('Last Action ', lastAction, 25) + '|']
  18. debug [functions.align('|Initial Price ', initial_price.toFixed(6),25) + ' |' + functions.align('Current Price ', current_price.toFixed(6),25) + '|']
  19. debug [functions.align('|Equity ' + pairs[1], currency.toFixed(2),25) + ' |' + functions.align('Start ', start_currency.toFixed(2),25) + '|']
  20. debug [functions.align('|Equity ' + pairs[0], assets.toFixed(2),25) + ' |' + functions.align('Start ', start_assets.toFixed(2),25) + '|']
  21. if p_curr_gain.toFixed(2) < 0
  22. warn [functions.align('|Profit ', p_curr_gain.toFixed(2) + '%',25) + ' |']
  23. else
  24. info [functions.align('|Profit ', p_curr_gain.toFixed(2) + '%',25) + ' |']
  25. @roundDown: (value, places) ->
  26. offset = Math.pow(10, places)
  27. return Math.floor(value*offset)/offset
  28. @buy: (instrument, split, timeout, price, curr, assets, n = 0) ->
  29. assetLimit = assets+curr/price
  30. minimum = 0.05*price
  31. amount = functions.roundDown(assetLimit/split, 4)
  32. debug "Beginning to buy #{assetLimit.toFixed(2)} in #{split} orders"
  33. buying = true
  34. while buying == true
  35. if curr >= 1.1*amount*price
  36. n++
  37. debug "Iceberg order #{n}"
  38. tradeamount = functions.roundDown(((0.9+0.2*Math.random())*amount),4)
  39. buy(instrument, tradeamount, null, timeout)
  40. curr = portfolio.positions[instrument.curr()].amount
  41. if curr < minimum
  42. debug "Last order"
  43. buy instrument
  44. buying = false
  45. n = 0
  46. debug "Finished Buying"
  47. else
  48. debug "Last Order"
  49. buy instrument
  50. buying = false
  51. n = 0
  52. debug "Finished Buying"
  53. @sell: (instrument, split, timeout, price, curr, assets, n = 0) ->
  54. assetLimit = assets+curr/price
  55. minimum = 0.05
  56. amount = functions.roundDown(assetLimit/split, 4)
  57. debug "Beginning to sell #{assetLimit.toFixed(3)} in #{split} orders"
  58. selling = true
  59. while selling == true
  60. if assets >= 1.1*amount
  61. n++
  62. debug "Iceberg order #{n}"
  63. tradeamount = functions.roundDown(((0.9+0.2*Math.random())*amount),4)
  64. sell(instrument, tradeamount, null, timeout)
  65. assets = portfolio.positions[instrument.asset()].amount
  66. if assets < minimum
  67. debug "Last order"
  68. sell instrument
  69. selling = false
  70. n = 0
  71. debug "Finished Selling"
  72. else
  73. debug "Last order"
  74. sell instrument
  75. selling = false
  76. n = 0
  77. debug "Finished Selling"
  78. init: (context) ->
  79. context.buyTreshold = 0.18
  80. context.sellTreshold = 0.25
  81. context.calcTicks = 0
  82. context.lastAction = 'None'
  83. handle: (context, data, storage) ->
  84. instrument = data.instruments[0]
  85.  
  86. short = instrument.ema(56)
  87. long = instrument.ema(84)
  88.  
  89. current_price = instrument.price
  90. current_assets = portfolio.positions[instrument.asset()].amount
  91. current_currency = portfolio.positions[instrument.curr()].amount
  92.  
  93. storage.initial_price ?= instrument.price
  94. storage.initial_assets ?= current_assets
  95. storage.initial_currency ?= current_currency
  96. storage.start_curr ?= (current_currency+current_assets*current_price)
  97. storage.curr_lim = (current_currency+current_assets*current_price)
  98. storage.p_curr_gain = ((storage.curr_lim/storage.start_curr-1)*100)
  99.  
  100. diff = 100 * (short - long) / ((short + long) / 2)
  101. if diff > context.buyTreshold && current_currency >= 0.01
  102. functions.buy instrument, 5, 30, current_price, current_currency, current_assets
  103. context.lastAction = 'Buy'
  104. else
  105. if diff < -context.sellTreshold && current_currency < 0.01
  106. functions.sell instrument, 5, 30, current_price, current_currency, current_assets
  107. context.lastAction = 'Sell'
  108. functions.stats(instrument.id, current_price, current_assets, current_currency, storage.initial_price, storage.initial_assets, storage.initial_currency, storage.p_curr_gain, context.calcTicks, instrument.period, context.lastAction)
  109.  
  110. context.calcTicks++
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement