總網頁瀏覽量

顯示具有 我的Python程式 標籤的文章。 顯示所有文章
顯示具有 我的Python程式 標籤的文章。 顯示所有文章

2023年10月29日 星期日

終於寫出我想要的點擊滑鼠在點擊處顯示點擊的座標(2023.10.29日)

from tkinter import *

r=Tk()

canvas=Canvas(r,width=1200,height=600)

canvas.pack()

def mouse(event):

    x=event.x

    y=event.y

    canvas.create_text(x,y-15,text="( {},{} )".format(x , y))

    canvas.create_oval(x-2,y-2,x+3,y+3,width=4) # 圓形

    canvas.pack()

r.bind("<Button-1>",mouse)

r.mainloop() 



2023年10月25日 星期三

(Python)點擊滑鼠左鍵印出座標(2023.10.25三)

from tkinter import *

r=Tk()

r.geometry("300x300")

def mouse(event):

    x=event.x

    y=event.y

    textvar=(x,y)

    var.set(textvar)

var=StringVar()

lab=Label(r,textvari=var)

lab.pack()

r.bind("<Button-1>",mouse)

r.mainloop() 

😃 著實花了我不少時間



2023年9月4日 星期一

Python點擊滑鼠座標(2023.9.4一)

 #如何讓滑鼠點到的地方顯示座標,隨點隨顯示?

import tkinter as tk

root = tk.Tk()

root.title('my window')

def mouse(event):#印出滑鼠座標

    t.insert('insert',(event.x,event.y))

    t.insert('insert',"\n")

root.bind("<Button-1>",mouse)

t = tk.Text(root,font=('Arial', 12),height=30,width=30)

t.pack()

root.mainloop()


2021年4月26日 星期一

找出K、RSI、威廉指標(W%R)、乖離率(BIAS)

 









這是我目前持有張數最多的個股

今天將3037剩餘的3張賣,換成這支

這1個多禮拜的收獲還不錯

不枉我賣掉持有1年的台GG做價差



2021年4月8日 星期四

Python寫的K值及RSI值再進化(2021.04.08)

 































K值: 16.353756273601547

RSI值: 27.0

這是大立光今天(2021.04.08)的K及RSI值

根據圖及值   昨天應該要買進的

昨天的K值是7點多   已是近期最低

昨天買進的話  今天就賺了

可見我寫的這個程式判斷買賣點應該還是有用的

2021年4月1日 星期四

K線圖Python程式(2021.04.01)

 
















由於目前網路上看到KD線圖都是9日的

我是深感日期有點久  想看5日線的K線  會不會比較準

於是自己寫一個日期可以自己決定的K線圖

利用這個程式  低於20買入   高於80賣出

來測試自己寫的程式會不會獲利

上圖是2330(台積電)5日的K線圖

下圖是它每日的成交量

開始日期是2021.01.01

結束日期是2021.04.01

接下來的這張是2317鴻海

上圖是18日的K線圖

下圖是每日的成交量

開始日期是202.08.01

結束日期是2021.04.01















2020年6月5日 星期五

利用函數庫的指令找標準差、相關係數與畫迴歸直線


@簡單幾個指令就好,可用於陽春的標準差、相關係數與迴歸直線編碼完後講解

import numpy as np
import matplotlib.pyplot as plt
print("請輸入X值,以空格隔開:")
x = [int(a) for a in input().split()]
print("請輸入Y值,以空格隔開:")
y = [int(b) for b in input().split()]
#找標準差
print("X的標準差:",np.std(x))
print("Y的標準差:",np.std(y))
#找相關係數,會形成2x2的矩陣,對角線元素即相關係數
print("X,Y之相關係數:",np.corrcoef(x,y)[0][1])
#找迴歸直線方程式,m為斜率,c為y截距
A = np.vstack([x, np.ones(len(x))]).T
m, c = np.linalg.lstsq(A, y, rcond=None)[0]
print("Y對X之迴歸直線方程式:","y=",m,"x+",c)
#畫出XY之對應點
plt.scatter(x,y,label="$Data$",color="blue",linewidth=2)
#取斜率m及y截距之近似值,否則m是一個超小的浮點數,不能畫
m=round(m,5)
c=round(c,5)
#欲畫出迴歸直線,取x最小的值再減1,取x最大的值再加1
MM=max(x)+1
mm=min(x)-1
#直線找二點即可
xx = np.linspace(mm,MM,2) 
#畫迴歸直線,r為紅色線
plt.plot(xx,m*xx+c,'r',label="$Regression-Line$",color="red",linewidth=2)
plt.legend()
plt.show()






















