价格范围内的网格数量及其宽度
所以我正在尝试创建一个类似于 Binance 交易网格的结构:
我的尝试:
grid_start = 2.5
grid_gap = 5
total_grids = 10
grids = 0
buy_grid_start = grid_start
buy_grids = []
# CALCULATE GRID
while True:
if grids == total_grids:
break
else:
grids += 1
buy_grid_start += (buy_grid_start * grid_gap / 100)
buy_grids.append(f"{buy_grid_start:0.3f}")
print(buy_grids)
print(f"Total grids: {grids}")
这只给了我 10 个网格,每个网格比前一个网格(价格)高 5%。
我的问题是如何获得网格结构,例如您可以在其中输入较低价格和较高价格以及网格数量 - 这将显示每个网格之间的百分比?
ATTEMPT 2:
import math
grid_start = 1.1
grid_end = 2.2
total_grids = 10
grids = 0
buy_grid_start = grid_start
buy_grid_end = grid_end
ratio = math.pow(buy_grid_start / buy_grid_end, 1 / total_grids) * 100
buy_grids = []
# CALCULATE GRID
while True:
if grids == total_grids:
break
else:
grids += 1
buy_grid_end == buy_grid_start * math.pow(ratio, grids)
buy_grids.append(f"{buy_grid_start:0.3f}")
print(buy_grids)
print(f"Total grids: {grids}")
输出:
['1.100', '1.100', '1.100', '1.100', '1.100', '1.100', '1.100', '1.100', '1.100', '1.100']
Total grids: 10
So I'm trying to create a Binance trading grid-like structure:
My attempt:
grid_start = 2.5
grid_gap = 5
total_grids = 10
grids = 0
buy_grid_start = grid_start
buy_grids = []
# CALCULATE GRID
while True:
if grids == total_grids:
break
else:
grids += 1
buy_grid_start += (buy_grid_start * grid_gap / 100)
buy_grids.append(f"{buy_grid_start:0.3f}")
print(buy_grids)
print(f"Total grids: {grids}")
This just gives me 10 grids, each grid 5% above the previous grid (price).
My question is how do I get a grid structure like where you can enter Lower Price and Upper Price, and the number of grids - which will show you the percentage between each grid?
ATTEMPT 2:
import math
grid_start = 1.1
grid_end = 2.2
total_grids = 10
grids = 0
buy_grid_start = grid_start
buy_grid_end = grid_end
ratio = math.pow(buy_grid_start / buy_grid_end, 1 / total_grids) * 100
buy_grids = []
# CALCULATE GRID
while True:
if grids == total_grids:
break
else:
grids += 1
buy_grid_end == buy_grid_start * math.pow(ratio, grids)
buy_grids.append(f"{buy_grid_start:0.3f}")
print(buy_grids)
print(f"Total grids: {grids}")
Output:
['1.100', '1.100', '1.100', '1.100', '1.100', '1.100', '1.100', '1.100', '1.100', '1.100']
Total grids: 10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数学。
对于网格之间的线性间距
所需的相等:
解决方案:
根据您的使用方式,可能需要将
total_grids
替换为(total_grids - 1)
以上。对于网格之间的指数(“比率”)间距
也就是说,每个网格的价格乘以前一个网格价格的比率。或者换句话说,从 lower_price 开始,反复乘以比率。
期望的相等:
解决方案:
期望比率略大于1。例如
1.05
代表每次增加5%
。对于百分比形式的比率:
要从比率中删除“1”,以显示增加,对于百分比形式:
根据需要舍入。
注意:与线性情况一样,您可能需要在公式中使用
(total_grids - 1)
。Math.
FOR LINEAR SPACING BETWEEN GRIDS
Desired equality:
Solution:
Depending on how you use it, might need to replace
total_grids
with(total_grids - 1)
in the above.FOR EXPONENTIAL ("RATIO") SPACING BETWEEN GRIDS
That is, each grid's price is multiplied by a RATIO to the previous grid's price. Or to say it another way, starting with lower_price, multiply by ratio repeatedly.
Desired equality:
Solution:
Expect ratio to be slightly greater than 1. E.g.
1.05
represents an increase by5%
each time.For ratio as a percent:
To remove "1" from ratio, to show increase, as a percent:
Round as desired.
NOTE: As in the Linear case, you might need
(total_grids - 1)
in formula.