Guest User

Untitled

a guest
Mar 20th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stderr",
  10. "output_type": "stream",
  11. "text": [
  12. "usage: ipykernel_launcher.py [-h] [--training TRAINING] [--testing TESTING]\n",
  13. " [--output OUTPUT]\n",
  14. "ipykernel_launcher.py: error: unrecognized arguments: -f /Users/johnodin99/Library/Jupyter/runtime/kernel-6523333e-89ff-46af-a10a-f80d42f57fb0.json\n"
  15. ]
  16. },
  17. {
  18. "ename": "SystemExit",
  19. "evalue": "2",
  20. "output_type": "error",
  21. "traceback": [
  22. "An exception has occurred, use %tb to see the full traceback.\n",
  23. "\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 2\n"
  24. ]
  25. },
  26. {
  27. "name": "stderr",
  28. "output_type": "stream",
  29. "text": [
  30. "/Users/johnodin99/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2918: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
  31. " warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
  32. ]
  33. }
  34. ],
  35. "source": [
  36. "# -*- coding: utf-8 -*- \n",
  37. "\"\"\" \n",
  38. "Created on Mon Mar 12 07:35:11 2018 \n",
  39. "\n",
  40. "@author: johnodin99 \n",
  41. "\"\"\" \n",
  42. "import os \n",
  43. "import pandas as pd \n",
  44. "\n",
  45. " \n",
  46. "################################training##################################### \n",
  47. "\"\"\" \n",
  48. "userhome = os.path.expanduser('~') \n",
  49. "traing_data_path = userhome+\"/Desktop/training_data.csv\" \n",
  50. "\n",
  51. "training_data = pd.read_csv(traing_data_path,header=None) \n",
  52. "training_data.columns=[\"open\",\"high\",\"low\",\"close\"] \n",
  53. "training_data.index=pd.to_datetime(training_data.index,unit=\"D\") \n",
  54. "\n",
  55. " \n",
  56. "#simple moving average \n",
  57. "training_simple_5 = training_data[\"close\"].rolling(window=5).mean() \n",
  58. "training_simple_20 = training_data[\"close\"].rolling(window=20).mean() \n",
  59. "\n",
  60. "#exponential moving average \n",
  61. "#exponential=pd.ewma(training_data[\"close\"],span=10,freq=\"D\",min_periods=10) \n",
  62. "\n",
  63. "training_action = [] \n",
  64. "slot_status = 0 \n",
  65. "\n",
  66. " \n",
  67. "for i in range(len(training_simple_5.index)): \n",
  68. " if (training_simple_5[i]>training_simple_20[i] and slot_status==0): \n",
  69. " print(\"1\") \n",
  70. " training_action.append(\"1\") \n",
  71. " slot_status = 1 \n",
  72. " \n",
  73. " elif (training_simple_5[i]<training_simple_20[i]and slot_status==1): \n",
  74. " print(\"-1\") \n",
  75. " training_action.append(\"-1\") \n",
  76. " slot_status = 0 \n",
  77. " \n",
  78. " \n",
  79. " else: \n",
  80. " training_action.append(\"0\") \n",
  81. " print(\"0\") \n",
  82. " \"\"\" \n",
  83. "################################testing#################################### \n",
  84. "userhome = os.path.expanduser('~') \n",
  85. "testing_data_path = userhome+\"/Desktop/testing_data.csv\" \n",
  86. "\n",
  87. "testing_data = pd.read_csv(testing_data_path,header=None) \n",
  88. "testing_data.columns=[\"open\",\"high\",\"low\",\"close\"] \n",
  89. "testing_data.index=pd.to_datetime(testing_data.index,unit=\"D\") \n",
  90. "\n",
  91. " \n",
  92. "#simple moving average \n",
  93. "testing_simple_5 = testing_data[\"close\"].rolling(window=5).mean() \n",
  94. "testing_simple_20 = testing_data[\"close\"].rolling(window=20).mean() \n",
  95. "\n",
  96. "#exponential moving average \n",
  97. "#exponential=pd.ewma(training_data[\"close\"],span=10,freq=\"D\",min_periods=10) \n",
  98. "\n",
  99. "testing_action = [] \n",
  100. "slot_status = 0 \n",
  101. "\n",
  102. " \n",
  103. "for i in range(len(testing_simple_5.index)): \n",
  104. " if (testing_simple_5[i]>testing_simple_20[i] and slot_status==0): \n",
  105. " testing_action.append(\"1\") \n",
  106. " slot_status = 1 \n",
  107. " \n",
  108. " elif (testing_simple_5[i]<testing_simple_20[i]and slot_status==1): \n",
  109. " testing_action.append(\"-1\") \n",
  110. " slot_status = 0 \n",
  111. " \n",
  112. " else: \n",
  113. " testing_action.append(\"0\") \n",
  114. " \n",
  115. "testing_action=pd.DataFrame(testing_action) \n",
  116. "testing_action = testing_action[:-1] \n",
  117. "path = userhome+\"/Desktop/output.csv\" \n",
  118. "testing_action.to_csv(path,sep=' ',header=0,index=0) \n",
  119. "\n",
  120. " \n",
  121. "############################################################################# \n",
  122. "\n",
  123. "if __name__ == '__main__': \n",
  124. " # You should not modify this part. \n",
  125. " import argparse \n",
  126. "\n",
  127. " parser = argparse.ArgumentParser() \n",
  128. " parser.add_argument('--training', \n",
  129. " default='training_data.csv', \n",
  130. " help='input training data file name') \n",
  131. " parser.add_argument('--testing', \n",
  132. " default='testing_data.csv', \n",
  133. " help='input testing data file name') \n",
  134. " parser.add_argument('--output', \n",
  135. " default='output.csv', \n",
  136. " help='output file name') \n",
  137. " args = parser.parse_args()\n",
  138. " "
  139. ]
  140. },
  141. {
  142. "cell_type": "markdown",
  143. "metadata": {},
  144. "source": [
  145. "# 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 "
  146. ]
  147. }
  148. ],
  149. "metadata": {
  150. "kernelspec": {
  151. "display_name": "Python 3",
  152. "language": "python",
  153. "name": "python3"
  154. },
  155. "language_info": {
  156. "codemirror_mode": {
  157. "name": "ipython",
  158. "version": 3
  159. },
  160. "file_extension": ".py",
  161. "mimetype": "text/x-python",
  162. "name": "python",
  163. "nbconvert_exporter": "python",
  164. "pygments_lexer": "ipython3",
  165. "version": "3.6.4"
  166. }
  167. },
  168. "nbformat": 4,
  169. "nbformat_minor": 2
  170. }
Add Comment
Please, Sign In to add comment