区块链不单单是一种技术,更是提供了一种服务模式和解决方案,为互联网产业的进一步发展起到了极其重要的推动作用。它的主要特点包括:去中心化、不可篡改、不依赖第三方机构、智能合约(本质上价值互联网)等,简单来说,就是一个商品或者证明,将这个产品进行数字化处理,然后进行哈希函数运算,同时标注时间,因为其单向不可逆性、防碰撞、加密速度快等特性,只要生成一个数值,标注上时间,就可以做到溯源。这些一旦和产业化力量结合,将深刻改变技术效率和安全性。
Perpetual contract is a new and unique contract.The goal of the contract is to copy the market situation of the spot market when the bar is high.The contract will not be handed over or cut,and can closely follow the reference price index through various mechanisms.
Perpetual contracts have the following five characteristics:
1.No delivery date
Perpetual contracts are not limited to time and have no delivery date.Traders can hold them for a long time to obtain greater investment returns.
2.Always anchor spot market prices
Another feature of the perpetual contract is that the transaction price always anchors the spot market price.The contract introduces the concept of spot price index,and through the corresponding mechanism,the price of the perpetual contract returns to the spot index price.Capital costs are an important means to ensure this goal.
Therefore,unlike traditional futures,the price of a perpetual contract will not deviate too much from the spot price most of the time.
3.Up to 100x flexible adjustable lever
Perpetual contracts provide up to 100 times of leverage.Traders can adjust flexibly after opening positions according to trading needs.The platform provides elastic risk protection while ensuring the best trading experience of traders.
4.Automatic position reduction mechanism ensures the interests of traders
We will adopt a complete position crossing mechanism rather than a risk sharing mechanism to ensure the interests of traders.This mechanism is used to determine who bears the compulsory position closing and effectively ensure that the interests of traders are protected from the huge losses caused by high-risk speculators.
5.Double price mechanism
The dual price mechanism is adopted,and the marked price is used as the trigger price for strong leveling.The marked price refers to the spot price of the global mainstream trading platform in real time.
huobipro=ccxt.huobipro({
'apiKey':'',
'secret':'',
})
先使用ccxt获取交易所的实例,然后获取历史k线,得到的数据使用dataframe格式接受
huobipro.fetch_ohlcv(symbol=symbol,limit=limit_num,timeframe=timeframe)
然后利用pandas提供的函数计算MA,
df['median_short']=df['close'].rolling(n_short,min_periods=1).mean()
df['median_long']=df['close'].rolling(n_long,min_periods=1).mean()
然后再找出买入卖出信号,
#找出买入信号
condition1=df['median_short']>df['median_long']#短均线上穿长均线
condition2=df['median_short'].shift(1)<=df['median_long'].shift(1)
df.loc[condition1&condition2,'signal']=1#产生买入信号的k线标记为1
#找出卖出信号
condition1=df['median_short']<df['median_long']#短均线上穿长均线
condition2=df['median_short'].shift(1)>=df['median_long'].shift(1)
df.loc[condition1&condition2,'signal']=0#产生卖出信号的k线标记为0
有了交易信号,就可以获取信号,再判断进行下单(huobipro.create_limit_buy/sell_order()了)
第五步:其实第四步就可以交易了,第五步是回测,一般来说先回测再根据回测结果选用策略,最后才进行实盘
回测分析的相关有很多种,在这方面我也不是很懂,目前我还是习惯用累积利润来进行分析。
#由signal计算出实际的每天持仓
df['pos']=df['signal'].shift()
df['pos'].fillna(method='ffill',inplace=True)
df['pos'].fillna(value=0,inplace=True)
到这里持仓信号就有了,就可以根据持仓和历史k线的价格计算累计利润了,
df['change']=df['close'].pct_change(1)#根据收盘价计算涨跌幅
df['by_at_open_change']=df['close']/df['open']-1#开盘买入到收盘的涨跌幅
df['sell_next_open_change']=df['open'].shift(-1)/df['close']-1#这根收盘到下根开盘的涨跌幅
df.at-Domain Name For Sale|Dan.com[len(df)-1,'sell_next_open_change']=0#补全空值df.at[4,'B']
#选取开仓条件
condition1=df['pos']==1
condition2=df['pos']!=df['pos'].shift(1)
open_pos_condition=condition1&condition2
#选取平仓条件
condition1=df['pos']==0
condition2=df['pos']!=df['pos'].shift(1)
close_pos_condition=condition1&condition2
#对每次交易进行分组
df.loc[open_pos_condition,'start_time']=df['open_time']
df['start_time'].fillna(method='ffill',inplace=True)
df.loc[df['pos']==0,'start_time']=pd.NaT
init_cash=1000#初始资金
#计算仓位变动
#开仓时仓位
df.loc[open_pos_condition,'position']=init_cash*(1+df['by_at_open_change'])
group_num=len(df.groupby('start_time'))
if group_num>1:
temp=df.groupby('start_time').apply(lambda x:x['close']/x.iloc0*x.iloc0)
temp=temp.reset_index(level=[0])
df['position']=temp['close']
df['position_max']=df['position']*df['high']/df['close']
df['position_min']=df['position']*df['low']/df['close']
##平仓时的仓位
#df.loc[close_pos_condition,'position']*=(1+df.loc[close_pos_condition,'sell_next_open_change'])
#计算持仓利润
df['porfit']=(df['position']-init_cash)*df['pos']#持仓利润或亏损
#df.loc[df['pos']==1,'porfit_min']=(df['position_min']-init_cash)*df['pos']#最小持仓盈利或亏损
#df.loc[df['pos']==0,'porfit_max']=(df['position_max']-init_cash)*df['pos']
#计算实际资金量
df['cash']=init_cash+df['porfit']#实际资金
#计算资金曲线
df['equity_change']=df['cash'].pct_change()
#开仓日收益率
df.loc[open_pos_condition,'equity_change']=df.loc[open_pos_condition,'cash']/init_cash-1
df['equity_change'].fillna(value=0,inplace=True)
df['equity_curve']=(1+df['equity_change']).cumprod()
df['equity_curve']=df['equity_curve']*init_cash