This commit is contained in:
2019-11-21 12:02:03 +08:00
parent 2b06c40594
commit 83e44ebbd5
24 changed files with 3026043 additions and 0 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,110 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "could not convert string to float: '1.5 总获利'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-57-ee6c3b7ef107>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[0msell\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mindex_spa\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[1;31m#print(index_bchl,index_spa2)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[0mprofit\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mindex_bchl\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mindex_spa2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 17\u001b[0m \u001b[0mbuy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msell\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mprofit\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msell\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mbuy\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m0.002\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mValueError\u001b[0m: could not convert string to float: '1.5 总获利'"
]
}
],
"source": [
"df2=pd.DataFrame(columns=['i','value'])\n",
"df3=pd.DataFrame(columns=['year','value'])\n",
"f = open('C:/Users/lenovo/Desktop/000338-3.txt')\n",
"i=0\n",
"value=100\n",
"year=2000\n",
"for line in f.readlines():\n",
" index_spa=line.find(' ',3)\n",
" index_bchl=line.find('本次获利')\n",
" index_spa2=line.find(' ',index_bchl+6)\n",
" index_zhl=line.find('总获利')\n",
" index_year=line.find(' ',index_zhl+5)+1\n",
" if index_bchl>0:\n",
" sell=float(line[3:index_spa])\n",
" #print(index_bchl,index_spa2)\n",
" profit=float(line[index_bchl+5:index_spa2])\n",
" buy=sell-profit\n",
" value=value*(sell/buy-0.002)\n",
" i+=1\n",
" df2=df2.append({'i':i, 'value':value},ignore_index=True)\n",
" if year!=line[index_year:index_year+4]:\n",
" df3=df3.append({'year':int(line[index_year:index_year+4]), 'value':value},ignore_index=True) \n",
" year=line[index_year:index_year+4]\n",
"df3=df3.append({'year':2020, 'value':value},ignore_index=True) \n",
"f.close()\n",
"print(value)\n",
"df2.plot(x='i',y='value',figsize=(16,3))\n",
"df3.plot(x='year',y='value',figsize=(16,3))"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.39999999999999"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float('4.39999999999999')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda root]",
"language": "python",
"name": "conda-root-py"
},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,805 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"import tushare as ts\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import MySQLdb\n",
"import time\n",
"import datetime\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")\n",
"from imp import reload\n",
"import chan_series"
]
},
{
"cell_type": "code",
"execution_count": 254,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"data loaded 578535\n",
"开始处理: 0 / 578535\n",
"开始处理: 10000 / 578535\n",
"开始处理: 20000 / 578535\n",
"开始处理: 30000 / 578535\n",
"开始处理: 40000 / 578535\n",
"开始处理: 50000 / 578535\n",
"开始处理: 60000 / 578535\n",
"开始处理: 70000 / 578535\n",
"开始处理: 80000 / 578535\n",
"开始处理: 90000 / 578535\n",
"开始处理: 100000 / 578535\n",
"开始处理: 110000 / 578535\n",
"开始处理: 120000 / 578535\n",
"开始处理: 130000 / 578535\n",
"开始处理: 140000 / 578535\n",
"开始处理: 150000 / 578535\n",
"开始处理: 160000 / 578535\n",
"开始处理: 170000 / 578535\n",
"开始处理: 180000 / 578535\n",
"开始处理: 190000 / 578535\n",
"开始处理: 200000 / 578535\n",
"开始处理: 210000 / 578535\n",
"开始处理: 220000 / 578535\n",
"开始处理: 230000 / 578535\n",
"开始处理: 240000 / 578535\n",
"开始处理: 250000 / 578535\n",
"开始处理: 260000 / 578535\n",
"开始处理: 270000 / 578535\n",
"开始处理: 280000 / 578535\n",
"开始处理: 290000 / 578535\n",
"开始处理: 300000 / 578535\n",
"开始处理: 310000 / 578535\n",
"开始处理: 320000 / 578535\n",
"开始处理: 330000 / 578535\n",
"开始处理: 340000 / 578535\n",
"开始处理: 350000 / 578535\n",
"开始处理: 360000 / 578535\n",
"开始处理: 370000 / 578535\n",
"开始处理: 380000 / 578535\n",
"开始处理: 390000 / 578535\n",
"开始处理: 400000 / 578535\n",
"开始处理: 410000 / 578535\n",
"开始处理: 420000 / 578535\n",
"开始处理: 430000 / 578535\n",
"开始处理: 440000 / 578535\n",
"开始处理: 450000 / 578535\n",
"开始处理: 460000 / 578535\n",
"开始处理: 470000 / 578535\n",
"开始处理: 480000 / 578535\n",
"开始处理: 490000 / 578535\n",
"开始处理: 500000 / 578535\n",
"开始处理: 510000 / 578535\n",
"开始处理: 520000 / 578535\n",
"开始处理: 530000 / 578535\n",
"开始处理: 540000 / 578535\n",
"开始处理: 550000 / 578535\n",
"开始处理: 560000 / 578535\n",
"开始处理: 570000 / 578535\n"
]
}
],
"source": [
"db = MySQLdb.connect(host=\"127.0.0.1\",user=\"sa\",passwd=\"sasasa\",db=\"quant\",charset=\"utf8\")\n",
"cursor = db.cursor()\n",
"sql = \"select * from stock_min where ts_code='000425.SZ' and trade_time>'2010-01-01' and trade_time<'2020-01-01' order by trade_time\"\n",
"df = pd.read_sql(sql,db)\n",
"cursor.close()\n",
"db.close()\n",
"print('data loaded',len(df))\n",
"reload(chan_series) #重新加载外部chan.py\n",
"df=chan_series.build(df)"
]
},
{
"cell_type": "code",
"execution_count": 265,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"买入 35.48 2010-01-15 09:52:00 bottom\n",
"卖出 35.28 本次获利 -0.19999999999999574 总获利 -0.19999999999999574 2010-01-26 13:29:00 100初仓累计 99.43630214205187\n",
"买入 34.98 2010-02-11 13:13:00 bottom\n",
"卖出 36.24 本次获利 1.2600000000000051 总获利 1.0600000000000094 2010-03-05 10:15:00 100初仓累计 103.01805573550486\n",
"买入 36.35 2010-03-09 13:34:00 bottom\n",
"卖出 35.79 本次获利 -0.5600000000000023 总获利 0.5000000000000071 2010-03-11 11:23:00 100初仓累计 101.43098252472404\n",
"买入 34.91 2010-03-17 14:41:00 bottom\n",
"卖出 35.9 本次获利 0.990000000000002 总获利 1.490000000000009 2010-03-25 09:32:00 100初仓累计 104.30742688735586\n",
"买入 36.23 2010-03-26 14:54:00 bottom\n",
"卖出 40.75 本次获利 4.520000000000003 总获利 6.010000000000012 2010-04-13 14:36:00 100初仓累计 117.32066369472126\n",
"买入 41.11 2010-04-15 10:24:00 bottom\n",
"卖出 41.6 本次获利 0.490000000000002 总获利 6.500000000000014 2010-04-22 13:43:00 100初仓累计 118.71903696668463\n",
"买入 30.5 2010-05-13 10:37:00 bottom\n",
"卖出 32.7 本次获利 2.200000000000003 总获利 8.700000000000017 2010-05-17 09:59:00 100初仓累计 127.28237733805204\n",
"买入 33.8 2010-05-20 13:09:00 bottom\n",
"卖出 35.49 本次获利 1.6900000000000048 总获利 10.390000000000022 2010-05-26 11:23:00 100初仓累计 133.64649620495464\n",
"买入 32.92 2010-06-03 10:18:00 bottom\n",
"卖出 32.3 本次获利 -0.6200000000000045 总获利 9.770000000000017 2010-06-07 14:03:00 100初仓累计 131.1294601281906\n",
"买入 32.35 2010-06-09 14:26:00 bottom\n",
"卖出 31.83 本次获利 -0.5200000000000031 总获利 9.250000000000014 2010-06-18 14:18:00 100初仓累计 129.02166045997856\n",
"买入 32.29 2010-06-21 13:15:00 bottom\n",
"卖出 32.63 本次获利 0.3400000000000034 总获利 9.590000000000018 2010-06-23 13:16:00 100初仓累计 130.38020380331685\n",
"买入 30.62 2010-07-05 14:19:00 bottom\n",
"卖出 32.71 本次获利 2.09 总获利 11.680000000000017 2010-07-13 10:06:00 100初仓累计 139.27944044436623\n",
"买入 32.0 2010-07-19 11:15:00 bottom\n",
"卖出 35.56 本次获利 3.5600000000000023 总获利 15.24000000000002 2010-07-29 14:37:00 100初仓累计 154.774278193802\n",
"买入 36.88 2010-08-02 10:27:00 bottom\n",
"卖出 37.0 本次获利 0.11999999999999744 总获利 15.360000000000017 2010-08-04 14:53:00 100初仓累计 155.27788213586425\n",
"买入 36.61 2010-08-09 14:29:00 bottom\n",
"卖出 35.33 本次获利 -1.2800000000000011 总获利 14.080000000000016 2010-08-20 10:28:00 100初仓累计 149.84888215952154\n",
"买入 34.46 2010-08-27 13:33:00 bottom\n",
"卖出 35.86 本次获利 1.3999999999999986 总获利 15.480000000000015 2010-09-08 10:54:00 100初仓累计 155.93676477772613\n",
"买入 37.0 2010-09-13 14:42:00 bottom\n",
"卖出 36.93 本次获利 -0.07000000000000028 总获利 15.410000000000014 2010-09-15 10:25:00 100初仓累计 155.6417492767953\n",
"买入 36.4 2010-09-21 10:58:00 bottom\n",
"卖出 42.13 本次获利 5.730000000000004 总获利 21.14000000000002 2010-10-19 10:57:00 100初仓累计 180.14249717119196\n",
"买入 43.2 2010-10-20 10:38:00 bottom\n",
"卖出 47.39 本次获利 4.189999999999998 总获利 25.330000000000016 2010-10-27 10:11:00 100初仓累计 197.61465141071264\n",
"买入 48.02 2010-10-29 11:17:00 bottom\n",
"卖出 48.6 本次获利 0.5799999999999983 总获利 25.910000000000014 2010-11-17 10:02:00 100初仓累计 200.0015005947654\n",
"买入 48.6 2010-11-18 14:28:00 bottom\n",
"卖出 48.05 本次获利 -0.5500000000000043 总获利 25.36000000000001 2010-11-19 13:39:00 100初仓累计 197.73810912712915\n",
"买入 52.18 2010-11-24 13:31:00 bottom\n",
"卖出 53.0 本次获利 0.8200000000000003 总获利 26.18000000000001 2010-11-26 14:34:00 100初仓累计 200.84553054307867\n",
"买入 53.91 2010-11-29 10:17:00 bottom\n",
"卖出 51.2 本次获利 -2.7099999999999937 总获利 23.470000000000017 2010-12-01 10:57:00 100初仓累计 190.7492332369807\n",
"买入 53.94 2010-12-02 10:40:00 bottom\n",
"卖出 50.65 本次获利 -3.289999999999999 总获利 20.180000000000017 2010-12-07 10:36:00 100初仓累计 179.11473235915966\n",
"买入 51.1 2010-12-08 11:25:00 bottom\n",
"卖出 50.8 本次获利 -0.30000000000000426 总获利 19.880000000000013 2010-12-09 10:25:00 100初仓累计 178.06317815744248\n",
"买入 53.99 2010-12-13 10:53:00 bottom\n",
"卖出 53.5 本次获利 -0.490000000000002 总获利 19.39000000000001 2010-12-23 14:21:00 100初仓累计 176.44712041902523\n",
"买入 51.15 2010-12-27 14:38:00 bottom\n",
"卖出 51.14 本次获利 -0.00999999999999801 总获利 19.380000000000013 2010-12-29 10:11:00 100初仓累计 176.41262440330306\n",
"买入 52.0 2010-12-30 11:15:00 bottom\n",
"卖出 59.3 本次获利 7.299999999999997 总获利 26.68000000000001 2011-01-07 14:54:00 100初仓累计 201.17824282915137\n",
"买入 51.03 2011-01-27 14:34:00 bottom\n",
"卖出 50.96 本次获利 -0.07000000000000028 总获利 26.61000000000001 2011-02-01 13:17:00 100初仓累计 200.90227816134734\n",
"买入 56.0 2011-02-16 14:26:00 bottom\n",
"卖出 54.97 本次获利 -1.0300000000000011 总获利 25.58000000000001 2011-02-22 13:14:00 100初仓累计 197.20711125945112\n",
"买入 56.1 2011-03-04 10:49:00 bottom\n",
"卖出 58.67 本次获利 2.5700000000000003 总获利 28.15000000000001 2011-03-10 14:06:00 100初仓累计 206.24137642766485\n",
"买入 63.17 2011-03-14 14:42:00 bottom\n",
"卖出 30.86 本次获利 -32.31 总获利 26.700000000000006 2011-03-16 10:04:00 100初仓累计 201.50732552027029\n",
"买入 30.81 2011-03-23 13:57:00 bottom\n",
"卖出 28.77 本次获利 -2.039999999999999 总获利 24.660000000000007 2011-03-30 14:28:00 100初仓累计 188.16506832905472\n",
"买入 28.47 2011-04-07 11:26:00 bottom\n",
"卖出 28.8 本次获利 0.33000000000000185 总获利 24.99000000000001 2011-04-12 11:09:00 100初仓累计 190.34611759314282\n",
"买入 25.12 2011-05-03 14:07:00 bottom\n",
"卖出 23.2 本次获利 -1.9200000000000017 总获利 23.070000000000007 2011-05-06 09:59:00 100初仓累计 175.7973697516287\n",
"买入 23.69 2011-05-17 13:37:00 bottom\n",
"卖出 23.3 本次获利 -0.39000000000000057 总获利 22.680000000000007 2011-05-20 11:16:00 100初仓累计 172.90328050708945\n",
"买入 22.89 2011-06-07 14:18:00 bottom\n",
"卖出 23.49 本次获利 0.5999999999999979 总获利 23.280000000000005 2011-06-10 13:03:00 100初仓累计 177.43547658853345\n",
"买入 23.43 2011-06-14 13:18:00 bottom\n",
"卖出 23.52 本次获利 0.08999999999999986 总获利 23.370000000000005 2011-06-16 14:11:00 100初仓累计 178.11704692113983\n",
"买入 23.2 2011-06-21 14:13:00 bottom\n",
"卖出 24.07 本次获利 0.870000000000001 总获利 24.240000000000006 2011-06-28 11:13:00 100初仓累计 184.7964361806826\n",
"买入 24.45 2011-06-29 13:13:00 bottom\n",
"卖出 25.0 本次获利 0.5500000000000007 总获利 24.790000000000006 2011-07-06 10:22:00 100初仓累计 188.95341122769182\n",
"买入 25.02 2011-07-11 14:07:00 bottom\n",
"卖出 24.85 本次获利 -0.16999999999999815 总获利 24.620000000000008 2011-07-18 15:00:00 100初仓累计 187.6695551162327\n",
"买入 24.61 2011-07-21 10:43:00 bottom\n",
"卖出 24.52 本次获利 -0.08999999999999986 总获利 24.53000000000001 2011-07-22 11:17:00 100初仓累计 186.98323817350774\n",
"买入 23.16 2011-07-27 13:05:00 bottom\n",
"卖出 22.2 本次获利 -0.9600000000000009 总获利 23.570000000000007 2011-08-05 10:11:00 100初仓累计 179.2326376274556\n",
"买入 21.52 2011-08-10 10:37:00 bottom\n",
"卖出 23.7 本次获利 2.1799999999999997 总获利 25.750000000000007 2011-08-17 11:00:00 100初仓累计 197.3891037068168\n",
"买入 23.18 2011-08-25 13:40:00 bottom\n",
"卖出 23.04 本次获利 -0.14000000000000057 总获利 25.610000000000007 2011-08-29 10:13:00 100初仓累计 196.19693483196974\n",
"买入 20.59 2011-09-13 14:57:00 bottom\n",
"卖出 20.24 本次获利 -0.3500000000000014 总获利 25.260000000000005 2011-09-19 10:33:00 100初仓累计 192.86187280228592\n",
"买入 20.83 2011-09-21 13:53:00 bottom\n",
"卖出 19.5 本次获利 -1.3299999999999983 总获利 23.930000000000007 2011-09-27 11:03:00 100初仓累计 180.5476005590291\n",
"买入 18.2 2011-10-12 13:56:00 bottom\n",
"卖出 18.13 本次获利 -0.07000000000000028 总获利 23.860000000000007 2011-10-14 10:52:00 100初仓累计 179.85318671072514\n",
"买入 16.33 2011-10-26 10:48:00 bottom\n",
"卖出 16.86 本次获利 0.5300000000000011 总获利 24.390000000000008 2011-11-07 13:51:00 100初仓累计 185.69043037004448\n",
"买入 16.55 2011-11-15 11:11:00 bottom\n",
"卖出 16.25 本次获利 -0.3000000000000007 总获利 24.090000000000007 2011-11-17 13:48:00 100初仓累计 182.324440695663\n",
"买入 13.42 2011-12-12 10:22:00 bottom\n",
"卖出 13.63 本次获利 0.21000000000000085 总获利 24.300000000000008 2011-12-15 13:01:00 100初仓累计 185.1775057140005\n",
"买入 13.37 2011-12-20 10:31:00 bottom\n",
"卖出 12.75 本次获利 -0.6199999999999992 总获利 23.680000000000007 2011-12-22 11:30:00 100初仓累计 176.59036633160107\n",
"买入 14.06 2011-12-29 13:46:00 bottom\n",
"卖出 14.01 本次获利 -0.05000000000000071 总获利 23.630000000000006 2012-01-05 11:15:00 100初仓累计 175.96237783113307\n",
"买入 13.86 2012-01-09 11:01:00 bottom\n",
"卖出 14.53 本次获利 0.6699999999999999 总获利 24.300000000000004 2012-01-13 11:10:00 100初仓累计 184.46849566279678\n",
"买入 15.29 2012-01-19 14:54:00 bottom\n",
"卖出 16.02 本次获利 0.7300000000000004 总获利 25.030000000000005 2012-01-31 11:02:00 100初仓累计 193.2756900273384\n",
"买入 15.75 2012-02-09 09:32:00 bottom\n",
"卖出 15.99 本次获利 0.2400000000000002 总获利 25.270000000000003 2012-02-14 14:29:00 100初仓累计 196.22084339918356\n",
"买入 16.1 2012-02-15 13:35:00 bottom\n",
"卖出 15.9 本次获利 -0.20000000000000107 总获利 25.07 2012-02-17 10:06:00 100初仓累计 193.78331739422475\n",
"买入 16.15 2012-02-23 10:23:00 bottom\n",
"卖出 16.23 本次获利 0.08000000000000185 总获利 25.150000000000002 2012-02-29 10:59:00 100初仓累计 194.74323475592993\n",
"买入 16.18 2012-03-02 14:40:00 bottom\n",
"卖出 15.88 本次获利 -0.29999999999999893 总获利 24.85 2012-03-06 11:07:00 100初仓累计 191.13242076169144\n",
"买入 15.65 2012-03-08 13:23:00 bottom\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"卖出 14.7 本次获利 -0.9500000000000011 总获利 23.9 2012-03-26 09:49:00 100初仓累计 179.53013323941624\n",
"买入 13.92 2012-04-05 13:39:00 bottom\n",
"卖出 14.48 本次获利 0.5600000000000005 总获利 24.46 2012-04-16 13:58:00 100初仓累计 186.75260986399047\n",
"买入 14.6 2012-04-18 14:12:00 bottom\n",
"卖出 14.95 本次获利 0.34999999999999964 总获利 24.810000000000002 2012-04-24 14:34:00 100初仓累计 191.22955599086694\n",
"买入 15.1 2012-04-26 09:31:00 bottom\n",
"卖出 15.52 本次获利 0.41999999999999993 总获利 25.230000000000004 2012-05-07 13:05:00 100初仓累计 196.54852377339435\n",
"买入 14.33 2012-05-17 14:20:00 bottom\n",
"卖出 14.56 本次获利 0.23000000000000043 总获利 25.460000000000004 2012-05-25 13:28:00 100初仓累计 199.70317558552838\n",
"买入 15.27 2012-05-28 14:09:00 bottom\n",
"卖出 15.58 本次获利 0.3100000000000005 总获利 25.770000000000003 2012-05-30 09:53:00 100初仓累计 203.75739853454695\n",
"买入 15.28 2012-06-01 10:07:00 bottom\n",
"卖出 15.0 本次获利 -0.27999999999999936 总获利 25.490000000000002 2012-06-04 10:51:00 100初仓累计 200.02362421585107\n",
"买入 14.69 2012-06-11 14:31:00 bottom\n",
"卖出 14.79 本次获利 0.09999999999999964 总获利 25.590000000000003 2012-06-15 14:42:00 100初仓累计 201.38525542222175\n",
"买入 14.12 2012-06-27 13:25:00 bottom\n",
"卖出 13.95 本次获利 -0.16999999999999993 总获利 25.42 2012-07-03 10:30:00 100初仓累计 198.9606454065151\n",
"买入 13.3 2012-07-09 09:40:00 bottom\n",
"卖出 13.21 本次获利 -0.08999999999999986 总获利 25.330000000000002 2012-07-12 10:33:00 100初仓累计 197.61429517444094\n",
"买入 12.68 2012-08-01 11:27:00 bottom\n",
"卖出 12.68 本次获利 0.0 总获利 25.330000000000002 2012-08-09 10:04:00 100初仓累计 197.61429517444094\n",
"买入 12.5 2012-08-16 14:40:00 bottom\n",
"卖出 12.38 本次获利 -0.11999999999999922 总获利 25.21 2012-08-20 10:28:00 100初仓累计 195.7171979407663\n",
"买入 12.46 2012-08-21 11:08:00 bottom\n",
"卖出 12.4 本次获利 -0.0600000000000005 总获利 25.15 2012-08-22 10:47:00 100初仓累计 194.77473952371605\n",
"买入 10.76 2012-09-07 10:10:00 bottom\n",
"卖出 11.06 本次获利 0.3000000000000007 总获利 25.45 2012-09-14 10:00:00 100初仓累计 200.2052620011431\n",
"买入 10.33 2012-09-28 09:32:00 bottom\n",
"卖出 10.52 本次获利 0.1899999999999995 总获利 25.64 2012-10-08 10:46:00 100初仓累计 203.88764339322606\n",
"买入 10.91 2012-10-10 13:57:00 bottom\n",
"卖出 10.83 本次获利 -0.08000000000000007 总获利 25.560000000000002 2012-10-11 13:06:00 100初仓累计 202.3925919292977\n",
"买入 10.92 2012-10-18 13:07:00 bottom\n",
"卖出 10.86 本次获利 -0.0600000000000005 总获利 25.5 2012-10-25 14:14:00 100初仓累计 201.28054472089497\n",
"买入 10.74 2012-11-01 13:16:00 bottom\n",
"卖出 10.85 本次获利 0.10999999999999943 总获利 25.61 2012-11-07 14:51:00 100初仓累计 203.34207730183525\n",
"买入 10.23 2012-11-19 14:01:00 bottom\n",
"卖出 10.2 本次获利 -0.030000000000001137 总获利 25.58 2012-11-26 10:35:00 100初仓累计 202.74576622470374\n",
"买入 10.03 2012-11-30 13:53:00 bottom\n",
"卖出 10.34 本次获利 0.3100000000000005 总获利 25.89 2012-12-06 10:58:00 100初仓累计 209.0120860182888\n",
"买入 10.64 2012-12-07 10:48:00 bottom\n",
"卖出 10.87 本次获利 0.22999999999999865 总获利 26.119999999999997 2012-12-12 13:11:00 100初仓累计 213.53020441906006\n",
"买入 11.59 2013-01-10 11:00:00 bottom\n",
"卖出 11.45 本次获利 -0.14000000000000057 总获利 25.979999999999997 2013-01-11 10:49:00 100初仓累计 210.9508922000205\n",
"买入 11.81 2013-01-15 13:47:00 bottom\n",
"卖出 13.43 本次获利 1.6199999999999992 总获利 27.599999999999994 2013-02-06 10:55:00 100初仓累计 239.88742440696655\n",
"买入 13.42 2013-02-08 11:21:00 bottom\n",
"卖出 12.74 本次获利 -0.6799999999999997 总获利 26.919999999999995 2013-02-19 13:12:00 100初仓累计 227.7321748841098\n",
"买入 12.16 2013-02-25 11:21:00 bottom\n",
"卖出 11.93 本次获利 -0.23000000000000043 总获利 26.689999999999994 2013-02-27 10:22:00 100初仓累计 223.42474065521628\n",
"买入 11.97 2013-02-28 10:18:00 bottom\n",
"卖出 11.53 本次获利 -0.4400000000000013 总获利 26.249999999999993 2013-03-11 11:12:00 100初仓累计 215.21196823347063\n",
"买入 11.48 2013-03-14 10:54:00 bottom\n",
"卖出 11.42 本次获利 -0.0600000000000005 总获利 26.18999999999999 2013-03-19 10:08:00 100初仓累计 214.08716700576954\n",
"买入 11.56 2013-03-20 13:10:00 bottom\n",
"卖出 11.55 本次获利 -0.009999999999999787 总获利 26.179999999999993 2013-03-22 14:42:00 100初仓累计 213.90197049451888\n",
"买入 10.67 2013-04-02 14:46:00 bottom\n",
"卖出 10.54 本次获利 -0.13000000000000078 总获利 26.04999999999999 2013-04-12 14:09:00 100初仓累计 211.29585464032135\n",
"买入 10.22 2013-04-18 14:11:00 bottom\n",
"卖出 10.32 本次获利 0.09999999999999964 总获利 26.14999999999999 2013-04-26 09:31:00 100初仓累计 213.36332875617575\n",
"买入 10.33 2013-05-08 11:00:00 bottom\n",
"卖出 10.54 本次获利 0.20999999999999908 总获利 26.359999999999992 2013-05-24 10:47:00 100初仓累计 217.70082140271947\n",
"买入 10.64 2013-05-29 10:40:00 bottom\n",
"卖出 10.39 本次获利 -0.25 总获利 26.109999999999992 2013-06-04 14:25:00 100初仓累计 212.58567052389617\n",
"买入 7.85 2013-06-27 10:05:00 bottom\n",
"卖出 7.67 本次获利 -0.17999999999999972 总获利 25.929999999999993 2013-07-02 10:59:00 100初仓累计 207.7110946392718\n",
"买入 7.73 2013-07-05 09:32:00 bottom\n",
"卖出 7.55 本次获利 -0.1800000000000006 总获利 25.749999999999993 2013-07-22 10:20:00 100初仓累计 202.8743550487066\n",
"买入 7.47 2013-07-30 13:35:00 bottom\n",
"卖出 8.2 本次获利 0.7299999999999995 总获利 26.479999999999993 2013-08-14 13:50:00 100初仓累计 222.7000952341893\n",
"买入 8.34 2013-09-03 09:31:00 bottom\n",
"卖出 8.2 本次获利 -0.14000000000000057 总获利 26.339999999999993 2013-09-05 10:22:00 100初仓累计 218.96172433097746\n",
"买入 8.36 2013-09-09 14:04:00 bottom\n",
"卖出 8.86 本次获利 0.5 总获利 26.839999999999993 2013-09-13 13:49:00 100初仓累计 232.0575212407249\n",
"买入 8.59 2013-09-23 10:48:00 bottom\n",
"卖出 8.23 本次获利 -0.35999999999999943 总获利 26.479999999999993 2013-09-26 10:45:00 100初仓累计 222.33217692795878\n",
"买入 8.29 2013-09-30 10:51:00 bottom\n",
"卖出 8.27 本次获利 -0.019999999999999574 总获利 26.459999999999994 2013-10-10 13:37:00 100初仓累计 221.79579049387445\n",
"买入 8.32 2013-10-11 14:48:00 bottom\n",
"卖出 8.4 本次获利 0.08000000000000007 总获利 26.539999999999992 2013-10-15 11:26:00 100初仓累计 223.9284423255463\n",
"买入 7.84 2013-10-31 09:44:00 bottom\n",
"卖出 7.89 本次获利 0.04999999999999982 总获利 26.589999999999993 2013-11-08 14:51:00 100初仓累计 225.35655739139798\n",
"买入 7.73 2013-11-15 13:34:00 bottom\n",
"卖出 7.85 本次获利 0.11999999999999922 总获利 26.709999999999994 2013-11-26 10:10:00 100初仓累计 228.8549774285219\n",
"买入 8.04 2013-11-28 14:54:00 bottom\n",
"卖出 7.98 本次获利 -0.05999999999999872 总获利 26.649999999999995 2013-12-10 14:55:00 100初仓累计 227.14710446263746\n",
"买入 7.77 2013-12-18 14:39:00 bottom\n",
"卖出 7.59 本次获利 -0.17999999999999972 总获利 26.469999999999995 2013-12-20 14:51:00 100初仓累计 221.88500937856094\n",
"买入 7.53 2013-12-30 10:01:00 bottom\n",
"卖出 7.49 本次获利 -0.040000000000000036 总获利 26.429999999999996 2014-01-03 14:22:00 100初仓累计 220.70633734998955\n",
"买入 6.95 2014-01-14 14:12:00 bottom\n",
"卖出 6.81 本次获利 -0.14000000000000057 总获利 26.289999999999996 2014-01-17 14:53:00 100初仓累计 216.26045429545738\n",
"买入 6.87 2014-01-21 10:49:00 bottom\n",
"卖出 6.93 本次获利 0.05999999999999961 总获利 26.349999999999994 2014-02-24 13:48:00 100初仓累计 218.14919188755744\n",
"买入 6.91 2014-03-03 09:31:00 bottom\n",
"卖出 6.83 本次获利 -0.08000000000000007 总获利 26.269999999999996 2014-03-06 10:50:00 100初仓累计 215.6235861927666\n",
"买入 6.59 2014-03-12 14:32:00 bottom\n",
"卖出 6.65 本次获利 0.0600000000000005 总获利 26.33 2014-03-19 13:43:00 100初仓累计 217.5867751414109\n",
"买入 6.85 2014-03-25 10:39:00 bottom\n",
"卖出 6.78 本次获利 -0.0699999999999994 总获利 26.259999999999998 2014-03-27 11:13:00 100初仓累计 215.36326065091473\n",
"买入 6.75 2014-04-01 11:04:00 bottom\n",
"卖出 6.93 本次获利 0.17999999999999972 总获利 26.439999999999998 2014-04-15 11:15:00 100初仓累计 221.1062809349391\n",
"买入 6.89 2014-04-24 11:20:00 bottom\n",
"卖出 6.58 本次获利 -0.3099999999999996 总获利 26.13 2014-04-28 11:24:00 100初仓累计 211.15810283772123\n",
"买入 6.62 2014-05-05 10:36:00 bottom\n",
"卖出 7.03 本次获利 0.41000000000000014 总获利 26.54 2014-05-19 11:23:00 100初仓累计 224.23587053612994\n",
"买入 6.86 2014-06-03 13:30:00 bottom\n",
"卖出 6.8 本次获利 -0.0600000000000005 总获利 26.479999999999997 2014-06-17 11:06:00 100初仓累计 222.2746238550559\n",
"买入 6.72 2014-06-26 13:31:00 bottom\n",
"卖出 6.83 本次获利 0.11000000000000032 总获利 26.589999999999996 2014-07-04 10:20:00 100初仓累计 225.9130477574452\n",
"买入 6.86 2014-07-08 14:19:00 bottom\n",
"卖出 7.73 本次获利 0.8700000000000001 总获利 27.459999999999997 2014-07-23 13:29:00 100初仓累计 254.5638278666256\n",
"买入 7.99 2014-07-25 13:12:00 bottom\n",
"卖出 7.91 本次获利 -0.08000000000000007 总获利 27.379999999999995 2014-07-30 10:18:00 100初仓累计 252.015003557573\n",
"买入 8.1 2014-08-01 10:34:00 bottom\n",
"卖出 7.85 本次获利 -0.25 总获利 27.129999999999995 2014-08-08 10:17:00 100初仓累计 244.23676270703064\n",
"买入 7.94 2014-08-11 10:36:00 bottom\n",
"卖出 8.1 本次获利 0.15999999999999925 总获利 27.289999999999996 2014-08-18 14:34:00 100初仓累计 249.15841031825542\n",
"买入 7.81 2014-08-29 13:13:00 bottom\n",
"卖出 8.78 本次获利 0.9699999999999998 总获利 28.259999999999994 2014-10-10 14:10:00 100初仓累计 280.1038210748121\n",
"买入 8.24 2014-10-28 10:07:00 bottom\n",
"卖出 9.6 本次获利 1.3599999999999994 总获利 29.619999999999994 2014-11-11 13:48:00 100初仓累计 326.33454882502383\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"买入 9.7 2014-11-21 10:47:00 bottom\n",
"卖出 10.23 本次获利 0.5300000000000011 总获利 30.149999999999995 2014-11-28 10:29:00 100初仓累计 344.1651994309272\n",
"买入 10.41 2014-12-01 11:12:00 bottom\n",
"卖出 13.54 本次获利 3.129999999999999 总获利 33.279999999999994 2014-12-17 11:22:00 100初仓累计 447.6461863875844\n",
"买入 14.07 2014-12-19 11:25:00 bottom\n",
"卖出 14.71 本次获利 0.6400000000000006 总获利 33.919999999999995 2014-12-22 14:25:00 100初仓累计 468.00820197308934\n",
"买入 14.79 2014-12-25 10:40:00 bottom\n",
"卖出 14.49 本次获利 -0.29999999999999893 总获利 33.62 2015-01-06 10:36:00 100初仓累计 458.5151349959476\n",
"买入 13.18 2015-01-23 10:48:00 bottom\n",
"卖出 12.93 本次获利 -0.25 总获利 33.37 2015-01-29 09:55:00 100初仓累计 449.81795868722327\n",
"买入 11.49 2015-02-09 10:20:00 bottom\n",
"卖出 13.75 本次获利 2.26 总获利 35.629999999999995 2015-03-04 13:38:00 100初仓累计 538.2939018232654\n",
"买入 13.94 2015-03-10 10:18:00 bottom\n",
"卖出 13.85 本次获利 -0.08999999999999986 总获利 35.53999999999999 2015-03-12 14:22:00 100初仓累计 534.8185466465011\n",
"买入 13.98 2015-03-16 09:59:00 bottom\n",
"卖出 14.68 本次获利 0.6999999999999993 总获利 36.239999999999995 2015-03-24 10:45:00 100初仓累计 561.5977299549811\n",
"买入 14.53 2015-03-25 14:56:00 bottom\n",
"卖出 15.61 本次获利 1.08 总获利 37.31999999999999 2015-03-31 10:05:00 100初仓累计 603.340713323968\n",
"买入 15.3 2015-04-07 10:17:00 bottom\n",
"卖出 15.17 本次获利 -0.13000000000000078 总获利 37.18999999999999 2015-04-14 14:46:00 100初仓累计 598.2142889623917\n",
"买入 14.47 2015-04-17 10:05:00 bottom\n",
"卖出 15.53 本次获利 1.0599999999999987 总获利 38.249999999999986 2015-04-28 10:49:00 100初仓累计 642.0364829015856\n",
"买入 13.86 2015-05-11 13:56:00 bottom\n",
"卖出 13.82 本次获利 -0.03999999999999915 总获利 38.20999999999999 2015-05-14 13:02:00 100初仓累计 640.1835637590125\n",
"买入 14.01 2015-05-15 13:51:00 bottom\n",
"卖出 16.91 本次获利 2.9000000000000004 总获利 41.109999999999985 2015-06-11 10:32:00 100初仓累计 772.6983628240472\n",
"买入 17.2 2015-06-12 10:01:00 bottom\n",
"卖出 16.9 本次获利 -0.3000000000000007 总获利 40.80999999999999 2015-06-16 10:52:00 100初仓累计 759.2210657980463\n",
"买入 15.51 2015-06-24 10:05:00 bottom\n",
"卖出 13.34 本次获利 -2.17 总获利 38.639999999999986 2015-07-22 11:12:00 100初仓累计 652.9986471789773\n",
"买入 11.45 2015-07-30 11:16:00 bottom\n",
"卖出 10.9 本次获利 -0.5499999999999989 总获利 38.08999999999999 2015-07-31 11:20:00 100初仓累计 621.6318999345724\n",
"买入 10.96 2015-08-03 09:56:00 bottom\n",
"卖出 11.87 本次获利 0.9099999999999984 总获利 38.999999999999986 2015-08-06 13:25:00 100初仓累计 673.2454974656363\n",
"买入 11.98 2015-08-10 10:44:00 bottom\n",
"卖出 12.17 本次获利 0.1899999999999995 总获利 39.18999999999998 2015-08-13 10:19:00 100初仓累计 683.9230137025704\n",
"买入 12.36 2015-08-14 10:38:00 bottom\n",
"卖出 10.71 本次获利 -1.6499999999999986 总获利 37.539999999999985 2015-08-21 10:34:00 100初仓累计 592.6226113879069\n",
"买入 11.5 2015-08-25 11:14:00 bottom\n",
"卖出 12.34 本次获利 0.8399999999999999 总获利 38.37999999999998 2015-09-01 09:55:00 100初仓累计 635.9098282197192\n",
"买入 11.36 2015-09-07 10:46:00 bottom\n",
"卖出 11.28 本次获利 -0.08000000000000007 总获利 38.29999999999998 2015-09-08 09:49:00 100初仓累计 631.4315899928198\n",
"买入 12.12 2015-09-09 09:56:00 bottom\n",
"卖出 11.12 本次获利 -1.0 总获利 37.29999999999998 2015-09-15 14:34:00 100初仓累计 579.3332739868116\n",
"买入 14.2 2015-09-22 09:31:00 bottom\n",
"卖出 4.72 本次获利 -9.48 总获利 37.259999999999984 2015-09-23 10:12:00 100初仓累计 577.7013492713559\n",
"买入 4.73 2015-09-23 14:05:00 bottom\n",
"卖出 4.57 本次获利 -0.16000000000000014 总获利 37.09999999999998 2015-09-25 10:21:00 100初仓累计 558.1596545814157\n",
"买入 4.19 2015-10-09 09:57:00 bottom\n",
"卖出 4.48 本次获利 0.29000000000000004 总获利 37.38999999999998 2015-10-14 14:12:00 100初仓累计 596.7912297195089\n",
"买入 4.5 2015-10-19 10:27:00 bottom\n",
"卖出 4.43 本次获利 -0.07000000000000028 总获利 37.31999999999998 2015-10-30 10:40:00 100初仓累计 587.5078105905387\n",
"买入 4.32 2015-11-04 10:52:00 bottom\n",
"卖出 4.5 本次获利 0.17999999999999972 总获利 37.49999999999998 2015-11-12 11:20:00 100初仓累计 611.9873026984777\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-265-93545bb80dc0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 33\u001b[0m \u001b[0mqs_mark_df\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mqs_mark_df\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m'zsg_start_dir'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mlast_zsg_start_dir\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'zsg_end_dir'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mlast_zsg_end_dir\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'zsg_dir'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mlast_zsg_dir\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'zsg_gg'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mlast_zsg_gg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'zsg_dd'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mlast_zsg_dd\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'zsg_nos'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mlast_zsg_nos\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mignore_index\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 34\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 35\u001b[1;33m \u001b[0mlast_zs_k\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"zs_no\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m>\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mzs_no\u001b[0m\u001b[1;33m==\u001b[0m\u001b[1;36m0\u001b[0m \u001b[1;32melse\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"zs_no\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m>\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 36\u001b[0m \u001b[0mlast_zg\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlast_zs_k\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mzg\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 37\u001b[0m \u001b[0mlast_zd\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlast_zs_k\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mzd\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32me:\\app\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 1476\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1477\u001b[0m \u001b[0mmaybe_callable\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_apply_if_callable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1478\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getitem_axis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmaybe_callable\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1479\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1480\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_is_scalar_access\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32me:\\app\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_getitem_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m 1866\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_slice_axis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1867\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mis_bool_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1868\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getbool_axis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1869\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mis_list_like_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1870\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32me:\\app\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_getbool_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m 1491\u001b[0m \u001b[0maxis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maxis\u001b[0m \u001b[1;32mor\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1492\u001b[0m \u001b[0mlabels\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_axis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1493\u001b[1;33m \u001b[0mkey\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcheck_bool_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabels\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1494\u001b[0m \u001b[0minds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnonzero\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1495\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32me:\\app\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36mcheck_bool_indexer\u001b[1;34m(ax, key)\u001b[0m\n\u001b[0;32m 2351\u001b[0m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2352\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mABCSeries\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mand\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mequals\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0max\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2353\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0max\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2354\u001b[0m \u001b[0mmask\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0misna\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_values\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2355\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mmask\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0many\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32me:\\app\\Anaconda3\\lib\\site-packages\\pandas\\core\\series.py\u001b[0m in \u001b[0;36mreindex\u001b[1;34m(self, index, **kwargs)\u001b[0m\n\u001b[0;32m 3320\u001b[0m \u001b[1;33m@\u001b[0m\u001b[0mAppender\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgeneric\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_shared_docs\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'reindex'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0m_shared_doc_kwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3321\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mreindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3322\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mSeries\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3323\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3324\u001b[0m def drop(self, labels=None, axis=0, index=None, columns=None,\n",
"\u001b[1;32me:\\app\\Anaconda3\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36mreindex\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 3683\u001b[0m \u001b[1;31m# perform the reindex on the axes\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3684\u001b[0m return self._reindex_axes(axes, level, limit, tolerance, method,\n\u001b[1;32m-> 3685\u001b[1;33m fill_value, copy).__finalize__(self)\n\u001b[0m\u001b[0;32m 3686\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3687\u001b[0m def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value,\n",
"\u001b[1;32me:\\app\\Anaconda3\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36m_reindex_axes\u001b[1;34m(self, axes, level, limit, tolerance, method, fill_value, copy)\u001b[0m\n\u001b[0;32m 3696\u001b[0m \u001b[0max\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_axis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3697\u001b[0m new_index, indexer = ax.reindex(labels, level=level, limit=limit,\n\u001b[1;32m-> 3698\u001b[1;33m tolerance=tolerance, method=method)\n\u001b[0m\u001b[0;32m 3699\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3700\u001b[0m \u001b[0maxis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_axis_number\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32me:\\app\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mreindex\u001b[1;34m(self, target, method, level, limit, tolerance)\u001b[0m\n\u001b[0;32m 3603\u001b[0m indexer = self.get_indexer(target, method=method,\n\u001b[0;32m 3604\u001b[0m \u001b[0mlimit\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlimit\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3605\u001b[1;33m tolerance=tolerance)\n\u001b[0m\u001b[0;32m 3606\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3607\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mmethod\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mlimit\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32me:\\app\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_indexer\u001b[1;34m(self, target, method, limit, tolerance)\u001b[0m\n\u001b[0;32m 3242\u001b[0m 'backfill or nearest reindexing')\n\u001b[0;32m 3243\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3244\u001b[1;33m \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtarget\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_ndarray_values\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3245\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3246\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0m_ensure_platform_int\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_indexer\u001b[1;34m()\u001b[0m\n",
"\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.Int64HashTable.lookup\u001b[1;34m()\u001b[0m\n",
"\u001b[1;32me:\\app\\Anaconda3\\lib\\site-packages\\numpy\\core\\numeric.py\u001b[0m in \u001b[0;36masarray\u001b[1;34m(a, dtype, order)\u001b[0m\n\u001b[0;32m 422\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 423\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 424\u001b[1;33m \u001b[1;32mdef\u001b[0m \u001b[0masarray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 425\u001b[0m \"\"\"Convert the input to an array.\n\u001b[0;32m 426\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"result_df=pd.DataFrame()\n",
"qs_mark_df=pd.DataFrame()\n",
"zs_no_pre=0\n",
"last_zg=0\n",
"last_zd=0\n",
"last_zsg_gg=0\n",
"last_zsg_dd=0\n",
"last_zsg_nos=''\n",
"last_zsg_dir=''\n",
"last_zsg_start_dir=''\n",
"last_zsg_end_dir=''\n",
"is_merge_last=False\n",
"is_buy=False\n",
"last_buy_price=0\n",
"profit=0\n",
"profit_100=100\n",
"for n, k in df.loc[(df[\"line\"]=='bottom') | (df[\"line\"]=='top')].iterrows():\n",
" #print(zs_no_pre,k.zs_no,zs_no_pre==0)\n",
" if zs_no_pre!=0 and (k.zs_no==0 or k.zs_no>zs_no_pre): #中枢结束\n",
" #print('中枢结束',zs_no_pre, '合并',is_merge_last)\n",
" last_zsg_end_dir='down' if k.line=='bottom' else 'up'\n",
" #print(last_zsg_start_dir,last_zsg_end_dir,last_zsg_dir,last_zsg_nos)\n",
" if is_merge_last:\n",
" #update last one\n",
" index=len(qs_mark_df)-1\n",
" qs_mark_df.ix[index,'zsg_gg']=last_zsg_gg \n",
" qs_mark_df.ix[index,'zsg_dd']=last_zsg_dd\n",
" qs_mark_df.ix[index,'zsg_nos']=last_zsg_nos\n",
" qs_mark_df.ix[index,'zsg_end_dir']=last_zsg_end_dir\n",
" else:\n",
" #append to list\n",
" #print(zs_no_pre,k.zs_no)\n",
" qs_mark_df=qs_mark_df.append({'zsg_start_dir':last_zsg_start_dir,'zsg_end_dir':last_zsg_end_dir, 'zsg_dir':last_zsg_dir, 'zsg_gg':last_zsg_gg, 'zsg_dd':last_zsg_dd, 'zsg_nos':last_zsg_nos},ignore_index=True)\n",
" \n",
" last_zs_k=df.loc[0:n].loc[df[\"zs_no\"]>0].iloc[-1] if k.zs_no==0 else df.loc[0:n-1].loc[df[\"zs_no\"]>0].iloc[-1]\n",
" last_zg=last_zs_k.zg\n",
" last_zd=last_zs_k.zd\n",
" #print(last_zs_k.zs_no,last_zg,last_zd)\n",
"\n",
" if k.zs_no!=0 and (zs_no_pre==0 or k.zs_no>zs_no_pre): #新中枢\n",
" #print('新中枢',k.zs_no,k.zg,k.zd)\n",
" #是否与前面中枢需要合并\n",
" if last_zsg_dd!=0 and last_zsg_gg!=0 and max(last_zsg_dd,k.dd)<min(last_zsg_gg,k.gg):\n",
" #print(last_zsg_gg,last_zsg_dd,k.gg,k.dd)\n",
" last_zsg_gg=max(last_zsg_gg,k.gg)\n",
" last_zsg_dd=min(last_zsg_dd,k.dd)\n",
" last_zsg_nos+=(str(int(k.zs_no))+',')\n",
" is_merge_last=True\n",
" else:\n",
" if last_zsg_dd==0:\n",
" last_zsg_dir=''\n",
" else:\n",
" last_zsg_dir='down' if k.gg<last_zsg_dd else 'up'\n",
" last_zsg_gg=k.gg\n",
" last_zsg_dd=k.dd\n",
" last_zsg_nos=(str(int(k.zs_no))+',')\n",
" last_zsg_start_dir='down' if k.line=='bottom' else 'up'\n",
" is_merge_last=False \n",
" \n",
"# if is_buy!=True and k.line=='bottom' and len(qs_mark_df)>=2: #找三买\n",
"# k1_dir=qs_mark_df.iloc[-2].zsg_dir\n",
"# k1_start_dir=qs_mark_df.iloc[-2].zsg_start_dir\n",
"# k1_end_dir=qs_mark_df.iloc[-2].zsg_end_dir\n",
"# k2_dir=qs_mark_df.iloc[-1].zsg_dir\n",
"# k2_start_dir=qs_mark_df.iloc[-1].zsg_start_dir\n",
"# k2_end_dir=qs_mark_df.iloc[-1].zsg_end_dir\n",
"# k2_gg=qs_mark_df.iloc[-1].zsg_gg \n",
"# k2_dd=qs_mark_df.iloc[-1].zsg_dd\n",
"# #print(k1_start_dir,k1_end_dir,k2_start_dir,k2_end_dir,k2_dir)\n",
"# #买点形态\n",
"# is_qs_ok=False\n",
"# if k1_start_dir=='down' and k1_end_dir=='down' and k2_start_dir=='down' and k2_end_dir=='down' and k2_dir=='down':\n",
"# is_qs_ok=True\n",
"# if k1_start_dir=='down' and k1_end_dir=='down' and k2_start_dir=='down' and k2_end_dir=='up' and k2_dir=='down':\n",
"# is_qs_ok=True\n",
"# #print('is_qs_ok')\n",
"# if is_qs_ok and k.low>k2_gg: #没有碰到中枢,反弹\n",
"# print('买入',k.close,k.trade_time, k.line, k2_gg, k2_dd)\n",
"# is_buy=True\n",
"# last_buy_price=k.close\n",
"\n",
" if is_buy!=True and k.line=='bottom' and len(qs_mark_df)>=2: #找买点\n",
" k2_dir=qs_mark_df.iloc[-1].zsg_dir\n",
" k2_start_dir=qs_mark_df.iloc[-1].zsg_start_dir\n",
" k2_end_dir=qs_mark_df.iloc[-1].zsg_end_dir\n",
" #print(k1_start_dir,k1_end_dir,k2_start_dir,k2_end_dir,k2_dir)\n",
" #买点形态\n",
" if k2_end_dir=='up' and k.low>last_zg: #没有碰到中枢,反弹\n",
" print('买入',k.close,k.trade_time, k.line)\n",
" is_buy=True\n",
" last_buy_price=k.close\n",
" \n",
" if is_buy and k.line=='top' and len(qs_mark_df)>=2: #找卖点\n",
" k2_dir=qs_mark_df.iloc[-1].zsg_dir\n",
" k2_start_dir=qs_mark_df.iloc[-1].zsg_start_dir\n",
" k2_end_dir=qs_mark_df.iloc[-1].zsg_end_dir\n",
" #print(k1_end_dir,k.high,last_zd)\n",
" #卖点形态\n",
" if k2_end_dir=='down' and k.high<last_zd: #没有碰到中枢zd下反弹\n",
" #一股变几股\n",
" rate=1\n",
" trade_time=k.trade_time.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
" if trade_time=='2011-03-16 10:04:00':\n",
" rate=2\n",
" elif trade_time=='2015-09-23 10:12:00':\n",
" rate=3\n",
" \n",
" profit+=k.close*rate-last_buy_price\n",
" profit_100=profit_100*(k.close*rate/last_buy_price)\n",
" print('卖出',k.close,'本次获利',k.close*rate-last_buy_price,'总获利',profit,k.trade_time,'100初仓累计',profit_100)\n",
" is_buy=False\n",
" \n",
" zs_no_pre=k.zs_no"
]
},
{
"cell_type": "code",
"execution_count": 228,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10201"
]
},
"execution_count": 228,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(df.loc[(df[\"line\"]=='bottom') | (df[\"line\"]=='top')])"
]
},
{
"cell_type": "code",
"execution_count": 215,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>zsg_dd</th>\n",
" <th>zsg_dir</th>\n",
" <th>zsg_end_dir</th>\n",
" <th>zsg_gg</th>\n",
" <th>zsg_nos</th>\n",
" <th>zsg_start_dir</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>23.61</td>\n",
" <td></td>\n",
" <td>up</td>\n",
" <td>24.33</td>\n",
" <td>1,</td>\n",
" <td>up</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>24.37</td>\n",
" <td>up</td>\n",
" <td>down</td>\n",
" <td>27.50</td>\n",
" <td>2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,18,...</td>\n",
" <td>up</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>24.06</td>\n",
" <td>down</td>\n",
" <td>down</td>\n",
" <td>24.19</td>\n",
" <td>31,</td>\n",
" <td>down</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>21.75</td>\n",
" <td>down</td>\n",
" <td>up</td>\n",
" <td>22.60</td>\n",
" <td>31,</td>\n",
" <td>down</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>20.61</td>\n",
" <td>down</td>\n",
" <td>down</td>\n",
" <td>21.23</td>\n",
" <td>32,</td>\n",
" <td>down</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>19.35</td>\n",
" <td>down</td>\n",
" <td>down</td>\n",
" <td>20.50</td>\n",
" <td>33,34,35,</td>\n",
" <td>down</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>17.35</td>\n",
" <td>down</td>\n",
" <td>up</td>\n",
" <td>17.58</td>\n",
" <td>36,</td>\n",
" <td>down</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>18.69</td>\n",
" <td>up</td>\n",
" <td>up</td>\n",
" <td>19.06</td>\n",
" <td>37,</td>\n",
" <td>up</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>19.54</td>\n",
" <td>up</td>\n",
" <td>down</td>\n",
" <td>20.60</td>\n",
" <td>38,</td>\n",
" <td>up</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>12.50</td>\n",
" <td>down</td>\n",
" <td>up</td>\n",
" <td>13.18</td>\n",
" <td>39,40,40,</td>\n",
" <td>down</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" zsg_dd zsg_dir zsg_end_dir zsg_gg \\\n",
"0 23.61 up 24.33 \n",
"1 24.37 up down 27.50 \n",
"2 24.06 down down 24.19 \n",
"3 21.75 down up 22.60 \n",
"4 20.61 down down 21.23 \n",
"5 19.35 down down 20.50 \n",
"6 17.35 down up 17.58 \n",
"7 18.69 up up 19.06 \n",
"8 19.54 up down 20.60 \n",
"9 12.50 down up 13.18 \n",
"\n",
" zsg_nos zsg_start_dir \n",
"0 1, up \n",
"1 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,18,... up \n",
"2 31, down \n",
"3 31, down \n",
"4 32, down \n",
"5 33,34,35, down \n",
"6 36, down \n",
"7 37, up \n",
"8 38, up \n",
"9 39,40,40, down "
]
},
"execution_count": 215,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qs_mark_df.loc[0:9]"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"direction\n",
"downdown 13.105517\n",
"upup 16.638000\n",
"Name: trade_price, dtype: float64"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_df.groupby('direction')['trade_price'].mean()"
]
},
{
"cell_type": "code",
"execution_count": 255,
"metadata": {},
"outputs": [],
"source": [
"outputpath='E:/jupyter/000425-10Y.csv'\n",
"df.to_csv(outputpath,sep=',',index=True,header=True)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda root]",
"language": "python",
"name": "conda-root-py"
},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because one or more lines are too long

2415465
code/Untitled.ipynb Normal file

File diff suppressed because one or more lines are too long

1690
code/Untitled1.ipynb Normal file

File diff suppressed because it is too large Load Diff

137
code/Untitled2-Copy1.ipynb Normal file

File diff suppressed because one or more lines are too long

129
code/Untitled2.ipynb Normal file

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

1025
code/analyses-single.ipynb Normal file

File diff suppressed because it is too large Load Diff

1263
code/analyses.ipynb Normal file

File diff suppressed because one or more lines are too long

443
code/chan.py Normal file
View File

@@ -0,0 +1,443 @@
def build(df_dr):
#一、先合并
group_no_last = 0 #最后的组号
ingroup_n_last = 0 #最后一个在组内的n
group_first_n = 0 #最后一个组的第一个n
chan_low_pre = None
chan_high_pre = None
for n, k in df_dr.iterrows():
open = k.open
close = k.close
low = k.low
high = k.high
#每条的变量
updown='flat' #升降
is_group=False #是否在组里
is_group_m=False #是否组长,组长是最高的
group_no=0
group_high=0
group_low=0
if(chan_low_pre==None or chan_high_pre==None):
chan_low_pre = low
chan_high_pre = high
else:
#上一个原始K
k_pre = df_dr.iloc[n-1-1]
low_pre = k_pre.low
high_pre = k_pre.high
#上升下降
#上升(跟原始K比较平均高度)
if high_pre+low_pre < high+low:
updown='up'
#下降(跟原始K比较平均高度)
elif high_pre+low_pre > high+low:
updown='down'
elif open<close:
updown = 'up'
elif open<close:
updown = 'down'
else:
updown = 'flat'
#包含 #不能太多的包含,限制一组成员个数
if ((chan_high_pre<=high and chan_low_pre>=low) or (chan_high_pre>=high and chan_low_pre<=low)) and n<group_first_n+5:
#组长
if chan_high_pre<=high and chan_low_pre>=low:
is_group_m = True
elif chan_high_pre>=high and chan_low_pre<=low and ingroup_n_last!=n-1: #新组并且前面的高,需要设置前面的是组长
df_dr.at[n-1,'group_master']= True
group_high=max(high, chan_high_pre)
group_low=min(low, chan_low_pre)
chan_high_pre = group_high
chan_low_pre = group_low
#上一个本来在组内,这个合并到原来的组
if ingroup_n_last==n-1:
group_no=group_no_last
#新组
else:
group_no=group_no_last+1
group_first_n=n-1
group_no_last=group_no
is_group=True
ingroup_n_last=n
else:
chan_low_pre = low
chan_high_pre = high
#写标识值到表中
df_dr.at[n,'updown']= updown
df_dr.at[n,'group']= is_group
df_dr.at[n,'group_no']= group_no
df_dr.at[n,'group_high']= group_high
df_dr.at[n,'group_low']= group_low
df_dr.at[n,'group_master']= is_group_m
if is_group:
df_dr.at[n-1,'group']= is_group
df_dr.at[n-1,'group_no']= group_no
#刷新同组内的group_high and group_low
for j in range(group_first_n, n):
df_dr.at[j,'group_high']= group_high
df_dr.at[j,'group_low']= group_low
if is_group_m:
df_dr.at[j,'group_master']= False #踢掉原来的组长
#二、找分型
fx_n_maybe_pre = 0 #前面可能是分型的n如果有方向则临时记录
direction_pre = "flat"
chan_low_pre = None
chan_high_pre = None
df_dr['fx']=''
for n, k in df_dr.iterrows():
is_group = k.group
is_group_master = k.group_master
group_high = k.group_high
group_low = k.group_low
low = k.low
high = k.high
is_confirm_fx = False #确定分型,前一个组或者个体是分型,为顶分型
is_confirm_fx_n = 0 #已确定分型的分型顶点
fx_type = ''
if(chan_low_pre==None or chan_high_pre==None):
chan_low_pre=low
chan_high_pre=high
elif (is_group and is_group_master) or not is_group: #只管组长和非组成员
cal_high = high
cal_low = low
if (is_group and is_group_master):
cal_high = group_high
cal_low = group_low
#与前面的比较
if chan_high_pre+chan_low_pre>cal_high+cal_low: #下降
if direction_pre=="up":
is_confirm_fx=True
is_confirm_fx_n=fx_n_maybe_pre
fx_type='top'
direction_pre="down"
fx_n_maybe_pre=n
elif chan_high_pre+chan_low_pre<cal_high+cal_low: #上升
if direction_pre=="down":
is_confirm_fx=True
is_confirm_fx_n=fx_n_maybe_pre
fx_type='bottom'
direction_pre="up"
fx_n_maybe_pre=n
chan_low_pre=cal_low
chan_high_pre=cal_high
#分型写入表
if is_confirm_fx:
df_dr.at[is_confirm_fx_n,'fx']= fx_type
#三、找笔
fx_n_pre = None
node_n_pre = None #node是前面确认的笔的节点
node_type_pre =''
node_high_pre = None
node_low_pre = None
df_dr['bi']=''
for n, k in df_dr.iterrows():
fx_type = k.fx
high = k.high
low = k.low
is_node = False
if fx_type=='top' or fx_type=='bottom':
#第一个分型确定为笔的节点
if fx_n_pre==None:
is_node=True
#与前节点类型相同且趋势延申删除上一个节点node
elif node_type_pre==fx_type and ((fx_type=='bottom' and low<node_low_pre) or (fx_type=='top' and high>node_high_pre)):
df_dr.at[node_n_pre,'bi']= ''
is_node=True
#与前节点类型不同且超过3个差,是新笔
elif node_type_pre!=fx_type and n-node_n_pre>3:
is_node=True
fx_n_pre=n
if is_node:
node_n_pre=n
node_type_pre=fx_type
node_high_pre=high
node_low_pre=low
if is_node:
df_dr.at[n,'bi']= fx_type
#四、找线段 (n是索引从0开始 i是第几个)
dot_i_pre = None #dot是前面确认的线段的节点
dot_type_pre =''
dot_high_pre = None
dot_low_pre = None
#只保留笔的K
df_bi = df_dr.loc[(df_dr["bi"]=='top') | (df_dr["bi"]=='bottom')]
df_dr['line']=''
#找第一个线段,第一个形成走势的三笔(如果上升,第三笔的结束点高于第一笔的结束点)
for i in range(len(df_bi)): #i是笔表的索引 n是原表的index字段即第几个
k=df_bi.iloc[i]
n=df_bi.index[i]
bi_type=k.fx
high=k.high
low=k.low
if i<len(df_bi)-3:
next3n=df_bi.index[i+3]
if bi_type=='top' and df_bi.iloc[i+1].low > df_bi.iloc[i+3].low:
df_dr.at[n,'line']='top'
df_dr.at[next3n,'line']='bottom'
dot_i_pre=i+3
dot_type_pre='bottom'
break
if bi_type=='bottom' and df_bi.iloc[i+1].high < df_bi.iloc[i+3].high:
df_dr.at[n,'line']='bottom'
df_dr.at[next3n,'line']='top'
dot_i_pre=i+3
dot_type_pre='top'
break
#继续找线段
i=dot_i_pre
while i!=None and i<len(df_bi):
k=df_bi.iloc[i]
n=df_bi.index[i]
bi_type=k.fx
high=k.high
low=k.low
#不确定延申还是反弹,一直往下找,找到能确定的点
j=i+2
is_found=False
while j<len(df_bi):
if dot_type_pre=='top':
if (j-i)%2==0 and j<len(df_bi) and df_bi.iloc[j].high>high: #看是否突破延申
j_n=df_bi.index[j]
df_dr.at[n,'line']=''
df_dr.at[j_n,'line']='top'
dot_i_pre=j_n
dot_type_pre='top'
i=j
is_found=True
break
elif (j-i)%2==1 and j<len(df_bi) and df_bi.iloc[j].low<df_bi.iloc[i+1].low: #没有延申,反弹形成新的线段
j_n=df_bi.index[j]
df_dr.at[j_n,'line']='bottom'
dot_i_pre=j_n
dot_type_pre='bottom'
i=j
is_found=True
break
elif dot_type_pre=='bottom':
if (j-i)%2==0 and j<len(df_bi) and df_bi.iloc[j].low<low: #看是否突破延申
j_n=df_bi.index[j]
df_dr.at[n,'line']=''
df_dr.at[j_n,'line']='bottom'
dot_i_pre=j_n
dot_type_pre='bottom'
i=j
is_found=True
break
elif (j-i)%2==1 and j<len(df_bi) and df_bi.iloc[j].high>df_bi.iloc[i+1].high: #没有延申,反弹形成新的线段
j_n=df_bi.index[j]
j_n=df_bi.index[j]
df_dr.at[j_n,'line']='top'
dot_i_pre=j_n
dot_type_pre='top'
i=j
is_found=True
break
j=j+1
if not is_found:
break #没找到下一个线段点,退出
else:
continue
#修正线段中间的凹凸漏点,例如上升的线段中有笔的低点低于线段的开始点,需要将这个线段的开始点移到这个更低的点,反之亦然
#有时候突然下来一笔,但是不成线段就会遗漏,此处待验证合理性
dot_type_pre =''
dot_n_pre = None
dot_high_pre = None
dot_low_pre = None
i=0
while i<len(df_bi):
n=df_bi.index[i]
k=df_bi.iloc[i]
bi_type=k.fx
high=k.high
low=k.low
dot_type = df_dr.at[n,'line']
if dot_type=='top' or dot_type=='bottom':
dot_n_pre=n
dot_type_pre=dot_type
dot_high_pre=high
dot_low_pre=low
else:
#移动不能使与后面小于三比差,即后面一个如果是线段点,就不处理这个笔点了
if i<len(df_bi)-1:
next_n = df_bi.index[i+1]
next_dot_type = df_dr.at[next_n,'line']
if next_dot_type=='top' or next_dot_type=='bottom':
i=i+1
continue
#移动
if dot_type_pre=='top' and high>dot_high_pre:
df_dr.at[dot_n_pre,'line']=''
df_dr.at[n,'line']='top'
dot_n_pre=n
dot_high_pre=high
dot_low_pre=low
elif dot_type_pre=='bottom' and low<dot_low_pre:
df_dr.at[dot_n_pre,'line']=''
df_dr.at[n,'line']='bottom'
dot_n_pre=n
dot_high_pre=high
dot_low_pre=low
i=i+1
'''
#找笔中枢
#只保留笔的K
df_bi = df_dr.loc[(df_dr["bi"]=='top') | (df_dr["bi"]=='bottom')]
df_dr['b_zs_no']=0
df_dr['b_zg']=0
df_dr['b_zd']=0
df_dr['b_gg']=0
df_dr['b_dd']=0
zs_no=0 #最后一个中枢号
i=0
while i <len(df_bi):
k=df_bi.iloc[i]
n=df_bi.index[i]
bi_type=k.bi
high=k.high
low=k.low
#找从这个点开始可能的中枢
zg=0
zd=0
gg=0
dd=0
j=i+1
while j<len(df_bi):
j_k=df_bi.iloc[j]
j_n=df_bi.index[j]
jp_n=df_bi.index[j-1]
j_bi_type=j_k.bi
j_high=j_k.high
j_low=j_k.low
#print(i,j,zg,zd)
if (j_bi_type=='top' and j_high<zd and j-i>5) or (j_bi_type=='bottom' and j_low>zg and j-i>5):
#找到中枢(这时肯定已经两个点脱离,没有走回去),保存,退出,往后面继续找考虑到两个中枢被一条线连接的情况需要从j-2开始找
# /\ -------------
# / or -------------
# ------------- \
# ------------- \/
#print(i,j,'找到中枢')
zs_no=zs_no+1
df_dr.b_zs_no[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.bi!='')]=zs_no
df_dr.b_zg[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.bi!='')]=zg
df_dr.b_zd[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.bi!='')]=zd
df_dr.b_gg[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.bi!='')]=gg
df_dr.b_dd[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.bi!='')]=dd
i=j-3
break
elif (j-i<=5 and bi_type=='bottom' and zd!=0 and zd<low) or (j-i<=5 and bi_type=='top' and zg!=0 and zg>high):
#前三段zg zd超出起始线段的高、低点中枢破坏
#print(i,j,'zg zd超出起始线段的高、低点中枢破坏')
break
elif j-i<=5 and j-i>2 and zg<zd:
#前三段zg<zd中枢破坏
#print(i,j,'zg<zd中枢破坏',zg,zd)
break
elif (j-i==5 and j_bi_type=='bottom' and j_low>zg) or (j-i==5 and j_bi_type=='top' and j_high<zd):
#第五个点没有回到或跨越zgzd中枢破坏
#print(i,j,'第五个点中枢破坏')
break
if j-i<5: #只对前三个线段作为zg zd的参考点
if j_bi_type=='top':
zg=min(zg,j_high) if zg!=0 else j_high
gg=max(gg,j_high) if gg!=0 else j_high
elif j_bi_type=='bottom':
zd=max(zd,j_low) if zd!=0 else j_low
dd=min(dd,j_low) if dd!=0 else j_low
j=j+1
i=i+1
'''
#找线段中枢
#只保留线段的K
df_line = df_dr.loc[(df_dr["line"]=='bottom') | (df_dr["line"]=='top')]
df_dr['zs_no']=0
df_dr['zg']=0
df_dr['zd']=0
df_dr['gg']=0
df_dr['dd']=0
df_dr['zs_direction']=''
df_dr['zs_confirm_time']=''
df_dr['zs_confirm_price']=''
zs_no=0 #最后一个中枢号
i=0
while i <len(df_line):
k=df_line.iloc[i]
n=df_line.index[i]
line_type=k.line
high=k.high
low=k.low
#找从这个点开始可能的中枢
zg=0
zd=0
gg=0
dd=0
start_direction='down' if k.line=='top' else 'up' #第一个点是top则是下降方向
j=i+1
while j<len(df_line):
j_k=df_line.iloc[j]
j_n=df_line.index[j]
jp_n=df_line.index[j-1]
j_line_type=j_k.line
j_high=j_k.high
j_low=j_k.low
#print(i,j,zg,zd)
if (j_line_type=='top' and j_high<zd and j-i>5) or (j_line_type=='bottom' and j_low>zg and j-i>5):
#找到中枢(这时肯定已经两个点脱离,没有走回去),保存,退出,往后面继续找考虑到两个中枢被一条线连接的情况需要从j-2开始找
# /\ -------------
# / or -------------
# ------------- \
# ------------- \/
#print(i,j,'找到中枢')
zs_no=zs_no+1
end_direction='down' if j_k.line=='top' else 'up' #小尾巴是top则是下降方向
df_dr.zs_no[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.line!='')]=zs_no
df_dr.zg[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.line!='')]=zg
df_dr.zd[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.line!='')]=zd
df_dr.gg[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.line!='')]=gg
df_dr.dd[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.line!='')]=dd
df_dr.zs_direction[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.line!='')]=start_direction+end_direction
df_dr.zs_confirm_time[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.line!='')]=j_k.trade_time
df_dr.zs_confirm_price[(df_dr.index>n) & (df_dr.index<jp_n) & (df_dr.line!='')]=j_k.close
i=j-3
break
elif (j-i<=5 and line_type=='bottom' and zd!=0 and zd<low) or (j-i<=5 and line_type=='top' and zg!=0 and zg>high):
#前三段zg zd超出起始线段的高、低点中枢破坏
#print(i,j,'zg zd超出起始线段的高、低点中枢破坏')
break
elif j-i<=5 and j-i>2 and zg<zd:
#前三段zg<zd中枢破坏
#print(i,j,'zg<zd中枢破坏',zg,zd)
break
elif (j-i==5 and j_line_type=='bottom' and j_low>zg) or (j-i==5 and j_line_type=='top' and j_high<zd):
#第五个点没有回到或跨越zgzd中枢破坏
#print(i,j,'第五个点中枢破坏')
break
if j-i<5: #只对前三个线段作为zg zd的参考点
if j_line_type=='top':
zg=min(zg,j_high) if zg!=0 else j_high
gg=max(gg,j_high) if gg!=0 else j_high
elif j_line_type=='bottom':
zd=max(zd,j_low) if zd!=0 else j_low
dd=min(dd,j_low) if dd!=0 else j_low
j=j+1
i=i+1
return df_dr

