Sunday, June 24, 2018

Kucoin比特幣交易程式

Kucoin比特幣交易程式


最後卡在官方api呼叫響應問題,嘿嘿嘿,google帳戶被鎖囉,信好coin都轉出去了
呼叫到官網鎖ip QQ

Kucoin.py



# -*- coding=utf-8 -*-
import time
from kucoin.client import Client
import json
import datetime
import os
import time
import sys
import math
import string
from operator import itemgetter, attrgetter
print sys.getdefaultencoding( )
reload(sys)
print sys.setdefaultencoding('utf-8')
print sys.getdefaultencoding( )
client = Client(key_id, api_key)
#目標貨幣
target_currency='KCS'
blance_tmp=0
def get_now_btc(target):
btc=0
balance = client.get_all_balances()
#目標
products = client.get_currencies([target, 'BTC'])
if(balance is -1 or products is -1):
return -1
#單筆轉btc轉hkd匯率
#單筆轉目標幣種轉hkd匯率
rate=products['rates'][target]['HKD']
#now price
blance_tmp=0
for x in range (0,len(balance),1):
try:
for key, value in balance[x].iteritems() :
if(key=='balance'):
blance_tmp=value
if(key=='coinType' and value== target):
products = client.get_currencies([value, 'BTC'])
rate=products['rates'][value]['HKD']
if( products is -1):
return -1
btc+=(rate*blance_tmp) /(float(products['rates']['BTC']['HKD']))
return btc
#print value,btc
except:
pass
return btc
def get_hold_currency(target):
btc=0
balance = client.get_all_balances()
#目標
products = client.get_currencies([target, 'BTC'])
if(balance is -1 or products is -1):
return -1
#單筆轉btc轉hkd匯率
#單筆轉目標幣種轉hkd匯率
rate=products['rates'][target]['HKD']
#now price
blance_tmp=0
for x in range (0,len(balance),1):
for key, value in balance[x].iteritems() :
if(key=='balance'):
blance_tmp=value
if(key=='coinType' and value== target):
products = client.get_currencies([value, 'BTC'])
if( products is -1):
return -1
rate=products['rates'][value]['HKD']
btc+=(rate*blance_tmp) /(float(products['rates']['BTC']['HKD']))
return blance_tmp
return blance_tmp
#與伺服器請求失敗紀錄
respone_error=0
ts = time.time()
i = 199 # 設定控制變數
tmp=""
count = 0
#目標貨幣
target_currency='DBC'
#假設比特必持有
#1320.063793760353 港幣 = tnd 5000
btc=0.02
base_btc=0.02
hold_currency=0
#購買狀態最大一筆
by_luck=0
#一次買量
hold_currency_max=20
buy_data=[]
sell_data=[]
o_avg_data=[]
h_avg_data=[]
l_avg_data=[]
ts = time.time()
btc=get_now_btc('BTC')
base_btc=((get_now_btc('BTC')+get_now_btc(target_currency))*0.01)+get_now_btc('BTC')
#max
hold_currency=get_hold_currency(target_currency)
buy_order_count=0
sell_order_count=0
buy_price=0
sell_price=0
while(1):
ts = time.time()
try:
#-648082
if(buy_order_count%15==0):
client.cancel_all_orders(target_currency+'-BTC', 'BUY')
elif(sell_order_count%15==0):
client.cancel_all_orders(target_currency+'-BTC', 'SELL')
else:
client.cancel_all_orders(target_currency+'-BTC', 'BUY')
client.cancel_all_orders(target_currency+'-BTC', 'SELL')
klines = client.get_kline_data_tv(target_currency+'-BTC', Client.RESOLUTION_1MINUTE, int(ts)-1000, int(ts))
depth = client.get_order_book(target_currency+'-BTC', limit=2)
products = client.get_currencies([target_currency, 'BTC'])
all_btc=get_now_btc(target_currency)+get_now_btc('BTC')
btc=get_now_btc(target_currency)+get_now_btc('BTC')
hold_currency=get_hold_currency(target_currency)
#sort_buy=sorted(depth['BUY'], key = itemgetter(1,0),reverse=False)
#sort_sell=sorted(depth['SELL'], key = itemgetter(1),reverse=True)
#print (depth)
#print (klines)
if(depth is None or klines is None or products is None or klines is str
or depth is -1 or klines is -1 or products is -1 or all_btc is -1 or btc is -1
or hold_currency is -1):
print (respone_error)
respone_error+=1
continue
print ("最多人入場點 買/賣".decode('utf8')),depth['BUY'][0],("/"),depth['SELL'][1]
#大單主力目前最快單
big_buy=depth['BUY'][0]
#大單賣力目前最快單
big_sell=depth['SELL'][1]
klines['o']=[float(0) if v is None else v for v in klines['o']]
klines['h']=[float(0) if v is None else v for v in klines['h']]
klines['l']=[float(0) if v is None else v for v in klines['l']]
klines['v']=[float(0) if v is None else v for v in klines['v']]
klines['t']=[float(0) if v is None else v for v in klines['t']]
#print klines['o']
#print klines['h']
#print klines['l']
if(len(big_buy)>=3 and len(big_sell) >=3 and len(klines)>=7 and len(products)>=2 ):
print ("sda10")
#print("我是兩津2號 交易怪物 "),float(klines['h'][len(klines['h'])-1]),("\n賣出基準點(獲利最高):"),base_btc,("\n目前持有BTC:"),btc,("\n目前持有幣:"),hold_currency,("\n帳戶總持有:"),all_btc,('\n帳戶總持有價值(HKD):'),(all_btc*float(products['rates']['BTC']['HKD'])),('\n帳戶總持有價值(NTD):'),(all_btc*float(products['rates']['BTC']['HKD']))*3.7,('\n與伺服器請求失敗次數'),respone_error,('\n')
print (klines['o'][len(klines['o'])-30:len(klines['o'])])
avg = sum(klines['o'][len(klines['o'])-30:len(klines['o'])]) / float(len(klines['o'][len(klines['o'])-30:len(klines['o'])]))
avg2 = sum(klines['o'][len(klines['o'])-59:len(klines['o'])-30]) / float(len(klines['o'][len(klines['o'])-59:len(klines['o'])-30]))
o_avg=float(avg2-avg)
#o_avg_data.append(o_avg)
print ('平均滑動'.decode('utf8'))
print '近60筆資料 開盤點平均 %+9f /'.decode('utf8')%(o_avg),float(o_avg)
avg = sum(klines['h'][len(klines['h'])-30:len(klines['h'])]) / float(len(klines['h'][len(klines['h'])-30:len(klines['h'])]))
avg2 = sum(klines['h'][len(klines['h'])-59:len(klines['h'])-30]) / float(len(klines['h'][len(klines['h'])-59:len(klines['h'])-30]))
h_avg=float(avg2-avg)
print '近60筆資料 最高點平均 %+9f /'.decode('utf8')%(h_avg),float(h_avg)
if(klines['h'][len(klines['h'])-1]==big_sell[0]):
rate=products['rates'][target_currency]['HKD']
#手續費
price=0.001
#單一hold_currency
hold_currency_pric=float(rate*hold_currency)
#bit 可換港元
bit_hkd=(float(products['rates']['BTC']['HKD']))
#總交易hold_currencyto bit
bit_hold_currency=((hold_currency_pric/bit_hkd)*price)+(hold_currency_pric/bit_hkd)
#增量
bit_hold_currencyex=bit_hold_currency+(hold_currency_pric/bit_hkd)*0.1
if( (bit_hold_currency+btc>btc) and sell_price > buy_price):
sell_price=big_buy[0]
hold_currency_max=math.floor((btc/big_sell[0])-0.5)
transaction = client.create_sell_order(target_currency+'-BTC', big_sell[0], int(hold_currency))
base_btc=bit_hold_currency+btc
sell_order_count+=1
tmp=[str(time.time()),klines['h'][len(klines['h'])-1],hold_currency,(rate*hold_currency),(rate*hold_currency*3.78),btc,transaction]
tmp2=("我是兩津2號 交易怪物 ").decode('utf8'),float(klines['h'][len(klines['h'])-1]),("\n賣出基準點(獲利最高):").decode('utf8'),base_btc,("\n目前持有BTC:").decode('utf8'),btc,("\n目前持有幣:").decode('utf8'),hold_currency,("\n帳戶總持有:").decode('utf8'),all_btc,('\n帳戶總持有價值(HKD):'),(all_btc*float(products['rates']['BTC']['HKD'])),('\n帳戶總持有價值(NTD):').decode('utf8'),(all_btc*float(products['rates']['BTC']['HKD']))*3.7,('\n與伺服器請求失敗次數').decode('utf8'),respone_error,('\n')
with open(r"c:\\sell.txt", "a") as myfile:
myfile.write(str(tmp))
with open(r"c:\\profile.txt", "a") as myfile:
myfile.write(str(tmp2))
sell_data.append(tmp)
#print (tmp)
del (h_avg_data[:])
avg = sum(klines['l'][len(klines['l'])-30:len(klines['l'])]) / float(len(klines['l'][len(klines['l'])-30:len(klines['l'])]))
avg2 = sum(klines['l'][len(klines['l'])-59:len(klines['l'])-30]) / float(len(klines['l'][len(klines['l'])-59:len(klines['l'])-30]))
l_avg=float(avg2-avg)
#l_avg_data.append(l_avg)
print '近60筆資料 最低點平均 %+9lf /'.decode('utf8')%(l_avg) ,float(l_avg)
if(klines['h'][len(klines['h'])-1]==big_buy[0] and sell_price > buy_price):
rate=products['rates'][target_currency]['HKD']
if(btc >0 ):
if (buy_price==0):
buy_price=sell_price=big_buy[0]
else:
buy_price=big_buy[0]
hold_currency_max=math.floor((btc/big_buy[0])-0.5)
transaction = client.create_buy_order(target_currency+'-BTC', big_buy[0], hold_currency_max)
buy_order_count+=1
print (hold_currency_max)
hold_currency_max_tmp=hold_currency_max
tmp=[str(time.time()),klines['h'][len(klines['h'])-1],hold_currency,(rate*hold_currency),(rate*hold_currency*3.78),btc,transaction]
with open(r"c:\\buy.txt", "a") as myfile:
myfile.write(str(tmp))
buy_data.append(tmp)
del(l_avg_data[:])
#tmp=['買入時間',str(time.time()),'買入價格',l_avg_data[59],'you_hold_currency:',hold_currency,'you_hkd:',(rate*hold_currency),'you_tnd:',(rate*hold_currency*3.78),'you_bit:',"{:.9f}".format(btc),'you_bit_cost:',"{:.9f}".format(cost))]
#print ('買').decode('utf8')
#for tmp in buy_data:
# print tmp
#print ('賣').decode('utf8')
#for tmp2 in sell_data:
# print tmp2
print("我是兩津2號 交易怪物 ").decode('utf8'),float(klines['h'][len(klines['h'])-1]),("\n賣出基準點(獲利最高):").decode('utf8'),base_btc,("\n目前持有BTC:").decode('utf8'),btc,("\n目前持有幣:").decode('utf8'),hold_currency,("\n帳戶總持有:").decode('utf8'),all_btc,('\n帳戶總持有價值(HKD):').decode('utf8'),(all_btc*float(products['rates']['BTC']['HKD'])),('\n帳戶總持有價值(NTD):').decode('utf8'),(all_btc*float(products['rates']['BTC']['HKD']))*3.7,('\n與伺服器請求失敗次數').decode('utf8'),respone_error,('\nBUY/SELL'),buy_order_count,('/'),sell_order_count,('\n'),('hold_currency_max'),hold_currency_max,('\n')
except ValueError:
print ('geterror')
view raw kucoin.py hosted with ❤ by GitHub