区块键量化交易学习笔记(二)

不能用signal替换signal_long、signal_short原因:
1.平仓开多无法实现
2.signal只有四个值,无法对signal=[1,-1]中间平仓进行判断,即未平空就开多,未平多就做空,应该是signal=[1,0,-1]
signal_long、signal_short有3个信号(1、0、空)或者(-1、0、空)有3的2次方即8个组合(实际不会同时出现[1、-1]、[0、0]的组合即只有6种情况)
3.平仓止损,即逻辑判断来平仓(即记录signal=[1,0,-1])不会出现未平空就开多,未平多就做空。布林线策略会出现的情况即做多时要平空、做空时要平多,MACD策略也是做多时要平空、做空时要平多,但其他策略如简单移动均线只做多平空,所以不用组合都可以

参考:https://blog.patricktriest.com/analyzing-cryptocurrencies-python/

翻译https://blog.csdn.net/dzjx2eotaa24adr/article/details/78774969

pandas_datareader库存包含众多类似tushare的大数据接口,如Tiingo IEX Econdb Enigma Quandl FRED Fama/French OECD等,详见https://pandas-datareader.readthedocs.io/en/latest/index.html,常用的是Quandl数据可导入quandl库或pandas_datareader库调用

方法1:用法参考https://www.quandl.com/tools/python

# import quandl
# quandl.ApiConfig.api_key='1sof-3kR9pZ25SpEjN55'
# df = quandl.get("WIKI/AAPL")
# print(df.head)
# import quandl
# data = quandl.get('WIKI/AAPL',authtoken='6rQMtqqz3yiSTFWwbmrt')
# print(data.head)

方法2:用法参考https://pandas-datareader.readthedocs.io/en/latest/

from pandas_datareader import data as web
# df = web.DataReader("WIKI/AAPL",'quandl',api_key='6rQMtqqz3yiSTFWwbmrt')
# print(df.head)
import sys
import os
import matplotlib as mpl
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import pickle
from datetime import datetime
import quandl
import plotly.offline as py
import plotly.graph_objs as go
import plotly.figure_factory as ff



def get_quandl_data(quandl_id):
    cache_path = '{}.pkl'.format(quandl_id).replace('/','-')
    try:
        f = open(cache_path,'rb')
        df = pickle.load(f)
        print('Loaded {} from cache'.format(quandl_id))
    except (OSError,IOError) as e:
        print('Downloading {} from Quandl'.format(quandl_id))
        df = quandl.get(quandl_id,returns='pandas')
        df.to_pickle(cache_path)
        print('Cached {} at {}'.format(quandl_id,cache_path))
    return df

# Pull Kraken BTC price exchange data
btc_usd_price_kraken = get_quandl_data('BCHARTS/KRAKENUSD')
# Chart the BTC pricing data
#btc_trace = go.Scatter(x=btc_usd_price_kraken.index, y=btc_usd_price_kraken['Weighted Price'])
#py.iplot([btc_trace])
# Pull pricing data for 3 more BTC exchanges
exchanges = ['COINBASE','BITSTAMP','ITBIT']
exchange_data = {}
exchange_data['KRAKEN'] = btc_usd_price_kraken

for exchange in exchanges:
    exchange_code = 'BCHARTS/{}USD'.format(exchange)
    btc_exchange_df = get_quandl_data(exchange_code)
    exchange_data[exchange] = btc_exchange_df
    
def merge_dfs_on_column(dataframes, labels, col):
    '''Merge a single column of each dataframe into a new combined dataframe'''
    series_dict = {}
    for index in range(len(dataframes)):
        series_dict[labels[index]] = dataframes[index][col]
        
    return pd.DataFrame(series_dict)
# Merge the BTC price dataseries' into a single dataframe
btc_usd_datasets = merge_dfs_on_column(list(exchange_data.values()), list(exchange_data.keys()), 'Weighted Price')

def df_scatter(df, title, seperate_y_axis=False, y_axis_label='', scale='linear', initial_hide=False):
    '''Generate a scatter plot of the entire dataframe'''
    label_arr = list(df)
    series_arr = list(map(lambda col: df[col], label_arr))
    
    layout = go.Layout(
        title=title,
        legend=dict(orientation="h"),
        xaxis=dict(type='date'),
        yaxis=dict(
            title=y_axis_label,
            showticklabels= not seperate_y_axis,
            type=scale
        )
    )
    
    y_axis_config = dict(
        overlaying='y',
        showticklabels=False,
        type=scale )
    
    visibility = 'visible'
    if initial_hide:
        visibility = 'legendonly'
        
    # Form Trace For Each Series
    trace_arr = []
    for index, series in enumerate(series_arr):
        trace = go.Scatter(
            x=series.index, 
            y=series, 
            name=label_arr[index],
            visible=visibility
        )
        
        # Add seperate axis for the series
        if seperate_y_axis:
            trace['yaxis'] = 'y{}'.format(index + 1)
            layout['yaxis{}'.format(index + 1)] = y_axis_config    
        trace_arr.append(trace)

    fig = go.Figure(data=trace_arr, layout=layout)
    py.iplot(fig)
