利用K线的最高值和最低值,来分析6支电子货币的强弱
有一种说法是高于120%可以买,低于50%可以开空单,具体的大家自己观察。
- import json
- from urllib.request import Request, urlopen
- import time,os
- import pandas as pd
- import requests
- #system config
- pd.set_option('expand_frame_repr',False)
- pd.set_option('display.max_rows',1000)
- #system config end
- # coinID
- os.system('cls')
- btc = '1'
- eth = '3'
- usdt = '2'
- # tradeType
- buy = '1'
- sell = '0'
- def dl_system():
- weizhi=49279
- weizi_0=input('几号电脑:1/2 : ')
- if weizi_0=='':
- weizhi=49279
- weizhi=input('其它主机,那么请输入端口号: ')
- if weizi_0=='1':
- weizhi=49279
- print('端口是',weizhi)
- if weizi_0=='2':
- weizhi=49194
- # proxy=['127.0.0.1',weizhi]
- # return proxy
- SOCKS5_PROXY_HOST = '127.0.0.1'
- # SOCKS5_PROXY_PORT = 1358 # socks 代理本地端口
- SOCKS5_PROXY_PORT = weizhi
- # default_socket = socket.socket
- # s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- # socks.set_default_proxy(socks.SOCKS5, SOCKS5_PROXY_HOST, SOCKS5_PROXY_PORT)
- # socket.socket = socks.socksocket
- dl_url=str('127.0.0.1:%s'%weizhi)
- return dl_url
- def mysave(file):
- file=file.content
- name="1.html"
- with open(name,"wb") as f:
- f.write(file)
- def getPrice(headers,proxies,bizhong,period='15min',size='100'):
- # def getPrice(coinID, tradeType):
- url_0='https://api.hbdm.com/'
- # url_0='https://api.huobi.pro'
- #这个是柱形图的
- # huobiapi = url_0+'market/trade'+'?'+'symbol='+bizhong+'_CW' #这个是获取实时报价的。
- #/market/history/kline?period=1min&size=200&symbol=BTC_CQ
- huobiapi = url_0+'market/history/kline?period='+period+'&size='+size+'&symbol=' + bizhong+'_CW'
- #新的地址huobi.pro
- # huobiapi = "https://api.huobi.br.com/market/history/kline?period=1min&size=1&symbol=" + str(bizhong)+'usdt' #旧的地址br.com
- # huobiapi = "https://www.hbg.com/zh-hk/markets/hb_dm/index.html" #这个也可以用
- # huobiapi = "https://api.huobi.br.com/v1/common/symbols" #这个也可以用
- # huobiapi = url_0+"/v1/common/symbols" #这个也可以用
- # huobiapi = "https://www.hbg.com/zh-hk/index.html" #可以用
- # huobiapi = "https://www.hbg.com/zh-hk/markets/hb_dm/"
- # print(huobiapi)
- html=requests.get(huobiapi,headers=headers,proxies=proxies)
- # mysave(html)
- # print(html.text)
- json1=html.json()
- '''
- 下面是打开现价的。
- # data_list=json1['tick']
- data_list0=(data_list['data'])
- data_list1=data_list[2]
- # print(data_list0)
- data_1=data_list0[0]['price']
- # print(data_1)
- df=data_1
- '''
- #这段是打开柱形图的
- data_list0=(json1['data'])
- data_1=data_list0
- df=pd.DataFrame(data_1)
- # print(df)
- return df
- pass
- # huobiapi = "https://api-otc.huobi.pro/v1/otc/trade/list/public"
- # api_url = huobiapi + "?coinId=" + coinID + "&tradeType=" + tradeType + "¤tPage=1&payWay=&country=&merchant=1&online=1&range=0"
- # firefox_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
- # request = Request( api_url, headers=firefox_headers )
- # html = urlopen( request )
- # data = html.read().decode( 'utf-8' )
- # dataJson = json.loads( data )
- # price = dataJson['data'][0]['price']
- # print(price)
- # return price
- def duibi(df):
- #对比第一卖价与第一买价,中间空值的变化。
- print(df)
- data_close=df['close'][0]
- data_high=df['high'][0]
- print(data_close,' ',data_high)
- data_cha=data_high-data_close
- data_cha_100=data_cha/data_close*100
- print(data_cha_100,'%')
- def jiagedongliang(df):
- #价格动量,
- '''
- 价格动量的计算公式
- AR = [N天所有(High-Open)的和/ N天所有(Open—Low)的和] * 100
- 价格动量在一段时间内,反应了开盘价在最高价和最低价之间的位子,这个位置就是我们判断双方力量拉锯的依据。
- 我们假设这个值为100左右,超过100,则多头力量开始增加,小于100,则空头力量开始聚集。
- AR值升高时表示行情活跃,人气旺盛,多头一路高歌猛进,但是过高则表示价格进入超买区域,应选择时机平仓。AR值的高度没有具体标准,一般情况下,AR值上升至120左右时,价格很有可能会回调下挫。
- AR值下降时表示行情衰退,空头气势正盛,需要多头努力,过低则暗示价格可能已经跌入超卖区域,可考虑伺机做多。一般AR值跌至50以下时,价格随时止跌反涨。
- 注意:以上数字皆是默认数值,绝非真理定式。在真正交易的过程中,我们要随着行情的变化,调试这个区间以适应当下的行情状态。
- 用Python实现价格动量的量化交易策略
- '''
- # print(df)
- # print(df.[['high']])
- # data_temp=df['amount'].sum()
- # print(df['high'].sum())
- sum_open=round(df['open'].sum(),6)
- sum_close=df['close'].sum()
- sum_high=round(df['high'].sum(),6)
- sum_low=df['low'].sum()
- data1=sum_high-sum_open
- data2=sum_open-sum_low
- data=data1/data2*100
- # print(sum_open,sum_close,sum_high,sum_low,data)
- return int(data)
- def main():
- headers = {'User-Agent': r'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0',
- 'Connection': r'keep-alive',
- 'Referer': r'https://www.hbg.com'}
- dl_url=dl_system()
- # dl_url='127.0.0.1:49280'
- # dl_url='127.0.0.1:49194'
- proxies={
- 'http':dl_url,
- 'https':dl_url
- # 'http':'127.0.0.1:53656',
- # 'https':'127.0.0.1:53656'
- # 'http':'127.0.0.1:49419',
- # 'https':'127.0.0.1:49419'
- }
- time_sleep=61
- '''
- #这是设置单一币种的,现在要查询合约币种,所以取消
- bizhong = "etcusdt"
- bizhong=input('请输入查询兑换usdt的币种,比如比特币就输入 btc : ')
- if bizhong=='':
- bizhong='btc'
- print('未选择,默认为 BTC:USDT ,\n%s秒更新一次数据,停止请按:CTRL+C\n'%time_sleep)
- time.sleep(1)
- '''
- bz_list=['BTC','ETC','ETH','EOS','XRP','BSV']
- # bz_list=['btc','etc','eth','eos','xrp','bsv']
- # print('i')
- size=str(input('数据要分析多少条柱形图的? '))
-
- print('可用K线类型:1min, 5min, 15min, 30min, 60min,4hour,1day, 1mon')
- period=str(input('输入需要分析的K线数据类型: '))
- # df_list=[]
- # df=getPrice(headers,proxies,bizhong)
- # df=getPrice(headers,proxies)
- # print('1')
- os.system('cls')
- print('数据量化%s条K线,%s数据,%s秒更新一次。'%(size,period,time_sleep))
- print('='*66,'\n')
- # print()
- for i in bz_list:
- print(i,end=' '*5)
- print('\n','='*66,'\n')
- # while True:
- while True:
- df_list=[]
- try:
- for bizhong in bz_list:
- df=getPrice(headers,proxies,bizhong,period,size)
- df1=jiagedongliang(df)
- # print(df)
- df_list.append(df1)
- # print(df)
- # duibi(df)
- # os.exit()
- for i1 in df_list:
- # print(i1,end=' '*2) #这个是实时价格的
- print(i1,'%',end=' '*4) #这个是柱形图的
- except:
- print('网络异常,检查代理服务器,%s秒后再次连接。 '%time_sleep,end='')
- print(' '*2,time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
- # print('='*16,' '*8,time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),' '*8,'='*16)
- # os.exit()
- time.sleep(time_sleep)
- main()
- '''
- def getAllPrice(showprice=0):
- btc_buy = getPrice( coinID=btc, tradeType=buy )
- btc_sell = getPrice( coinID=btc, tradeType=sell )
- eth_buy = getPrice( coinID=eth, tradeType=buy )
- eth_sell = getPrice( coinID=eth, tradeType=sell )
- usdt_buy = getPrice( coinID=usdt, tradeType=buy )
- usdt_sell = getPrice( coinID=usdt, tradeType=sell )
- prices = {
- "btc": {
- "buy": btc_buy,
- "sell": btc_sell
- },
- "eth": {
- "buy": eth_buy,
- "sell": eth_sell
- },
- "usdt": {
- "buy": usdt_buy,
- "sell": usdt_sell
- }
- }
- prices_df = pd.DataFrame( prices )
- print(prices)
- if showprice:
- print( prices_df )
- return prices_df
- def getChajia(prices,showChajia = 0):
- cj = prices.iloc[0]-prices.iloc[1]
- if showChajia:
- print('差价:')
- print(cj)
- return cj
- num = 0
- r = 0
- # while(1):
- t = time.strftime( '%H:%M:%S', time.localtime( time.time() ) )
- print( t,r )
- try:
- prices = getAllPrice( showprice=1 )
- print('1.1')
- cj = getChajia( prices=prices, showChajia=1 )
- if (cj['btc'] < 0 or cj['eth'] < 0 or cj['usdt'] < 0):
- num = num + 1
- print( u"第",num,u"次发现价差")
- except:
- pass
- r = r+1
- time.sleep( 3 )
- '''
复制代码
|
|