mango-explorer/bin/simple-market-maker

77 lines
3.2 KiB
Plaintext
Raw Normal View History

2021-06-15 15:30:11 -07:00
#!/usr/bin/env pyston3
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
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
import mango.marketmaking.simplemarketmaker # nopep8
parser = argparse.ArgumentParser(description="Runs a simple market-maker.")
mango.Context.add_command_line_parameters(parser)
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"),
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")
parser.add_argument("--dry-run", action="store_true", default=False,
help="runs as read-only and does not perform any transactions")
args = parser.parse_args()
logging.getLogger().setLevel(args.log_level)
logging.warning(mango.WARNING_DISCLAIMER_TEXT)
try:
context = mango.Context.from_command_line_parameters(args)
wallet = mango.Wallet.from_command_line_parameters_or_raise(args)
2021-06-15 15:30:11 -07:00
market_symbol = args.market.upper()
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-06-25 02:33:40 -07:00
market_operations: mango.MarketOperations = mango.create_market_operations(
context, wallet, args.dry_run, market, print)
2021-06-15 15:30:11 -07:00
oracle_provider: mango.OracleProvider = mango.create_oracle_provider("serum")
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)
market_maker = mango.marketmaking.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()
except Exception as exception:
print(f"Caught exception: {exception}")
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()}")