最陽春的標準差、相關係數、迴歸直線方程式


@完全沒用到函數庫,可以用來選修課教學生,可幫學生了解公式:

print("請輸入X值,以空格隔開:")
x = [int(a) for a in input().split()]
print("請輸入Y值,以空格隔開:")
y = [int(b) for b in input().split()]

def Sum(x): #陣列各項數字和
    s=0
    for i in range(len(x)):
        s+=x[i]
    return s

def Average(x):#陣列平均
    return Sum(x)/len(x)

def Variance(x):#陣列變異數
    var=0
    for i in range(len(x)):#
        var+=((x[i]-Average(x))**2)/len(x)
    return var

def Std(x):#陣列標準差
   return Variance(x)**0.5

def Cov(x,y):#二陣列的共變異數
    s=0
    for i in range(len(x)):
        s+=(x[i]-Average(x))*(y[i]-Average(y))
    return s/len(x)

def Corr(x,y):#二陣列的相關係數
    return Cov(x,y)/Std(x)/Std(y)

def Slope(x,y):#迴歸直線的斜率
    return Corr(x,y)*Std(y)/Std(x)

#迴歸直線方程式
print("y=",Slope(x,y),"x",Average(y)-Slope(x,y)*Average(x))



2020年2月5日 星期三

潮州高中排監考程式(完成度65%),卡住了

import xlwt
import random
c=int(input("輸入國文科總人數:"))
cc=input("輸入國文科老師的監考節數:").split()
cn=[int(x) for x in cc]#每個國文老師的監考節數

e=int(input("輸入英文科總人數:"))
ee=input("輸入英文科老師的監考節數:").split()
en=[int(x) for x in ee]#每個英文老師的監考節數

m=int(input("輸入數學科總人數:"))
mm=input("輸入數學科老師的監考節數:").split()
mn=[int(x) for x in mm]#每個數學老師的監考節數

def t_sum(a):#把每一科老師監考時數加起來
    s=0
    for i in range(len(a)):
        s+=a[i]
    return s

teacher_sum=t_sum(cn)+t_sum(en)+t_sum(mn)#所有科的老師

person=[[0]*10 for i in range(c+e+m)]#每個老師的數值

exam=input("輸入第一到第十節的總節數(10個數字,以空格隔開):").split()
examth=[int(x) for x in exam]

workbook = xlwt.Workbook(encoding='utf-8')       #新建工作簿
sheet1 = workbook.add_sheet("月考")          #新建sheet
sheet1.write(0,0,"姓名")     
sheet1.write(0,1,"編號")
sheet1.write(0,2,"監考時數")
sheet1.write(0,3,"第一節")     
sheet1.write(0,4,"第二節")     
sheet1.write(0,5,"第三節")
sheet1.write(0,6,"第四節")
sheet1.write(0,7,"第五節")
sheet1.write(0,8,"第六節")
sheet1.write(0,9,"第七節")
sheet1.write(0,10,"第八節")
sheet1.write(0,11,"第九節")
sheet1.write(0,12,"第十節")
sheet1.write(0,13,"個人總計")

ten1=[0,1,2,3,4]#欲填入的1的各節為第一天5節
ten2=[5,6,7,8,9]#欲填入的1的各節為第二天5節 
ten12=[0,1,2,3,4,5,6,7,8,9]#節數超過5節,二天任意排
chinesesum=0
englishsum=0
mathsum=0