464
code/chan_series.py Normal file
View File

@@ -0,0 +1,464 @@
def build(df_dr):
#合并 相关变量
group_no_last = 0 #最后的组号
group_master_n_last = 0 #最后的组长
ingroup_n_last = 0 #最后一个在组内的n
group_first_n = 0 #最后一个组的第一个n
chan_low_pre = None #chan_是指合并后的k的高低
chan_high_pre = None
df_dr['updown']= '' #K线是向上up 向下down 平flat
df_dr['group']= '' #是否在组内
df_dr['group_no']= '' #组的编号
df_dr['group_high']= '' #组的最高值
df_dr['group_low']= '' #组的最低值
df_dr['group_master']= '' #是否是组长
#分型 相关变量
fx_n_maybe_pre = 0 #前面可能是分型的n如果有方向则临时记录
fx_direction_pre = "flat" #平均值的方向
fx_high_direction_pre = "flat" #高点的方向
fx_low_direction_pre = "flat" #低点的方向
fx_chan_low_pre = None #chan_是指合并后的k的高低
fx_chan_high_pre = None
df_dr['fx']='' #分型的类型底bottom 顶top
#笔 相关变量
fx_n_pre = None
node_n_pre = None #node是前面确认的笔的节点
node_type_pre =''
node_high_pre = None
node_low_pre = None
df_dr['bi']='' #笔的类型底bottom 顶top
bi_count=0
bi_first_n=0 #第一个笔的n
bi_second_n=0 #第二个笔的n
bi_third_n=0 #第三个笔的n
#线段相关变量
dot_n_pre = None #dot是前面确认的线段的节点
dot_bi_count_pre = None #前一个dot是第几个笔
dot_next_bi_n_pre = None #前一个dot的后一笔的n 用于判断新线段反弹
dot_type_pre =''
dot_high_pre = None
dot_low_pre = None
df_dr['line']='' #线段的类型底bottom 顶top
#中枢相关变量
zs_no=0 #最后一个中枢号
zs_first_n_pre=0 #未结束的中枢的开始n
zs_end_n_pre=0 #前面已结束的中枢的结束n
zg=0
zd=0
gg=0
dd=0
start_direction=''
df_dr['zs_no']=0
df_dr['zg']=0
df_dr['zd']=0
df_dr['gg']=0
df_dr['dd']=0
df_dr['zs_direction']=''
df_dr['zs_confirm_time']=''
df_dr['zs_confirm_price']=''
#逐笔循环
for n, k in df_dr.iterrows():
if n%10000==0:
print('开始处理:',n,'/',len(df_dr))
open = k.open
close = k.close
low = k.low
high = k.high
###一、合并 #############################################################################
updown='flat' #升降
is_group=False #是否在组里
is_group_master=False #是否组长,组长是最高的
group_no=0
group_high=0
group_low=0
is_confirm_v_group = False #确定虚拟组包括单个不成组的K,即前面的K跟这个肯定不同组那么前面的虚拟组确认
is_confirm_v_group_n = 0 #已确定虚拟组的组长单个K就是这个K
if(chan_low_pre==None or chan_high_pre==None):
chan_low_pre = low
chan_high_pre = high
else:
#上一个原始K
k_pre = df_dr.iloc[n-1-1]
low_pre = k_pre.low
high_pre = k_pre.high
is_group_pre = k_pre.group
#上升下降
#上升(跟原始K比较平均高度)
if high_pre+low_pre < high+low:
updown='up'
#下降(跟原始K比较平均高度)
elif high_pre+low_pre > high+low:
updown='down'
elif open<close:
updown = 'up'
elif open<close:
updown = 'down'
else:
updown = 'flat'
#包含 #不能太多的包含,限制一组成员个数
if ((chan_high_pre<=high and chan_low_pre>=low) or (chan_high_pre>=high and chan_low_pre<=low)) and not (n>group_first_n+2 and ingroup_n_last==n-1):
#上一个本来在组内,这个合并到原来的组
if ingroup_n_last==n-1:
group_no=group_no_last
#新组
else:
group_no=group_no_last+1
group_first_n=n-1
group_no_last=group_no
#组长
if chan_high_pre<=high and chan_low_pre>=low:
is_group_master = True
group_master_n_last=n
elif chan_high_pre>=high and chan_low_pre<=low and ingroup_n_last!=n-1: #新组并且前面的高,需要设置前面的是组长
df_dr.at[n-1,'group_master']= True
group_master_n_last=n-1
group_high=max(high, chan_high_pre)
group_low=min(low, chan_low_pre)
chan_high_pre = group_high
chan_low_pre = group_low
is_group=True
ingroup_n_last=n
else: #不包含
chan_low_pre = low
chan_high_pre = high
is_confirm_v_group = True #这个与前面没有包含关系,则前面的确认
is_confirm_v_group_n = group_master_n_last if is_group_pre else n-1
#写标识值到表中
df_dr.at[n,'updown']= updown
df_dr.at[n,'group']= is_group
df_dr.at[n,'group_no']= group_no
df_dr.at[n,'group_high']= group_high
df_dr.at[n,'group_low']= group_low
df_dr.at[n,'group_master']= is_group_master
if is_group:
df_dr.at[n-1,'group']= is_group
df_dr.at[n-1,'group_no']= group_no
#刷新同组内的group_high and group_low
for j in range(group_first_n, n):
df_dr.at[j,'group_high']= group_high
df_dr.at[j,'group_low']= group_low
if is_group_master:
df_dr.at[j,'group_master']= False #踢掉原来的组长
###二、分型 #############################################################################
## 分型需要出现的k线或者组明确如果不确认是否被分组或者分组未结束即不确认组长的需要等确认后再确认分型
is_confirm_fx = False #确定分型,前一个组或者个体是分型,为顶分型
is_confirm_fx_n = 0 #已确定分型的分型顶点
fx_type = ''
fx_direction = "flat" #平均值的方向
fx_high_direction = "flat" #高点的方向
fx_low_direction = "flat" #低点的方向
if is_confirm_v_group:
last_group_high=df_dr.at[is_confirm_v_group_n,'high']
last_group_low=df_dr.at[is_confirm_v_group_n,'low']
if(fx_chan_low_pre==None or fx_chan_high_pre==None):
fx_chan_low_pre=last_group_low
fx_chan_high_pre=last_group_high
else:
#与前面的比较
if fx_chan_high_pre+fx_chan_low_pre<last_group_high+last_group_low:
fx_direction='up'
elif fx_chan_high_pre+fx_chan_low_pre>last_group_high+last_group_low:
fx_direction='down'
if last_group_high>fx_chan_high_pre:
fx_high_direction='up'
elif last_group_high<fx_chan_high_pre:
fx_high_direction='down'
if last_group_low>fx_chan_low_pre:
fx_low_direction='up'
elif last_group_low<fx_chan_low_pre:
fx_low_direction='down'
if fx_high_direction_pre=='up' and fx_high_direction=='down': #顶
is_confirm_fx=True
is_confirm_fx_n=fx_n_maybe_pre
fx_type='top'
elif fx_low_direction_pre=='down' and fx_low_direction=='up': #底
is_confirm_fx=True
is_confirm_fx_n=fx_n_maybe_pre
fx_type='bottom'
if fx_high_direction=='up' or fx_low_direction=='down' or fx_high_direction=='flat' or fx_low_direction=='flat':
fx_n_maybe_pre=is_confirm_v_group_n
if fx_direction!='flat':
fx_direction_pre=fx_direction
if fx_high_direction!='flat':
fx_high_direction_pre=fx_high_direction
if fx_low_direction!='flat':
fx_low_direction_pre=fx_low_direction
fx_chan_low_pre=last_group_low
fx_chan_high_pre=last_group_high
#分型写入表
if is_confirm_fx:
df_dr.at[is_confirm_fx_n,'fx']= fx_type
###三、笔 #############################################################################
##笔的确认是检查上面确认的分型的is_confirm_fx_n不是当前循环的k线n因为当前的k线n不可能确认分型
is_confirm_bi = False
is_confirm_bi_n = 0
is_bi_extend = False
if is_confirm_fx:
last_fx_high=df_dr.at[is_confirm_fx_n,'high']
last_fx_low=df_dr.at[is_confirm_fx_n,'low']
#第一个分型确定为笔的节点
if fx_n_pre==None:
is_confirm_bi=True
bi_count+=1
#与前节点类型相同且趋势延申删除上一个节点node
elif node_type_pre==fx_type and ((fx_type=='bottom' and last_fx_low<node_low_pre) or (fx_type=='top' and last_fx_high>node_high_pre)):
df_dr.at[node_n_pre,'bi']= ''
is_confirm_bi=True
is_bi_extend=True
#与前节点类型不同且超过3个差,是新笔
elif node_type_pre!=fx_type and is_confirm_fx_n-node_n_pre>3:
if (fx_type=='bottom' and last_fx_low<node_high_pre) or (fx_type=='top' and last_fx_high>node_low_pre): #顶底不能高低反
is_confirm_bi=True
bi_count+=1
fx_n_pre=is_confirm_fx_n
if is_confirm_bi:
node_n_pre=is_confirm_fx_n
node_type_pre=fx_type
node_high_pre=last_fx_high
node_low_pre=last_fx_low
is_confirm_bi_n=is_confirm_fx_n
df_dr.at[is_confirm_bi_n,'bi']= fx_type
if bi_count==1:
bi_first_n=is_confirm_bi_n
elif bi_count==2:
bi_second_n=is_confirm_bi_n
elif bi_count==3:
bi_third_n=is_confirm_bi_n
###三、线段 #############################################################################
###
is_confirm_line=False
is_extend_line=False
if is_confirm_bi:
last_bi_type=df_dr.at[is_confirm_bi_n,'bi']
last_bi_high=df_dr.at[is_confirm_bi_n,'high']
last_bi_low=df_dr.at[is_confirm_bi_n,'low']
#如果上一个笔确认了线段,那么需要记录相邻的下一个笔,用于判断反弹的新线段(是否超越这个笔)
if dot_bi_count_pre!=None and dot_bi_count_pre+1==bi_count:
dot_next_bi_n_pre=is_confirm_bi_n
#找第一个线段,第一个形成走势的三+笔(如果上升,第三+笔的结束点高于第一笔的结束点)
if dot_n_pre==None and bi_count>3:
bi_first2second_direction='up' if df_dr.at[bi_second_n,'bi']=='top' else 'down'
bi_second_high=df_dr.at[bi_second_n,'high']
bi_second_low=df_dr.at[bi_second_n,'low']
bi_third_high=df_dr.at[bi_third_n,'high']
bi_third_low=df_dr.at[bi_third_n,'low']
if last_bi_type=='top' and last_bi_high>bi_second_high and bi_first2second_direction=='up': #上升突破第二个笔节点
df_dr.at[bi_first_n,'line']='bottom'
df_dr.at[is_confirm_bi_n,'line']='top'
dot_n_pre=is_confirm_bi_n
dot_type_pre='top'
dot_bi_count_pre=bi_count
is_confirm_line=True
elif last_bi_type=='bottom' and last_bi_low<bi_second_low and bi_first2second_direction=='down': #下降突破第二个笔节点
df_dr.at[bi_first_n,'line']='top'
df_dr.at[is_confirm_bi_n,'line']='bottom'
dot_n_pre=is_confirm_bi_n
dot_type_pre='bottom'
dot_bi_count_pre=bi_count
is_confirm_line=True
elif last_bi_type=='top' and last_bi_high>bi_third_high and bi_first2second_direction=='down': #上升突破第三个笔节点
df_dr.at[bi_second_n,'line']='bottom'
df_dr.at[is_confirm_bi_n,'line']='top'
dot_n_pre=is_confirm_bi_n
dot_type_pre='top'
dot_bi_count_pre=bi_count
is_confirm_line=True
elif last_bi_type=='bottom' and last_bi_low<bi_third_low and bi_first2second_direction=='up': #下降突破第三个笔节点
df_dr.at[bi_second_n,'line']='top'
df_dr.at[is_confirm_bi_n,'line']='bottom'
dot_n_pre=is_confirm_bi_n
dot_type_pre='bottom'
dot_bi_count_pre=bi_count
is_confirm_line=True
#继续找线段,不确定延申还是反弹,一直往下找,找到能确定的点
if dot_type_pre=='top':
dot_high_pre=df_dr.at[dot_n_pre,'high']
dot_low_pre=df_dr.at[dot_n_pre,'low']
if last_bi_type=='top' and last_bi_high>dot_high_pre: #看是否突破延申
df_dr.at[dot_n_pre,'line']=''
df_dr.at[is_confirm_bi_n,'line']='top'
dot_n_pre=is_confirm_bi_n
dot_type_pre='top'
dot_bi_count_pre=bi_count
is_confirm_line=True
is_extend_line=True
elif last_bi_type=='bottom' and bi_count>dot_bi_count_pre+2 : #没有延申,反弹形成新的线段
df_dr.at[is_confirm_bi_n,'line']='bottom'
dot_n_pre=is_confirm_bi_n
dot_type_pre='bottom'
dot_bi_count_pre=bi_count
is_confirm_line=True
elif dot_type_pre=='bottom':
dot_high_pre=df_dr.at[dot_n_pre,'high']
dot_low_pre=df_dr.at[dot_n_pre,'low']
if last_bi_type=='bottom' and last_bi_low<dot_low_pre: #看是否突破延申
df_dr.at[dot_n_pre,'line']=''
df_dr.at[is_confirm_bi_n,'line']='bottom'
dot_n_pre=is_confirm_bi_n
dot_type_pre='bottom'
dot_bi_count_pre=bi_count
is_confirm_line=True
is_extend_line=True
elif last_bi_type=='top' and bi_count>dot_bi_count_pre+2 : #没有延申,反弹形成新的线段
df_dr.at[is_confirm_bi_n,'line']='top'
dot_n_pre=is_confirm_bi_n
dot_type_pre='top'
dot_bi_count_pre=bi_count
is_confirm_line=True
#修正线段中间的凹凸漏点,例如上升的线段中有笔的低点低于线段的开始点,需要将这个线段的开始点移到这个更低的点,反之亦然
#有时候突然下来一笔,但是不成线段就会遗漏,此处待验证合理性
if is_confirm_line:
df_dr_line=df_dr[df_dr.line!='']
last_line_n = dot_n_pre
last2_line_n = df_dr_line.index[-2]
last2_line_high = df_dr_line.iloc[-2].high
last2_line_low = df_dr_line.iloc[-2].low
last2_line_type = df_dr_line.iloc[-2].line
df_mod = df_dr.loc[last2_line_n:last_line_n][df_dr.bi!='']
mod_i=0
modified_n_pre=None
for mod_n, mod_k in df_mod.iterrows():
mod_high=mod_k.high
mod_low=mod_k.low
mod_line_type = df_mod.at[mod_n,'line']
if mod_line_type=='':
#移动不能使与后面小于三比差,即后面一个如果是线段点,就不处理这个笔点了
if mod_i>len(df_mod)-4:
break
#移动
if last2_line_type=='top' and mod_high>last2_line_high:
df_dr.at[last2_line_n,'line']=''
if modified_n_pre!=None:
df_dr.at[modified_n_pre,'line']=''
df_dr.at[mod_n,'line']='top'
modified_n_pre=mod_n
elif last2_line_type=='bottom' and mod_low<last2_line_low:
df_dr.at[last2_line_n,'line']=''
if modified_n_pre!=None:
df_dr.at[modified_n_pre,'line']=''
df_dr.at[mod_n,'line']='bottom'
modified_n_pre=mod_n
mod_i+=1
###四、中枢 #############################################################################
###
is_confirm_zs=False
if is_confirm_line:
df_dr_line=df_dr.loc[max(zs_first_n_pre,zs_end_n_pre):][df_dr.line!=''] #只取最后需要考虑的一段
#线段延申需要处理,上一个结束点到这个点正好是三个,是判断中枢结束的小尾巴点,小尾巴延申可能延长中枢
#但是线段的修正不会影响中枢因为可能影响的就是gg dd但是计算gg dd是计算到倒数第三个不包括刚修正的线段
if is_extend_line and len(df_dr_line)==3 and zs_end_n_pre>zs_first_n_pre:
dot_last_n=df_dr_line.index[-1] #最后一个线段dot
dot_last_type=df_dr_line.at[dot_last_n,'line']
dot_last_high=df_dr_line.at[dot_last_n,'high']
dot_last_low=df_dr_line.at[dot_last_n,'low']
if (dot_last_type=='top' and dot_last_high>zd) or (dot_last_type=='bottom' and dot_last_low<zg):
#把结束点放到上一个结束点去
zs_end_n_pre=0 if zs_no==0 else df_dr[df_dr.zs_no==zs_no].index[-1]
#print('线段延申导致中枢延申,取消上次结束点,继续延申下去')
#寻找新的中枢
if len(df_dr_line)>=6 and zs_first_n_pre<=zs_end_n_pre:
dot1_k_n=df_dr_line.index[-6] #可能的前导头
dot2_k_n=df_dr_line.index[-5] #可能的中枢第一个点
dot3_k_n=df_dr_line.index[-4]
dot4_k_n=df_dr_line.index[-3]
dot5_k_n=df_dr_line.index[-2]
dot6_k_n=df_dr_line.index[-1] #最后一个线段dot
dot1_high=df_dr_line.at[dot1_k_n,'high']
dot2_high=df_dr_line.at[dot2_k_n,'high']
dot3_high=df_dr_line.at[dot3_k_n,'high']
dot4_high=df_dr_line.at[dot4_k_n,'high']
dot5_high=df_dr_line.at[dot5_k_n,'high']
dot6_high=df_dr_line.at[dot6_k_n,'high']
dot1_low=df_dr_line.at[dot1_k_n,'low']
dot2_low=df_dr_line.at[dot2_k_n,'low']
dot3_low=df_dr_line.at[dot3_k_n,'low']
dot4_low=df_dr_line.at[dot4_k_n,'low']
dot5_low=df_dr_line.at[dot5_k_n,'low']
dot6_low=df_dr_line.at[dot6_k_n,'low']
if(dot1_k_n>=zs_end_n_pre):
#往前找三个点,如果不在中枢里,则判断是否是中枢的头部
start_direction='down' if df_dr_line.at[dot1_k_n,'line']=='top' else 'up' #前导点是top则是下降方向
if start_direction=='down':
if min(dot3_high,dot5_high)>max(dot2_low,dot4_low) and dot1_high>max(dot3_high,dot5_high) and dot6_high<min(dot3_high,dot5_high):
zs_first_n_pre=dot2_k_n #dot1是前导点这个点是中枢第一个点
zg=min(dot3_high,dot5_high)
zd=max(dot2_low,dot4_low)
gg=max(dot3_high,dot5_high)
dd=min(dot2_low,dot4_low)
zs_no+=1
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=dot5_k_n) & (df_dr.line!=''),'zs_no']=zs_no
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=dot5_k_n) & (df_dr.line!=''),'zg']=zg
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=dot5_k_n) & (df_dr.line!=''),'zd']=zd
#print('新中枢确立',zd,zg,zs_first_n_pre)
elif start_direction=='up':
if min(dot2_high,dot4_high)>max(dot3_low,dot5_low) and dot1_low<min(dot3_low,dot5_low) and dot6_low>max(dot3_low,dot5_low):
zs_first_n_pre=dot2_k_n #dot1是前导点这个点是中枢第一个点
zg=min(dot2_high,dot4_high)
zd=max(dot3_low,dot5_low)
gg=max(dot2_high,dot4_high)
dd=min(dot3_low,dot5_low)
zs_no+=1
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=dot5_k_n) & (df_dr.line!=''),'zs_no']=zs_no
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=dot5_k_n) & (df_dr.line!=''),'zg']=zg
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=dot5_k_n) & (df_dr.line!=''),'zd']=zd
#print('新中枢确立',zd,zg,zs_first_n_pre)
#在中枢里则看是否生长
elif len(df_dr_line)>5 and zs_first_n_pre>zs_end_n_pre:
dot_last_n=df_dr_line.index[-1] #最后一个线段dot
dot_last_type=df_dr_line.at[dot_last_n,'line']
dot_last_high=df_dr_line.at[dot_last_n,'high']
dot_last_low=df_dr_line.at[dot_last_n,'low']
dot_last3_n=df_dr_line.index[-3] #倒数第三个线段dot
dot_last3_type=df_dr_line.at[dot_last3_n,'line']
dot_last3_high=df_dr_line.at[dot_last3_n,'high']
dot_last3_low=df_dr_line.at[dot_last3_n,'low']
#刷新gg dd只算到倒数第三个确认中枢时正好为中枢结束点
if dot_last3_type=='top':
gg=max(gg,dot_last3_high)
elif dot_last3_type=='bottom':
dd=min(dd,dot_last3_low)
dot_last_trade_time=df_dr_line.at[dot_last_n,'trade_time']
dot_last_close=df_dr_line.at[dot_last_n,'close']
if (dot_last_type=='top' and dot_last_high<zd) or (dot_last_type=='bottom' and dot_last_low>zg):
#中枢完成(这时肯定已经两个点脱离,没有走回去),保存,退出,往后面继续找
# /\ -------------
# / or -------------
# ------------- \
# ------------- \/
#print('中枢完成',zg,zd,df_dr_line.index[-3])
is_confirm_zs=True
zs_end_n_pre=df_dr_line.index[-3] #中枢结束点是倒数第三个dot结束点不是后导点而开始点是前导点计算时前后衔接方便
end_direction='down' if dot_last_type=='top' else 'up' #小尾巴是top则是下降方向
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=zs_end_n_pre) & (df_dr.line!=''),'zs_no']=zs_no
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=zs_end_n_pre) & (df_dr.line!=''),'zg']=zg
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=zs_end_n_pre) & (df_dr.line!=''),'zd']=zd
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=zs_end_n_pre) & (df_dr.line!=''),'gg']=gg
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=zs_end_n_pre) & (df_dr.line!=''),'dd']=dd
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=zs_end_n_pre) & (df_dr.line!=''),'zs_direction']=start_direction+end_direction
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=zs_end_n_pre) & (df_dr.line!=''),'zs_confirm_time']=dot_last_trade_time
df_dr.at[(df_dr.index>=zs_first_n_pre) & (df_dr.index<=zs_end_n_pre) & (df_dr.line!=''),'zs_confirm_price']=dot_last_close
return df_dr

10710
code/getData.ipynb Normal file

File diff suppressed because one or more lines are too long