Algorithmic Trading A-z With Python- Machine Le... | 480p |
if prob > 0.6 and position == 0: # Buy position = capital / current_price capital = 0 elif prob < 0.4 and position > 0: # Sell capital = position * current_price position = 0
A 51% accuracy is phenomenal in finance. If you see 99% accuracy, you have look-ahead bias (leaked future data into your training set). Part F: Backtesting the ML Strategy Accuracy doesn't pay bills. Profit does. You need to simulate trading based on the model's confidence. Algorithmic Trading A-Z with Python- Machine Le...
# Predict probabilities probabilities = model.predict_proba(X_test)[:, 1] # Probability of class "1" (Up) 1. If probability > 0.6 -> Buy $10,000 2. If probability < 0.4 -> Short $10,000 3. Else -> Do nothing capital = 100000 position = 0 equity_curve = [] if prob > 0
Predict whether the price will go up (1) or down (0) in the next 5 minutes. Profit does
print(data[['Close', 'Volatility', 'BB_upper']].tail())
import pandas as pd import yfinance as yf import numpy as np data = yf.download('AAPL', start='2019-01-01', end='2024-01-01') Calculate essential features data['Returns'] = data['Close'].pct_change() data['Log_Returns'] = np.log(1 + data['Returns']) data['Volatility'] = data['Returns'].rolling(20).std() * np.sqrt(252) Feature Engineering (The secret sauce) data['SMA_20'] = data['Close'].rolling(20).mean() data['BB_upper'] = data['SMA_20'] + (data['Close'].rolling(20).std() * 2) data['BB_lower'] = data['SMA_20'] - (data['Close'].rolling(20).std() * 2)
trading_client = TradingClient(API_KEY, SECRET_KEY)