FX & Indices Trader
Gold, NAS100, US30
Dashboard
Trade Ideas
Trading
Market Analysis
Live Analysis
Bot Coder
AI Chat
Trade History
Settings
© 2024 AI Trader Inc.
Toggle Sidebar
FX & Indices AI Trader
U
Bot Coder IDE
Develop Python trading bots with AI assistance. Manage files, generate a UI for your script's parameters, and preview it live.
Files
New File
New Folder
rsi_strategy.py
Code Editor
UI Preview
Generate with AI
Refine Code
Generate UI
Backtest
Download
import ccxt import time import os # --- Configuration --- # These variables can be controlled by the generated UI API_KEY = os.getenv('API_KEY', 'YOUR_API_KEY') API_SECRET = os.getenv('API_SECRET', 'YOUR_API_SECRET') SYMBOL = 'BTC/USDT' # The trading pair POSITION_SIZE = 0.001 # Amount of BTC to trade RSI_PERIOD = 14 RSI_OVERBOUGHT = 70 RSI_OVERSOLD = 30 # --- End Configuration --- def initialize_exchange(): """Initializes the exchange with API credentials.""" exchange = ccxt.binance({ 'apiKey': API_KEY, 'secret': API_SECRET, 'options': {'defaultType': 'future'}, }) return exchange def calculate_rsi(prices, period=14): """Calculates the Relative Strength Index (RSI).""" if len(prices) < period: return 50 deltas = [prices[i] - prices[i-1] for i in range(1, len(prices))] gains = [delta for delta in deltas if delta > 0] losses = [-delta for delta in deltas if delta < 0] avg_gain = sum(gains[:period]) / period if gains else 0 avg_loss = sum(losses[:period]) / period if losses else 1 if avg_loss == 0: return 100 rs = avg_gain / avg_loss return 100 - (100 / (1 + rs)) def main(): """Main trading loop.""" print("Initializing trading bot...") exchange = initialize_exchange() in_position = False print(f"Bot started. Monitoring {SYMBOL}...") while True: try: ohlcv = exchange.fetch_ohlcv(SYMBOL, '5m', limit=100) close_prices = [candle[4] for candle in ohlcv] current_rsi = calculate_rsi(close_prices, RSI_PERIOD) print(f"Current RSI for {SYMBOL} is {current_rsi:.2f}") if current_rsi > RSI_OVERBOUGHT and in_position: print(f"RSI is overbought. Selling position.") # order = exchange.create_market_sell_order(SYMBOL, POSITION_SIZE) # print("Sell order executed:", order) in_position = False elif current_rsi < RSI_OVERSOLD and not in_position: print(f"RSI is oversold. Buying position.") # order = exchange.create_market_buy_order(SYMBOL, POSITION_SIZE) # print("Buy order executed:", order) in_position = True print("Waiting for the next candle...") time.sleep(300) except Exception as e: print(f"An error occurred: {e}") break if __name__ == "__main__": main()