for z in range(100):
    for i in range(c):
        ss=0
        sheet1.write(i+1,0,"國文")     
        sheet1.write(i+1,1,i+1) #填入編號
        sheet1.write(i+1,2,cn[i])#填入監考節數
   
        if cn[i]<=5 and cn[i]>=1: #監考時數小於5節,排在同一天
            rand=random.randint(1,cn[i])#實際排的節數需小於應排節數
            s1=random.sample(ten1,rand)#第一天的隨機串列
            s2=random.sample(ten2,rand)#第二天的隨機串列
            s=[s1,s2]
            com=random.choice(s)#二天的隨機串列再隨機選
            for j in range(rand):
                person[i][com[j]]=1
        else:
            s4=random.sample(ten12,cn[i])#監考時數超過5節,需2天都排
            for j in range(cn[i]):
                person[i][s4[j]]=1
        for j in range(3,13):#寫入EXCL
            ss+=person[i][j-3]#總計個人實際監考節數
            sheet1.write(i+1,j,person[i][j-3])
        sheet1.write(i+1,13,ss)
        chinesesum+=ss #國文科總和   
       
    for i in range(e):
        ss=0
        sheet1.write(c+i+1,0,"英文")     
        sheet1.write(c+i+1,1,i+1)#填入編號
        sheet1.write(c+i+1,2,en[i])#填入監考節數
   
        if en[i]<=5 and en[i]>=1:#監考時數小於5節,排在同一天
            rand=random.randint(1,en[i])#實際排的節數需小於應排節數
            s1=random.sample(ten1,rand)#第一天的隨機串列
            s2=random.sample(ten2,rand)#第二天的隨機串列
            s=[s1,s2]
            com=random.choice(s)#二天的隨機串列再隨機選
            for j in range(rand):
                person[c+i][com[j]]=1
        else:
            s4=random.sample(ten12,en[i])#監考時數超過5節,需2天都排
            for j in range(en[i]):
                person[c+i][s4[j]]=1
        for j in range(3,13):#寫入EXCEL
            ss+=person[c+i][j-3]#總計個人實際監考節數
            sheet1.write(c+i+1,j,person[c+i][j-3])
        sheet1.write(c+i+1,13,ss)
        englishsum+=ss#英文科總和
   
    for i in range(m):
        ss=0
        sheet1.write(c+e+i+1,0,"數學")     
        sheet1.write(c+e+i+1,1,i+1) #填入編號
        sheet1.write(c+e+i+1,2,mn[i])#填入監考節數
   
        if mn[i]<=5 and mn[i]>=1:#監考時數小於5節,排在同一天
            rand=random.randint(1,mn[i])#實際排的節數需小於應排節數
            s1=random.sample(ten1,rand)#第一天的隨機串列
            s2=random.sample(ten2,rand)#第二天的隨機串列
            s=[s1,s2]
            com=random.choice(s)#二天的隨機串列再隨機選
            for j in range(rand):
                person[c+e+i][com[j]]=1
        else:
            s4=random.sample(ten12,mn[i])#監考時數超過5節,需2天都排
            for j in range(mn[i]):
                person[c+e+i][s4[j]]=1
        for j in range(3,13):#寫入EXCEL
            ss+=person[c+e+i][j-3]#總計個人實際監考節數
            sheet1.write(c+e+i+1,j,person[c+e+i][j-3])
        sheet1.write(c+e+i+1,13,ss)
        mathsum+=ss#數學科總和

    total=chinesesum+englishsum+mathsum#每人的實際監考總和   
    sheet1.write(c+e+m+1,0,"目前總計")
    sheet1.write(c+e+m+1,13,total) #所有人實際監考總節數   

    true1to10=[] #總計目前第一到第十節的總節數
    for j in range(10):
        s=0
        for i in range(c+e+m):
            s+=person[i][j]
        true1to10.append(s)
   
    for j in range(3,13):
        sheet1.write(c+e+m+1,j,true1to10[j-3])

    sheet1.write(c+e+m+2,0,"實際要監考節數")
    for j in range(3,13):#寫入EXCEL
        sheet1.write(c+e+m+2,j,examth[j-3])
   
    sheet1.write(c+e+m+1,2,teacher_sum) #目前老師監考時數和 
    sheet1.write(c+e+m+2,13,sum(examth))
    workbook.save(r'D:\監考表.xls')   #儲存excel檔
    if true1to10==examth:
        break

2020年1月15日 星期三

Normal 與 Lognormal pdf 圖形


import numpy as np
import matplotlib.pyplot as plt

mu=float(input("請輸入算術平均數:"))
sigma=float(input("請輸入標準差:"))
x=np.linspace(-3,3,1000)
y=(np.exp(-((np.log(x)-mu)**2.)/2./sigma/sigma))/x/sigma/((2.*np.pi)**0.5)
yy=(np.exp(-((x-mu)**2.)/2./sigma/sigma))/sigma/((2.*np.pi)**0.5)

#繪圖
fig=plt.figure()
ax=fig.add_subplot(111)
#ax.scatter(x,yn,color='yellow')
plt.title('Normal and Lognormal pdf Graph', fontsize=18)
ax.plot(x,y,'blue')
plt.plot(x,y,label="Lognormal",color="blue",linewidth=2)
ax.plot(x,yy,'red')
plt.plot(x,yy,label="Normal",color="red",linewidth=2)
plt.legend()
plt.show()


                 mu=0  and sigma=1























