Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sell HNS automatically
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from decimal import Decimal
from datetime import datetime
try:
import requests
import simplejson as json
except ImportError:
raise ImportError('You must install requests and simplejson first!')
namebase_cookie = 's:'
btc_address = 'bc1q3hkc265vxqu8dk4dkk68sf607086vmckwm2z6e'
# The unit of expect_prices is mBTC! 1 BTC = 1,000 mBTC. the smallest unit of mBTC is 1e−5
expect_prices = [
# min unit price (mBTC per HNS), max unit price (mBTC per HNS), the amount of HNS to sell
('0.02', 999, 5000),
(0, '0.005', 3000),
('0.018', '0.01999', 2000),
]
reserve_amount = 500
def time():
return datetime.now().isoformat()
def normalize(d):
return '{:f}'.format(d.normalize())
def prin(*args, **kwargs):
args = [normalize(arg) if isinstance(arg, Decimal) else arg for arg in args]
return print(time(), *args, **kwargs)
def get_hns_balance(s):
r = s.get('https://www.namebase.io/api/user')
# Format: str Unit: μHNS
balance = Decimal(r.json()['hns_balance']) / 1000 / 1000
prin('You have', balance, 'HNS.')
return balance
def get_current_price(s, p):
r = s.get('https://www.namebase.io/api/exchange/sell', timeout=2)
# Format: float Unit: BTC
price = r.json(use_decimal=True)['price'] * 1000
if p:
prin('Current price is', price, 'mBTC.')
return price
def sell_hns(amount, s):
r = s.post('https://www.namebase.io/api/exchange/trade/hns/btc', json={
'address': btc_address,
'amount': normalize(amount * 1000 * 1000),
})
# {'success': bool, 'txHash': str, 'baseAmount': str HNS, 'quoteAmount': str BTC, 'user': {'hns_balance': str μHNS}}
# {'errorCode': str, 'errorData': dict}
res = r.json()
if 'success' in res and res['success']:
balance = Decimal(res['user']['hns_balance']) / 1000 / 1000
prin('Sold', Decimal(res['baseAmount']), 'HNS for', Decimal(res['quoteAmount']) / 1000, 'mBTC.',
'You have', balance, 'HNS left.')
else:
prin('Sell HNS failed:', 'errorCode' in res and res['errorCode'] or 'UnknownError', file=sys.stderr)
balance = get_hns_balance(s)
return balance
def run():
with requests.Session() as s:
requests.utils.add_dict_to_cookiejar(s.cookies, {
'namebase-main': namebase_cookie,
})
balance = get_hns_balance(s)
reserved = Decimal(reserve_amount)
i = 0
while True:
if balance <= reserved:
prin('The reserve amount is', reserved, 'HNS, but you only have', balance, 'HNS.')
break
try:
price = get_current_price(s, i % 10 == 0)
for inx, (min_price, max_price, amount) in enumerate(expect_prices):
if Decimal(min_price) <= price <= Decimal(max_price):
amount = Decimal(amount)
if amount > balance:
amount = balance
if balance - amount < reserved:
amount = balance - reserved
prin('Selling', amount, 'HNS for', price, 'mBTC each.')
balance = sell_hns(amount, s)
expect_prices.pop(inx)
break
except Exception as e:
prin(e, file=sys.stderr)
i += 1
if __name__ == '__main__':
run()
@jat001

This comment has been minimized.

Copy link
Owner Author

jat001 commented Feb 20, 2020

namebase_cookie: Cookie named namebase-main.
btc_address: Your BTC address. Keep it if you wanna donate to me😆
reserve_amount: The amount of HNS that you do not want to sell.
expect_prices: Well, hard to explain. Just see the examples.
第一行空仓跑路,第二行割肉止损,第三行落袋为安。

@shenqihui

This comment has been minimized.

Copy link

shenqihui commented Feb 20, 2020

老哥你这写死的btc地址有点狠。

@jat001

This comment has been minimized.

Copy link
Owner Author

jat001 commented Feb 20, 2020

老哥你这写死的btc地址有点狠。

就一脚本,啥参数不改都跑不起来,上面这4个参数都是让你改的
namebase_cookie不设连价格都获取不了,btc_address改成你自己的,reserve_amount不看好这币的前景就设成0,看好的话想留多少自己设,expect_prices说不定明天又腰斩了,根据行情和自己的心理价位设

@jat001

This comment has been minimized.

Copy link
Owner Author

jat001 commented Feb 20, 2020

再解释下expect_prices,就跟交易所挂单一样,只不过不占用任何实际额度,优先级从前到后,匹配到第一个就停止,卖出成功后该规则自动失效,一直到余额<=reserve_amount为止。

expect_prices = [
    ('0.02', 999, 5000),
    (0, '0.005', 3000),
    ('0.018', '0.01999', 2000),
]
reserve_amount = 500

针对这个示例说明,假设我现在有4000HNS,我的心理价位是0.02mBTC/HNS,所以我第一条规则就设置了当价格>=0.02mBTC/HNS时,卖出5000HNS,因为我只有4000HNS,还设置了保留500HNS,如果价格涨到0.02BTC/HNS,那么脚本就会卖出3500HNS。但HNS的价格一直在跌,我怕不仅涨不到我的心理价位,还会一直跌下去,所以我设置了当价格低于0.005mBTC/HNS时,卖出3000HNS以止损。不过无论是0.02还是0.005,都距离现在的行情有些距离,所以我在第三条设置了当价格为0.018-0.01999mBTC/HNS,卖出2000HNS以保证收益,所以只要这条规则被触发过,那就只剩下4000-2000-500=1500HNS可卖了。如果价格一直往上涨,直到大于0.02mBTC/HNS,那么我的收益当然比不加最后一条规则要少,但如果价格涨到0.018mBTC/HNS后又一路下跌,那么我至少保证了2000HNS*0.018mBTC/HNS=36mBTC的收益

@shenqihui

This comment has been minimized.

Copy link

shenqihui commented Feb 25, 2020

https://www.namebase.io/pro
发现这里能直接挂单。。。已售。

@jat001

This comment has been minimized.

Copy link
Owner Author

jat001 commented Feb 25, 2020

https://www.namebase.io/pro
发现这里能直接挂单。。。已售。

我也是一开始没发现,白写了个脚本

@shenqihui

This comment has been minimized.

Copy link

shenqihui commented Feb 25, 2020

https://www.namebase.io/pro
发现这里能直接挂单。。。已售。

我也是一开始没发现,白写了个脚本

我也是。。。。看着你这个挂单,后面发现朋友说那里能挂单。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.