# Remove "0" values
btc_usd_datasets.replace(0, np.nan, inplace=True)

# Calculate the average BTC price as a new column
btc_usd_datasets['avg_btc_price_usd'] = btc_usd_datasets.mean(axis=1)
# Plot the average BTC price
btc_trace = go.Scatter(x=btc_usd_datasets.index, y=btc_usd_datasets['avg_btc_price_usd'])
py.iplot([btc_trace])
def get_json_data(json_url, cache_path):
    '''Download and cache JSON data, return as a dataframe.'''
    try:        
        f = open(cache_path, 'rb')
        df = pickle.load(f)   
        print('Loaded {} from cache'.format(json_url))
    except (OSError, IOError) as e:
        print('Downloading {}'.format(json_url))
        df = pd.read_json(json_url)
        df.to_pickle(cache_path)
        print('Cached {} at {}'.format(json_url, cache_path))
    return df

base_polo_url = 'https://poloniex.com/public?command=returnChartData&currencyPair={}&start={}&end={}&period={}'
start_date = datetime.strptime('2015-01-01', '%Y-%m-%d') # get data from the start of 2015
end_date = datetime.now() # up until today
pediod = 86400 # pull daily data (86,400 seconds per day)

def get_crypto_data(poloniex_pair):
    '''Retrieve cryptocurrency data from poloniex'''
    json_url = base_polo_url.format(poloniex_pair, start_date.timestamp(), end_date.timestamp(), pediod)
    data_df = get_json_data(json_url, poloniex_pair)
    data_df = data_df.set_index('date')
    return data_df

altcoins = ['ETH','LTC','XRP','ETC','STR','DASH','SC','XMR','XEM']

altcoin_data = {}
for altcoin in altcoins:
    coinpair = 'BTC_{}'.format(altcoin)
    crypto_price_df = get_crypto_data(coinpair)
    altcoin_data[altcoin] = crypto_price_df

# Calculate USD Price as a new column in each altcoin dataframe
for altcoin in altcoin_data.keys():
    altcoin_data[altcoin]['price_usd'] =  altcoin_data[altcoin]['weightedAverage'] * btc_usd_datasets['avg_btc_price_usd']
    
# Merge USD price of each altcoin into single dataframe 
combined_df = merge_dfs_on_column(list(altcoin_data.values()), list(altcoin_data.keys()), 'price_usd')
# Add BTC price to the dataframe
combined_df['BTC'] = btc_usd_datasets['avg_btc_price_usd']
# Calculate the pearson correlation coefficients for cryptocurrencies in 2016
combined_df_2016 = combined_df[combined_df.index.year == 2016]
combined_df_2016.pct_change().corr(method='pearson')

 ETHLTCXRPETCSTRDASHSCXMRXEM
ETH1.000000-0.0646520.085630-0.1819910.0350930.1226950.1696420.0872160.043205
LTC-0.0646521.0000000.053712-0.1310790.113523-0.0121940.0122530.1294750.160667
XRP0.0856300.0537121.000000-0.0540950.3201160.0886570.0210980.0276490.101326
ETC-0.181991-0.131079-0.0540951.000000-0.1026540.003992-0.008066-0.105898-0.080938
STR0.0350930.1135230.320116-0.1026541.0000000.0580830.1432520.0279980.225132
DASH0.122695-0.0121940.0886570.0039920.0580831.0000000.0266020.1215370.014571
SC0.1696420.0122530.021098-0.0080660.1432520.0266021.0000000.0479100.106153
XMR0.0872160.1294750.027649-0.1058980.0279980.1215370.0479101.0000000.016438
XEM0.0432050.1606670.101326-0.0809380.2251320.0145710.1061530.0164381.000000
def correlation_heatmap(df, title, absolute_bounds=True):
    '''Plot a correlation heatmap for the entire dataframe'''
    heatmap = go.Heatmap(
        z=df.corr(method='pearson').as_matrix(),
        x=df.columns,
        y=df.columns,
        colorbar=dict(title='Pearson Coefficient'),
    )
    
    layout = go.Layout(title=title)
    
    if absolute_bounds:
        heatmap['zmax'] = 1.0
        heatmap['zmin'] = -1.0
        
    fig = go.Figure(data=[heatmap], layout=layout)
    py.iplot(fig)

correlation_heatmap(combined_df_2016.pct_change(), "Cryptocurrency Correlations in 2016")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值