Board logo

標題: [發問] 如何使用巨集自動計算股價漲跌停價? [打印本頁]

作者: shuasa    時間: 2016-4-24 20:34     標題: 如何使用巨集自動計算股價漲跌停價?

本帖最後由 shuasa 於 2016-4-24 20:39 編輯

請問要如何用使巨集自動計算股價票漲跌停價?
作者: jackyq    時間: 2016-4-24 21:43

大大應該先舉例說一下
哪一日的值計算有錯誤
而正確的值應該又是多少
作者: shuasa    時間: 2016-4-24 22:09

回復 2# jackyq


    我原本是用儲存格用函數公式套用,如4/6日的漲停價是4/1日的收盤價套上公式
   M14儲存格的公式是 =IF(F15="","",FLOOR(F15*1.1,LOOKUP(F15*1.1,{0,10,50,100,500,1000},{0.01,0.05,0.1,0.5,1,5})))

漲停價公式
=FLOOR(A1*1.1,LOOKUP(A1*1.1,{0,10,50,100,500,1000},{0.01,0.05,0.1,0.5,1,5}))
跌停價公式
=CEILING(A1*0.9,LOOKUP(A1*0.9,{0,10,50,100,500,1000},{0.01,0.05,0.1,0.5,1,5}))

但是如何用巨集的方式呈現?
作者: jackyq    時間: 2016-4-24 22:38

大大我照你公式翻譯而已

Public Function 漲跌停價(昨收,  漲or跌 As Long)

漲跌停價 = ""
If 昨收 = "" Then Exit Function

   Price_Sample = Array(0, 10, 50, 100, 500, 1000)
interval_Sample = Array(0.01, 0.05, 0.1, 0.5, 1, 5)

interval = Application.WorksheetFunction.Lookup(昨收, Price_Sample, interval_Sample)

If 漲or跌 > 0 Then 漲跌停價 = Application.WorksheetFunction.Floor(昨收 * 1.1, interval)
If 漲or跌 < 0 Then 漲跌停價 = Application.WorksheetFunction.Ceiling(昨收 * 0.9, interval)

End Function
作者: shuasa    時間: 2016-4-25 01:40

回復 4# jackyq


    感謝jackyq大大指教,讓我又多學到知識:)
作者: c_c_lai    時間: 2016-4-25 08:24

回復 3# shuasa
應用 jackyq  大大的函式稍加修改如下,
參考一下:
  1. Public Function UpDown(ud As Range, Optional UpDn As Boolean = True)
  2.     If ud.Value = 0 Then UpDown = 0: Exit Function
  3.       
  4.     Price_Sample = Array(0, 10, 50, 100, 500, 1000)
  5.     interval_Sample = Array(0.01, 0.05, 0.1, 0.5, 1, 5)
  6.          
  7.     interval = Application.WorksheetFunction.Lookup(ud.Value * IIf(UpDn, 1.1, 0.9), Price_Sample, interval_Sample)
  8.     UpDown = Application.WorksheetFunction.Floor(ud.Value * IIf(UpDn, 1.1, 0.9), interval)
  9. End Function
複製代碼
漲停價函式
=UpDown(F8)
等於
=UpDown(F8,1)
等於
=UpDown(F8,True)

跌停價函式
=UpDown(F8,0)
等於
=UpDown(F8,False)
[attach]23964[/attach]
作者: c_c_lai    時間: 2016-4-25 08:26

回復 3# shuasa
亦可直接以 VBA 程式段來處理:
  1. Sub Ex()
  2.     Dim xVal As Double
  3.    
  4.     xVal = UpDown([F8])
  5.     MsgBox "UpDown([F8]) (漲停價) = " & xVal
  6.     xVal = UpDown([F8], 1)
  7.     MsgBox "UpDown([F8], 1) (漲停價) = " & xVal
  8.     xVal = UpDown([F8], True)
  9.     MsgBox "UpDown([F8], True) (漲停價) = " & xVal
  10.    
  11.     xVal = UpDown([F8], 0)
  12.     MsgBox "UpDown([F8], 0) (跌停價) = " & xVal
  13.     xVal = UpDown([F8], False)
  14.     MsgBox "UpDown([F8], False) (跌停價) = " & xVal
  15. End Sub
複製代碼
[attach]23965[/attach]
作者: jackyq    時間: 2016-4-25 09:07

本帖最後由 jackyq 於 2016-4-25 09:15 編輯

回復 5# shuasa

我有理解錯歐     

應該這樣才對

Public Function 漲跌停價(昨收,  漲or跌 As Long)

漲跌停價 = ""
If 昨收 = "" Then Exit Function
if 漲or跌 = 0 Then Exit Function

   Price_Sample = Array(0, 10, 50, 100, 500, 1000)
interval_Sample = Array(0.01, 0.05, 0.1, 0.5, 1, 5)

漲跌停價 = 昨收 * ( 1 + sgn( 漲or跌 )   )  * 0.1
interval = Application.WorksheetFunction.Lookup(漲跌停價, Price_Sample, interval_Sample)

If 漲or跌 > 0 Then 漲跌停價 = Application.WorksheetFunction.Floor(漲跌停價 , interval)
If 漲or跌 < 0 Then 漲跌停價 = Application.WorksheetFunction.Ceiling(漲跌停價 , interval)

End Function
作者: jackyq    時間: 2016-4-25 09:09

跌停一定要用 Ceiling 不能用 floor
作者: jackyq    時間: 2016-4-25 09:43

回復 8# jackyq

挖哩勒 !!!!  0.1 放錯位置

再更正一次

Public Function 漲跌停價(昨收,  漲or跌 As Long)

漲跌停價 = ""
If 昨收 = "" Then Exit Function
if 漲or跌 = 0 Then Exit Function

   Price_Sample = Array(0, 10, 50, 100, 500, 1000)
interval_Sample = Array(0.01, 0.05, 0.1, 0.5, 1, 5)

漲跌停價 = 昨收 * ( 1 + sgn( 漲or跌 )  * 0.1  )  
interval = Application.WorksheetFunction.Lookup(漲跌停價, Price_Sample, interval_Sample)

If 漲or跌 > 0 Then 漲跌停價 = Application.WorksheetFunction.Floor(漲跌停價 , interval)
If 漲or跌 < 0 Then 漲跌停價 = Application.WorksheetFunction.Ceiling(漲跌停價 , interval)

End Function
作者: c_c_lai    時間: 2016-4-25 11:46

回復 3# shuasa
沒留意到 Ceiling 的公式:
修正如下
  1. Public Function UpDown(ud As Range, Optional UpDn As Boolean = True)
  2.     If ud.Value = 0 Then UpDown = 0: Exit Function
  3.       
  4.     Price_Sample = Array(0, 10, 50, 100, 500, 1000)
  5.     interval_Sample = Array(0.01, 0.05, 0.1, 0.5, 1, 5)
  6.          
  7.     interval = Application.WorksheetFunction.Lookup(ud.Value * IIf(UpDn, 1.1, 0.9), Price_Sample, interval_Sample)
  8.    
  9.     If UpDn Then
  10.         UpDown = Application.WorksheetFunction.Floor(ud.Value * 1.1, interval)
  11.     Else
  12.         UpDown = Application.WorksheetFunction.Ceiling(ud.Value * 0.9, interval)
  13.     End If
  14. End Function
複製代碼

作者: shuasa    時間: 2016-4-25 18:34

回復 7# c_c_lai


     感謝 c_c_lai大大指教,感謝您們指點迷津




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)