币圈搬砖,从入门到实战(六)三角搬砖

144
提问者
2023-03-15 16:06 悬赏 0财富值 阅读 780回答 1

三角套利三角搬砖,指在一个交易所内三种币之间的搬砖。举例说明:在币安交易所,既有EOS/BTC交易对,也有EOS/ETH交易对。当EOS/BTC的价格(折合人民

默认分类
登录 后发表回答
1楼 · 2023-03-15 16:36.采纳回答

三角套利

三角搬砖,指在一个交易所内三种币之间的搬砖。

举例说明:

在币安交易所,既有EOS/BTC交易对,也有EOS/ETH交易对。

当EOS/BTC的价格(折合人民币的价格)大于EOS/ETH的价格(折合人民币的价格)时,假设:EOS/BTC 的价格是 41元,EOS/ETH 的价格是 40元。

经过下面三步操作即可实现三角套利:


  1. 用ETH以40元的价格买入10个EOS,价值400元;
  2. 将10个EOS以41元的价格换成BTC,得到价值410元的BTC;
  3. 将410元的BTC换成ETH,得到价值410元的ETH,盈利10元。

三角套利需要注意的点:


  1. 一定要考虑手续费,因为一共有3次交易;
  2. 要将价格全部换算成统一的单位,可以是RMB,也可以是BTC或者ETH。
自动三角套利

把上述的步骤写成程序,让程序不休不眠的帮你套利。这个程序主要有两方面的内容:


  1. 查价
  2. 交易

同样,简易的模型代码很简单,但是要做到稳定盈利也不是那么容易,需要考虑很多方面的内容。下面贴一段我之前写的查价的代码,作为写代码的战5渣,大家随意感受一下就好了,你写的肯定比我的好。

知乎编写的不太好看哈

from binance.client import Client

api_key = ""
api_secret = ""

client = Client(api_key, api_secret)

coin = "XRP" def check(): # 获取X-ETH数量、价格
x_eth_depth = client.get_order_book(symbol= coin+'ETH')

# asks 要卖的人 我们对他的买 需要低价 【买入】
x_eth_price = x_eth_depth["asks"][0][0]
x_eth_num = x_eth_depth["asks"][0][1]

# print("X-ETH价格:{}".format(x_eth_price)) # print("X-ETH数量:{}".format(x_eth_num)) # 获取X-BTC数量、价格
x_btc_depth = client.get_order_book(symbol= coin+'BTC')

# bids 要买的人 我们对他是卖 需要高价 【卖出】
x_btc_price = x_btc_depth["bids"][0][0]
x_btc_num = x_btc_depth["bids"][0][1]

# print("X-BTC价格:{}".format(x_btc_price)) # print("X-BTC数量:{}".format(x_btc_num)) # 获取 ETH - BTC数量、价格, ETH 需要买入 【买入】
eth_btc_depth = client.get_order_book(symbol="ETHBTC")

eth_btc_price = eth_btc_depth["asks"][0][0]
eth_btc_num = eth_btc_depth["asks"][0][1]

# print("ETH-BTC价格.{}".format(eth_btc_price)) # print("ETH-BTC数量.{}".format(eth_btc_num)) # 比较差价
x_btc_price_2 = float(x_eth_price) * float(eth_btc_price)

diff_price = float(x_btc_price) - x_btc_price_2
# print(diff_price) # print(x_btc_price_2)

diff_price_rate = diff_price/float(x_btc_price)

# print(diff_price_rate) if(diff_price_rate > 0.005):
# print("有差价啦:{}".format(diff_price))
print("情况1价差:{:.2%}".format(diff_price_rate))
add_record(diff_price_rate)



# 另一种方向 # 获取X-ETH数量、价格
x_eth_depth = client.get_order_book(symbol= coin+'ETH')

# 【卖出】
x_eth_price = x_eth_depth["bids"][0][0]
x_eth_num = x_eth_depth["bids"][0][1]

# print("X-ETH价格:{}".format(x_eth_price)) # print("X-ETH数量:{}".format(x_eth_num)) # 获取X-BTC数量、价格
x_btc_depth = client.get_order_book(symbol= coin+'BTC')

# 【买入】
x_btc_price = x_btc_depth["asks"][0][0]
x_btc_num = x_btc_depth["asks"][0][1]

# print("X-BTC价格:{}".format(x_btc_price)) # print("X-BTC数量:{}".format(x_btc_num)) # 【卖出】
eth_btc_depth = client.get_order_book(symbol="ETHBTC")

eth_btc_price = eth_btc_depth["bids"][0][0]
eth_btc_num = eth_btc_depth["bids"][0][1]

# print("ETH-BTC价格.{}".format(eth_btc_price)) # print("ETH-BTC数量.{}".format(eth_btc_num)) # 比较差价
x_btc_price_2 = float(x_eth_price) * float(eth_btc_price)

diff_price2 = x_btc_price_2 - float(x_btc_price)

diff_price2_rate = diff_price2/x_btc_price_2

if diff_price2_rate > 0.005:
# print("有差价啦:{}".format(diff_price))
print("情况2价差:{:.2%}".format(diff_price2_rate))
add_record(diff_price2_rate)


# 计算数量 #交易 if __name__ == '__main__':
while(1):
try:
check()
# print("hello") except Exception as e:
# print(e) pass



作者:唐晓阳

链接:https://www.jianshu.com/p/14dbaa02777a

來源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。