2021-10-08 10:15:40 -07:00
|
|
|
#!/usr/bin/env python3
|
2021-06-15 15:30:11 -07:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import sys
|
2021-06-25 02:33:40 -07:00
|
|
|
import threading
|
2021-06-15 15:30:11 -07:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
from decimal import Decimal
|
2021-09-02 09:19:17 -07:00
|
|
|
from solana.publickey import PublicKey
|
2021-06-15 15:30:11 -07:00
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.abspath(
|
2021-06-25 02:33:40 -07:00
|
|
|
os.path.join(os.path.dirname(__file__), "..")))
|
2021-06-15 15:30:11 -07:00
|
|
|
import mango # nopep8
|
2021-07-16 09:29:48 -07:00
|
|
|
import mango.simplemarketmaking.simplemarketmaker # nopep8
|
2021-06-15 15:30:11 -07:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Runs a simple market-maker.")
|
2021-07-23 02:20:44 -07:00
|
|
|
mango.ContextBuilder.add_command_line_parameters(parser)
|
2021-06-17 04:06:39 -07:00
|
|
|
mango.Wallet.add_command_line_parameters(parser)
|
2021-06-15 15:30:11 -07:00
|
|
|
parser.add_argument("--market", type=str, required=True, help="market symbol to buy (e.g. ETH/USDC)")
|
|
|
|
parser.add_argument("--spread-ratio", type=Decimal, required=True,
|
|
|
|
help="fraction of the mid price to be added and subtracted to calculate buy and sell prices")
|
|
|
|
parser.add_argument("--position-size-ratio", type=Decimal, required=True,
|
|
|
|
help="fraction of the token inventory to be bought or sold in each order")
|
2021-06-25 02:33:40 -07:00
|
|
|
parser.add_argument("--existing-order-tolerance", type=Decimal, default=Decimal("0.001"),
|
2021-06-16 04:06:12 -07:00
|
|
|
help="fraction of the token inventory to be bought or sold in each order")
|
2021-06-15 15:30:11 -07:00
|
|
|
parser.add_argument("--pause-duration", type=int, default=10,
|
|
|
|
help="number of seconds to pause between placing orders and cancelling them")
|
2021-07-13 12:01:32 -07:00
|
|
|
parser.add_argument("--oracle-provider", type=str, default="serum",
|
|
|
|
help="name of the oracle service providing the prices")
|
2021-09-02 09:19:17 -07:00
|
|
|
parser.add_argument("--account-address", type=PublicKey,
|
|
|
|
help="address of the specific account to use, if more than one available")
|
2021-06-15 15:30:11 -07:00
|
|
|
parser.add_argument("--dry-run", action="store_true", default=False,
|
|
|
|
help="runs as read-only and does not perform any transactions")
|
2021-09-13 06:05:19 -07:00
|
|
|
args: argparse.Namespace = mango.parse_args(parser)
|
2021-06-15 15:30:11 -07:00
|
|
|
|
|
|
|
try:
|
2021-07-23 02:20:44 -07:00
|
|
|
context = mango.ContextBuilder.from_command_line_parameters(args)
|
2021-06-17 04:06:39 -07:00
|
|
|
wallet = mango.Wallet.from_command_line_parameters_or_raise(args)
|
2021-08-26 02:31:02 -07:00
|
|
|
group = mango.Group.load(context, context.group_address)
|
2021-09-02 09:19:17 -07:00
|
|
|
account = mango.Account.load_for_owner_by_address(context, wallet.address, group, args.account_address)
|
2021-06-15 15:30:11 -07:00
|
|
|
|
|
|
|
market_symbol = args.market.upper()
|
2021-06-18 05:50:33 -07:00
|
|
|
market = context.market_lookup.find_by_symbol(market_symbol)
|
|
|
|
if market is None:
|
2021-06-15 15:30:11 -07:00
|
|
|
raise Exception(f"Could not find spot market {market_symbol}")
|
|
|
|
|
2021-07-31 06:43:39 -07:00
|
|
|
market_operations: mango.MarketOperations = mango.create_market_operations(
|
|
|
|
context, wallet, account, market, args.dry_run)
|
2021-06-15 15:30:11 -07:00
|
|
|
|
2021-07-26 10:07:56 -07:00
|
|
|
oracle_provider: mango.OracleProvider = mango.create_oracle_provider(context, args.oracle_provider)
|
2021-06-18 05:50:33 -07:00
|
|
|
oracle = oracle_provider.oracle_for_market(context, market)
|
2021-06-15 15:30:11 -07:00
|
|
|
if oracle is None:
|
|
|
|
raise Exception(f"Could not find oracle for spot market {market_symbol}")
|
|
|
|
|
|
|
|
pause_duration = timedelta(seconds=args.pause_duration)
|
2021-07-16 09:29:48 -07:00
|
|
|
market_maker = mango.simplemarketmaking.simplemarketmaker.SimpleMarketMaker(
|
2021-06-25 02:33:40 -07:00
|
|
|
context, wallet, market, market_operations, oracle, args.spread_ratio, args.position_size_ratio, args.existing_order_tolerance, pause_duration)
|
2021-06-15 15:30:11 -07:00
|
|
|
|
|
|
|
print(f"Starting {market_maker} - use <Enter> to stop.")
|
|
|
|
thread = Thread(target=market_maker.start)
|
|
|
|
thread.start()
|
|
|
|
|
2021-06-25 02:33:40 -07:00
|
|
|
# Wait - don't exit. Exiting will be handled by signals/interrupts.
|
|
|
|
waiter = threading.Event()
|
|
|
|
try:
|
|
|
|
waiter.wait()
|
2021-07-14 06:03:51 -07:00
|
|
|
except:
|
|
|
|
pass
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-06-15 15:30:11 -07:00
|
|
|
print(f"Stopping {market_maker} on next iteration...")
|
|
|
|
market_maker.stop()
|
|
|
|
except Exception as exception:
|
|
|
|
logging.critical(f"Market maker stopped because of exception: {exception} - {traceback.format_exc()}")
|
|
|
|
except:
|
|
|
|
logging.critical(f"Market maker stopped because of uncatchable error: {traceback.format_exc()}")
|