HQBSH.com的whois记录显示注册于2013年1月18日,至今已经持续运营了:0年0个月0天零0小时0分钟0秒

最新报价
 找回密码
 立即注册

QQ登录

只需一步,快速开始

华强北商行 › 门户 › 查看主题

火币合约帐户,量化分析K线图

发布者: yh6788 | 发布时间: 2019-11-2 12:30| 查看数: 39| 评论数: 0|帖子模式

利用K线的最高值和最低值,来分析6支电子货币的强弱
有一种说法是高于120%可以买,低于50%可以开空单,具体的大家自己观察。
2019-11-02_122920.jpg


  1. import json
  2. from urllib.request import Request, urlopen
  3. import time,os
  4. import pandas as pd

  5. import requests


  6. #system config

  7. pd.set_option('expand_frame_repr',False)
  8. pd.set_option('display.max_rows',1000)

  9. #system config end


  10. # coinID
  11. os.system('cls')
  12. btc = '1'
  13. eth = '3'
  14. usdt = '2'
  15. # tradeType
  16. buy = '1'
  17. sell = '0'

  18. def dl_system():
  19.     weizhi=49279
  20.     weizi_0=input('几号电脑:1/2  :   ')
  21.     if weizi_0=='':
  22.             weizhi=49279
  23.             weizhi=input('其它主机,那么请输入端口号: ')
  24.     if weizi_0=='1':
  25.         weizhi=49279
  26.         print('端口是',weizhi)
  27.     if weizi_0=='2':
  28.         weizhi=49194
  29.     # proxy=['127.0.0.1',weizhi]
  30.     # return proxy
  31.     SOCKS5_PROXY_HOST = '127.0.0.1'
  32.     # SOCKS5_PROXY_PORT = 1358  # socks 代理本地端口
  33.     SOCKS5_PROXY_PORT = weizhi
  34.     # default_socket = socket.socket
  35.     # s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  36.     # socks.set_default_proxy(socks.SOCKS5, SOCKS5_PROXY_HOST, SOCKS5_PROXY_PORT)
  37.     # socket.socket = socks.socksocket
  38.     dl_url=str('127.0.0.1:%s'%weizhi)
  39.     return dl_url

  40. def mysave(file):
  41.     file=file.content
  42.     name="1.html"
  43.     with open(name,"wb") as f:
  44.         f.write(file)
  45. def getPrice(headers,proxies,bizhong,period='15min',size='100'):
  46.         # def getPrice(coinID, tradeType):
  47.         url_0='https://api.hbdm.com/'
  48.         # url_0='https://api.huobi.pro'


  49.         #这个是柱形图的
  50.         # huobiapi = url_0+'market/trade'+'?'+'symbol='+bizhong+'_CW'        #这个是获取实时报价的。
  51.         #/market/history/kline?period=1min&size=200&symbol=BTC_CQ
  52.         huobiapi = url_0+'market/history/kline?period='+period+'&size='+size+'&symbol=' + bizhong+'_CW'
  53.         #新的地址huobi.pro
  54.         # huobiapi = "https://api.huobi.br.com/market/history/kline?period=1min&size=1&symbol=" + str(bizhong)+'usdt'        #旧的地址br.com


  55.         # huobiapi = "https://www.hbg.com/zh-hk/markets/hb_dm/index.html"        #这个也可以用
  56.         # huobiapi = "https://api.huobi.br.com/v1/common/symbols"        #这个也可以用
  57.         # huobiapi = url_0+"/v1/common/symbols"        #这个也可以用
  58.         # huobiapi = "https://www.hbg.com/zh-hk/index.html"     #可以用
  59.         # huobiapi = "https://www.hbg.com/zh-hk/markets/hb_dm/"
  60.         # print(huobiapi)
  61.         html=requests.get(huobiapi,headers=headers,proxies=proxies)
  62.         # mysave(html)
  63.         # print(html.text)
  64.         json1=html.json()
  65.         '''
  66.         下面是打开现价的。
  67.         # data_list=json1['tick']
  68.         data_list0=(data_list['data'])
  69.         data_list1=data_list[2]
  70.         # print(data_list0)
  71.         data_1=data_list0[0]['price']
  72.         # print(data_1)
  73.         df=data_1
  74.         '''

  75.         #这段是打开柱形图的
  76.         data_list0=(json1['data'])
  77.         data_1=data_list0
  78.         df=pd.DataFrame(data_1)



  79.         # print(df)
  80.         return df
  81.         pass
  82.         # huobiapi = "https://api-otc.huobi.pro/v1/otc/trade/list/public"
  83.         # api_url = huobiapi + "?coinId=" + coinID + "&tradeType=" + tradeType + "¤tPage=1&payWay=&country=&merchant=1&online=1&range=0"
  84.         # firefox_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
  85.         # request = Request( api_url, headers=firefox_headers )
  86.         # html = urlopen( request )
  87.         # data = html.read().decode( 'utf-8' )
  88.         # dataJson = json.loads( data )
  89.         # price = dataJson['data'][0]['price']
  90.         # print(price)
  91.         # return price
  92. def duibi(df):
  93.         #对比第一卖价与第一买价,中间空值的变化。
  94.         print(df)
  95.         data_close=df['close'][0]
  96.         data_high=df['high'][0]
  97.         print(data_close,'          ',data_high)
  98.         data_cha=data_high-data_close
  99.         data_cha_100=data_cha/data_close*100
  100.         print(data_cha_100,'%')

  101. def jiagedongliang(df):

  102.         #价格动量,
  103.         '''
  104.         价格动量的计算公式
  105.         AR = [N天所有(High-Open)的和/ N天所有(Open—Low)的和] * 100
  106.         价格动量在一段时间内,反应了开盘价在最高价和最低价之间的位子,这个位置就是我们判断双方力量拉锯的依据。

  107.         我们假设这个值为100左右,超过100,则多头力量开始增加,小于100,则空头力量开始聚集。
  108.         AR值升高时表示行情活跃,人气旺盛,多头一路高歌猛进,但是过高则表示价格进入超买区域,应选择时机平仓。AR值的高度没有具体标准,一般情况下,AR值上升至120左右时,价格很有可能会回调下挫。
  109.         AR值下降时表示行情衰退,空头气势正盛,需要多头努力,过低则暗示价格可能已经跌入超卖区域,可考虑伺机做多。一般AR值跌至50以下时,价格随时止跌反涨。
  110.         注意:以上数字皆是默认数值,绝非真理定式。在真正交易的过程中,我们要随着行情的变化,调试这个区间以适应当下的行情状态。

  111.         用Python实现价格动量的量化交易策略
  112.         '''
  113.         # print(df)
  114.         # print(df.[['high']])
  115.         # data_temp=df['amount'].sum()
  116.         # print(df['high'].sum())
  117.         sum_open=round(df['open'].sum(),6)
  118.         sum_close=df['close'].sum()
  119.         sum_high=round(df['high'].sum(),6)
  120.         sum_low=df['low'].sum()
  121.         data1=sum_high-sum_open
  122.         data2=sum_open-sum_low
  123.         data=data1/data2*100
  124.         # print(sum_open,sum_close,sum_high,sum_low,data)
  125.         return int(data)

  126. def main():
  127.         headers = {'User-Agent': r'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0',
  128.                    'Connection': r'keep-alive',
  129.                    'Referer': r'https://www.hbg.com'}
  130.         dl_url=dl_system()
  131.         # dl_url='127.0.0.1:49280'
  132.         # dl_url='127.0.0.1:49194'
  133.         proxies={
  134.             'http':dl_url,
  135.             'https':dl_url
  136.                 # 'http':'127.0.0.1:53656',
  137.                 # 'https':'127.0.0.1:53656'
  138.                 # 'http':'127.0.0.1:49419',
  139.                 # 'https':'127.0.0.1:49419'
  140.         }

  141.         time_sleep=61


  142.         '''
  143.         #这是设置单一币种的,现在要查询合约币种,所以取消
  144.         bizhong = "etcusdt"
  145.         bizhong=input('请输入查询兑换usdt的币种,比如比特币就输入 btc   :   ')
  146.         if bizhong=='':
  147.                 bizhong='btc'
  148.                 print('未选择,默认为   BTC:USDT ,\n%s秒更新一次数据,停止请按:CTRL+C\n'%time_sleep)
  149.                 time.sleep(1)
  150.         '''

  151.         bz_list=['BTC','ETC','ETH','EOS','XRP','BSV']
  152.         # bz_list=['btc','etc','eth','eos','xrp','bsv']
  153.         # print('i')
  154.         size=str(input('数据要分析多少条柱形图的? '))
  155.        
  156.         print('可用K线类型:1min, 5min, 15min, 30min, 60min,4hour,1day, 1mon')
  157.         period=str(input('输入需要分析的K线数据类型: '))
  158.                 # df_list=[]
  159.     # df=getPrice(headers,proxies,bizhong)
  160.     # df=getPrice(headers,proxies)
  161.     # print('1')
  162.         os.system('cls')
  163.         print('数据量化%s条K线,%s数据,%s秒更新一次。'%(size,period,time_sleep))
  164.         print('='*66,'\n')

  165.         # print()
  166.         for i in bz_list:
  167.                 print(i,end=' '*5)
  168.         print('\n','='*66,'\n')
  169.         # while True:
  170.         while True:
  171.                 df_list=[]
  172.                 try:
  173.                         for bizhong in bz_list:
  174.                                 df=getPrice(headers,proxies,bizhong,period,size)
  175.                                 df1=jiagedongliang(df)
  176.                                 # print(df)
  177.                                 df_list.append(df1)
  178.                                 # print(df)
  179.                 # duibi(df)
  180.                 # os.exit()
  181.                         for i1 in df_list:
  182.                                 # print(i1,end=' '*2)        #这个是实时价格的
  183.                                 print(i1,'%',end=' '*4)        #这个是柱形图的
  184.                 except:
  185.                         print('网络异常,检查代理服务器,%s秒后再次连接。              '%time_sleep,end='')
  186.                 print(' '*2,time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
  187.                 # print('='*16,' '*8,time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),' '*8,'='*16)
  188.                 # os.exit()
  189.                 time.sleep(time_sleep)
  190. main()

  191. '''

  192. def getAllPrice(showprice=0):
  193.     btc_buy = getPrice( coinID=btc, tradeType=buy )
  194.     btc_sell = getPrice( coinID=btc, tradeType=sell )

  195.     eth_buy = getPrice( coinID=eth, tradeType=buy )
  196.     eth_sell = getPrice( coinID=eth, tradeType=sell )

  197.     usdt_buy = getPrice( coinID=usdt, tradeType=buy )
  198.     usdt_sell = getPrice( coinID=usdt, tradeType=sell )

  199.     prices = {
  200.         "btc": {
  201.             "buy": btc_buy,
  202.             "sell": btc_sell
  203.         },
  204.         "eth": {
  205.             "buy": eth_buy,
  206.             "sell": eth_sell
  207.         },
  208.         "usdt": {
  209.             "buy": usdt_buy,
  210.             "sell": usdt_sell
  211.         }
  212.     }
  213.     prices_df = pd.DataFrame( prices )
  214.     print(prices)
  215.     if showprice:
  216.         print( prices_df )
  217.     return prices_df

  218. def getChajia(prices,showChajia = 0):
  219.     cj = prices.iloc[0]-prices.iloc[1]
  220.     if showChajia:
  221.         print('差价:')
  222.         print(cj)
  223.     return cj


  224. num = 0
  225. r = 0

  226. # while(1):
  227. t = time.strftime( '%H:%M:%S', time.localtime( time.time() ) )
  228. print( t,r )

  229. try:
  230.     prices = getAllPrice( showprice=1 )
  231.     print('1.1')
  232.     cj = getChajia( prices=prices, showChajia=1 )

  233.     if (cj['btc'] < 0 or cj['eth'] < 0 or cj['usdt'] < 0):
  234.         num = num + 1
  235.     print( u"第",num,u"次发现价差")

  236. except:
  237.     pass
  238. r = r+1

  239. time.sleep( 3 )
  240. '''
复制代码


最新评论

在线客服
马上联系
加好友78950405
微信联系tel18938079527
微信联系
电话联系
联系电话18938079527
工作时间
11:00-22:00