import ccxt
import pandas as pd
import numpy as np
# Exchange setup
exchange = ccxt.binance({
'apiKey': 'YOUR_API_KEY',
'secret': 'YOUR_SECRET',
})
symbol = 'XAU/USD'
timeframe = '30m'
balance = 1000 # Example balance in USD
risk_percent = 0.01 # 1% risk
# Fetch data
data = exchange.fetch_ohlcv(symbol, timeframe, limit=100)
df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
# Indicators
df['ma_short'] = df['close'].rolling(5).mean()
df['ma_long'] = df['close'].rolling(20).mean()
df['rsi'] = 100 - (100 / (1 + df['close'].diff(1).apply(lambda x: max(x, 0)).rolling(14).mean() / df['close'].diff(1).apply(lambda x: abs(x)).rolling(14).mean()))
df['atr'] = df[['high', 'low', 'close']].apply(lambda row: max(row.high - row.low, abs(row.high - row.close), abs(row.low - row.close)), axis=1).rolling(14).mean()
# Trading logic
last_row = df.iloc[-1]
if last_row['ma_short'] > last_row['ma_long'] and last_row['rsi'] < 30:
# Buy signal
entry_price = last_row['close']
stop_loss = last_row['close'] - (1.5 * last_row['atr'])
take_profit = entry_price * 1.02
stop_loss_pips = (1.5 * last_row['atr'])
position_size = (balance * risk_percent) / stop_loss_pips
lot_size = position_size / last_row['close']
print(f"Buy XAU/USD at {entry_price} with {lot_size:.2f} lots")
print(f"Stop-loss: {stop_loss}, Take-profit: {take_profit}")
elif last_row['ma_short'] < last_row['ma_long'] and last_row['rsi'] > 70:
# Sell signal
entry_price = last_row['close']
stop_loss = last_row['close'] + (1.5 * last_row['atr'])
take_profit = entry_price * 0.98
stop_loss_pips = (1.5 * last_row['atr'])
position_size = (balance * risk_percent) / stop_loss_pips
lot_size = position_size / last_row['close']
print(f"Sell XAU/USD at {entry_price} with {lot_size:.2f} lots")
print(f"Stop-loss: {stop_loss}, Take-profit: {take_profit}")
Подробнее здесь: https://stackoverflow.com/questions/798 ... des-for-me