Guest User

Untitled

a guest
Jan 12th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. import os
  2. import sys
  3. import time
  4. import config
  5. import argparse
  6. import threading
  7.  
  8. from BinanceAPI import *
  9.  
  10. client = BinanceAPI(config.api_key, config.api_secret)
  11.  
  12. parser = argparse.ArgumentParser()
  13. # parser.add_argument("--quantity", type=int, help="Buy/Sell Quantity", default=100)
  14. parser.add_argument("--coin", type=str, help="Market Symbol (Ex: XVG)", required=True)
  15. parser.add_argument("--basecoin", type=str, help="Market Symbol (Ex: BTC)", default="ETH")
  16. parser.add_argument("--percentFall", type=float, help="% Percent to which the value has to fluncate before action is taken.", default=3)
  17. parser.add_argument("--actionTime", type=float, help="Seconds between buy/sell attempts", default=0.1)
  18. # parser.add_argument("--loopTime", type=float, help="Seconds for loop between value checking", default=0.5)
  19.  
  20. option = parser.parse_args()
  21.  
  22. # _quantity = option.quantity
  23. _coin = option.coin
  24. _basecoin = option.basecoin
  25. _percentFall = option.percentFall
  26. _actionTime = option.actionTime
  27. # _loopTime = option.loopTime
  28.  
  29.  
  30.  
  31. def getBalance(symbol):
  32. balance = client.get_account()["balances"]
  33. specBalance = balance['balances']
  34. for index, item in enumerate(specBalance):
  35. if item["asset"] == symbol : # coin to trade
  36. return item["free"]
  37.  
  38. def getMinimumRule(symbol):
  39. if symbol == "BTC" :
  40. return 0.002
  41. # ADD MORE!
  42.  
  43. def buy():
  44. while (1):
  45. order = client.buy_market(_coin + _basecoin, getBalance(_basecoin))
  46. if 'msg' in order:
  47. message("Error: " + order['msg'])
  48.  
  49. print ("__Order(buy) placed: %s Id: %d, Price: %.8f, Quantity: %.8f" % (_coin + _basecoin, order['orderId'], order['price'], order['origQty']))
  50.  
  51. time.sleep(_actionTime)
  52.  
  53. qorder = client.query_order(_coin + _basecoin, order['orderId'])
  54.  
  55. if qorder['orderId'] != None:
  56. if qorder['status'] == "FILLED":
  57. print ("____Order(buy) FILLED. Done.")
  58. break
  59. else:
  60. print ("____Order(buy) Not FILLED. Canceling order.")
  61. client.cancel(_coin + _basecoin, orderId)
  62. if getBalance(_basecoin) < getMinimumRule(_basecoin):
  63. print ("______Order(buy): No more %s." % (_basecoin))
  64. break
  65.  
  66. def sell():
  67. while (1) :
  68. order = client.sell_market(_coin + _basecoin, getBalance(_coin))
  69. if 'msg' in order:
  70. message("Error: " + order['msg'])
  71.  
  72. print ("__Order(sell): %s Id: %d, Price: %.8f, Quantity: %.8f" % (_coin + _basecoin, order['orderId'], order['price'], order['origQty']))
  73.  
  74. time.sleep(_actionTime)
  75.  
  76. qorder = client.query_order(_coin + _basecoin, order['orderId'])
  77.  
  78. if qorder['orderId'] != None:
  79. if qorder['status'] == "FILLED" :
  80. print ("____Order(sell) FILLED. Done.")
  81. break
  82. else:
  83. print ("____Order(sell) Not FILLED. Canceling order.")
  84. client.cancel(_coin + _basecoin, orderId)
  85. if getBalance(_coin) < getMinimumRule(_coin):
  86. print ("______Order(sell): No more %s." % (_coin))
  87. break
  88.  
  89. def main():
  90. print ("---- Welcome to PnDBot by au limbo ----")
  91. print ("-- Coin: %s" % (_coin))
  92. print ("-- Basecoin: %s" % (_basecoin))
  93. print ("-- Percent Fall: %i" % (_percentFall))
  94. print ("-- Action time: %f\n\n" % (_actionTime))
  95.  
  96. buy()
  97.  
  98. preValue = client.get_ticker(_coin + _basecoin)
  99.  
  100. while (1) :
  101. value = client.get_ticker(_coin + _basecoin)
  102. print ("Value: %.8f, preValue: %.8f" % (value, preValue))
  103. if value + (value * _percentFall / 100) < preValue :
  104. sell()
  105. break
  106. elif value > preValue :
  107. preValue = client.get_ticker(_coin + _basecoin)
  108. time.sleep(_actionTime)
Advertisement
Add Comment
Please, Sign In to add comment