-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrun_market_maker.py
53 lines (41 loc) · 1.61 KB
/
run_market_maker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import random
import requests
from ViaBTCAPI.ViaBTCAPI import ViaBTCAPI
from config import *
api = ViaBTCAPI(EXCHANGE_URL)
def get_btc_eth_price(url=BTC_ETH_PRICE_URL):
resp = requests.get(url)
price = float(resp.json()["BTC"])
mult = 1 + BTC_ETH_PRICE_ERROR_PRC / 100 * 2 * (random.random() - 0.5)
return price * mult
def get_balance(api, user_id=BOT_USER_ID):
return api.balance_query(user_id)["result"]
def get_orderbook(api, market=MARKET_NAME):
return api.order_depth(market)["result"]
def put_order(api, action, amount, price):
return api.order_put_limit(
user_id=BOT_USER_ID,
market=MARKET_NAME,
side=action,
amount=amount,
price=price
)
balances = get_balance(api)
print("{0} balance: {1} {2}, {3} {4}".format(BOT_USER_ID,
balances[MONEY_NAME]["available"], MONEY_NAME,
balances[STOCK_NAME]["available"], STOCK_NAME
))
current_btc_eth_price = get_btc_eth_price()
print("Current BTCETH price: {}".format(current_btc_eth_price))
action = "BUY" if random.random() > BOT_BUY_PROB else "SELL"
direction = action != "SELL"
spread = current_btc_eth_price * BOT_SPREAD_PRC / 100
price_delta = (-1) ** direction * random.random() * spread
price = current_btc_eth_price + price_delta
asset_name_to_trade = MONEY_NAME if action == "BUY" else STOCK_NAME
amount = float(balances[asset_name_to_trade]["available"]) * BOT_TRADE_BALANCE_PRC / 100
print("Going to {} {} {} for {} {}".format(action, amount, STOCK_NAME, price, MONEY_NAME))
resp = put_order(api, action, amount, price)
if resp["error"] is not None:
print(resp["error"]["message"])
print()