Files
quant/code/.ipynb_checkpoints/Untitled2-checkpoint.ipynb

111 lines
4.3 KiB
Plaintext
Raw Normal View History

2019-11-21 12:02:03 +08:00
{
"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
}