Guest User

Untitled

a guest
Jul 19th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #importing libraries
  2. from kucoin.client import Client
  3. import json
  4. import math
  5. import time
  6. import datetime
  7.  
  8.  
  9. #API KEYS to the exchange
  10. api_key = "xxxxxx"
  11. api_secret = "xxxx"
  12.  
  13. #instantiate client
  14. apiclient = Client(api_key, api_secret)
  15.  
  16. #set buyin and selling price
  17. pricebuy = 0.00000029
  18. pricesell = 0.00000030
  19.  
  20. #tickers to trade
  21. ticker1 = 'CV'
  22. ticker2 = 'BTC'
  23.  
  24. #Each exchange can have different fees
  25. fee_perc = 0.1
  26.  
  27. #Saving logs
  28. def saveLog(logmessage):
  29. with open('/Users/jc_admin/Documents/GitHub/Bittrex/log', 'a') as file:
  30. file.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + " ")
  31. file.write(logmessage +"\n")
  32. file.close()
  33.  
  34. return
  35.  
  36. #main function
  37. def mainf():
  38.  
  39. saveLog("Running\n")
  40. balance_json1 = apiclient.get_coin_balance(ticker1)
  41. balance_json2 = apiclient.get_coin_balance(ticker2)
  42.  
  43. coin_available1 = truncate(balance_json1["balance"],2)
  44. coin_available2 = balance_json2["balance"]
  45.  
  46. print(coin_available1)
  47. print(coin_available2)
  48.  
  49. #checks if we have minimum amount of coins to sell (100 is arbitrary, change it to your number)
  50. if coin_available1 > 100:
  51. #selling
  52. message = "Setting sell order for: " + str(ticker1)
  53. print(message)
  54. saveLog(message)
  55.  
  56. #executing sell order
  57. apiclient.create_sell_order("CV-BTC", pricesell, coin_available1)
  58.  
  59. #Checks if we have enough to buy
  60. if coin_available2 > 0:
  61. #buing
  62. message = "Setting buy order for: " + str(ticker1)
  63. print(message)
  64. saveLog(message)
  65. buy = calcBuy(coin_available2,pricebuy)
  66. if buy > 100:
  67. #executing buy order
  68. apiclient.create_buy_order("CV-BTC",pricebuy, buy)
  69.  
  70. return
  71.  
  72. #calcuates how
  73. def calcBuy(available,price):
  74. gross = available/price
  75. fee = fee_perc/100.0
  76. net = gross * (1 - fee)
  77. print(net)
  78. rounded = math.floor(net)
  79.  
  80. print(rounded)
  81. return rounded
  82.  
  83. #for continuos execution
  84. def update():
  85. while True:
  86. mainf()
  87. time.sleep(30 * 60)
  88.  
  89. #APIs might have different number formats/precision
  90. def truncate(f, n):
  91. return math.floor(f * 10 ** n) / 10 ** n
  92.  
  93.  
  94. mainf() #one time execution
  95. #update() #uncomment to have continuos execution
Add Comment
Please, Sign In to add comment