{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-08T08:21:33.725436Z","iopub.execute_input":"2022-07-08T08:21:33.725786Z","iopub.status.idle":"2022-07-08T08:21:33.734626Z","shell.execute_reply.started":"2022-07-08T08:21:33.725756Z","shell.execute_reply":"2022-07-08T08:21:33.733418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# GPU를 On 해주세요:) \n[Kaggle 설명](https://teddylee777.github.io/kaggle/kaggle%EC%97%90%EC%84%9C-%EC%A0%9C%EA%B3%B5%ED%95%98%EB%8A%94-notebook-%ED%99%9C%EC%9A%A9%ED%95%98%EA%B8%B0)","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Activation\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:33.749848Z","iopub.execute_input":"2022-07-08T08:21:33.750203Z","iopub.status.idle":"2022-07-08T08:21:33.757134Z","shell.execute_reply.started":"2022-07-08T08:21:33.750174Z","shell.execute_reply":"2022-07-08T08:21:33.755298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = (\n    pd.read_csv(\"/kaggle/input/ai-finance-korea/model_data.csv\", parse_dates=['date'])\n    .set_index(['symbol', 'date'])\n    .sort_index()\n    .dropna()\n).iloc[:50000]","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:33.780985Z","iopub.execute_input":"2022-07-08T08:21:33.781946Z","iopub.status.idle":"2022-07-08T08:21:49.008276Z","shell.execute_reply.started":"2022-07-08T08:21:33.781904Z","shell.execute_reply":"2022-07-08T08:21:49.007281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## GPU를 사용하고 있나 확인","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:59:13.495205Z","iopub.execute_input":"2022-07-08T07:59:13.495669Z","iopub.status.idle":"2022-07-08T07:59:15.108796Z","shell.execute_reply.started":"2022-07-08T07:59:13.495623Z","shell.execute_reply":"2022-07-08T07:59:15.107733Z"}}},{"cell_type":"code","source":"gpu_devices = tf.config.experimental.list_physical_devices('GPU')\nif gpu_devices:\n    print('Using GPU')\n    tf.config.experimental.set_memory_growth(gpu_devices[0], True)\nelse:\n    print('Using CPU')","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.010393Z","iopub.execute_input":"2022-07-08T08:21:49.010844Z","iopub.status.idle":"2022-07-08T08:21:49.017763Z","shell.execute_reply.started":"2022-07-08T08:21:49.010807Z","shell.execute_reply":"2022-07-08T08:21:49.016686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_columns = ['dollar_vol_rank', 'rsi', 'bb_high', 'bb_low', 'NATR', 'ATR', 'PPO',\n       'MACD', 'r01', 'r05', 'r10', 'r21', 'r42', 'r63', 'sector', 'r01dec',\n       'r05dec', 'r10dec', 'r21dec', 'r42dec', 'r63dec', 'r01q_sector',\n       'r05q_sector', 'r10q_sector', 'r21q_sector', 'r42q_sector',\n       'r63q_sector', 'year', 'month', 'weekday']","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.019217Z","iopub.execute_input":"2022-07-08T08:21:49.020377Z","iopub.status.idle":"2022-07-08T08:21:49.028506Z","shell.execute_reply.started":"2022-07-08T08:21:49.020341Z","shell.execute_reply":"2022-07-08T08:21:49.027505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5일 forward return을 Y로 사용","metadata":{}},{"cell_type":"code","source":"X = data.loc[:, feature_columns]","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.031422Z","iopub.execute_input":"2022-07-08T08:21:49.032238Z","iopub.status.idle":"2022-07-08T08:21:49.041962Z","shell.execute_reply.started":"2022-07-08T08:21:49.032200Z","shell.execute_reply":"2022-07-08T08:21:49.041084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = data[['r05_fwd']]","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.044554Z","iopub.execute_input":"2022-07-08T08:21:49.045156Z","iopub.status.idle":"2022-07-08T08:21:49.055614Z","shell.execute_reply.started":"2022-07-08T08:21:49.045121Z","shell.execute_reply":"2022-07-08T08:21:49.054632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_remain, y_train, y_remain = train_test_split(X, Y, test_size=.3)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.058395Z","iopub.execute_input":"2022-07-08T08:21:49.059020Z","iopub.status.idle":"2022-07-08T08:21:49.080720Z","shell.execute_reply.started":"2022-07-08T08:21:49.058981Z","shell.execute_reply":"2022-07-08T08:21:49.079863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_valid, x_test, y_valid, y_test = train_test_split(x_remain, y_remain, test_size=.8)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.082150Z","iopub.execute_input":"2022-07-08T08:21:49.082696Z","iopub.status.idle":"2022-07-08T08:21:49.092996Z","shell.execute_reply.started":"2022-07-08T08:21:49.082639Z","shell.execute_reply":"2022-07-08T08:21:49.092100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Scaling 실행","metadata":{}},{"cell_type":"code","source":"standard_scaler = StandardScaler()\nx_train = standard_scaler.fit_transform(x_train)\nx_valid = standard_scaler.transform(x_valid)\nx_test = standard_scaler.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.094809Z","iopub.execute_input":"2022-07-08T08:21:49.095736Z","iopub.status.idle":"2022-07-08T08:21:49.125824Z","shell.execute_reply.started":"2022-07-08T08:21:49.095698Z","shell.execute_reply":"2022-07-08T08:21:49.124915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model 생성","metadata":{}},{"cell_type":"code","source":"(X.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.127432Z","iopub.execute_input":"2022-07-08T08:21:49.127975Z","iopub.status.idle":"2022-07-08T08:21:49.134915Z","shell.execute_reply.started":"2022-07-08T08:21:49.127939Z","shell.execute_reply":"2022-07-08T08:21:49.133835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Dense(32, input_dim=X.shape[1], activation='relu'),\n    Dense(16, activation='relu'),\n    Dropout(.3),\n    Dense(4, activation='relu'),\n    Dropout(.3),\n    Dense(1)\n])","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.138960Z","iopub.execute_input":"2022-07-08T08:21:49.140275Z","iopub.status.idle":"2022-07-08T08:21:49.171276Z","shell.execute_reply.started":"2022-07-08T08:21:49.140232Z","shell.execute_reply":"2022-07-08T08:21:49.170436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='mse', optimizer='Adam')","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.172543Z","iopub.execute_input":"2022-07-08T08:21:49.173394Z","iopub.status.idle":"2022-07-08T08:21:49.182388Z","shell.execute_reply.started":"2022-07-08T08:21:49.173359Z","shell.execute_reply":"2022-07-08T08:21:49.181484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.183928Z","iopub.execute_input":"2022-07-08T08:21:49.184350Z","iopub.status.idle":"2022-07-08T08:21:49.192596Z","shell.execute_reply.started":"2022-07-08T08:21:49.184316Z","shell.execute_reply":"2022-07-08T08:21:49.191703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(patience=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.194245Z","iopub.execute_input":"2022-07-08T08:21:49.195015Z","iopub.status.idle":"2022-07-08T08:21:49.203276Z","shell.execute_reply.started":"2022-07-08T08:21:49.194975Z","shell.execute_reply":"2022-07-08T08:21:49.202318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model 훈련","metadata":{}},{"cell_type":"code","source":"history = model.fit(x_train, y_train, batch_size=64, epochs=10000, validation_data=(x_valid, y_valid), callbacks=[early_stopping])","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:21:49.206322Z","iopub.execute_input":"2022-07-08T08:21:49.206845Z","iopub.status.idle":"2022-07-08T08:22:50.272007Z","shell.execute_reply.started":"2022-07-08T08:21:49.206818Z","shell.execute_reply":"2022-07-08T08:22:50.271101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model 평가","metadata":{}},{"cell_type":"code","source":"model.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:22:50.273377Z","iopub.execute_input":"2022-07-08T08:22:50.273750Z","iopub.status.idle":"2022-07-08T08:23:12.101363Z","shell.execute_reply.started":"2022-07-08T08:22:50.273709Z","shell.execute_reply":"2022-07-08T08:23:12.100398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predcit값 비교","metadata":{}},{"cell_type":"code","source":"y_test['r05_fwd_pred'] = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:23:12.102608Z","iopub.execute_input":"2022-07-08T08:23:12.103075Z","iopub.status.idle":"2022-07-08T08:23:12.466177Z","shell.execute_reply.started":"2022-07-08T08:23:12.103021Z","shell.execute_reply":"2022-07-08T08:23:12.465178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# AAPL을 예시\ny_test.loc['AAPL'].plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T08:23:12.467812Z","iopub.execute_input":"2022-07-08T08:23:12.468189Z","iopub.status.idle":"2022-07-08T08:23:12.730016Z","shell.execute_reply.started":"2022-07-08T08:23:12.468155Z","shell.execute_reply":"2022-07-08T08:23:12.728978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}