Fix day_trade_count attribute error and add connection reset error handling for API calls
This commit is contained in:
+25
-1
@@ -903,7 +903,13 @@ def advanced_signal_generator(symbol):
|
|||||||
|
|
||||||
def wait_until_market_open():
|
def wait_until_market_open():
|
||||||
# Wait until the market opens
|
# Wait until the market opens
|
||||||
|
try:
|
||||||
clock = api.get_clock()
|
clock = api.get_clock()
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"⚠️ Failed to get clock: {e}")
|
||||||
|
time.sleep(60)
|
||||||
|
return
|
||||||
|
|
||||||
now = clock.timestamp
|
now = clock.timestamp
|
||||||
next_open = clock.next_open
|
next_open = clock.next_open
|
||||||
|
|
||||||
@@ -949,7 +955,7 @@ def get_day_trade_count():
|
|||||||
# Get the current day trade count
|
# Get the current day trade count
|
||||||
try:
|
try:
|
||||||
account = api.get_account()
|
account = api.get_account()
|
||||||
return int(account.day_trade_count)
|
return int(account.daytrade_count)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"❌ Failed to fetch day trade count: {e}")
|
logger.error(f"❌ Failed to fetch day trade count: {e}")
|
||||||
return 0
|
return 0
|
||||||
@@ -1138,6 +1144,7 @@ def pdt_allows_new_trade():
|
|||||||
|
|
||||||
def get_market_status():
|
def get_market_status():
|
||||||
# Get current market status
|
# Get current market status
|
||||||
|
try:
|
||||||
clock = api.get_clock()
|
clock = api.get_clock()
|
||||||
status = "open" if clock.is_open else "closed"
|
status = "open" if clock.is_open else "closed"
|
||||||
next_event = clock.next_open if not clock.is_open else clock.next_close
|
next_event = clock.next_open if not clock.is_open else clock.next_close
|
||||||
@@ -1149,6 +1156,14 @@ def get_market_status():
|
|||||||
"event_type": event_type,
|
"event_type": event_type,
|
||||||
"timestamp": clock.timestamp
|
"timestamp": clock.timestamp
|
||||||
}
|
}
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"⚠️ Failed to get market status: {e}")
|
||||||
|
return {
|
||||||
|
"status": "unknown",
|
||||||
|
"next_event": None,
|
||||||
|
"event_type": "unknown",
|
||||||
|
"timestamp": datetime.now()
|
||||||
|
}
|
||||||
|
|
||||||
def calculate_position_size(equity, stop_loss, entry_price, regime='normal'):
|
def calculate_position_size(equity, stop_loss, entry_price, regime='normal'):
|
||||||
# Calculate position size based on fixed risk per trade
|
# Calculate position size based on fixed risk per trade
|
||||||
@@ -1392,10 +1407,15 @@ def main():
|
|||||||
# Trading session loop
|
# Trading session loop
|
||||||
while True:
|
while True:
|
||||||
# Check market still open
|
# Check market still open
|
||||||
|
try:
|
||||||
clock = api.get_clock()
|
clock = api.get_clock()
|
||||||
if not clock.is_open:
|
if not clock.is_open:
|
||||||
logger.info("❌ Market closed")
|
logger.info("❌ Market closed")
|
||||||
break
|
break
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"⚠️ Clock check failed: {e}")
|
||||||
|
time.sleep(60)
|
||||||
|
continue
|
||||||
|
|
||||||
# Check day changed
|
# Check day changed
|
||||||
if datetime.now().date() != current_date:
|
if datetime.now().date() != current_date:
|
||||||
@@ -1528,7 +1548,11 @@ def main():
|
|||||||
|
|
||||||
# Status display
|
# Status display
|
||||||
position_status = f"{position_type.upper()}" if position_active else "FLAT"
|
position_status = f"{position_type.upper()}" if position_active else "FLAT"
|
||||||
|
try:
|
||||||
current_time = clock.timestamp.strftime("%I:%M:%S %p")
|
current_time = clock.timestamp.strftime("%I:%M:%S %p")
|
||||||
|
except:
|
||||||
|
current_time = datetime.now().strftime("%I:%M:%S %p")
|
||||||
|
|
||||||
hourly_trend = check_multiframe_confluence(SYMBOL)
|
hourly_trend = check_multiframe_confluence(SYMBOL)
|
||||||
|
|
||||||
status_msg = f"⏱️ {current_time} | {position_status} | {regime.upper()}"
|
status_msg = f"⏱️ {current_time} | {position_status} | {regime.upper()}"
|
||||||
|
|||||||
Reference in New Issue
Block a user