- Introduced the CryptoComTrader class to handle WebSocket connections for real-time trading data and operations. - Implemented methods for fetching price, order book, user balance, and placing orders. - Added functionality to retrieve candlestick data and available trading instruments. - Created a main script to initialize the trader, fetch instruments, and display candlestick data in a loop. - Integrated Plotly for visualizing candlestick data, enhancing user interaction and data representation.
85 lines
2.3 KiB
Python
85 lines
2.3 KiB
Python
import time
|
|
import plotly.graph_objs as go
|
|
import plotly.io as pio
|
|
from cryptocom_trader import CryptoComTrader
|
|
|
|
|
|
def plot_candlesticks(df):
|
|
if df.empty:
|
|
print("No data to plot.")
|
|
return None
|
|
|
|
# Convert columns to float
|
|
for col in ['open', 'high', 'low', 'close', 'volume']:
|
|
df[col] = df[col].astype(float)
|
|
|
|
# Plotly expects datetime for x-axis
|
|
fig = go.Figure(data=[go.Candlestick(
|
|
x=df['timestamp'],
|
|
open=df['open'],
|
|
high=df['high'],
|
|
low=df['low'],
|
|
close=df['close'],
|
|
increasing_line_color='#089981',
|
|
decreasing_line_color='#F23645'
|
|
)])
|
|
|
|
fig.update_layout(
|
|
title='BTC/USDC Realtime Candlestick (1m)',
|
|
yaxis_title='Price (USDC)',
|
|
xaxis_title='Time',
|
|
xaxis_rangeslider_visible=False,
|
|
template='plotly_dark'
|
|
)
|
|
return fig
|
|
|
|
|
|
def main():
|
|
trader = CryptoComTrader()
|
|
pio.renderers.default = "browser" # Open in browser
|
|
|
|
# Fetch and print BTC/USDC-related instruments
|
|
instruments = trader.get_instruments()
|
|
btc_usdc_instruments = [
|
|
inst for inst in instruments
|
|
if (
|
|
('BTC' in inst.get('base_ccy', '') or 'BTC' in inst.get('base_currency', '')) and
|
|
('USDC' in inst.get('quote_ccy', '') or 'USDC' in inst.get('quote_currency', ''))
|
|
)
|
|
]
|
|
print("BTC/USDC-related instruments:")
|
|
for inst in btc_usdc_instruments:
|
|
print(inst)
|
|
|
|
# Optionally, show balance (private API)
|
|
try:
|
|
balance = trader.get_balance("USDC")
|
|
print("USDC Balance:", balance)
|
|
except Exception as e:
|
|
print("[WARN] Could not fetch balance (private API):", e)
|
|
|
|
all_instruments = trader.get_instruments()
|
|
for inst in all_instruments:
|
|
print(inst)
|
|
|
|
while True:
|
|
try:
|
|
df = trader.get_candlesticks(timeframe='1m', count=60)
|
|
# fig = plot_candlesticks(df)
|
|
# if fig:
|
|
# fig.show()
|
|
if not df.empty:
|
|
print(df[['high', 'low', 'open', 'close', 'volume']])
|
|
else:
|
|
print("No data to print.")
|
|
time.sleep(10)
|
|
except KeyboardInterrupt:
|
|
print('Exiting...')
|
|
break
|
|
except Exception as e:
|
|
print(f'Error: {e}')
|
|
time.sleep(10)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|