Add logging to iron condor entry script

This commit is contained in:
moshferatu 2023-12-07 08:04:57 -08:00
parent bb8ad7c23e
commit b13e5d1525

View File

@ -1,3 +1,6 @@
import logging
import traceback
from datetime import datetime from datetime import datetime
from dotenv import load_dotenv from dotenv import load_dotenv
from ibkr import Client, OptionLeg from ibkr import Client, OptionLeg
@ -8,6 +11,13 @@ from os import getenv
load_dotenv() load_dotenv()
def enter_iron_condor(): def enter_iron_condor():
logging.basicConfig(filename=f'iron_condor_{datetime.now().strftime("%H%M")}.log', level=logging.INFO)
try:
_enter_iron_condor()
except Exception as e:
logging.error("Error: %s", traceback.format_exc())
def _enter_iron_condor():
ibkr_host = getenv('IBKR_HOST') ibkr_host = getenv('IBKR_HOST')
ibkr_port = getenv('IBKR_PORT') ibkr_port = getenv('IBKR_PORT')
ibkr_client = Client(host = ibkr_host, port = ibkr_port) ibkr_client = Client(host = ibkr_host, port = ibkr_port)
@ -27,7 +37,7 @@ def enter_iron_condor():
# The weekly symbol for SPX (SPXW) is required in order to distinguish from monthly options. # The weekly symbol for SPX (SPXW) is required in order to distinguish from monthly options.
option_chain = ibkr_client.get_option_chain(symbol, expiration, sub_symbol = sub_symbol, contract_filter = contract_filter) option_chain = ibkr_client.get_option_chain(symbol, expiration, sub_symbol = sub_symbol, contract_filter = contract_filter)
print(option_chain) logging.info(option_chain)
target_delta = 0.10 target_delta = 0.10
@ -59,10 +69,10 @@ def enter_iron_condor():
short_call_strike = float(short_call_contract['Strike']) short_call_strike = float(short_call_contract['Strike'])
long_call_strike = float(long_call_contract['Strike']) long_call_strike = float(long_call_contract['Strike'])
print('Short Put Strike:', short_put_strike) logging.info(f'Short Put Strike: {short_put_strike}')
print('Long Put Strike:', long_put_strike) logging.info(f'Long Put Strike: {long_put_strike}')
print('Short Call Strike:', short_call_strike) logging.info(f'Short Call Strike: {short_call_strike}')
print('Long Call Strike:', long_call_strike) logging.info(f'Long Call Strike: {long_call_strike}')
short_call_leg = OptionLeg(symbol, expiration, short_call_strike, CALL, SELL, sub_symbol) short_call_leg = OptionLeg(symbol, expiration, short_call_strike, CALL, SELL, sub_symbol)
long_call_leg = OptionLeg(symbol, expiration, long_call_strike, CALL, BUY, sub_symbol) long_call_leg = OptionLeg(symbol, expiration, long_call_strike, CALL, BUY, sub_symbol)