Created
January 14, 2023 16:02
-
-
Save IperGiove/060d5ab44cfb290570c6c46bc88cffee to your computer and use it in GitHub Desktop.
testing gpt to create a script to trade with binance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import typing | |
from binance.client import Client | |
import talib | |
def connect_to_binance(api_key: str, api_secret: str) -> Client: | |
""" | |
Connects to the Binance API using the provided API key and secret. | |
Returns a Binance client object. | |
""" | |
return Client(api_key, api_secret) | |
def get_historical_data(client: Client, symbol: str, interval: str) -> dict: | |
""" | |
Fetches historical data for a specific cryptocurrency on Binance. | |
Returns the historical data as a dictionary. | |
""" | |
return client.fetch_ohlcv(symbol, interval) | |
def moving_average_strategy(data: dict, symbol: str, quantity: int, short_window: int, long_window: int) -> None: | |
""" | |
Executes trades based on the moving average crossover strategy. | |
Does not return anything. | |
""" | |
close_prices = [x[4] for x in data] | |
short_ma = talib.SMA(close_prices, timeperiod=short_window) | |
long_ma = talib.SMA(close_prices, timeperiod=long_window) | |
# Check if short MA crosses above long MA (buy signal) | |
if short_ma[-2] <= long_ma[-2] and short_ma[-1] > long_ma[-1]: | |
client.create_order(symbol=symbol, side=Client.SIDE_BUY, type=Client.ORDER_TYPE_MARKET, quantity=quantity) | |
# Check if short MA crosses below long MA (sell signal) | |
elif short_ma[-2] >= long_ma[-2] and short_ma[-1] < long_ma[-1]: | |
client.create_order(symbol=symbol, side=Client.SIDE_SELL, type=Client.ORDER_TYPE_MARKET, quantity=quantity) | |
def rsi_strategy(data: dict, symbol: str, quantity: int, period: int) -> None: | |
""" | |
Executes trades based on the relative strength index (RSI) strategy. | |
Does not return anything. | |
""" | |
close_prices = [x[4] for x in data] | |
rsi = talib.RSI(close_prices, timeperiod=period) | |
# Check if RSI is above 70 (overbought, sell signal) | |
if rsi[-1] > 70: | |
client.create_order(symbol=symbol, side=Client.SIDE_SELL, type=Client.ORDER_TYPE_MARKET, quantity=quantity) | |
# Check if RSI is below 30 (oversold, buy signal) | |
elif rsi[-1] < 30: | |
client.create_order(symbol=symbol, side=Client.SIDE_BUY, type=Client.ORDER_TYPE_MARKET, quantity=quantity) | |
# Example usage: | |
api_key = 'YOUR_API_KEY' | |
api_secret = 'YOUR_API_SECRET' | |
symbol = 'BTCUSDT' | |
interval = '1h' | |
quantity = 1 | |
client = connect_to_binance(api_key, api_secret) | |
data = get_historical_data(client, symbol, interval) | |
# Execute trades based on moving average strategy | |
moving_average_strategy(data, symbol, quantity, 20, 50) | |
# Execute trades based on RSI strategy | |
rsi_strategy(data, symbol, quantity, 14) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment