|
|
|
@@ -0,0 +1,374 @@
|
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Routes and views for the application.
|
|
|
|
|
"""
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
import re
|
|
|
|
|
import asyncio
|
|
|
|
|
from ib_insync import IB, MarketOrder, ExecutionFilter
|
|
|
|
|
from sanic import Sanic, response
|
|
|
|
|
from sanic.exceptions import InvalidUsage
|
|
|
|
|
import logging
|
|
|
|
|
from contract import get_next_contract_month, contract_type_check
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
# Create the Sanic app
|
|
|
|
|
app = Sanic("unique_app_name")
|
|
|
|
|
|
|
|
|
|
# Initialize the logger
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
# IB constants
|
|
|
|
|
TWS_HOST = "127.0.0.1"
|
|
|
|
|
TWS_LIVE_PORT = 7496
|
|
|
|
|
TWS_PAPER_PORT = 7497
|
|
|
|
|
IBG_HOST = "127.0.0.1"
|
|
|
|
|
IBG_LIVE_PORT = 4001
|
|
|
|
|
IBG_PAPER_PORT = 4002
|
|
|
|
|
|
|
|
|
|
# Configuration: Set to True for paper trading, False for live trading
|
|
|
|
|
USE_PAPER_TRADING = True
|
|
|
|
|
# Configuration: Set to True for IB Gateway, False for TWS
|
|
|
|
|
USE_IB_GATEWAY = True
|
|
|
|
|
|
|
|
|
|
# Determine the correct host and port based on the configuration
|
|
|
|
|
if USE_IB_GATEWAY:
|
|
|
|
|
HOST = IBG_HOST
|
|
|
|
|
PORT = IBG_PAPER_PORT if USE_PAPER_TRADING else IBG_LIVE_PORT
|
|
|
|
|
else:
|
|
|
|
|
HOST = TWS_HOST
|
|
|
|
|
PORT = TWS_PAPER_PORT if USE_PAPER_TRADING else TWS_LIVE_PORT
|
|
|
|
|
|
|
|
|
|
# Initialize the IB connection
|
|
|
|
|
app_ib = IB()
|
|
|
|
|
|
|
|
|
|
# Check every minute if we need to reconnect to IB
|
|
|
|
|
async def check_if_reconnect() -> None:
|
|
|
|
|
global app_ib
|
|
|
|
|
logger.info("Checking if we need to reconnect...")
|
|
|
|
|
# Reconnect if needed
|
|
|
|
|
if app_ib is None or not app_ib.isConnected():
|
|
|
|
|
try:
|
|
|
|
|
logger.info("Reconnecting...")
|
|
|
|
|
if app_ib is not None:
|
|
|
|
|
app_ib.disconnect()
|
|
|
|
|
await app_ib.connectAsync(HOST, PORT, clientId=0) # Using clientId=0 as Master Client
|
|
|
|
|
app_ib.errorEvent += check_on_ib_error
|
|
|
|
|
logger.info("Successfully reconnected to TWS")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
|
|
|
file_name = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
|
|
|
|
|
logger.error(f"Exception: {exc_type}, File: {file_name}, Line: {exc_tb.tb_lineno}")
|
|
|
|
|
logger.warning(f"Make sure TWS or Gateway is open with the correct port: {str(e)}")
|
|
|
|
|
|
|
|
|
|
# Helper function to wait for the order to be filled
|
|
|
|
|
async def wait_for_order_fill(trade, timeout=60):
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
while trade.orderStatus.status not in ('Filled', 'Cancelled'):
|
|
|
|
|
if time.time() - start_time > timeout:
|
|
|
|
|
raise TimeoutError("Order fill timeout")
|
|
|
|
|
await app_ib.sleep(1)
|
|
|
|
|
return trade.orderStatus.status
|
|
|
|
|
|
|
|
|
|
# Request account updates
|
|
|
|
|
def request_account_updates(subscribe=True, accountCode=""):
|
|
|
|
|
app_ib.reqAccountUpdates(subscribe, accountCode)
|
|
|
|
|
|
|
|
|
|
# Event handler for order status
|
|
|
|
|
def on_order_status(trade):
|
|
|
|
|
logger.info(f"Order Status: OrderId={trade.order.orderId}, Status={trade.orderStatus.status}, Filled={trade.orderStatus.filled}, Remaining={trade.orderStatus.remaining}, AvgFillPrice={trade.orderStatus.avgFillPrice}")
|
|
|
|
|
|
|
|
|
|
# Event handler for account updates
|
|
|
|
|
def on_account_update(account, key, value, currency):
|
|
|
|
|
logger.info(f"Account update: Account={account}, Key={key}, Value={value}, Currency={currency}")
|
|
|
|
|
|
|
|
|
|
# Event handler for portfolio updates
|
|
|
|
|
def on_portfolio_update(account, contract, position, marketPrice, marketValue, averageCost, unrealizedPNL, realizedPNL):
|
|
|
|
|
logger.info(f"Portfolio update: Account={account}, Contract={contract}, Position={position}, MarketPrice={marketPrice}, MarketValue={marketValue}, AverageCost={averageCost}, UnrealizedPNL={unrealizedPNL}, RealizedPNL={realizedPNL}")
|
|
|
|
|
|
|
|
|
|
# Event handler for open orders
|
|
|
|
|
def on_open_order(trade):
|
|
|
|
|
logger.info(f"Open order: OrderId={trade.order.orderId}, Contract={trade.contract}, Order={trade.order}, OrderState={trade.orderState}")
|
|
|
|
|
|
|
|
|
|
# Event handler for IB error
|
|
|
|
|
def check_on_ib_error(reqId, error_code, error_string, contract):
|
|
|
|
|
logger.error(f"Error code: {error_code}, Message: {error_string}")
|
|
|
|
|
|
|
|
|
|
# Set event handlers
|
|
|
|
|
def set_event_handlers():
|
|
|
|
|
app_ib.orderStatusEvent += on_order_status
|
|
|
|
|
app_ib.accountSummaryEvent += on_account_update
|
|
|
|
|
app_ib.updatePortfolioEvent += on_portfolio_update
|
|
|
|
|
app_ib.openOrderEvent += on_open_order
|
|
|
|
|
app_ib.errorEvent += check_on_ib_error
|
|
|
|
|
|
|
|
|
|
# Periodic update function
|
|
|
|
|
async def periodic_update():
|
|
|
|
|
while True:
|
|
|
|
|
request_account_updates()
|
|
|
|
|
await asyncio.sleep(60) # Update every 60 seconds
|
|
|
|
|
|
|
|
|
|
@app.route('/portfolio', methods=['GET'])
|
|
|
|
|
async def portfolio(request) -> response.HTTPResponse:
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
|
try:
|
|
|
|
|
await check_if_reconnect()
|
|
|
|
|
positions = await app_ib.reqPositions() # Use reqPositions to get real-time positions
|
|
|
|
|
|
|
|
|
|
if not positions:
|
|
|
|
|
logger.error("No positions retrieved from IB TWS API.")
|
|
|
|
|
return response.json({"status": "error", "message": "Failed to retrieve positions from IB TWS API."}, status=400)
|
|
|
|
|
|
|
|
|
|
portfolio_data = []
|
|
|
|
|
for position in positions:
|
|
|
|
|
contract = position.contract
|
|
|
|
|
avg_price = position.avgCost
|
|
|
|
|
quantity = position.position
|
|
|
|
|
unrealized_pnl = position.unrealizedPNL
|
|
|
|
|
realized_pnl = position.realizedPNL
|
|
|
|
|
|
|
|
|
|
portfolio_data.append({
|
|
|
|
|
"Instrument": contract.symbol,
|
|
|
|
|
"Average Price": avg_price,
|
|
|
|
|
"Positions": quantity,
|
|
|
|
|
"Unrealized P&L": unrealized_pnl,
|
|
|
|
|
"Realized P&L": realized_pnl
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return response.json(portfolio_data)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error retrieving portfolio: {e}")
|
|
|
|
|
return response.json({"status": "error", "message": "Failed to retrieve portfolio"}, status=500)
|
|
|
|
|
return response.json({"status": "error", "message": "Invalid request method"}, status=405)
|
|
|
|
|
|
|
|
|
|
@app.route('/webhook', methods=['POST'])
|
|
|
|
|
async def webhook(request) -> response.HTTPResponse:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
try:
|
|
|
|
|
# Check if we need to reconnect with IB
|
|
|
|
|
await check_if_reconnect()
|
|
|
|
|
|
|
|
|
|
# Log the raw request body for debugging
|
|
|
|
|
raw_body = request.body.decode('utf-8')
|
|
|
|
|
logger.info(f"Received raw body: {raw_body}")
|
|
|
|
|
|
|
|
|
|
# Extract the required fields using regular expressions
|
|
|
|
|
action_pattern = r"order (?P<action>\w+) (?P<contracts>\d+) @ [\d\.]+ filled on (?P<ticker>\w+)"
|
|
|
|
|
position_pattern = r"New strategy position is (?P<position_size>-?\d+)"
|
|
|
|
|
|
|
|
|
|
action_match = re.search(action_pattern, raw_body)
|
|
|
|
|
position_match = re.search(position_pattern, raw_body)
|
|
|
|
|
|
|
|
|
|
if not action_match or not position_match:
|
|
|
|
|
logger.error("Failed to parse webhook message.")
|
|
|
|
|
return response.json({"status": "error", "message": "Failed to parse webhook message."}, status=400)
|
|
|
|
|
|
|
|
|
|
action = action_match.group("action").upper()
|
|
|
|
|
contracts = int(action_match.group("contracts"))
|
|
|
|
|
ticker = action_match.group("ticker")
|
|
|
|
|
position_size = int(position_match.group("position_size"))
|
|
|
|
|
|
|
|
|
|
logger.info(f"Parsed values - Action: {action}, Contracts: {contracts}, Ticker: {ticker}, Position Size: {position_size}")
|
|
|
|
|
|
|
|
|
|
# Determine the IB ticker based on the TradingView ticker
|
|
|
|
|
ib_ticker = None
|
|
|
|
|
if ticker == "SPX":
|
|
|
|
|
ib_ticker = "MES"
|
|
|
|
|
elif ticker == "NDX":
|
|
|
|
|
ib_ticker = "MNQ"
|
|
|
|
|
elif ticker == "HSI":
|
|
|
|
|
ib_ticker = "MHI"
|
|
|
|
|
elif ticker == "TOPIX":
|
|
|
|
|
ib_ticker = "MNTPX"
|
|
|
|
|
else:
|
|
|
|
|
logger.error(f"Invalid ticker: {ticker}")
|
|
|
|
|
return response.json({"status": "error", "message": "Invalid ticker"}, status=400)
|
|
|
|
|
|
|
|
|
|
# Map TradingView actions to IB actions
|
|
|
|
|
if action not in ["BUY", "SELL"]:
|
|
|
|
|
logger.error(f"Invalid action: {action}")
|
|
|
|
|
return response.json({"status": "error", "message": "Invalid action"}, status=400)
|
|
|
|
|
|
|
|
|
|
# Get the current contract month
|
|
|
|
|
current_contract_month = get_next_contract_month(ib_ticker)
|
|
|
|
|
current_contract = contract_type_check(ib_ticker, contract_month=current_contract_month)
|
|
|
|
|
|
|
|
|
|
if not current_contract:
|
|
|
|
|
return response.json({"status": "error", "message": "Invalid contract"}, status=400)
|
|
|
|
|
|
|
|
|
|
# 1. Place Initial Order: Execute the webhook order with contract month as of today
|
|
|
|
|
initial_order = MarketOrder(action, contracts, account=app_ib.wrapper.accounts[0])
|
|
|
|
|
logger.info(f"Placing initial order: {action} {contracts} contracts of {ib_ticker}")
|
|
|
|
|
|
|
|
|
|
# Place the order
|
|
|
|
|
try:
|
|
|
|
|
trade = app_ib.placeOrder(current_contract, initial_order)
|
|
|
|
|
logger.info(f"Order placed: {trade}")
|
|
|
|
|
|
|
|
|
|
# Log order status after placing
|
|
|
|
|
await asyncio.sleep(2) # Wait for a moment to allow the order to process
|
|
|
|
|
logger.info(f"Order status after placing: {trade.orderStatus.status}")
|
|
|
|
|
|
|
|
|
|
# Check for errors in the IB API
|
|
|
|
|
if trade.orderStatus.status == 'PendingSubmit':
|
|
|
|
|
logger.error("Order stuck in PendingSubmit.")
|
|
|
|
|
return response.json({"status": "error", "message": "Order stuck in PendingSubmit."}, status=400)
|
|
|
|
|
|
|
|
|
|
# 2. Wait for the initial order to be filled or cancelled
|
|
|
|
|
order_status = await wait_for_order_fill(trade)
|
|
|
|
|
logger.info(f"Order status after waiting: {order_status}")
|
|
|
|
|
|
|
|
|
|
if order_status != 'Filled':
|
|
|
|
|
logger.error("Order was not filled.")
|
|
|
|
|
return response.json({"status": "error", "message": "Order was not filled."}, status=400)
|
|
|
|
|
|
|
|
|
|
# 3. Retrieve Execution Details: Retrieve execution details for the order
|
|
|
|
|
exec_filter = ExecutionFilter(clientId=0)
|
|
|
|
|
executions = await app_ib.reqExecutions(exec_filter)
|
|
|
|
|
for execution in executions:
|
|
|
|
|
if execution.orderId == trade.order.orderId:
|
|
|
|
|
logger.info(f"Execution details: {execution}")
|
|
|
|
|
|
|
|
|
|
# 4. Retrieve Latest Position Details: Retrieve the current positions from IB TWS API
|
|
|
|
|
positions = await app_ib.reqPositions() # Use reqPositions to get real-time positions
|
|
|
|
|
if not positions:
|
|
|
|
|
logger.error("No positions retrieved from IB TWS API.")
|
|
|
|
|
return response.json({"status": "error", "message": "Failed to retrieve positions from IB TWS API."}, status=400)
|
|
|
|
|
|
|
|
|
|
current_position = sum(pos.position for pos in positions if pos.contract.symbol == ib_ticker)
|
|
|
|
|
ticker_positions = {pos.contract.symbol: pos.position for pos in positions}
|
|
|
|
|
logger.info(f"Current Positions: {ticker_positions}")
|
|
|
|
|
|
|
|
|
|
return response.json({"status": "success", "message": "Order placed and position details retrieved."})
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error processing webhook: {e}")
|
|
|
|
|
return response.json({"status": "error", "message": f"Failed to process webhook: {str(e)}"}, status=500)
|
|
|
|
|
except InvalidUsage as e:
|
|
|
|
|
logger.error(f"Invalid JSON received: {e}")
|
|
|
|
|
return response.json({"status": "error", "message": "Invalid JSON format"}, status=400)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error processing webhook: {e}")
|
|
|
|
|
return response.json({"status": "error", "message": f"Failed to process webhook: {str(e)}"}, status=500)
|
|
|
|
|
return response.json({"status": "error", "message": "Invalid request method"}, status=405)
|
|
|
|
|
|
|
|
|
|
@app.route('/adjust_positions', methods=['POST'])
|
|
|
|
|
async def adjust_positions(request) -> response.HTTPResponse:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
try:
|
|
|
|
|
# Check if we need to reconnect with IB
|
|
|
|
|
await check_if_reconnect()
|
|
|
|
|
|
|
|
|
|
# Log the raw request body for debugging
|
|
|
|
|
raw_body = request.body.decode('utf-8')
|
|
|
|
|
logger.info(f"Received raw body: {raw_body}")
|
|
|
|
|
|
|
|
|
|
# Extract the required fields using regular expressions
|
|
|
|
|
action_pattern = r"order (?P<action>\w+) (?P<contracts>\d+) @ [\d\.]+ filled on (?P<ticker>\w+)"
|
|
|
|
|
position_pattern = r"New strategy position is (?P<position_size>-?\d+)"
|
|
|
|
|
|
|
|
|
|
action_match = re.search(action_pattern, raw_body)
|
|
|
|
|
position_match = re.search(position_pattern, raw_body)
|
|
|
|
|
|
|
|
|
|
if not action_match or not position_match:
|
|
|
|
|
logger.error("Failed to parse webhook message.")
|
|
|
|
|
return response.json({"status": "error", "message": "Failed to parse webhook message."}, status=400)
|
|
|
|
|
|
|
|
|
|
action = action_match.group("action").upper()
|
|
|
|
|
contracts = int(action_match.group("contracts"))
|
|
|
|
|
ticker = action_match.group("ticker")
|
|
|
|
|
position_size = int(position_match.group("position_size"))
|
|
|
|
|
|
|
|
|
|
logger.info(f"Parsed values - Action: {action}, Contracts: {contracts}, Ticker: {ticker}, Position Size: {position_size}")
|
|
|
|
|
|
|
|
|
|
# Determine the IB ticker based on the TradingView ticker
|
|
|
|
|
ib_ticker = None
|
|
|
|
|
if ticker == "SPX":
|
|
|
|
|
ib_ticker = "MES"
|
|
|
|
|
elif ticker == "NDX":
|
|
|
|
|
ib_ticker = "MNQ"
|
|
|
|
|
elif ticker == "HSI":
|
|
|
|
|
ib_ticker = "MHI"
|
|
|
|
|
elif ticker == "TOPIX":
|
|
|
|
|
ib_ticker = "MNTPX"
|
|
|
|
|
else:
|
|
|
|
|
logger.error(f"Invalid ticker: {ticker}")
|
|
|
|
|
return response.json({"status": "error", "message": "Invalid ticker"}, status=400)
|
|
|
|
|
|
|
|
|
|
# Get the current contract month
|
|
|
|
|
current_contract_month = get_next_contract_month(ib_ticker)
|
|
|
|
|
correct_contract = contract_type_check(ib_ticker, contract_month=current_contract_month)
|
|
|
|
|
rollover_needed = get_next_contract_month(ib_ticker, check_rollover=True)
|
|
|
|
|
|
|
|
|
|
positions = await app_ib.reqPositions()
|
|
|
|
|
positions_in_correct_month = [pos for pos in positions if pos.contract.lastTradeDateOrContractMonth == current_contract_month]
|
|
|
|
|
positions_not_in_correct_month = [pos for pos in positions if pos.contract.lastTradeDateOrContractMonth != current_contract_month]
|
|
|
|
|
|
|
|
|
|
# Contract month rollover if needed
|
|
|
|
|
if positions_not_in_correct_month:
|
|
|
|
|
logger.info(f"Contract month rollover needed: Adjusting positions to the correct contract month {current_contract_month}")
|
|
|
|
|
for pos in positions_not_in_correct_month:
|
|
|
|
|
close_action = "SELL" if pos.position > 0 else "BUY"
|
|
|
|
|
close_order = MarketOrder(close_action, abs(pos.position), account=app_ib.wrapper.accounts[0])
|
|
|
|
|
app_ib.placeOrder(pos.contract, close_order)
|
|
|
|
|
logger.info(f"Closed positions for outdated contract month {pos.contract.lastTradeDateOrContractMonth}")
|
|
|
|
|
|
|
|
|
|
# Open positions only for the difference between webhook and IB TWS positions
|
|
|
|
|
open_order_quantity = position_size - sum(pos.position for pos in positions_in_correct_month)
|
|
|
|
|
if open_order_quantity != 0:
|
|
|
|
|
open_order = MarketOrder("SELL" if open_order_quantity < 0 else "BUY", abs(open_order_quantity), account=app_ib.wrapper.accounts[0])
|
|
|
|
|
app_ib.placeOrder(correct_contract, open_order)
|
|
|
|
|
logger.info(f"Opened positions for the difference: {open_order_quantity} contracts of {ib_ticker} with contract month {current_contract_month}")
|
|
|
|
|
else:
|
|
|
|
|
logger.info(f"Contract month rollover check completed: Positions in IB TWS are using the correct contract month {current_contract_month}")
|
|
|
|
|
|
|
|
|
|
# Adjust Positions if Needed: Adjust positions to match the webhook
|
|
|
|
|
while True:
|
|
|
|
|
positions = await app_ib.reqPositions() # Use reqPositions to get real-time positions
|
|
|
|
|
if not positions:
|
|
|
|
|
logger.error("No positions retrieved from IB TWS API.")
|
|
|
|
|
return response.json({"status": "error", "message": "Failed to retrieve positions from IB TWS API."}, status=400)
|
|
|
|
|
|
|
|
|
|
current_position = sum(pos.position for pos in positions if pos.contract.symbol == ib_ticker)
|
|
|
|
|
ticker_positions = {pos.contract.symbol: pos.position for pos in positions}
|
|
|
|
|
logger.info(f"Current Positions: {ticker_positions}")
|
|
|
|
|
|
|
|
|
|
if current_position != position_size:
|
|
|
|
|
adjustment_quantity = position_size - current_position
|
|
|
|
|
adjustment_action = "SELL" if adjustment_quantity < 0 else "BUY"
|
|
|
|
|
adjustment_order = MarketOrder(adjustment_action, abs(adjustment_quantity), account=app_ib.wrapper.accounts[0])
|
|
|
|
|
logger.info(f"Adjusting Position: {adjustment_action} {abs(adjustment_quantity)} contracts of {ib_ticker}")
|
|
|
|
|
app_ib.placeOrder(correct_contract, adjustment_order)
|
|
|
|
|
logger.info(f"Position adjusted for {ib_ticker}: Before adjustment: {current_position}, After adjustment: {position_size}")
|
|
|
|
|
else:
|
|
|
|
|
logger.info(f"Position check completed: Positions in IB TWS match the positions from the webhook for contract month {current_contract_month}")
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
return response.json({"status": "success", "message": "Order placed and position adjusted if needed"})
|
|
|
|
|
except InvalidUsage as e:
|
|
|
|
|
logger.error(f"Invalid JSON received: {e}")
|
|
|
|
|
return response.json({"status": "error", "message": "Invalid JSON format"}, status=400)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error adjusting positions: {e}")
|
|
|
|
|
return response.json({"status": "error", "message": f"Failed to adjust positions: {str(e)}"}, status=500)
|
|
|
|
|
return response.json({"status": "error", "message": "Invalid request method"}, status=405)
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
try:
|
|
|
|
|
# Connect to IB on init
|
|
|
|
|
logger.info("Connecting to IB...")
|
|
|
|
|
app_ib.connect(HOST, PORT, clientId=0) # Using clientId=0 as Master Client
|
|
|
|
|
logger.info("Successfully Connected to IB")
|
|
|
|
|
|
|
|
|
|
# Set event handlers
|
|
|
|
|
set_event_handlers()
|
|
|
|
|
|
|
|
|
|
# Start the periodic update loop
|
|
|
|
|
asyncio.ensure_future(periodic_update())
|
|
|
|
|
|
|
|
|
|
app.run(port=8080)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Failed to start the application: {e}")
|