价格范围内的网格数量及其宽度

发布于 2025-01-17 15:28:16 字数 1355 浏览 3 评论 0原文

所以我正在尝试创建一个类似于 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?

See example:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和我恋爱吧 2025-01-24 15:28:16

数学。


对于网格之间的线性间距

所需的相等:

grid_end == grid_start + (total_grids * grid_gap)

解决方案:

grid_gap = (grid_end - grid_start) / total_grids

根据您的使用方式,可能需要将 total_grids 替换为 (total_grids - 1)以上。


对于网格之间的指数(“比率”)间距

也就是说,每个网格的价格乘以前一个网格价格的比率。或者换句话说,从 lower_price 开始,反复乘以比率。

期望的相等:

upper_price == lower_price * math.pow(ratio, total_grids)

解决方案:

ratio = math.pow(upper_price / lower_price, 1 / total_grids)

期望比率略大于1。例如1.05代表每次增加5%

对于百分比形式的比率:

ratio * 100   // %

要从比率中删除“1”,以显示增加,对于百分比形式:

grid_gap = (ratio - 1) * 100   // %

根据需要舍入。

注意:与线性情况一样,您可能需要在公式中使用 (total_grids - 1)

Math.


FOR LINEAR SPACING BETWEEN GRIDS

Desired equality:

grid_end == grid_start + (total_grids * grid_gap)

Solution:

grid_gap = (grid_end - grid_start) / total_grids

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:

upper_price == lower_price * math.pow(ratio, total_grids)

Solution:

ratio = math.pow(upper_price / lower_price, 1 / total_grids)

Expect ratio to be slightly greater than 1. E.g. 1.05 represents an increase by 5% each time.

For ratio as a percent:

ratio * 100   // %

To remove "1" from ratio, to show increase, as a percent:

grid_gap = (ratio - 1) * 100   // %

Round as desired.

NOTE: As in the Linear case, you might need (total_grids - 1) in formula.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文