股票 出仓点 及 补仓点预测 python代码

使用python的tkinter自制gui界面

 

代码如下:

import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

# 用于plt显示正常的中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 用来正常显示负号
plt.rcParams['axes.unicode_minus'] = False


def stock_predict_increase(buy_price, buy_num, new_price, new_num):
    """
    补仓点
    :param buy_price: 买入价格
    :param buy_num: 买入股数
    :param new_price: 当前价格
    :param new_num: 当前买入股数
    :return:
    now_price  总股本买入价格
    now_num  总股本数
    """
    now_num = buy_num + new_num
    old_cost = buy_price * buy_num
    new_cost = new_price * new_num
    cost = old_cost + new_cost

    now_price = cost / now_num
    return now_price, now_num


def stock_predict_decrease(buy_price, buy_num, new_price, new_num):
    """
    出仓点
    :param buy_price: 买入价格
    :param buy_num: 买入股数
    :param new_price: 当前价格
    :param new_num: 当前买出股数
    :return:
    now_price  总股本买入价格
    now_num  总股本数
    """
    now_num = buy_num - new_num
    old_cost = buy_price * buy_num
    new_gain = new_price * new_num
    cost = old_cost - new_gain

    if now_num <= 0:
        now_price = 0
    else:
        now_price = cost / now_num
    return now_price, now_num


def stock_trend_point(buy_price, buy_num, new_price, flag):
    now_price_list = []
    new_num_list = list(range(0, 1000, 100))
    for new_num in range(0, 1000, 100):
        if flag:
            now_price, now_num = stock_predict_increase(buy_price, buy_num, new_price, new_num)
        else:
            now_price, now_num = stock_predict_decrease(buy_price, buy_num, new_price, new_num)
        now_price_list.append(now_price)
    return new_num_list, now_price_list


def _quit():
    """点击退出按钮时调用这个函数"""
    global window
    window.quit()  # 结束主循环
    window.destroy()  # 销毁窗口


def stock_gui(buy_price, buy_num, new_price, flag):
    new_num_list, now_price_list = stock_trend_point(buy_price, buy_num, new_price, flag)

    figure.clf()
    a = figure.add_subplot(111)  # 添加子图:1行1列第1个
    a.plot(new_num_list, now_price_list)
    if flag:
        a.set_xlabel("买入数量变化")
    else:
        a.set_xlabel("卖出数量变化")
    a.set_ylabel("本仓总体价格变化")
    a.set_title('股票价格随买入数量的变化关系')

    list_1 = Listbox(window)  # 创建两个列表组件
    list_2 = Listbox(window)
    for item in new_num_list:  # 第一个小部件插入数据
        list_1.insert(0, item)

    for item in now_price_list:  # 第二个小部件插入数据
        list_2.insert(0, item)

    # list_1.pack()  # 将小部件放置到主窗口中
    # list_2.pack()

    # 将绘制的图形显示到tkinter,并创建属于window的canvas画布,将图f置于画布上
    canvas = FigureCanvasTkAgg(figure, master=window)
    canvas.draw()
    canvas.get_tk_widget().place(x=100, y=100, anchor='nw')


window = tk.Tk()
window.title('stock predict')
window.geometry('900x600')

figure = plt.figure(figsize=(5, 4), dpi=100)

stock_num = 1000
stock_price = 30
stock_new_price = 30

var_string_1 = tk.StringVar()
var_string_1.set('当前股票价格:' + str(stock_price))
var_string_2 = tk.StringVar()
var_string_2.set('当前股票股数:' + str(stock_num))
var_string_3 = tk.StringVar()
var_string_3.set('新股票价格:' + str(stock_new_price))
var_string_4 = tk.StringVar()


def price_control(flag):
    global stock_price
    if flag:
        stock_price = stock_price + 1
        var_string_1.set('当前股票价格:' + str(stock_price))
    else:
        stock_price = stock_price - 1
        var_string_1.set('当前股票价格:' + str(stock_price))


def number_control(flag):
    global stock_num
    if flag:
        stock_num = stock_num + 100
        var_string_2.set('当前股票股数:' + str(stock_num))
    else:
        stock_num = stock_num - 100
        var_string_2.set('当前股票股数:' + str(stock_num))


def new_price_control(flag):
    global stock_new_price
    if flag:
        stock_new_price = stock_new_price + 1
        var_string_3.set('新股票价格:' + str(stock_new_price))
    else:
        stock_new_price = stock_new_price - 1
        var_string_3.set('新股票价格:' + str(stock_new_price))


label1 = tk.Label(window, text='股票预测信息', anchor='nw')
label1.place(x=370, y=0, anchor='nw')

button1 = tk.Button(window, text='买入预测 ', command=lambda: stock_gui(stock_price, stock_num, stock_new_price, True))
button1.place(x=100, y=50, anchor='nw')

button2 = tk.Button(window, text='卖出预测 ', command=lambda: stock_gui(stock_price, stock_num, stock_new_price, False))
button2.place(x=200, y=50, anchor='nw')

button3 = tk.Button(window, text='当前价格增', command=lambda: price_control(True))
button3.place(x=300, y=50, anchor='nw')

button4 = tk.Button(window, text='当前价格减', command=lambda: price_control(False))
button4.place(x=400, y=50, anchor='nw')

button5 = tk.Button(window, text='当前股数增', command=lambda: number_control(True))
button5.place(x=500, y=50, anchor='nw')

button6 = tk.Button(window, text='当前股数减', command=lambda: number_control(False))
button6.place(x=600, y=50, anchor='nw')

button7 = tk.Button(window, text='新股价格增', command=lambda: new_price_control(True))
button7.place(x=700, y=50, anchor='nw')

button8 = tk.Button(window, text='新股价格减', command=lambda: new_price_control(False))
button8.place(x=800, y=50, anchor='nw')

# 创建一个按钮,并把上面那个函数绑定过来
button10 = tk.Button(master=window, text="退出", command=_quit)
# 按钮放在下边
button10.place(x=460, y=550, anchor='nw')

# text1 = tk.Text(window)
# text1.insert()

label2 = tk.Label(window, textvariable=var_string_1, anchor='nw')
label2.place(x=700, y=250, anchor='nw')

label3 = tk.Label(window, textvariable=var_string_2, anchor='nw')
label3.place(x=700, y=300, anchor='nw')

label4 = tk.Label(window, textvariable=var_string_3, anchor='nw')
label4.place(x=700, y=350, anchor='nw')

window.mainloop()  # 进入消息循环

# if __name__ == "__main__":

 

posted @ 2022-04-29 16:38  wangssd  阅读(181)  评论(0编辑  收藏  举报