market update
This commit is contained in:
55
market.py
55
market.py
@ -45,29 +45,26 @@ MARKETS = [
|
||||
# Global variable to store latest prices with timestamps
|
||||
latest_prices = {}
|
||||
|
||||
# Debug flag to control printing
|
||||
DEBUG = False
|
||||
|
||||
def print_market_info(market_info):
|
||||
if DEBUG:
|
||||
def print_market_info(market_info, debug=False):
|
||||
if debug:
|
||||
timestamp = datetime.now().strftime("%H:%M:%S")
|
||||
print('[{0}] {1} : {2:.4f}'.format(timestamp, market_info["market"], market_info["price"]))
|
||||
|
||||
|
||||
def market_info():
|
||||
def market_info(debug=False):
|
||||
"""Fetch market information once and update global prices"""
|
||||
client = Client(
|
||||
eth_private_key=PRIVATE_KEY,
|
||||
rpc_url=RPC_URL
|
||||
)
|
||||
|
||||
if DEBUG:
|
||||
if debug:
|
||||
print("Fetching market data...\n")
|
||||
|
||||
for market in MARKETS:
|
||||
try:
|
||||
market_info_data = client.public.get_market_info(market)
|
||||
print_market_info(market_info_data)
|
||||
print_market_info(market_info_data, debug)
|
||||
|
||||
# Store the latest price and timestamp in global dictionary
|
||||
market_name = market_info_data["market"]
|
||||
@ -78,32 +75,41 @@ def market_info():
|
||||
'timestamp_str': current_timestamp.strftime("%Y-%m-%d %H:%M:%S")
|
||||
}
|
||||
|
||||
time.sleep(2) # Wait 2 seconds between requests
|
||||
time.sleep(2) # Wait 2 seconds between requests to avoid rate limiting
|
||||
|
||||
except Exception as e:
|
||||
if DEBUG:
|
||||
if debug:
|
||||
print(f"Error fetching market {market}: {e}")
|
||||
print('-' * 50)
|
||||
# Skip this market and continue with the next one
|
||||
continue
|
||||
|
||||
def market_info_loop():
|
||||
def market_info_loop(debug=False):
|
||||
"""Continuously fetch market information in a loop"""
|
||||
if DEBUG:
|
||||
if debug:
|
||||
print("Starting market data loop - press Ctrl+C to stop")
|
||||
print("-" * 50)
|
||||
|
||||
try:
|
||||
while True:
|
||||
market_info()
|
||||
except KeyboardInterrupt:
|
||||
if DEBUG:
|
||||
print("\nMarket data loop stopped by user")
|
||||
try:
|
||||
market_info(debug)
|
||||
# if debug:
|
||||
# print(f"\nSleeping for 30 seconds...\n")
|
||||
# time.sleep(30) # Wait 30 seconds between full cycles
|
||||
except Exception as e:
|
||||
if debug:
|
||||
print(f"Error in market_info cycle: {e}")
|
||||
print("Waiting 15 seconds before retry...")
|
||||
time.sleep(15) # Wait 15 seconds before retrying on error
|
||||
continue
|
||||
# except KeyboardInterrupt:
|
||||
# if debug:
|
||||
# print("\nMarket data loop stopped by user")
|
||||
except Exception as e:
|
||||
if DEBUG:
|
||||
print(f"An error occurred: {e}")
|
||||
|
||||
def set_debug(enabled):
|
||||
"""Enable or disable debug printing"""
|
||||
global DEBUG
|
||||
DEBUG = enabled
|
||||
if debug:
|
||||
print(f"Fatal error occurred: {e}")
|
||||
print("Market data loop stopped")
|
||||
|
||||
def get_latest_price(market_name):
|
||||
"""Get the latest price for a specific market"""
|
||||
@ -124,5 +130,4 @@ def get_all_latest_prices_only():
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Enable debug when running directly
|
||||
set_debug(True)
|
||||
market_info_loop()
|
||||
market_info_loop(debug=True)
|
||||
Reference in New Issue
Block a user