Skip to content

Commit

Permalink
working on risk update and cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
timkpaine committed Jul 2, 2019
1 parent bb9ffab commit ef8a206
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 39 deletions.
48 changes: 24 additions & 24 deletions aat/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,33 @@ def _parse_currencies(currencies):
return ret


def _parse_exchanges(argv):
return [str_to_exchange(x) for x in argv['exchanges'].split() if x]


def _parse_synthetic_config(config, argv):
new_config = SyntheticExchangeConfig()

new_config.exchange_types = config.exchange_options.exchange_types
new_config.trading_type = config.exchange_options.trading_type
new_config.currency_pairs = config.exchange_options.currency_pairs
new_config.instruments = config.exchange_options.instruments

if argv.get('direction'):
new_config.direction = argv.get('direction')
if argv.get('volatility'):
new_config.volatility = argv.get('volatility')
return new_config


def _parse_options(argv, config: TradingEngineConfig) -> None:
if argv.get('exchanges'):
config.exchange_options.exchange_types = [str_to_exchange(x) for x in argv['exchanges'].split() if x]
config.exchange_options.exchange_types = _parse_exchanges(argv)
for exchange in config.exchange_options.exchange_types:
if config.type == TradingType.LIVE and exchange == ExchangeType.SYNTHETIC:
raise ConfigException('Cannot run synthetic exchange in live mode!')
elif exchange == ExchangeType.SYNTHETIC:
new_config = SyntheticExchangeConfig()
new_config.exchange_types = config.exchange_options.exchange_types
new_config.trading_type = config.exchange_options.trading_type
new_config.currency_pairs = config.exchange_options.currency_pairs
new_config.instruments = config.exchange_options.instruments

if argv.get('direction'):
new_config.direction = argv.get('direction')
if argv.get('volatility'):
new_config.volatility = argv.get('volatility')
config.exchange_options = new_config
config.exchange_options = _parse_synthetic_config(config, argv)
config.exchange_options.exchange_type = ExchangeType.COINBASE
else:
raise ConfigException('No exchange set!')
Expand Down Expand Up @@ -188,20 +197,10 @@ def _parse_backtest_options(argv, config) -> None:
config.backtest_options = BacktestConfig()

if argv.get('exchanges'):
config.exchange_options.exchange_types = [str_to_exchange(x) for x in argv['exchanges'].split() if x]
config.exchange_options.exchange_types = _parse_exchanges(argv)
for exchange in config.exchange_options.exchange_types:
if exchange == ExchangeType.SYNTHETIC:
new_config = SyntheticExchangeConfig()
new_config.exchange_types = config.exchange_options.exchange_types
new_config.trading_type = config.exchange_options.trading_type
new_config.currency_pairs = config.exchange_options.currency_pairs
new_config.instruments = config.exchange_options.instruments

if argv.get('direction'):
new_config.direction = argv.get('direction')
if argv.get('volatility'):
new_config.volatility = argv.get('volatility')
config.exchange_options = new_config
config.exchange_options = _parse_synthetic_config(config, argv)
config.exchange_options.exchange_type = ExchangeType.COINBASE
else:
raise ConfigException('No exchange set!')
Expand Down Expand Up @@ -254,6 +253,7 @@ def parse_command_line_config(argv: list) -> TradingEngineConfig:

def set_all_trading_types(trading_type: TradingType,
config: TradingEngineConfig) -> None:
'''Set all trading types to match'''
config.type = trading_type
config.exchange_options.trading_type = trading_type
config.risk_options.trading_type = trading_type
Expand Down
2 changes: 1 addition & 1 deletion aat/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self,
self._execution = execution

def registerStrategy(self, strat: TradingStrategy):
self._strats.append(strat) # add to tickables
self._strats.append(strat)

def query_instruments(self, exchange=None) -> List[PairType]:
if exchange:
Expand Down
31 changes: 18 additions & 13 deletions aat/risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,10 @@ def _constructResp(self,
risk_check=status,
risk_reason=reason,
)

if status == TradeResult.FILLED: # FIXME
self.outstanding += abs(vol * price) * (1 if side == Side.BUY else -1)

self.max_running_outstanding = max(self.max_running_outstanding,
self.outstanding)
self.max_running_outstanding_incr.append(
self.max_running_outstanding)

# TODO self.max_running_risk =
# TODO self.max_running_drawdown =
return resp

def request(self, req: TradeRequest) -> TradeRequest:
# update risk as soon as order will go out
log.info('Requesting %f @ %f', req.volume, req.price)
total = req.volume * req.price
total = total * -1 if req.side == Side.SELL else total
Expand Down Expand Up @@ -110,8 +100,23 @@ def requestSell(self, req: TradeRequest):

def update(self, resp: TradeResponse):
'''update risk after execution'''
log.critical('NOT IMPLEMENTED')
log.critical('risk not fully implemented')
if resp.status == TradeResult.FILLED:
# FIXME
self.outstanding += abs(resp.volume * resp.price) * (1 if resp.side == Side.BUY else -1)

# FIXME
# self.max_running_outstanding = max(self.max_running_outstanding,
# self.outstanding)
# self.max_running_outstanding_incr.append(
# self.max_running_outstanding)

# TODO self.max_running_risk =
# TODO self.max_running_drawdown =
elif resp.status == TradeResult.REJECTED:
self.outstanding -= abs(resp.volume * resp.price) * (1 if resp.side == Side.BUY else -1)

def cancel(self, resp: TradeResponse):
'''update risk after cancelling order'''
log.critical('NOT IMPLEMENTED')
log.critical('risk not fully implemented')
self.outstanding -= abs(resp.volume * resp.price) * (1 if resp.side == Side.BUY else -1)
15 changes: 14 additions & 1 deletion aat/tests/test_risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,17 @@ def test_request2(self):
# assert resp.risk_check == False

def test_update(self):
self.risk.update(None)
from ..structs import TradeRequest, TradeResponse, Instrument
from ..enums import Side, PairType, OrderType, ExchangeType, TradeResult
req = TradeRequest(side=Side.SELL, instrument=Instrument(underlying=PairType.BTCUSD), order_type=OrderType.MARKET, volume=50.0, price=1.0, exchange=ExchangeType.COINBASE, time=datetime.now())
resp = TradeResponse(side=Side.SELL, instrument=Instrument(underlying=PairType.BTCUSD), request=req, order_id='1', volume=50.0, price=1.0, exchange=ExchangeType.COINBASE, time=datetime.now(), status=TradeResult.FILLED)
self.risk.update(resp)
assert self.risk.outstanding == -50

def test_cancel(self):
from ..structs import TradeRequest, TradeResponse, Instrument
from ..enums import Side, PairType, OrderType, ExchangeType, TradeResult
req = TradeRequest(side=Side.SELL, instrument=Instrument(underlying=PairType.BTCUSD), order_type=OrderType.MARKET, volume=50.0, price=1.0, exchange=ExchangeType.COINBASE, time=datetime.now())
resp = TradeResponse(side=Side.SELL, instrument=Instrument(underlying=PairType.BTCUSD), request=req, order_id='1', volume=50.0, price=1.0, exchange=ExchangeType.COINBASE, time=datetime.now(), status=TradeResult.FILLED)
self.risk.cancel(resp)
assert self.risk.outstanding == 50

0 comments on commit ef8a206

Please sign in to comment.