SHARE
TWEET

Untitled

a guest Oct 21st, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import os
  4. sys.path.append('..'+os.sep)
  5. import ABXToolkit
  6. import ABXstatus
  7. import time
  8. import talib
  9. import talib.abstract as ta
  10. import numpy as np
  11. import matplotlib.pyplot as plt
  12. import pandas as pd
  13. import datetime
  14. import configparser
  15. USER_PATH = os.path.dirname(__file__)
  16. ABXToolkit.load_ini(USER_PATH, 'h94_ab')
  17. # line bot
  18. from linebot import LineBotApi
  19. from linebot.models import TextSendMessage
  20. from linebot.exceptions import LineBotApiError
  21. line_bot_api = LineBotApi("xzbRVRWKFwPyzhHx/xS478lfjMNM1OHBNGgZdobGYp/QR85KVAE0jBpv4e+YC5DptU4VEusq+8pi/qZj610IGBrxsucE/kNEKE8HG9S+fA5lJpD6reioi5cZfdEr5RCTriiShLYD9YztNnO19jpOAQdB04t89/1O/w1cDnyilFU=")
  22.  
  23. fileisread = False
  24. historydf = None
  25. tmpday = None
  26. tmpdaydf = None
  27. historydate = None
  28. anchorprice = 0.0
  29.  
  30. # 如果想要回測 初始化ptrade.ini取得自訂義設定檔,成功時返回物件-class ConfigParser(RawConfigParser)
  31. ptrade = ABXToolkit.ptrade_ini('ptrade.ini')
  32.  
  33. # 初始化通知間隔物件
  34. ABXToolkit.intervalinit()
  35.  
  36. # 自定義line通知方法
  37. def linesendmessage(ptext):
  38.     try:
  39.         # ptext = stext
  40.         line_bot_api.push_message("C02af7a312631c0e59d32660b332a3ea0", \
  41.                                   TextSendMessage(text=ptext))
  42.     except LineBotApiError as e:
  43.         print(e)
  44. # 即時報價callback
  45. def my_func(watch_num, stkid, dframe):
  46.     global historydf
  47.     global anchorprice
  48.     global tmpdaydf
  49.    
  50.     if watch_num == 'dfhistorydata':
  51.         print("已下載歷史報價", stkid)
  52.         fileisread = True
  53.     if watch_num == 'dftrade':
  54.         # 成交價若一樣就直接返回
  55.         if anchorprice == dframe.iloc[-1]['TradePrice']:
  56.             return
  57.         anchorprice = dframe.iloc[-1]['TradePrice']
  58.         # 將今日成交明細轉換成今日線
  59.         locatnum = ABXToolkit.getIndexbystkid(stkid)
  60.         tmpday = ABXToolkit.getdayx(sub=locatnum)
  61.         # 取得歷史報價. key d = 歷史日線
  62.         tsub = ABXToolkit.getsubbystkid(stkid)
  63.         historydf = tsub['dfhistorydata']['d']
  64.         if historydf is None:
  65.             return
  66.         # 今日線併入歷史日線
  67.         tmpdaydf = historydf.append(tmpday, ignore_index=True)
  68.         # ptrade.ini策略,將歷史&今日日線計算技術分析判斷策略.
  69.         ABXToolkit.ptrade_buy(ptrade, tmpdaydf, locatnum)
  70. #
  71. def backtestfunc(pframe):
  72.     ABXToolkit.ptrade_buy(ptrade, pframe, 1)
  73. # 將callback設定給api
  74. ABXToolkit.set_func(my_func)
  75. # 設定回測func
  76. ABXToolkit.set_backtestfunc(backtestfunc)
  77. # 設定ptrade條件成立func,目前把送line通知設定給api跑
  78. ABXToolkit.set_ptradefunc(linesendmessage)
  79. pdict = {}
  80.  
  81. # 150天前日期
  82. historydate = ABXToolkit.get_day_of_day(n=-150)
  83. # 0050 download history
  84. if ABXToolkit.checkquotefile(stkid=ABXToolkit.getstkidbyindex(1)[2:], \
  85.                              fdate=historydate) == False:
  86.     pdict["User"] = "2|0987313526"
  87.     pdict["Password"] = "admin2257"
  88.     pdict["StockFullID"] = ABXToolkit.getstkidbyindex(1)
  89.     pdict["DataType"] = "d"
  90.     pdict["StartDate"] = historydate
  91.     pdict["DataDays"] = "0"
  92.     ABXToolkit.getquotefile(pdict)
  93. else:
  94.     print('target already exist.')
  95.     print('No need to download.')
  96.     historydf = ABXToolkit.read_file(stkid=ABXToolkit.getstkidbyindex(1)[2:], \
  97.                                      fdate=historydate)
  98.     tsub1 = ABXToolkit.getsubbyindex(n=1)
  99.     tsub1['dfhistorydata']['d'] = historydf
  100.     fileisread = True
  101.    
  102. # 2317 download history
  103. if ABXToolkit.checkquotefile(stkid=ABXToolkit.getstkidbyindex(2)[2:], \
  104.                              fdate=historydate) == False:
  105.     pdict["User"] = "2|0987313526"
  106.     pdict["Password"] = "admin2257"
  107.     pdict["StockFullID"] = ABXToolkit.getstkidbyindex(2)
  108.     pdict["DataType"] = "d"
  109.     pdict["StartDate"] = historydate
  110.     pdict["DataDays"] = "0"
  111.     ABXToolkit.getquotefile(pdict)
  112. else:
  113.     print('target already exist.')
  114.     print('No need to download.')
  115.     historydf = ABXToolkit.read_file(stkid=ABXToolkit.getstkidbyindex(2)[2:], \
  116.                                      fdate=historydate)
  117.     tsub2 = ABXToolkit.getsubbyindex(n=2)
  118.     tsub2['dfhistorydata']['d'] = historydf    
  119.     fileisread = True
  120.  
  121. # 訂閱及時
  122. pdict["User"] = "2|0987313526"
  123. pdict["Password"] = "admin2257"
  124. # 即時訂閱、與回補當天交易明細一起呼叫
  125. ABXToolkit.subscrip(pdict)# 此功能有設定代碼的會一起訂閱
  126. # 開盤之後才執行程式需回補之前的成交明細
  127. # 0050
  128. pdict['sub'] = 1# server.ini訂閱的序位,與上面訂閱功能不同,回補功能需指定
  129. ABXToolkit.rebuildTrade(pdict)
  130. # 2317
  131. pdict['sub'] = 2# server.ini訂閱的序位
  132. ABXToolkit.rebuildTrade(pdict)
  133.  
  134. # 回測 非測即時時使用,原理:歷史檔案模擬一筆一筆讀入回測,目前先註解
  135. # 不會與上述subscrip、rebuildTrade同時使用
  136. # ABXToolkit.backtest(historydf, 1)
  137.  
  138. # 卡住程式不結束、輸入任意鍵即可結束程式
  139. input()
  140. # 以下結束前寫檔:
  141. # ptrade_buy的log
  142. ABXToolkit.plogsave()
  143.  
  144. # debug用:
  145. # 歷史報價容器寫到檔案 debug用
  146. tsub1 = ABXToolkit.getsubbyindex(n=1)
  147. tsub2 = ABXToolkit.getsubbyindex(n=2)
  148. ABXToolkit.dftocsv(filename='tsub1.csv', df=tsub1['dfhistorydata']['d'])
  149. ABXToolkit.dftocsv(filename='tsub2.csv', df=tsub2['dfhistorydata']['d'])
  150. # 歷史報價+上今日成交明細轉日線
  151. historydf = tsub1['dfhistorydata']['d']
  152. tmpday = ABXToolkit.getdayx(sub=1)
  153. tmpdaydf = historydf.append(tmpday, ignore_index=True)
  154. ABXToolkit.dftocsv(filename='tmpdaydf1.csv', df=tmpdaydf)
  155. historydf = tsub2['dfhistorydata']['d']
  156. tmpday = ABXToolkit.getdayx(sub=2)
  157. tmpdaydf = historydf.append(tmpday, ignore_index=True)
  158. ABXToolkit.dftocsv(filename='tmpdaydf2.csv', df=tmpdaydf)
  159. # 成交明細
  160. ABXToolkit.dftocsv(filename='dftrade1.csv', df=tsub1['dftrade'].data)
  161. ABXToolkit.dftocsv(filename='dftrade2.csv', df=tsub2['dftrade'].data)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top