Using ta library:
import ta
data['rsi'] = ta.momentum.RSIIndicator(data['Close'], window=14).rsi() data['macd'] = ta.trend.MACD(data['Close']).macd() data['bb_high'] = ta.volatility.BollingerBands(data['Close']).bollinger_hband() data['bb_low'] = ta.volatility.BollingerBands(data['Close']).bollinger_lband() data['volume_ratio'] = data['Volume'] / data['Volume'].rolling(20).mean()
Add target variable (future return):
data['target'] = data['Close'].shift(-1) / data['Close'] - 1 # next day return
You cannot trade what you cannot test. Use Python to fetch data:
import yfinance as yf import pandas as pdimport time from alpaca.trading.client import TradingClientAPI_KEY = "your_key" SECRET_KEY = "your_secret"
trading_client = TradingClient(API_KEY, SECRET_KEY) Algorithmic Trading A-Z with Python- Machine Le...
def live_run(): while True: # 1. Fetch latest 5-minute bars latest_data = fetch_recent_bars()
# 2. Engineer features (same as training) features = engineer_features(latest_data) # 3. Predict direction prob = model.predict_proba(features)[0, 1] # 4. Risk Management if prob > 0.65 and get_current_position() == 0: submit_order(symbol="AAPL", qty=10, side="buy") # 5. Wait for next iteration time.sleep(60) # Run every minute
account = trading_client.get_account() order = trading_client.submit_order( symbol='AAPL', qty=10, side='buy', type='market', time_in_force='gtc' )
Participants in this course typically engage with the following technology stack: Using ta library: import ta data['rsi'] = ta
data = yf.download('AAPL', start='2020-01-01', end='2024-01-01') data['Returns'] = data['Close'].pct_change() print(data.head())
Alternatives: Alpha Vantage (free tier), Polygon.io (professional), Binance API (crypto).
test_data = data.iloc[split:].copy()
test_data['prediction'] = preds
test_data['signal'] = 0 # 1 = buy, -1 = sell
test_data.loc[test_data['prediction'] == 1, 'signal'] = 1
test_data.loc[test_data['prediction'] == 0, 'signal'] = -1