# -*- coding: UTF-8 -*- # 載入相關套件 import haohaninfo, indicator, datetime, time, sys # 選擇報價平台 Broker = sys.argv[1] # 定義商品名稱 Prod = sys.argv[2] # K棒物件 Today = time.strftime('%Y%m%d') KBar = indicator.KBar(Today, 1) # 訂閱報價物件 GO = haohaninfo.GOrder.GOQuote() # 訂閱報價 for row in GO.Subscribe(Broker, 'match', Prod): # 定義時間 Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f') # 定義成交價、成交量 Price = int(row[2]) Qty = int(row[3]) # 將成交欄位填入 KBar.AddPrice(Time, Price, Qty) # 取得BBANDS print(KBar.GetTime(), KBar.GetClose(), KBar.GetBBands(20))
# -*- coding: UTF-8 -*- # 載入相關套件 import sys, indicator, datetime, haohaninfo # 券商 Broker = 'Masterlink_Future' # 定義資料類別 Table = 'match' # 定義商品名稱 Prod = sys.argv[1] # 取得當天日期 Date = datetime.datetime.now().strftime("%Y%m%d") # K棒物件 KBar = indicator.KBar(Date, 'time', 1) # 定義趨勢判斷的時間 StartTime = datetime.datetime.strptime(Date + '09:00:00', '%Y%m%d%H:%M:%S') # 進場前判斷 GO = haohaninfo.GOrder.GOQuote() for i in GO.Describe(Broker, Table, Prod): time = datetime.datetime.strptime(i[0], '%Y/%m/%d %H:%M:%S.%f') price = float(i[2]) qty = int(i[3]) tag = KBar.TimeAdd(time, price, qty) # 如果時間到達指定的時間 if time > StartTime: # 則取出當前最高以及最低 High = max(KBar.GetHigh()) Low = min(KBar.GetLow()) Spread = High - Low
import numpy as np # 取得必要參數 券商代號 商品名稱 Broker = sys.argv[1] Prod = sys.argv[2] KMinute= int(sys.argv[3]) ShortRSIPeriod= int(sys.argv[4]) LongRSIPeriod= int(sys.argv[5]) StopLoss= int(sys.argv[6]) # 部位管理物件 RC=order.Record() # K棒物件 Today = time.strftime('%Y%m%d') KBar = indicator.KBar(Today,KMinute) # 訂閱報價物件 GO = haohaninfo.GOrder.GOQuote() # 訂閱報價 for row in GO.Subscribe(Broker, 'match', Prod): # 定義時間 Time = datetime.datetime.strptime(row[0],'%Y/%m/%d %H:%M:%S.%f') # 定義成交價、成交量 Price=float(row[2]) Qty=int(row[3]) # 更新K棒 若新增K棒則判斷開始判斷 策略 if KBar.AddPrice(Time,Price,Qty) == 1: CloseList=KBar.GetClose() # 如果 長RSI 計算出值 則開始判斷進出場 if len(CloseList) >= LongRSIPeriod+2:
# -*- coding: UTF-8 -*- # 載入相關套件 import datetime, function, indicator import sys # 取得當天日期 Date = datetime.datetime.now().strftime("%Y%m%d") # 定義股票代碼 Sid = sys.argv[1] # 一分鐘K棒的物件 KBar1M = indicator.KBar(date=Date) # 假設只有多單進場,本進場條件毫無意義,僅供測試 for i in function.getSIDMatch(Date, Sid): time = datetime.datetime.strptime(Date + i[0], '%Y%m%d%H:%M:%S.%f') price = float(i[2]) Index = 1 OrderTime = time OrderPrice = price print(OrderTime, "Order Buy Price:", OrderPrice, "Success!") break # 停損停利比率 StopLossRatio = 0.02 TakeProfitRatio = 0.03 # 出場判斷 if Index == 1: # 多單出場判斷 StopLoss = OrderPrice * (1 - StopLossRatio) TakeProfit = OrderPrice * (1 + TakeProfitRatio)
# -*- coding: UTF-8 -*- # 載入相關套件 import sys,indicator,datetime,haohaninfo # 券商 Broker = 'Masterlink_Future' # 定義資料類別 Table = 'match' # 定義商品名稱 Prod = sys.argv[1] # 取得當天日期 Date = datetime.datetime.now().strftime("%Y%m%d") # K棒物件 KBar = indicator.KBar(Date,'volume',100) # 計算成交量K棒 GO = haohaninfo.GOrder.GOQuote() for i in GO.Describe(Broker, Table, Prod): price = int(i[2]) amount = int(i[4]) KBar.VolumeAdd(price,amount) print(KBar.GetOpen(),KBar.GetHigh(),KBar.GetLow(),KBar.GetClose())