astacn commited on
Commit
ddf0578
·
verified ·
1 Parent(s): 2fc58fd

Create app2.py

Browse files
Files changed (1) hide show
  1. app2.py +116 -0
app2.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import baostock as bs
4
+ from sklearn.preprocessing import MinMaxScaler, StandardScaler
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, mean_absolute_error
7
+ from neuralprophet import NeuralProphet, set_log_level
8
+ from torch.optim import Adam
9
+ from torch.nn import LSTM
10
+ import torch
11
+ import torch.nn as nn
12
+ import os
13
+
14
+ # Baostock API settings
15
+ bs.login()
16
+
17
+ # Collect historical data
18
+ data = bs.query_history_k_data_plus(
19
+ "sz.000001", # Shanghai Composite Index
20
+ "date,open,high,low,close,volume",
21
+ start_date="2005-05-30",
22
+ end_date="2024-01-31",
23
+ frequency="d"
24
+ )
25
+
26
+ # Convert ResultData object to pandas DataFrame
27
+ data_list = []
28
+ while (data.error_code == '0') & data.next():
29
+ # 获取一条记录,将记录合并在一起
30
+ data_list.append(data.get_row_data())
31
+ data_df = pd.DataFrame(data_list, columns=data.fields)
32
+
33
+ # Convert 'open' and 'close' columns to numeric type
34
+ data_df['open'] = pd.to_numeric(data_df['open'])
35
+ data_df['close'] = pd.to_numeric(data_df['close'])
36
+
37
+ # Filter out stocks that meet the conditions
38
+ # Added fillna(0) to handle the None value introduced by shift(1)
39
+ data_df = data_df[(data_df["open"] >= 0.98 * data_df["close"].shift(1).fillna(0)) & (data_df["open"] <= 1.02 * data_df["close"].shift(1).fillna(0))]
40
+ data_df = data_df[(data_df["high"] == data_df["close"]) & (data_df["low"] == data_df["close"])] # limit-up condition
41
+ data_df = data_df[(data_df["open"]!= 0) & (data_df["close"]!= 0)] # exclude zero prices
42
+
43
+ # Check if data_df is empty before proceeding
44
+ if data_df.empty:
45
+ print("Warning: data_df is empty after filtering. Check your filtering conditions.")
46
+ # Optionally, you can raise an exception to stop execution:
47
+ # raise ValueError("data_df is empty after filtering.")
48
+ else:
49
+ # Now use data_df (the DataFrame) in train_test_split
50
+ train_data, val_data = train_test_split(data_df, test_size=0.2, random_state=42)
51
+
52
+ # Set log level to ERROR to suppress unnecessary warnings
53
+ set_log_level("ERROR")
54
+
55
+ # Specify the custom layer configuration
56
+ custom_layer = LSTM(input_size=1, hidden_size=128, num_layers=1, batch_first=True)
57
+
58
+ # Now initialize the NeuralProphet model
59
+ model_np = NeuralProphet(
60
+ n_forecasts=1,
61
+ n_lags=30,
62
+ n_changepoints=10,
63
+ changepoints_range=0.8,
64
+ learning_rate=1e-3,
65
+ optimizer=Adam,
66
+ )
67
+
68
+ # Create a custom model by combining NeuralProphet with PyTorch's LSTM
69
+ class CustomModel(nn.Module):
70
+ def __init__(self):
71
+ super(CustomModel, self).__init__()
72
+ self.neural_prophet = NeuralProphet(
73
+ n_forecasts=1,
74
+ n_lags=30,
75
+ n_changepoints=10,
76
+ changepoints_range=0.8,
77
+ learning_rate=1e-3,
78
+ optimizer=Adam,
79
+ )
80
+ self.lstm = LSTM(input_size=1, hidden_size=128, num_layers=1, batch_first=True)
81
+
82
+ def forward(self, x):
83
+ x = self.neural_prophet(x)
84
+ x = self.lstm(x)
85
+ return x
86
+
87
+ def predict(self, df):
88
+ """
89
+ Custom predict method for CustomModel. Utilizes NeuralProphet's prediction.
90
+
91
+ Args:
92
+ df: The input DataFrame for prediction.
93
+
94
+ Returns:
95
+ Predictions from the NeuralProphet model.
96
+ """
97
+ # Assuming your NeuralProphet model expects a DataFrame in a specific format
98
+ # You might need to adjust this based on your data and model setup
99
+ future = self.neural_prophet.make_future_dataframe(df, periods=1) # Adjust periods as needed
100
+ forecast = self.neural_prophet.predict(future)
101
+ return forecast['yhat1'].values # Or access the relevant prediction column
102
+
103
+ # Instantiate your model
104
+ model = CustomModel()
105
+
106
+ # Define loss function and optimizer
107
+ criterion = nn.BCELoss()
108
+ optimizer = optim.Adam(model.parameters(), lr=1e-3)
109
+
110
+ # Training loop
111
+ def fit(model, train_data, epochs, batch_size, validation_data):
112
+ """
113
+ Custom training loop for the CustomModel.
114
+
115
+ Args:
116
+ model: The Custom