永续合约及秒合约交易所开发源码逻辑丨永续合约及秒合约交易所系统开发(开发规则)

简介:  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 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

相关文章
|
5月前
|
存储 算法 区块链
合约跟单/永续合约/秒合约交易所系统开发详细逻辑丨源码说明
  基于区块链技术的智能合约不仅可以发挥智能合约在成本效率方面的优势,而且可以避免恶意行为对合约正常执行的干扰。将智能合约以数字化的形式写入区块链中,由区块链技术的特性保障存储、读取、执行整个过程透明可跟踪、不可攥改。同时,由区块链自带的共识算法构建出一套状态机系统,使得智能合约能够高效地运行。
|
7月前
|
安全 区块链
合约跟单/合约交易/量化交易/永续合约/秒合约/系统开发详情案例
区块链的最重要特性是去中心化,它不依赖于任何中心机构或第三方信任。
|
供应链 区块链 数据库
永续合约交易所系统开发|秒合约系统开发案例
区块链与普通数据库的主要区别在于,有关于如何将数据放入数据库,有一些特定规则。
|
供应链 安全 区块链
永续合约丨合约交易丨秒合约丨币币合约交易所系统开发|规则方案
区块链采用分布式记账模式,无论是登记结算场景下的实时对账能力,还是数据存证场景下的不可篡改能力
|
存储 算法 区块链
秒合约合约跟单永续合约交易所系统开发方案
区块链有两个最核心的地方,一个是分布式,一个是公开,这也就是我们今天一直强调的区块链是一种去中心化的技术
|
存储 供应链 安全
秒合约|现货合约|合约跟单系统开发(源码部署)
Web3.0的优势主要在于其将数据的存储和交互方式进行了彻底的改进
|
数据采集 算法 机器人
量化交易/合约交易/永续合约/秒合约/合约跟单系统开发方案项目/案例设计/源码版
量化交易/合约交易/永续合约/秒合约/合约跟单系统开发方案项目/案例设计/源码版
|
存储 算法 安全
币币交易所/永续合约/秒合约/合约交易/合约跟单系统开发功能与指南
因为各种原因,在我们的社会中存在侵权的事件不在少数
|
存储 区块链 人工智能
|
存储 JavaScript 前端开发