import threading
import tushare as ts
import pandas as pd
import datetime

STOCK = {#'002594':[1,170.15],   ## 比亚迪 / 几手，成本价
         '601012':[11,99.9],    ## 隆基股份
         '002340':[12,8.72],    ## 格林美
         '603259':[1,141.7],    ## 药明康德
         '002346':[10,10.68],    ## 柘中股份
         #'600438':[9,42.96],    ## 通威股份
         #'002475':[3,59.51],    ## 立讯精密
         #'603308':[1,33.49],     ## 应流股份
         #'002415': [3, 66.40],   ## 海康威视
         # '600559':[3,35.3],     ## 老白干
         # '601100':[1, 114.5],  ## 恒立液压
         # '603466':[6, 22.40]    ## 风语筑
}
TimerNum = 20.0 # s
Total = 0
# #rodo
def get_all_price():
    '''process all stock'''
    stockCode = list(STOCK.keys())
    df = ts.get_realtime_quotes(stockCode)

    lp = list(STOCK.values())

    stockNum = []
    stockCostPrice = []
    for i in range(len(lp)):
        stockNum.append(lp[i][0])
        stockCostPrice.append(lp[i][1])

    df['num'] = stockNum
    df['stockCostPrice'] = stockCostPrice
    # 处理
    # profit and lost ratio 盈亏率
    plRatio = round((df['price'].astype(float) / df['stockCostPrice'] - 1)*100,2)

    # profit and lost 盈亏
    df['plRatio'] = plRatio
    df['stockNum'] = stockNum
    pl = round(df['plRatio'].astype(float) * df['stockNum'] * df['stockCostPrice'].astype(float),2)
    df['pl'] = pl

    # 当日涨幅 Rise and fall
    currentRF = round((df['price'].astype(float) / df['pre_close'].astype(float) - 1)*100,2)
    df['currentRF'] = currentRF

    df1 = df[[ 'open', 'price', 'stockCostPrice', 'plRatio', 'num','pl', 'currentRF','name']]
    pd.set_option('display.unicode.ambiguous_as_wide', True)
    pd.set_option('display.unicode.east_asian_width', True)
    pd.set_option('display.width', 180)  # 设置打印宽度(**重要**)
    pd.set_option('display.max_columns', 1000)
    pd.set_option('display.width', 1000)
    pd.set_option('display.max_colwidth', 1000)
    sss = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f)")[:-4]

    print('\n')
    print("----------------" + sss +"------------------")

    print(df1)
    sum_int = round(df['pl'].sum(),2)

    print("total profit and lost is " + sum_int.astype(str))
    print('\n')

    # df.to_csv('stock_data.csv', encoding='utf_8_sig', index=None)
    global timer
    timer = threading.Timer(TimerNum, get_all_price, [])
    timer.start()


if __name__ == '__main__':
    print(STOCK)
    get_all_price()
    timer = threading.Timer(TimerNum, get_all_price, [])
    timer.start()

[((25, 9, 25, 42), 'tushare.get_realtime_quotes', 'ts.get_realtime_quotes', ({(25, 32, 25, 41): 'stockCode'}, {}), '(stockCode)', True, 'import tushare as ts\n'), ((52, 4, 52, 60), 'pandas.set_option', 'pd.set_option', ({(52, 18, 52, 53): '"""display.unicode.ambiguous_as_wide"""', (52, 55, 52, 59): '(True)'}, {}), "('display.unicode.ambiguous_as_wide', True)", True, 'import pandas as pd\n'), ((53, 4, 53, 59), 'pandas.set_option', 'pd.set_option', ({(53, 18, 53, 52): '"""display.unicode.east_asian_width"""', (53, 54, 53, 58): '(True)'}, {}), "('display.unicode.east_asian_width', True)", True, 'import pandas as pd\n'), ((54, 4, 54, 39), 'pandas.set_option', 'pd.set_option', ({(54, 18, 54, 33): '"""display.width"""', (54, 35, 54, 38): '(180)'}, {}), "('display.width', 180)", True, 'import pandas as pd\n'), ((55, 4, 55, 46), 'pandas.set_option', 'pd.set_option', ({(55, 18, 55, 39): '"""display.max_columns"""', (55, 41, 55, 45): '(1000)'}, {}), "('display.max_columns', 1000)", True, 'import pandas as pd\n'), ((56, 4, 56, 40), 'pandas.set_option', 'pd.set_option', ({(56, 18, 56, 33): '"""display.width"""', (56, 35, 56, 39): '(1000)'}, {}), "('display.width', 1000)", True, 'import pandas as pd\n'), ((57, 4, 57, 47), 'pandas.set_option', 'pd.set_option', ({(57, 18, 57, 40): '"""display.max_colwidth"""', (57, 42, 57, 46): '(1000)'}, {}), "('display.max_colwidth', 1000)", True, 'import pandas as pd\n'), ((71, 12, 71, 56), 'threading.Timer', 'threading.Timer', ({(71, 28, 71, 36): 'TimerNum', (71, 38, 71, 51): 'get_all_price', (71, 53, 71, 55): '[]'}, {}), '(TimerNum, get_all_price, [])', False, 'import threading\n'), ((78, 12, 78, 56), 'threading.Timer', 'threading.Timer', ({(78, 28, 78, 36): 'TimerNum', (78, 38, 78, 51): 'get_all_price', (78, 53, 78, 55): '[]'}, {}), '(TimerNum, get_all_price, [])', False, 'import threading\n'), ((58, 10, 58, 33), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n')]