Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
import ccxt
import pandas as pd
import time
Replace these with your actual API keys
api_key = 'YOUR_API_KEY'
api_secret = 'YOUR_API_SECRET'
Initialize the exchange
exchange = ccxt.binance({
'apiKey': api_key,
'secret': api_secret,
})
Define the trading parameters
symbol = 'BTC/USDT'
timeframe = '1h'
limit = 100
sma_period = 20
def fetch_data():
# Fetch historical data
ohlcv = exchange.fetch_ohlcv(symbol, timeframe, limit=limit)
df = pd.DataFrame(ohlcv, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
df.set_index('timestamp', inplace=True)
return df
def calculate_sma(df):
df['sma'] = df['close'].rolling(window=sma_period).mean()
return df
def trade_logic(df):
last_row = df.iloc[-1]
if last_row['close'] > last_row['sma']:
return 'buy'
elif last_row['close'] < last_row['sma']:
return 'sell'
return 'hold'
def execute_trade(signal):
if signal == 'buy':
print("Placing buy order")
exchange.create_market_buy_order(symbol, 0.001) # Adjust the amount as needed
elif signal == 'sell':
print("Placing sell order")
exchange.create_market_sell_order(symbol, 0.001) # Adjust the amount as needed
def main():
while True:
df = fetch_data()
df = calculate_sma(df)
signal = trade_logic(df)
execute_trade(signal)
time.sleep(60 * 60) # Sleep for an hour before running again
if name == "main":
main()