Trading Algorithm Code

Our script

# main.py
import requests
from sell import sell_asset
from buy import buy_asset
from google_sheets import log_transaction

from strategy import strategy, Refresh_Time
import time  # Module to control intervals

Run = True  # Variable to start and pause the script

Refresh_Time = 1800  # The script waits this long before it goes through again, 3600 seconds = 1 hour

# Define the percentage of USDT to use for buying
buy_percentage = 0.2

api_key = "Add here your KEY"
secret_key = "Add here your KEY"

# List of API URLs to check
api_urls = ['https://api1.binance.com', 'https://api2.binance.com', 'https://api3.binance.com']

# Initialize the url variable
url = None

# Iterate through the API URLs
for api_url in api_urls:
    try:
        # Attempt to send a GET request to the API
        resp = requests.get(api_url)
        # Check if the status code is 200 (success)
        if resp.status_code == 200:
            # Set the url variable to the URL of the successful API
            url = api_url
            # Break the loop if a working API is found
            break
    except:
        # Continue if an exception occurs
        pass

# Check if a working API URL was found
if not url:
    print("All APIs are unreachable.")
    exit()

def order_signal(order_signal, api_key, secret_key, api_url):
    # Split the trading pair and the action
    symbol, action = order_signal.split(":")
    
    # Execute the corresponding action based on the signal
    if action == "BUY":
        print("Kaufsignal")
        response = buy_asset(symbol, api_url, api_key, secret_key, buy_percentage)
    elif action == "SELL":
        print("Verkaufssignal")
        response = sell_asset(symbol, api_url, api_key, secret_key)
    else:
        print("Ungültige Aktion")
        return

    if response:
        log_transaction(response, action, api_key, secret_key, api_url)

# structure main-file

symbols = ["HBARUSDT", "BTCUSDT", "ETHUSDT", "ALGOUSDT", "LINKUSDT"]

while Run == True:
    for symbol in symbols:
        order = strategy(symbol)
        if order:
            print(order)
            order_signal(order, api_key, secret_key, url)
    time.sleep(Refresh_Time)  # Then the loop is paused for the duration of Refresh_Time

Last updated

Was this helpful?