Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "usage: ipykernel_launcher.py [-h] [--training TRAINING] [--testing TESTING]\n",
- " [--output OUTPUT]\n",
- "ipykernel_launcher.py: error: unrecognized arguments: -f /Users/johnodin99/Library/Jupyter/runtime/kernel-6523333e-89ff-46af-a10a-f80d42f57fb0.json\n"
- ]
- },
- {
- "ename": "SystemExit",
- "evalue": "2",
- "output_type": "error",
- "traceback": [
- "An exception has occurred, use %tb to see the full traceback.\n",
- "\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 2\n"
- ]
- },
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "/Users/johnodin99/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2918: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
- " warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
- ]
- }
- ],
- "source": [
- "# -*- coding: utf-8 -*- \n",
- "\"\"\" \n",
- "Created on Mon Mar 12 07:35:11 2018 \n",
- "\n",
- "@author: johnodin99 \n",
- "\"\"\" \n",
- "import os \n",
- "import pandas as pd \n",
- "\n",
- " \n",
- "################################training##################################### \n",
- "\"\"\" \n",
- "userhome = os.path.expanduser('~') \n",
- "traing_data_path = userhome+\"/Desktop/training_data.csv\" \n",
- "\n",
- "training_data = pd.read_csv(traing_data_path,header=None) \n",
- "training_data.columns=[\"open\",\"high\",\"low\",\"close\"] \n",
- "training_data.index=pd.to_datetime(training_data.index,unit=\"D\") \n",
- "\n",
- " \n",
- "#simple moving average \n",
- "training_simple_5 = training_data[\"close\"].rolling(window=5).mean() \n",
- "training_simple_20 = training_data[\"close\"].rolling(window=20).mean() \n",
- "\n",
- "#exponential moving average \n",
- "#exponential=pd.ewma(training_data[\"close\"],span=10,freq=\"D\",min_periods=10) \n",
- "\n",
- "training_action = [] \n",
- "slot_status = 0 \n",
- "\n",
- " \n",
- "for i in range(len(training_simple_5.index)): \n",
- " if (training_simple_5[i]>training_simple_20[i] and slot_status==0): \n",
- " print(\"1\") \n",
- " training_action.append(\"1\") \n",
- " slot_status = 1 \n",
- " \n",
- " elif (training_simple_5[i]<training_simple_20[i]and slot_status==1): \n",
- " print(\"-1\") \n",
- " training_action.append(\"-1\") \n",
- " slot_status = 0 \n",
- " \n",
- " \n",
- " else: \n",
- " training_action.append(\"0\") \n",
- " print(\"0\") \n",
- " \"\"\" \n",
- "################################testing#################################### \n",
- "userhome = os.path.expanduser('~') \n",
- "testing_data_path = userhome+\"/Desktop/testing_data.csv\" \n",
- "\n",
- "testing_data = pd.read_csv(testing_data_path,header=None) \n",
- "testing_data.columns=[\"open\",\"high\",\"low\",\"close\"] \n",
- "testing_data.index=pd.to_datetime(testing_data.index,unit=\"D\") \n",
- "\n",
- " \n",
- "#simple moving average \n",
- "testing_simple_5 = testing_data[\"close\"].rolling(window=5).mean() \n",
- "testing_simple_20 = testing_data[\"close\"].rolling(window=20).mean() \n",
- "\n",
- "#exponential moving average \n",
- "#exponential=pd.ewma(training_data[\"close\"],span=10,freq=\"D\",min_periods=10) \n",
- "\n",
- "testing_action = [] \n",
- "slot_status = 0 \n",
- "\n",
- " \n",
- "for i in range(len(testing_simple_5.index)): \n",
- " if (testing_simple_5[i]>testing_simple_20[i] and slot_status==0): \n",
- " testing_action.append(\"1\") \n",
- " slot_status = 1 \n",
- " \n",
- " elif (testing_simple_5[i]<testing_simple_20[i]and slot_status==1): \n",
- " testing_action.append(\"-1\") \n",
- " slot_status = 0 \n",
- " \n",
- " else: \n",
- " testing_action.append(\"0\") \n",
- " \n",
- "testing_action=pd.DataFrame(testing_action) \n",
- "testing_action = testing_action[:-1] \n",
- "path = userhome+\"/Desktop/output.csv\" \n",
- "testing_action.to_csv(path,sep=' ',header=0,index=0) \n",
- "\n",
- " \n",
- "############################################################################# \n",
- "\n",
- "if __name__ == '__main__': \n",
- " # You should not modify this part. \n",
- " import argparse \n",
- "\n",
- " parser = argparse.ArgumentParser() \n",
- " parser.add_argument('--training', \n",
- " default='training_data.csv', \n",
- " help='input training data file name') \n",
- " parser.add_argument('--testing', \n",
- " default='testing_data.csv', \n",
- " help='input testing data file name') \n",
- " parser.add_argument('--output', \n",
- " default='output.csv', \n",
- " help='output file name') \n",
- " args = parser.parse_args()\n",
- " "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Use moving average method(5 and 20 days) to forecast the stock when to reverse the trend and then provide the suggestion to invest or sell , the method is to recognize the cross point and slot status "
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "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.6.4"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment