Beispiel #1
0
# -*- coding: utf-8 -*-
import sys, haohaninfo, datetime
from order import Record
from indicator import KBar
# 定義交易商品
Product = sys.argv[1]
# 定義券商
# Broker='Masterlink_Future'
Broker = 'Simulator'

# 定義K棒物件
Today = datetime.datetime.now().strftime('%Y%m%d')
KBar1M = KBar(Today, 1)
# 定義初始倉位
OrderRecord = Record()
# 定義突破n分鐘的高低點
BeforeTime = 30
# 定義持倉最久時間(180分鐘)
HoldTime = datetime.timedelta(minutes=180)
# 移動停損點數
StopLoss = 30

# 訂閱報價
GO = haohaninfo.GOrder.GOQuote()
# 進場判斷
for row in GO.Subscribe(Broker, 'match', Product):
    # 取得時間、價格、量欄位
    Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f')
    Price = float(row[2])
    Qty = float(row[3])
    # 將資料填入K棒
Beispiel #2
0
# -*- coding: utf-8 -*-
import sys, haohaninfo, datetime, numpy
from order import Record
from indicator import KBar

# 定義交易商品
Product = sys.argv[1]
# 定義券商
Broker = 'Simulator'

# 定義初始倉位
OrderRecord = Record()
# 定義K棒物件
Today = datetime.datetime.now().strftime('%Y%m%d')
KBar1M = KBar(Today, 1)
# 定義布林通道週期
Period = 10

# 訂閱報價
GO = haohaninfo.GOrder.GOQuote()
# 進場判斷
for row in GO.Subscribe(Broker, 'match', Product):
    # 取得時間、價格欄位
    Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f')
    Price = float(row[2])
    Qty = float(row[3])
    ChangeKFlag = KBar1M.AddPrice(Time, Price, Qty)
    # 每分鐘判斷一次
    if ChangeKFlag == 1:
        # 取得布林通道上下界
        Upper, Middle, Lower = KBar1M.GetBBands(Period)
Beispiel #3
0
# 匯入模組
from indicator import GetHistoryDataByPeriod, KBar
from order import Record
import datetime, sys

# 指定日期
DataPath = sys.argv[1]  # 'C:/Data'
Broker = sys.argv[2]  # 'simulator'
Product = sys.argv[3]  # 'TXFI9'
Start = sys.argv[4]  # '20190830'
End = sys.argv[5]  # '20190911'

# 定義初始倉位
OrderRecord = Record()
# 定義K棒物件(判斷區間)
MinuteKbar = KBar(Start, 1)
# 定義MA、RSI週期
MAPeriod = 10
RSIPeriod = 30

# 策略迴圈 (GetHistoryDataByPeriod 改為取得即時報價即可轉為實單交易)
for row in GetHistoryDataByPeriod(DataPath, Broker, Product, 'Match', Start,
                                  End):
    Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f')
    Price = float(row[2])
    Qty = int(row[3])

    # 餵資料進K線物件
    ChangeFlag = MinuteKbar.AddPrice(Time, Price, Qty)
    # 如果有換K棒才會取得MA以及RSI
    if ChangeFlag == 1:
# -*- coding: utf-8 -*-
import sys, haohaninfo, datetime
from order import Record
from indicator import KBar

# 定義交易商品
Product = sys.argv[1]
# 定義券商
Broker = 'Simulator'

# 定義初始倉位
OrderRecord = Record()
# 定義K棒物件
Today = datetime.datetime.now().strftime('%Y%m%d')
KBar1M = KBar(Today, 1)
# 定義爆量口數
BigOrder = 1000

# 訂閱報價
GO = haohaninfo.GOrder.GOQuote()
# 進場判斷
for row in GO.Subscribe(Broker, 'match', Product):
    # 取得時間、價格欄位
    Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f')
    Price = float(row[2])
    Qty = float(row[3])
    ChangeKFlag = KBar1M.AddPrice(Time, Price, Qty)
    # 每分鐘判斷一次爆量
    if ChangeKFlag == 1:
        # 判斷是否爆量
        Volume = KBar1M.GetVolume()
Beispiel #5
0
# 匯入模組
from indicator import GetHistoryDataByPeriod,KBar
import datetime,sys  

# 指定日期
DataPath=sys.argv[1]    # 'C:/Data'
Broker=sys.argv[2]      # 'simulator'
Product=sys.argv[3]     # 'TXFI9' 
Start=sys.argv[4]       # '20190830'
End=sys.argv[5]         # '20190911'

# 定義1分K棒的物件
MinuteKBar=KBar(Start,1)

for row in GetHistoryDataByPeriod(DataPath,Broker,Product,'Match',Start,End):
    Time = datetime.datetime.strptime(row[0],'%Y/%m/%d %H:%M:%S.%f')
    Price = float(row[2])
    Volume = int(row[4])
    # 餵資料進K棒物件
    ChangeFlag = MinuteKBar.AddPrice(time,price,volume)
    # 接下來策略會寫在下方
    # ...
    # 如果 ChangeFlag 變數為1,則代表換新K棒
    # if ChangeFlag == 1:
    # 取用加權移動平均線方式
    # MinuteKBar.GetWMA(15)

Beispiel #6
0
# -*- coding: utf-8 -*-
import sys, haohaninfo, datetime, numpy
from order import Record
from indicator import KBar

# 定義交易商品
Product = sys.argv[1]
# 定義券商
Broker = 'Simulator'

# 定義初始倉位
OrderRecord = Record()
# 定義K棒物件
Today = datetime.datetime.now().strftime('%Y%m%d')
KBar1M = KBar(Today, 1)
# 定義MACD週期
FastPeriod = 10
SlowPeriod = 30
SignalPeriod = 10

# 訂閱報價
GO = haohaninfo.GOrder.GOQuote()
# 進場判斷
for row in GO.Subscribe(Broker, 'match', Product):
    # 取得時間、價格欄位
    Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f')
    Price = float(row[2])
    Qty = float(row[3])
    ChangeKFlag = KBar1M.AddPrice(Time, Price, Qty)
    # 每分鐘判斷一次
    if ChangeKFlag == 1:
Beispiel #7
0
# 取得資料
# Date='20190902'
# Product='TXFI9'
Date=sys.argv[1]
Product=sys.argv[2]
FilePath='C:/Data/'
Broker='Simulator'
Table='Match'
Data=GetHistoryData(FilePath,Broker,Date,Product,Table)

# 取特定時間的資料
Data = [ line for line in Data if strptime(line[0],"%Y/%m/%d %H:%M:%S.%f").strftime('%H%M') < '1345' ]

# 定義1分K棒的物件
MinuteKBar=KBar(Date,1)
# 計算K棒
for line in Data:
    time=strptime(line[0],"%Y/%m/%d %H:%M:%S.%f")
    price=int(line[2])
    qty=int(line[3])
    MinuteKBar.AddPrice(time,price,qty)

# 取得K線物件
KData=MinuteKBar.GetChartTypeData()
# 取得繪製累計成交量的素材
Time=MinuteKBar.GetTime()
Volume=MinuteKBar.GetVolume()
# 繪製累計成交量的顏色(漲紅跌綠)
VolumeColor=[ 'red' if line[4]>line[1] else 'green'  for line in KData ]
Beispiel #8
0
# -*- coding: utf-8 -*-
import sys,haohaninfo,datetime,numpy
from order import Record
from indicator import KBar

# 定義交易商品
Product=sys.argv[1]
# 定義券商
Broker='Simulator'

# 定義初始倉位
OrderRecord=Record()
# 定義K棒物件
Today=datetime.datetime.now().strftime('%Y%m%d')
KBar1M=KBar(Today,1)
# 定義KD週期
RSVPeriod=9
KPeriod=3
DPeriod=3

# 訂閱報價
GO = haohaninfo.GOrder.GOQuote()
# 進場判斷
for row in GO.Subscribe( Broker, 'match', Product ):    
    # 取得時間、價格欄位
    Time=datetime.datetime.strptime(row[0],'%Y/%m/%d %H:%M:%S.%f')
    Price=float(row[2])
    Qty=float(row[3])
    ChangeKFlag=KBar1M.AddPrice(Time,Price,Qty)
    # 每分鐘判斷一次
    if ChangeKFlag==1:
Beispiel #9
0
# -*- coding: utf-8 -*-
import sys, haohaninfo, datetime, numpy
from order import Record
from indicator import KBar, getPutCallRatio

# 定義交易商品
Product = sys.argv[1]
# 定義券商
Broker = 'Simulator'

# 定義初始倉位
OrderRecord = Record()
# 定義K棒物件
Today = datetime.datetime.now().strftime('%Y%m%d')
KBar1M = KBar(Today, 1)
# 定義MA週期
FastPeriod = 9
SlowPeriod = 16
# MA穿越次數
CrossFlag = 0

# 取得前30日的Put Call Ratio 來判斷當天趨勢
PCRData = getPutCallRatio()
PCR = [float(i[-1]) for i in PCRData]
# 取得Put Call Ratio的平均、最後一筆
PCRAver = sum(PCR) / len(PCR)
PCRLast = PCR[-1]

# 訂閱報價
GO = haohaninfo.GOrder.GOQuote()
# 進場判斷
Beispiel #10
0
# Product='TXFI9'
Date = sys.argv[1]
Product = sys.argv[2]
FilePath = 'C:/Data/'
Broker = 'Simulator'
Table = 'Match'
Data = GetHistoryData(FilePath, Broker, Date, Product, Table)

# 取特定時間的資料
Data = [
    line for line in Data
    if strptime(line[0], "%Y/%m/%d %H:%M:%S.%f").strftime('%H%M') < '1345'
]

# 定義1分K棒的物件
MinuteKBar = KBar(Date, 1)
# 計算K棒
for line in Data:
    time = strptime(line[0], "%Y/%m/%d %H:%M:%S.%f")
    price = int(line[2])
    qty = int(line[3])
    MinuteKBar.AddPrice(time, price, qty)

# 計算技術指標
CloseArray = MinuteKBar.GetClose()
# 移動平均線計算(5分K)
SMAArray = SMA(CloseArray, timeperiod=5)
print(SMAArray[:10])
# 移動平均線計算(5分K)
UpperArray, MiddleArray, LowerArray = BBANDS(CloseArray, timeperiod=5)
print(UpperArray[:10])
Beispiel #11
0
import datetime,sys  

# 指定日期
DataPath=sys.argv[1]    # 'C:/Data'
Broker=sys.argv[2]      # 'simulator'
Product=sys.argv[3]     # 'TXFI9' 
Start=sys.argv[4]       # '20190830'
End=sys.argv[5]         # '20190911'

# 定義初始倉位
OrderRecord=Record()
# 停損停利
TakeProfit=20
TakeProfitPrice=0
# 定義K棒物件(判斷區間)
MinuteKbar=KBar(Start,1) 
# 定義區間
Ceil=99999999999
Floor=0
Spread=0
GapRatio=0.1

# 策略迴圈 (GetHistoryDataByPeriod 改為取得即時報價即可轉為實單交易)
for row in GetHistoryDataByPeriod(DataPath,Broker,Product,'Match',Start,End):
    Time = datetime.datetime.strptime(row[0],'%Y/%m/%d %H:%M:%S.%f')
    Price = float(row[2])
    Qty = int(row[3])
    
    # 餵資料進K線物件
    ChangeFlag = MinuteKbar.AddPrice(Time,Price,Qty)
    # 如果有換K棒才會更新最高最低值
Beispiel #12
0
# -*- coding: utf-8 -*-
import sys, haohaninfo, datetime, numpy
from order import Record
from indicator import KBar

# 定義交易商品
Product = sys.argv[1]
# 定義券商
Broker = 'Simulator'

# 定義初始倉位
OrderRecord = Record()
# 定義K棒物件
Today = datetime.datetime.now().strftime('%Y%m%d')
KBar1M = KBar(Today, 1)
# 定義RSI週期
Period = 10

# 訂閱報價
GO = haohaninfo.GOrder.GOQuote()
# 進場判斷
for row in GO.Subscribe(Broker, 'match', Product):
    # 取得時間、價格欄位
    Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f')
    Price = float(row[2])
    Qty = float(row[3])
    ChangeKFlag = KBar1M.AddPrice(Time, Price, Qty)
    # 每分鐘判斷一次
    if ChangeKFlag == 1:
        # 取得RSI
        RSI = KBar1M.GetRSI(Period)
# -*- coding: utf-8 -*-
import sys, haohaninfo, datetime, numpy
from order import Record
from indicator import KBar

# 定義交易商品
Product = sys.argv[1]
# 定義券商
Broker = 'Simulator'

# 定義初始倉位
OrderRecord = Record()
# 定義K棒物件
Today = datetime.datetime.now().strftime('%Y%m%d')
KBar1M = KBar(Today, 5)
# 定義MA週期
FastPeriod = 9
SlowPeriod = 16
# 定義KD週期
RSVPeriod = 9
KPeriod = 3
DPeriod = 3

# 訂閱報價
GO = haohaninfo.GOrder.GOQuote()
# 進場判斷
for row in GO.Subscribe(Broker, 'match', Product):
    # 取得時間、價格、量欄位
    Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f')
    Price = float(row[2])
    Qty = float(row[3])
Beispiel #14
0
from indicator import GetHistoryData, KBar
import datetime, sys
strptime = datetime.datetime.strptime

# 取得資料
# Date='20190902'
# Product='TXFI9'
Date = sys.argv[1]
Product = sys.argv[2]
FilePath = 'C:/Data/'
Broker = 'Simulator'
Table = 'Match'
Data = GetHistoryData(FilePath, Broker, Date, Product, Table)

# 取特定時間的資料
Data = [
    line for line in Data
    if strptime(line[0], "%Y/%m/%d %H:%M:%S.%f").strftime('%H%M') < '1345'
]

# 定義1分K棒的物件
MinuteKBar = KBar(Date, 1)
# 計算K棒
for line in Data:
    time = strptime(line[0], "%Y/%m/%d %H:%M:%S.%f")
    price = int(line[2])
    qty = int(line[3])
    MinuteKBar.AddPrice(time, price, qty)

print(MinuteKBar.GetChartTypeData()[:5])