disparity_talk.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import time
  2. import requests
  3. from binance.client import Client
  4. # Binance API credentials
  5. api_key = 'WFIEaBuUafZ9ldAiMHLZ4Z1qHdVnpoDUVZM8MughMf6zmbSqGx5pEmQFBJysjwFp' # Optional for public data
  6. api_secret = 'C9HtmLk7fBCidyJI2lPtUbYhj4T0FdmJ0NE0LLipFiwUSR7euf1TEzJblEx1O6oA' # Optional for public data
  7. # Initialize Binance client
  8. client = Client(api_key, api_secret)
  9. # Define the threshold for disparity (e.g., 0.05 for 5%)
  10. DISPARITY_THRESHOLD = 0.05
  11. # Nextcloud Talk configuration
  12. NEXTCLOUD_URL = 'https://1984.algometic.com' # Replace with your Nextcloud URL
  13. NEXTCLOUD_USERNAME = 'larry' # Replace with your Nextcloud username
  14. NEXTCLOUD_PASSWORD = 'jEr6f-8Q5y5-nsSdA-koWsk-58jRT' # Replace with your app password
  15. TALK_CONVERSATION_TOKEN = 'jg65znzn' # Replace with your Talk conversation token
  16. # Function to fetch current prices of BTC and ETH
  17. def get_prices():
  18. btc_price = float(client.get_symbol_ticker(symbol='BTCUSDT')['price'])
  19. eth_price = float(client.get_symbol_ticker(symbol='ETHUSDT')['price'])
  20. return btc_price, eth_price
  21. # Function to calculate disparity
  22. def calculate_disparity(btc_price, eth_price):
  23. parity = eth_price / btc_price
  24. return parity
  25. # Function to send alert to Nextcloud Talk
  26. def send_alert_to_talk(message):
  27. talk_api_url = f"{NEXTCLOUD_URL}/ocs/v2.php/apps/spreed/api/v1/chat/{TALK_CONVERSATION_TOKEN}"
  28. headers = {
  29. 'OCS-APIRequest': 'true',
  30. 'Content-Type': 'application/json',
  31. 'Accept': 'application/json',
  32. }
  33. payload = {
  34. 'message': message,
  35. }
  36. try:
  37. response = requests.post(
  38. talk_api_url,
  39. headers=headers,
  40. json=payload,
  41. auth=(NEXTCLOUD_USERNAME, NEXTCLOUD_PASSWORD)
  42. )
  43. response.raise_for_status() # Raise an error for bad responses
  44. print(f"Alert sent to Nextcloud Talk: {message}")
  45. except requests.exceptions.RequestException as e:
  46. print(f"Failed to send alert to Nextcloud Talk: {e}")
  47. # Main monitoring loop
  48. def monitor_disparity():
  49. while True:
  50. try:
  51. btc_price, eth_price = get_prices()
  52. parity = calculate_disparity(btc_price, eth_price)
  53. print(f"BTC Price: {btc_price}, ETH Price: {eth_price}, Parity: {parity}")
  54. if parity > (1 + DISPARITY_THRESHOLD):
  55. send_alert_to_talk(f"ETH is overvalued compared to BTC! Parity: {parity}")
  56. elif parity < (1 - DISPARITY_THRESHOLD):
  57. send_alert_to_talk(f"ETH is undervalued compared to BTC! Parity: {parity}")
  58. time.sleep(60) # Check every 60 seconds
  59. except Exception as e:
  60. print(f"Error: {e}")
  61. time.sleep(60)
  62. if __name__ == "__main__":
  63. monitor_disparity()