Skip to content

Commit

Permalink
Allow configuring order exchange routing.
Browse files Browse the repository at this point in the history
  • Loading branch information
brndnmtthws committed Oct 3, 2022
1 parent fd84212 commit cb87c60
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
5 changes: 5 additions & 0 deletions thetagang.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ margin_usage = 0.5
# https://interactivebrokers.github.io/tws-api/market_data_type.html)
market_data_type = 1

[orders]
# The exchange to route orders to. Can be overridden if desired. This is also
# used for fetching tickers/prices.
exchange = "SMART"

[orders.algo]
# By default we use adaptive orders with patient priority which gives reasonable
# results. You can also experiment with TWAP or other options, however the
Expand Down
3 changes: 2 additions & 1 deletion thetagang/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ def validate_config(config):
"market_data_type": And(int, lambda n: 1 <= n <= 4),
},
"orders": {
"exchange": And(str, len),
"algo": {
"strategy": And(str, len),
"params": [And([str], lambda p: len(p) == 2)],
}
},
},
"option_chains": {
"expirations": And(int, lambda n: 1 <= n),
Expand Down
1 change: 1 addition & 0 deletions thetagang/config_defaults.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DEFAULT_CONFIG = {
"orders": {
"exchange": "SMART",
"algo": {
"strategy": "Adaptive",
"params": [["adaptivePriority", "Patient"]],
Expand Down
13 changes: 8 additions & 5 deletions thetagang/portfolio_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ def roll_calls(self, calls, account_summary, portfolio_positions):
def close_positions(self, positions):
for position in positions:
try:
position.contract.exchange = "SMART"
position.contract.exchange = self.get_order_exchange()
buy_ticker = self.get_ticker_for(position.contract, midpoint=True)
price = round(get_lowest_price(buy_ticker), 2)

Expand Down Expand Up @@ -871,7 +871,7 @@ def roll_positions(self, positions, right, account_summary, portfolio_positions=
try:
symbol = position.contract.symbol

position.contract.exchange = "SMART"
position.contract.exchange = self.get_order_exchange()
buy_ticker = self.get_ticker_for(position.contract, midpoint=True)

strike_limit = get_strike_limit(self.config, symbol, right)
Expand Down Expand Up @@ -929,13 +929,13 @@ def roll_positions(self, positions, right, account_summary, portfolio_positions=
ComboLeg(
conId=position.contract.conId,
ratio=1,
exchange="SMART",
exchange=self.get_order_exchange(),
action="BUY",
),
ComboLeg(
conId=sell_ticker.contract.conId,
ratio=1,
exchange="SMART",
exchange=self.get_order_exchange(),
action="SELL",
),
]
Expand All @@ -945,7 +945,7 @@ def roll_positions(self, positions, right, account_summary, portfolio_positions=
secType="BAG",
symbol=symbol,
currency="USD",
exchange="SMART",
exchange=self.get_order_exchange(),
comboLegs=comboLegs,
)

Expand Down Expand Up @@ -1147,3 +1147,6 @@ def get_algo_strategy(self):

def get_algo_params(self):
return [TagValue(p[0], p[1]) for p in self.config["orders"]["algo"]["params"]]

def get_order_exchange(self):
return self.config["orders"]["exchange"]
4 changes: 4 additions & 0 deletions thetagang/thetagang.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def start(config):
click.echo()

click.secho(" Order settings:", fg="green")
click.secho(
f" Exchange = {config['orders']['exchange']}",
fg="cyan",
)
click.secho(
f" Strategy = {config['orders']['algo']['strategy']}",
fg="cyan",
Expand Down

0 comments on commit cb87c60

Please sign in to comment.