2020年1月14日 星期二

python安裝套件twstock


1、在Anaconda3(64-bit)點選 Anaconda Prompt(Anaconda3)


2、輸入pip install twstock


3、讀取台積電近5天的收盤價

import twstock
stock = twstock.Stock('2330')


stock.price[-5:]   # 近五日之收盤價





2019年12月26日 星期四

好用的 Lagrange 插值多項式模組


from scipy.interpolate import lagrange

x=[99,101,102,103]

y=[2008,2012,2005,2016]

a=lagrange(x,y)

print(a)


輸出:
     3          2
3 x - 909 x + 9.18e+04 x - 3.088e+06

超厲害的.........
連次方都幫你算好了!!!!

如果像上面用科學記號表示的
再加上指令simplify(a)
就可以得到確切的數字

poly1d([3.00000000000011, -909.000000000000, 91799.0000000037,-3087881.00000000], dtype=object)










2019年12月18日 星期三

方程式的解(轉成小數估計值)


指令: .evalf()



















⇒ 解的型式為串列,所以要找出個別的解,需以串列來表示。




2019年10月24日 星期四

列出集合的所有子集合


print("請輸入一個集合,將列所有的子集合:")
s=input().split()
sk=[x for x in s]
n=len(sk)
for i in range(2**n):
    subset=[]
    for j in range(n):
        if (i>>j)%2:
            subset.append(sk[j])
    print(subset)




2019年7月25日 星期四

手寫畫板


from tkinter import *
class plant:
    def __init__(self):
     
        window = Tk()
        window.title("手寫畫板")

        self.canvas = Canvas(window,width =600, height = 500,bg = "White")
     
        frame = Frame(window)
         
        btClear=Button(frame, text=' Clear All ',font=('Arial', 15),borderwidth=10
                       ,bg='light blue', command=self.clear).pack()

        window.columnconfigure(0, weight=1)
        window.rowconfigure(0, weight=1)

 
        self.canvas.grid(column=0, row=0, sticky='nwes')
        self.canvas.bind('<Button-1>', self.xy)
        self.canvas.bind('<B1-Motion>', self.line)
        self.canvas.pack()
        frame.pack()
        window.mainloop()
     
    lasty, lastx = 0, 0
 
    def clear(self):
        self.canvas.delete('li')
     
    def xy(self,event):
        global lasty, lastx
        lastx = event.x
        lasty = event.y
     
    def line(self,event):
        global lasty, lastx
   
        self.canvas.create_line(lastx,lasty,event.x,event.y,
                               fill="red",width=3,tags='li')
        lasty = event.y
        lastx = event.x
                           
plant()


執行檔下載










2019年7月16日 星期二

海龍公式視窗版 Ⅱ


import tkinter as tk

window = tk.Tk()
window.title('給三角形三邊長求面積')
window.geometry('600x400')

tk.Label(window,bg='pink',text='@ 請輸入三角形三邊長 @',font=('Arial',17),
         width=30,height=2).pack()
tk.Label(window,bg='cyan',text='# 第 一 邊 長 #',font=('Arial', 15)).pack()
first=tk.Entry(window,font=('Arial', 13))
first.pack()

tk.Label(window,bg='yellow',text='# 第 二 邊 長 #',font=('Arial', 15)).pack()
second=tk.Entry(window,font=('Arial', 13))
second.pack()

tk.Label(window,bg='light green',text='# 第 三 邊 長 #',font=('Arial', 15)).pack()
third=tk.Entry(window,font=('Arial', 13))
third.pack()

def heron():
    a=float(first.get())
    b=float(second.get())
    c=float(third.get()) 
    s=0.5*(a+b+c)
    area=(s*(s-a)*(s-b)*(s-c))**(0.5)
    var='三角形不存在!!'
    if area<=0:
        t.insert('insert',var)
    else:
        t.insert('insert', area)
 
tk.Button(window,font=('Arial', 15),borderwidth=10,bg='light blue',
          text="==> 按我計算三角形面積 <==",command=heron).pack()

t = tk.Text(window,bg='gold',font=('Arial', 13),
            height=3,width=30)
t.pack()
window.mainloop()













😕😕😕 
放心,它不是病毒,可以執行,小弟能力還不夠強大寫病毒 !!