比特币搬砖对冲策略Python源码
策略复制地址:https://www.fmz.com/strategy/21023
策略原理
比特币搬砖策略是入门程序化交易的基础策略。原理简单,是新手尝试程序化的好选择,在其黄金时期,比特币搬砖也带来大量的利润。掌握此策略需要有一定的基础。
长期来看,两个交易所比特币的差价应该稳定的,如果存在足够的差价,我们便可以在价格低的交易所买入币,在价格高的交易所卖出币。这样一来两个交易所持有的币总量没变,但价值却增加了。这就是搬砖的基础原理。
策略参数:
主要设置差价和操作量,其中差价设为交互模式

策略源码:
import time
import json def cancelAll():
ret = False
for e in exchanges:
while True:
n = 0
for order in _C(e.GetOrders):
ret = True
e.CancelOrder(order.Id)
n+=1
if n == 0:
break
return ret def main():
global MinSpreadA, MinSpreadB
SetErrorFilter("canceled")
if len(exchanges) != 2:
raise Exception("只支持两个交易所对冲") LogReset()
LogProfitReset()
cancelAll() initStocks = 0.0
initBalance = 0.0
minAmount = 0.1
lastTradeTime = 0
lastTradeErrExchange = ''
accountsCache = []
hedgeNum = [0, 0]
names = []
baseCurrency = exchange.GetCurrency()
for e in exchanges:
if e.GetCurrency() != baseCurrency:
raise Exception("必须是同样的货币才可以对冲 " + baseCurrency)
names.append(e.GetName())
account = _C(e.GetAccount)
accountsCache.append(account)
initStocks += account.Stocks
initBalance += account.Balance
Log("Switch", e.GetLabel(), "To", e.IO("websocket"))
minAmount = 0.01 if baseCurrency == "BTC" else 0.1
Log("总钱:", _N(initBalance), "总币", _N(initStocks), 'Python:', __import__('sys').version)
while True:
if not accountsCache:
accountsCache = [_C(e.GetAccount) for e in exchanges]
Sleep(LoopInterval)
cmd = GetCommand()
if cmd:
Log("CMD", cmd)
arr = cmd.split(':')
if arr[0] == 'A->B':
MinSpreadA = float(arr[1])
elif arr[0] == 'B->A':
MinSpreadB = float(arr[1]) depthA = exchanges[0].GetDepth()
if not depthA:
continue
depthB = exchanges[1].GetDepth()
if not depthB:
continue
if lastTradeTime > 0 and time.time() - lastTradeTime > BalanceTime:
needUpdate = cancelAll()
if not needUpdate:
for account in accountsCache:
if account.FrozenBalance >= 0.1 or account.FrozenStocks > 0.001:
needUpdate = True
break
if needUpdate:
accountsCache = [_C(e.GetAccount) for e in exchanges]
nowStocks = 0.0
nowBalance = 0.0
for account in accountsCache:
nowStocks += account.Stocks
nowBalance += account.Balance
diff = _N(nowStocks - initStocks, 5)
isReverse = None
if abs(diff) < minAmount:
LogProfit(_N(nowBalance-initBalance, 3), "总钱:", _N(nowBalance), "总币", _N(nowStocks), "币差:", diff)
lastTradeTime = 0
elif diff > minAmount:
isReverse = depthA.Bids[0].Price < depthB.Bids[0].Price
elif -diff > minAmount:
isReverse = depthA.Asks[0].Price > depthB.Asks[0].Price
if isReverse is not None:
depths = [depthA, depthB]
opAmount = None
for pos in ([1, 0] if isReverse else [0, 1]):
if diff >= minAmount:
opAmount = min(diff, accountsCache[pos].Stocks, depths[pos].Bids[0].Amount + depths[pos].Bids[1].Amount)
diff -= opAmount
if opAmount >= minAmount:
exchanges[pos].Sell(depths[pos].Bids[1].Price, opAmount)
elif -diff >= minAmount:
opAmount = min(-diff, _N(accountsCache[pos].Balance / depths[pos].Asks[1].Price, 3), depths[pos].Asks[0].Amount + depths[pos].Asks[1].Amount)
diff += opAmount
if opAmount >= minAmount:
exchanges[pos].Buy(depths[pos].Asks[1].Price, opAmount)
if opAmount is not None:
lastTradeTime = time.time()
accountsCache = []
continue
# end of balanceAccount diffA = _N(depthA.Bids[0].Price - depthB.Asks[0].Price, 3)
diffB = _N(depthB.Bids[0].Price - depthA.Asks[0].Price, 3)
LogStatus('`' + json.dumps({'type': 'table', 'title': '运行信息', 'cols': ['名称', '钱', '冻结的钱', '币', '冻结的币', '买一', '卖一', '阀值', '差价', '次数'], 'rows': [[names[0], accountsCache[0].Balance, accountsCache[0].FrozenBalance, accountsCache[0].Stocks, accountsCache[0].FrozenStocks, depthA.Bids[0].Price, depthA.Asks[0].Price, MinSpreadA, diffA, hedgeNum[0]], [names[1], accountsCache[1].Balance, accountsCache[1].FrozenBalance, accountsCache[1].Stocks, accountsCache[1].FrozenStocks, depthB.Bids[0].Price, depthB.Asks[0].Price, MinSpreadB, diffB, hedgeNum[0]]]}) + '`')
HPos = 0
if diffA >= MinSpreadA:
orderH = depthA.Bids[0]
orderL = depthB.Asks[0]
exchangeH = exchanges[0]
exchangeL = exchanges[1]
accountH = accountsCache[0]
accountL = accountsCache[1]
elif diffB >= MinSpreadB:
HPos = 1
orderH = depthB.Bids[0]
orderL = depthA.Asks[0]
exchangeH = exchanges[1]
exchangeL = exchanges[0]
accountH = accountsCache[1]
accountL = accountsCache[0]
else:
continue opPrice = _N((orderH.Price + orderL.Price) / 2.0, 2)
opAmount = min(MaxAmount, orderH.Amount, orderL.Amount, accountH.Stocks, _N(accountL.Balance / opPrice, 3))
if opAmount >= minAmount:
tasks = [[exchangeH.Sell, "H"], [exchangeL.Buy, "L"]]
if lastTradeErrExchange == "L":
tasks.reverse()
lastTradeErrExchange = ""
for task in tasks:
if task[0](opPrice, opAmount) is None:
lastTradeErrExchange = task[1]
break
lastTradeTime = time.time()
accountsCache = []
hedgeNum[HPos] += 1
比特币搬砖对冲策略Python源码的更多相关文章
- Python 源码剖析(六)【内存管理机制】
六.内存管理机制 1.内存管理架构 2.小块空间的内存池 3.循环引用的垃圾收集 4.python中的垃圾收集 1.内存管理架构 Python内存管理机制有两套实现,由编译符号PYMALLOC_DEB ...
- Omega System Trading and Development Club内部分享策略Easylanguage源码 (第二期)
更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流. 我们曾经在前文(链接),为大家分享我们精心整理的私货:"System Trading and ...
- 读python源码--对象模型
学python的人都知道,python中一切皆是对象,如class生成的对象是对象,class本身也是对象,int是对象,str是对象,dict是对象....所以,我很好奇,python是怎样实现这些 ...
- VS2013编译python源码
系统:win10 手头有个python模块,是用C写的,想编译安装就需要让python调用C编译器.直接编译发现使用的是vc9编译,不支持C99标准(两个槽点:为啥VS2008都还不支持C99?手头这 ...
- 三种排序算法python源码——冒泡排序、插入排序、选择排序
最近在学习python,用python实现几个简单的排序算法,一方面巩固一下数据结构的知识,另一方面加深一下python的简单语法. 冒泡排序算法的思路是对任意两个相邻的数据进行比较,每次将最小和最大 ...
- 《python源码剖析》笔记一——python编译
1.python的架构: 2.python源码的组织结构: 3.windows环境下编译python:
- 转换器5:参考Python源码,实现Php代码转Ast并直接运行
前两个周末写了<手写PHP转Python编译器>的词法,语法分析部分,上个周末卡文了. 访问器部分写了两次都不满意,没办法,只好停下来,参考一下Python的实现.我实现的部分正好和Pyt ...
- python源码书籍
<Python源码剖析>一书现在很难买到,目前大部分都是电子书. 为了更好地利用Python语言,无论是使用Python语言本身,还是将Python与C/C++交互使用,深刻理解Pytho ...
- 类似py2exe软件真的能保护python源码吗
类似py2exe软件真的能保护python源码吗 背景 最近写了个工具用于对项目中C/C++文件的字符串常量进行自动化加密处理,用python写的,工具效果不错,所以打算在公司内部推广.为了防止代码泄 ...
随机推荐
- ATcoder 1983 BBQ Hard
E - BBQ Hard Time limit : 2sec / Memory limit : 256MB Score : 1400 points Problem Statement Snuke is ...
- java基础编程题
1. 某公司每月标准上班时间是160小时,每小时工资是30元. 如果上班时间超出了160小时,超出部分每小时按1.5倍工资发放.请编写程序计算员工月工资. package com.num2.lianx ...
- Linux终端Shell下的常用快捷键收集
删除 [Ctrl]+[D]删除光标所在位置上的字符相当于VIM里x或者dl [Ctrl]+[H]删除光标所在位置前的字符相当于VIM里hx或者dh [Ctrl]+[K]删除光标后面所有字符相当于VIM ...
- 2016.3.15__H5页面实战__第七天
假设您认为这篇文章还不错,能够去H5专题介绍中查看很多其它相关文章. 个人简书地址: dhttp://www.jianshu.com/users/5a2fd0b8fb30/latest_article ...
- 破解电信光猫华为HG8120C关闭路由功能方法
昨天电信的工作人员来安装了电信的光纤宽带,使用的是华为HG8120C这款光电转换器与路由器一体机 这导致下级路由无法直接使用PPPOE拨号连接到互联网,且无法使用端口映射来实现外网访问 而华为开放给用 ...
- 【Todo】秒杀系统 & 乐观锁 & Nginx反向代理
http://www.csdn.net/article/2014-11-28/2822858 1. 单点帐号验证,不用读,而是用写入,Redis,看是否加watch 2. 抢宝的最终购买冲突.包装称“ ...
- 一个重绘MFC的文件传输client
一个重绘MFC的文件传输client,TCP/IP协议的文件传输client(支持上传.下载.续传.管理等功能,本处仅仅选择了上传功能).从用户视觉上看,肯定比原生MFC界面要有感觉,啥也不说了 ...
- Pacemaker 安装与使用
Pacemaker 仅仅做资源管理器(CRM).底下的消息系统採用 corosync. 安装 以 ubuntu 为例, sudo aptitude install -y pacemaker coros ...
- ACM之数论数字根
先来看一道杭电的数字根问题 此题的大大意是输入一个数.假设它不是一位的数字的话,那么我们就将它的每一位都相加,相加后假设还是两位或者很多其它的话那么我们继续取出它的每一位数字进行相加.知道等到单个数字 ...
- sql server 笔记1--case、WAITFOR、TRY CATCH
一.case 转自:http://blog.csdn.net/add8849/article/details/576424 深入使用:http://blog.csdn.net/akuoma/artic ...