iT邦幫忙

2021 iThome 鐵人賽

DAY 15
0
永豐金融APIs

在賭場尋求財富是否搞錯了什麼系列 第 15

賭場線仙 - K棒與移動平均線的華爾滋

  • 分享至 

  • xImage
  •  

最近研究K棒,跟著某知名投顧分析師看盤後解析,「站上五日線買,跌破五日線賣,投信看十日」各種台詞朗朗上口,真的要真金白銀進去真的是被割韭菜

我們來研究一下一種策略

短均線向上穿越長均線是買點

長均線向下穿越短均線是賣點

今天邊看著K棒邊研究看看這個策略是否有搞頭,再看看怎麼調整這個策略

contracts = [api.Contracts.Futures.TXF['TXF202110']]

minute_close = pd.Series()
stock = 0
price = 0
reward = 0
sig = []

# 紀錄前5個close
for i in range(0,5):
  snapshots = api.snapshots(contracts)
  minute_close = minute_close.append(pd.Series(
      [snapshots[0].close], 
      index=[pd.to_datetime(snapshots[0].ts, unit='ns')]
  ))
  time.sleep(60)

# 開始跑
for i in range(0,300):
  # 抓snapshot
  snapshots = api.snapshots(contracts)
  
  # 存到分k收盤價的series
  minute_close = minute_close.append(pd.Series(
      [snapshots[0].close], 
      index=[pd.to_datetime(snapshots[0].ts, unit='ns')]
  ))
  
  # 計算ma
  ma5 = talib.SMA(minute_close, timeperiod=5)
  ma10 = talib.SMA(minute_close, timeperiod=10)

  ma_dif = ma5 - ma10

  now = datetime.now()
  current_time = now.strftime("%H:%M:%S")

  # 5MA往上穿越10MA
  if ma_dif[i-1] < 0 and ma_dif[i] > 0 and stock == 0:
    stock += 1
    price = snapshots[0].close
    sig.append(1)
    print("Current Time =", current_time, "buy :", price)
    
  # 5MA往下穿越10MA
  elif ma_dif[i-1] > 0 and ma_dif[i] < 0 and stock == 1:
    stock -= 1
    reward = price - snapshots[0].close
    sig.append(-1)
    print("Current Time =", current_time, "sell :", price, "reward :", reward)
  else:
    sig.append(0)

  time.sleep(60)

結果是

Current Time = 03:41:23 buy : 16870.0
Current Time = 03:57:27 sell : 16870.0 reward : -8.0
Current Time = 04:02:28 buy : 16883.0
Current Time = 04:05:28 sell : 16883.0 reward : 9.0
Current Time = 04:20:31 buy : 16871.0
Current Time = 04:20:31 buy : 16871.0
Current Time = 04:32:34 buy : 16905.0
Current Time = 04:58:39 sell : 16905.0 reward : -35.0
Current Time = 05:00:40 buy : 16936.0
Current Time = 05:01:40 sell : 16936.0 reward : -12.0
Current Time = 05:06:41 buy : 16942.0
Current Time = 05:13:43 sell : 16942.0 reward : -11.0
Current Time = 05:15:43 buy : 16953.0
Current Time = 05:26:46 sell : 16953.0 reward : 1.0
Current Time = 05:42:49 buy : 16920.0
Current Time = 05:46:50 sell : 16920.0 reward : 3.0

結果 -53 點,1點200台票的話,虧損 -10,600

其中還有手續費跟交易稅

看起來似乎可以,但要設定停損機制


上一篇
我跟你一百萬,再大你一百萬,再show hand - 跟莊家吃香喝辣 ?
下一篇
沒收工就罵髒話 - 適時停損停利
系列文
在賭場尋求財富是否搞錯了什麼22
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言