forked from Rockyzsu/stock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulation.py
75 lines (60 loc) · 2.41 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*-coding=utf-8-*-
__author__ = 'Rocky'
'''
http://30daydo.com
Contact: [email protected]
'''
# 模拟买入 纯粹纪录。
import os, sys, chardet
import pandas as pd
import datetime
import numpy as np
import tushare as ts
from send_mail import sender_139
from setting import get_engine, get_mysql_conn, is_holiday, llogger, DATA_PATH
filename=os.path.basename(__file__)
logger = llogger('log/'+filename)
class Simulation():
def __init__(self):
# path=os.path.join(os.getcwd(),'data')
path = DATA_PATH
if os.path.exists(path) == False:
os.mkdir(path)
os.chdir(path)
self.name = 'simulation.xls'
self.df = pd.read_excel(self.name)
self.df['代码'] = self.df['代码'].map(lambda x: str(x).zfill(6))
self.engine = get_engine('db_stock')
self.engine1=get_engine('db_daily')
# self.base = pd.read_sql('tb_basic_info', self.engine, index_col='index')
self.money = 10000
self.today = datetime.datetime.now().strftime('%Y-%m-%d')
def caculation(self):
df_t = pd.read_sql(self.today,con=self.engine1)
# df_t = ts.get_today_all()
for i in self.df['代码'].values:
self.df.loc[self.df['代码'] == i, '当前日期'] = self.today
pchange = df_t.loc[df_t['code'] == i, 'changepercent'].values[0]
self.df.loc[self.df['代码'] == i, '今日涨幅'] = pchange
trade = df_t[df_t['code'] == i]['trade'].values[0]
self.df.loc[self.df['代码'] == i, '当前价格'] = trade
current_profit = (trade - self.df[self.df['代码'] == i]['买入价格'].values[0]) / \
self.df[self.df['代码'] == i]['买入价格'].values[0]
self.df.loc[self.df['代码'] == i, '目前盈亏'] = round(current_profit * 100, 2)
self.df.to_excel(self.name, encoding='utf-8',index=None)
self.df.to_sql('tb_simulation',self.engine,if_exists='replace')
# ali_engine = get_engine('',False)
# self.df.to_sql('tb_simulation',ali_engine,if_exists='replace')
del self.df['买入理由']
df_str = self.df.to_html()
# print(df_str)
sender_139('模拟盘 {}'.format(self.today),df_str,types='html')
def main():
obj = Simulation()
obj.caculation()
if __name__ == '__main__':
if is_holiday():
logger.info("Holidy")
exit()
logger.info("Start")
main()