Initialize Empty Commit

This commit is contained in:
klein panic
2024-12-13 02:58:54 -05:00
parent bd407a6951
commit 0378714eef
42 changed files with 0 additions and 188484 deletions

View File

@@ -1,184 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"id": "69d88f26-f288-4a23-8be5-3e8317e23731",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR -1 2104 Market data farm connection is OK:usfarm.nj\n",
"ERROR -1 2104 Market data farm connection is OK:usfuture\n",
"ERROR -1 2104 Market data farm connection is OK:cashfarm\n",
"ERROR -1 2104 Market data farm connection is OK:usfarm\n",
"ERROR -1 2106 HMDS data farm connection is OK:ushmds\n",
"ERROR -1 2158 Sec-def data farm connection is OK:secdefnj\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Historical Data Ended\n",
" Date Open High Low Close Volume\n",
"0 20241030 18:00:00 69.10 69.10 68.96 69.02 378\n",
"1 20241030 18:05:00 69.02 69.07 69.01 69.05 99\n",
"2 20241030 18:10:00 69.06 69.07 69.01 69.01 103\n",
"3 20241030 18:15:00 69.01 69.02 69.00 69.00 54\n",
"4 20241030 18:20:00 69.01 69.01 68.99 69.00 25\n",
"5 20241030 18:25:00 69.00 69.05 69.00 69.04 40\n",
"6 20241030 18:30:00 69.05 69.05 69.03 69.03 63\n",
"7 20241030 18:35:00 69.03 69.03 69.00 69.00 64\n",
"8 20241030 18:40:00 68.99 69.01 68.98 68.99 60\n",
"9 20241030 18:45:00 68.99 68.99 68.95 68.97 66\n",
"10 20241030 18:50:00 68.97 69.00 68.96 68.99 44\n",
"11 20241030 18:55:00 68.98 68.98 68.97 68.98 23\n",
"12 20241030 19:00:00 68.98 69.02 68.98 69.01 48\n",
"13 20241030 19:05:00 69.02 69.03 69.00 69.01 31\n",
"14 20241030 19:10:00 69.02 69.02 69.00 69.00 22\n",
"15 20241030 19:15:00 69.00 69.00 68.99 68.99 11\n",
"16 20241030 19:20:00 68.99 68.99 68.95 68.95 40\n",
"17 20241030 19:25:00 68.95 68.95 68.94 68.94 55\n",
"18 20241030 19:30:00 68.94 68.96 68.93 68.95 54\n",
"19 20241030 19:35:00 68.95 68.97 68.95 68.96 29\n",
"20 20241030 19:40:00 68.96 68.98 68.96 68.98 47\n",
"21 20241030 19:45:00 68.98 68.99 68.95 68.95 65\n",
"22 20241030 19:50:00 68.96 68.98 68.96 68.97 16\n",
"23 20241030 19:55:00 68.97 68.97 68.94 68.94 35\n",
"24 20241030 20:00:00 68.95 68.99 68.91 68.92 369\n",
"25 20241030 20:05:00 68.91 68.94 68.91 68.93 74\n",
"26 20241030 20:10:00 68.93 68.95 68.89 68.94 187\n",
"27 20241030 20:15:00 68.94 68.95 68.92 68.94 81\n",
"28 20241030 20:20:00 68.95 68.97 68.94 68.96 89\n",
"29 20241030 20:25:00 68.96 68.96 68.92 68.94 96\n",
"30 20241030 20:30:00 68.94 68.98 68.93 68.96 94\n",
"31 20241030 20:35:00 68.97 68.97 68.93 68.94 66\n",
"32 20241030 20:40:00 68.95 68.95 68.93 68.94 44\n",
"33 20241030 20:45:00 68.93 68.96 68.93 68.94 98\n",
"34 20241030 20:50:00 68.94 68.94 68.92 68.92 95\n"
]
}
],
"source": [
"from ibapi.client import EClient\n",
"from ibapi.wrapper import EWrapper\n",
"from ibapi.contract import Contract\n",
"import threading\n",
"import time\n",
"import pandas as pd\n",
"\n",
"# Define the IB API app\n",
"class IBApi(EWrapper, EClient):\n",
" def __init__(self):\n",
" EClient.__init__(self, self)\n",
" self.data = [] # Initialize an empty list to store data\n",
"\n",
" # Override the historicalData function to process and store incoming data\n",
" def historicalData(self, reqId, bar):\n",
" # Append the data as a dictionary to self.data\n",
" self.data.append({\n",
" \"Date\": bar.date,\n",
" \"Open\": bar.open,\n",
" \"High\": bar.high,\n",
" \"Low\": bar.low,\n",
" \"Close\": bar.close,\n",
" \"Volume\": bar.volume\n",
" })\n",
"\n",
" def historicalDataEnd(self, reqId, start, end):\n",
" print(\"Historical Data Ended\")\n",
" # Convert the data to a DataFrame when data collection is complete\n",
" self.df = pd.DataFrame(self.data)\n",
" print(self.df) # Display the DataFrame to verify\n",
" self.disconnect() # Disconnect after data collection is complete\n",
"\n",
"# Define the app handler for running in the notebook\n",
"class IBApp:\n",
" def __init__(self):\n",
" self.app = IBApi()\n",
"\n",
" def connect(self):\n",
" self.app.connect(\"127.0.0.1\", 7496, 0) # Change port if needed\n",
" thread = threading.Thread(target=self.run_app, daemon=True)\n",
" thread.start()\n",
" time.sleep(1) # Allow time for the connection to establish\n",
"\n",
" def run_app(self):\n",
" self.app.run()\n",
"\n",
" def request_oil_data(self):\n",
" # Define the contract for Crude Oil Futures\n",
" contract = Contract()\n",
" contract.symbol = \"CL\"\n",
" contract.secType = \"FUT\"\n",
" contract.exchange = \"NYMEX\"\n",
" contract.currency = \"USD\"\n",
" contract.lastTradeDateOrContractMonth = \"202412\" # Example: Dec 2024 contract\n",
"\n",
" # Request historical data\n",
" self.app.reqHistoricalData(\n",
" reqId=1,\n",
" contract=contract,\n",
" endDateTime='',\n",
" durationStr='1 D', # 1 month\n",
" barSizeSetting='5 mins',\n",
" whatToShow='TRADES',\n",
" useRTH=0,\n",
" formatDate=1,\n",
" keepUpToDate=False,\n",
" chartOptions=[]\n",
" )\n",
"\n",
" def disconnect(self):\n",
" self.app.disconnect()\n",
"\n",
"# Create an instance and connect\n",
"app = IBApp()\n",
"app.connect()\n",
"\n",
"# Request data and output to a DataFrame\n",
"app.request_oil_data()\n",
"\n",
"# Wait for data retrieval to complete\n",
"time.sleep(10)\n",
"\n",
"# Access the DataFrame\n",
"df = app.app.df if hasattr(app.app, 'df') else pd.DataFrame()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "2088c621-81d3-46f0-8596-ce05d1a89fd4",
"metadata": {},
"outputs": [],
"source": [
"data = df.to_csv()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

View File

@@ -1,246 +0,0 @@
import numpy as np
import pandas as pd
import yfinance as yf
from scipy.optimize import minimize
def ticker_info():
ticker = "gush"
return ticker.upper()
def fetch_expiration_dates(ticker):
print(f"Fetching available expiration dates for {ticker}...")
stock = yf.Ticker(ticker)
expiration_dates = stock.options
print(f"Available expiration dates: {expiration_dates}")
return expiration_dates
def select_expiration_date(expiration_dates):
print("Selecting the first available expiration date...")
expiration_date = expiration_dates[0]
print(f"Selected expiration date: {expiration_date}")
return expiration_date
def fetch_option_chain(ticker, expiration_date):
print(f"Fetching option chain for {ticker} with expiration date {expiration_date}...")
stock = yf.Ticker(ticker)
options_chain = stock.option_chain(expiration_date)
print("Option chain fetched successfully!")
return options_chain
def get_price_data(ticker, start_date, end_date):
print(f"Fetching price data for {ticker} from {start_date} to {end_date}...")
data = yf.download(ticker, start=start_date, end=end_date)
print(f"Price data fetched successfully for {ticker}!")
return data
def moving_average_strategy(data, short_window=20, long_window=50):
data['Short_MA'] = data['Close'].rolling(window=short_window).mean()
data['Long_MA'] = data['Close'].rolling(window=long_window).mean()
data['Signal'] = np.where(data['Short_MA'] > data['Long_MA'], 1, -1)
return data['Signal']
def rsi_strategy(data, window=14, overbought=70, oversold=30):
delta = data['Close'].diff(1)
gain = np.where(delta > 0, delta, 0).flatten() # Flatten to 1D array
loss = np.where(delta < 0, abs(delta), 0).flatten() # Flatten to 1D array
avg_gain = pd.Series(gain).rolling(window=window).mean()
avg_loss = pd.Series(loss).rolling(window=window).mean()
# Avoid division by zero by using np.where to replace 0 with np.nan in avg_loss
rs = avg_gain / np.where(avg_loss == 0, np.nan, avg_loss)
rsi = 100 - (100 / (1 + rs))
signal = np.where(rsi < oversold, 1, np.where(rsi > overbought, -1, 0))
return pd.Series(signal, index=data.index)
def bollinger_bands_strategy(data, window=20, num_std=2):
# Calculate moving average
data['Moving_Avg'] = data['Close'].rolling(window=window).mean()
# Calculate rolling standard deviation and force it to be a Series
rolling_std = data['Close'].rolling(window).std()
rolling_std = rolling_std.squeeze() # Ensure rolling_std is a Series
# Print shapes for debugging
print(f"Shape of Moving_Avg: {data['Moving_Avg'].shape}")
print(f"Shape of Rolling Std: {rolling_std.shape}")
# Calculate upper and lower bands
data['Band_Upper'] = data['Moving_Avg'] + (num_std * rolling_std)
data['Band_Lower'] = data['Moving_Avg'] - (num_std * rolling_std)
# Print shapes after assignments for debugging
print(f"Shape of Band_Upper: {data['Band_Upper'].shape}")
print(f"Shape of Band_Lower: {data['Band_Lower'].shape}")
# Check for NaN values
print(f"NaNs in Close: {data['Close'].isna().sum()}")
print(f"NaNs in Band_Upper: {data['Band_Upper'].isna().sum()}")
print(f"NaNs in Band_Lower: {data['Band_Lower'].isna().sum()}")
# Print the columns of the DataFrame
print(f"Columns in data before dropping NaNs: {data.columns.tolist()}")
# Optionally drop rows with NaNs
data = data.dropna(subset=['Close', 'Band_Upper', 'Band_Lower'])
# Generate signals based on the bands
signal = np.where(data['Close'] < data['Band_Lower'], 1,
np.where(data['Close'] > data['Band_Upper'], -1, 0))
return pd.Series(signal, index=data.index)
def generate_signals(data):
ma_signal = moving_average_strategy(data)
rsi_signal = rsi_strategy(data)
bollinger_signal = bollinger_bands_strategy(data)
return pd.DataFrame({'MA': ma_signal, 'RSI': rsi_signal, 'Bollinger': bollinger_signal})
def backtest_option_trades(option_chain, signals, stock_data):
"""
Backtest option trades based on the given signals and stock data.
"""
trades = []
current_position = None
# Ensure both stock_data and option_chain indices are sorted in ascending order
stock_data = stock_data.sort_index()
# Convert 'lastTradeDate' or any date-related columns to datetime in option_chain
if 'lastTradeDate' in option_chain.columns:
option_chain['lastTradeDate'] = pd.to_datetime(option_chain['lastTradeDate'])
option_chain = option_chain.set_index('lastTradeDate')
# If option_chain index isn't datetime, convert it to datetime (ensuring compatibility)
option_chain.index = pd.to_datetime(option_chain.index)
# Remove the timezone from option_chain index
option_chain.index = option_chain.index.tz_localize(None)
# Now reindex the option chain to match the stock data index (forward fill missing option prices)
option_chain = option_chain.sort_index()
option_chain = option_chain.reindex(stock_data.index, method='ffill')
for i in range(len(signals)):
if signals.iloc[i]['MA'] == 1 and current_position is None:
# BUY signal
entry_price = option_chain['lastPrice'].iloc[i]
if pd.isna(entry_price): # If price is nan, log the error and continue
print(f"Missing entry price on {stock_data.index[i]}, skipping trade.")
continue
entry_date = stock_data.index[i]
current_position = {
'entry_price': entry_price,
'entry_date': entry_date
}
print(f"BUY signal on {entry_date}: Entry Price = {entry_price}")
elif signals.iloc[i]['MA'] == -1 and current_position is not None:
# SELL signal
exit_price = option_chain['lastPrice'].iloc[i]
if pd.isna(exit_price): # If price is nan, log the error and continue
print(f"Missing exit price on {stock_data.index[i]}, skipping trade.")
continue
exit_date = stock_data.index[i]
pnl = (exit_price - current_position['entry_price']) * 100
print(f"SELL signal on {exit_date}: Exit Price = {exit_price}, P&L = {pnl}")
trades.append({
'entry_date': current_position['entry_date'],
'entry_price': current_position['entry_price'],
'exit_date': exit_date,
'exit_price': exit_price,
'pnl': pnl
})
current_position = None
cumulative_pnl = sum(trade['pnl'] for trade in trades)
total_wins = sum(1 for trade in trades if trade['pnl'] > 0)
total_trades = len(trades)
win_rate = total_wins / total_trades if total_trades > 0 else 0
return cumulative_pnl, trades, win_rate
def objective_function_profit(weights, strategy_signals, data, option_chain):
weights = np.array(weights)
weights /= np.sum(weights) # Normalize weights
weighted_signals = np.sum([signal * weight for signal, weight in zip(strategy_signals.T.values, weights)], axis=0)
# Since `backtest_option_trades` returns 3 values, we only unpack those
cumulative_pnl, _, _ = backtest_option_trades(option_chain, weighted_signals, data)
# Return negative cumulative P&L to maximize profit
return -cumulative_pnl
def optimize_weights(strategy_signals, data, option_chain):
initial_weights = [1 / len(strategy_signals.columns)] * len(strategy_signals.columns)
constraints = ({'type': 'eq', 'fun': lambda weights: np.sum(weights) - 1})
bounds = [(0, 1)] * len(strategy_signals.columns)
result = minimize(objective_function_profit, initial_weights, args=(strategy_signals, data, option_chain),
method='SLSQP', bounds=bounds, constraints=constraints)
return result.x # Optimal weights
def weighted_signal_combination(strategy_signals, weights):
weighted_signals = np.sum([signal * weight for signal, weight in zip(strategy_signals.T.values, weights)], axis=0)
return weighted_signals
def main_decision(weighted_signals):
last_signal = weighted_signals[-1] # Latest signal
if last_signal > 0:
return "BUY"
elif last_signal < 0:
return "SELL"
else:
return "HOLD"
def run_backtest():
ticker = ticker_info()
expiration_dates = fetch_expiration_dates(ticker)
expiration_date = select_expiration_date(expiration_dates)
options_chain = fetch_option_chain(ticker, expiration_date)
# Fetch training data
train_data = get_price_data(ticker, '2010-01-01', '2022-01-01')
# Generate signals
strategy_signals_train = generate_signals(train_data)
# Optimize weights
optimal_weights = optimize_weights(strategy_signals_train, train_data, options_chain.calls)
# Fetch test data
test_data = get_price_data(ticker, '2022-01-02', '2024-01-01')
# Generate test signals
strategy_signals_test = generate_signals(test_data)
# Combine signals and backtest
weighted_signals = weighted_signal_combination(strategy_signals_test, optimal_weights)
cumulative_pnl, trades, win_rate = backtest_option_trades(options_chain.calls, weighted_signals, test_data)
# Make final decision
decision = main_decision(weighted_signals)
print(f"Final decision: {decision}")
# Output results
print(f"Cumulative P&L: {cumulative_pnl}")
print(f"Win Rate: {win_rate * 100:.2f}%")
# Call the main function
run_backtest()

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,78 +0,0 @@
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.callbacks import EarlyStopping
# Load the training and testing data
training_data = pd.read_csv("3_years_training_data.csv")
testing_data = pd.read_csv("3_month_testing_data.csv")
# Drop unnecessary columns
training_data = training_data.drop(columns=["Unnamed: 0", "Date"])
testing_data = testing_data.drop(columns=["Unnamed: 0", "Date"])
# Create lagged features for the model
def create_lagged_features(data, n_lags=3):
df = data.copy()
for lag in range(1, n_lags + 1):
df[f'Close_lag_{lag}'] = df['Close'].shift(lag)
df.dropna(inplace=True) # Remove rows with NaN values due to shifting
return df
# Apply lagged features to the training and testing datasets
training_data = create_lagged_features(training_data)
testing_data = create_lagged_features(testing_data)
# Separate features and target
X_train = training_data.drop(columns=["Close"]).values
y_train = training_data["Close"].values
X_test = testing_data.drop(columns=["Close"]).values
y_test = testing_data["Close"].values
# Standardize the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Build the neural network model
model = Sequential([
Dense(64, activation='sigmoid', input_shape=(X_train.shape[1],)),
Dense(32, activation='sigmoid'),
Dense(16, activation='sigmoid'),
Dense(1) # Output layer for regression
])
# Compile the model
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
# Use early stopping to prevent overfitting
early_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
# Train the model
history = model.fit(
X_train, y_train,
epochs=100,
batch_size=32,
validation_split=0.2,
callbacks=[early_stopping],
verbose=1
)
# Evaluate the model on the test set
y_pred = model.predict(X_test).flatten()
mse = mean_squared_error(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
print(f"Neural Network MSE: {mse:.2f}")
print(f"Neural Network MAE: {mae:.2f}")
# Prepare the latest data to predict tomorrow's price
latest_data = testing_data.tail(1).drop(columns=["Close"])
latest_data_scaled = scaler.transform(latest_data)
# Predict tomorrow's close price
tomorrow_pred = model.predict(latest_data_scaled)
print(f"Predicted Close Price for Tomorrow: {tomorrow_pred[0][0]:.2f}")

View File

@@ -1,47 +0,0 @@
absl-py==2.1.0
astunparse==1.6.3
certifi==2024.8.30
charset-normalizer==3.4.0
flatbuffers==24.3.25
gast==0.6.0
google-pasta==0.2.0
grpcio==1.67.1
h5py==3.12.1
ibapi==9.81.1.post1
idna==3.10
importlib_metadata==8.5.0
joblib==1.4.2
keras==3.6.0
libclang==18.1.1
Markdown==3.7
markdown-it-py==3.0.0
MarkupSafe==3.0.2
mdurl==0.1.2
ml-dtypes==0.4.1
namex==0.0.8
numpy==2.0.2
opt_einsum==3.4.0
optree==0.13.0
packaging==24.1
pandas==2.2.3
protobuf==5.28.3
Pygments==2.18.0
python-dateutil==2.9.0.post0
pytz==2024.2
requests==2.32.3
rich==13.9.4
scikit-learn==1.5.2
scipy==1.13.1
six==1.16.0
tensorboard==2.18.0
tensorboard-data-server==0.7.2
tensorflow==2.18.0
tensorflow-io-gcs-filesystem==0.37.1
termcolor==2.5.0
threadpoolctl==3.5.0
typing_extensions==4.12.2
tzdata==2024.2
urllib3==2.2.3
Werkzeug==3.1.1
wrapt==1.16.0
zipp==3.20.2