kroitor / asciichart

Nice-looking lightweight console ASCII line charts ╭┈╯ for NodeJS, browsers and terminal, no dependencies
MIT License
1.83k stars 94 forks source link

Python Port Docs #78

Open jonnyhoff opened 1 year ago

jonnyhoff commented 1 year ago

Hiya,

Please add the Python port docs.

Cheers, Jonny

Rolladex commented 9 months ago
import time
import ccxt
import asciichartpy

#Define a dictionary for your crypto information
cryptocom = {
    "exchange_name": 'cryptocom',  # Use the exchange name as a string
    "pairs": ["ETH/BTC", "ETH/USD"]
}

#List of pairs
pairs = cryptocom["pairs"]

#Initialize the exchange using the exchange name
exchange = getattr(ccxt, cryptocom["exchange_name"])()

#Function to get and print the spot price
def get_spot_price(pair):
    ticker = exchange.fetch_ticker(pair)
    spot_price = ticker['last']
    print(f"Spot price of {pair}: {spot_price}")
    return spot_price

#Get and print your ETH balance in USD
get_USD = ETH_BAL * get_spot_price(pairs[2])
print(f"Your ETH balance in USD is {get_USD}")

#Refactored code for updating ETH spot price
interval = 30
ETH_SPOT = []
plot_data = []

def fetch_and_update_spot_price():
    while True:
        ticker = get_spot_price(pairs[2])
        ETH_SPOT.append(ticker)

        if len(ETH_SPOT) == 2:
            change = ETH_SPOT[1] - ETH_SPOT[0]
            plot_data.append(change)
            s = plot_data
            chart = asciichartpy.plot(s, {'height': 10, 'padding': '      ', 'offset': 5})
            print(chart)
            ETH_SPOT.pop(0)

        time.sleep(0.5)

        #Sleep for 30 seconds, not in a loop
        time.sleep(interval)

#Call the function to start updating the spot price
fetch_and_update_spot_price()