首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在增加索引值时,我在哪里犯了逻辑错误?

在增加索引值时,我在哪里犯了逻辑错误?
EN

Stack Overflow用户
提问于 2021-09-24 20:54:39
回答 1查看 50关注 0票数 0

我正在开发通过扫描加密货币的条形值来测试策略的软件。当我想要的情况发生时,该软件通过一个接一个地读取条形图来模拟职位的打开。索引值表示我当时正在读取的条形图。但作为测试的结果,我意识到在我打开酒吧后,这个位置忽略了酒吧。这就是为什么我得到了错误的结果。

在我打开一个职位后,忽略下一个酒吧的原因是什么?你能看到我在哪里犯了错吗?

代码:

代码语言:javascript
复制
ps_file = "files/ps-" + symbol.lower() + ".csv"

ops.run_strategy(datetime, math, time, csv, ps_file, symbol, candlesticks, candlesticks_len,
                         close_prices, rr_ratio, value, ema, macd, signal, rsi, upper, lower)

函数

代码语言:javascript
复制
def run_strategy(datetime, math, time, csv, file_name, symbol, candlesticks, candlesticks_len,
                 close_prices, rr_ratio, xbar, ema, macd, signal, rsi, upper, lower):
    index = 0

    while index < candlesticks_len:

        direction = get_direction("rsi", index, close_prices, ema, macd, signal, rsi)

        if index == 0:
            clear_file(file_name)

        check_allowed = is_allowed(direction, index, close_prices, candlesticks, macd, signal, rsi, upper, lower)

        if direction == "buy" and check_allowed == True:
            index = open_position(datetime, math, time, csv, file_name, symbol, direction, index, candlesticks,
                                  close_prices, rr_ratio, xbar)

        if direction == "sell" and check_allowed == True:
            index = open_position(datetime, math, time, csv, file_name, symbol, direction, index, candlesticks,
                                  close_prices, rr_ratio, xbar)

        index += 1

def open_position(datetime, math, time, csv, file_name, symbol, direction, index, candlesticks, close_prices,
                  rr_ratio, xbar):
    ts = int(candlesticks[index][0])
    dt_obj = datetime.fromtimestamp(ts / 1000)

    stop_loss = calc_stop_loss("xbar", direction, float(close_prices[index]), index, candlesticks, xbar)

    take_profit = calc_take_profit(direction, float(close_prices[index]), stop_loss, rr_ratio)

    leverage = calc_leverage(math, time, direction, float(close_prices[index]), stop_loss)

    save_position(csv, file_name, symbol, candlesticks[index][0], dt_obj, index, direction, leverage,
                  close_prices[index], take_profit, stop_loss, "e")

    index += 1

    while True:

        try:
            if direction == "buy":

                if float(candlesticks[index][3]) <= stop_loss:
                    pos_result = "LOST"
                    save_result(csv, file_name, pos_result)
                    return index

                if float(candlesticks[index][2]) >= take_profit:
                    pos_result = "WON"
                    save_result(csv, file_name, pos_result)
                    return index

            if index < len(candlesticks) - 1:
                index += 1
            else:
                return index

            if direction == "sell":

                if float(candlesticks[index][2]) >= stop_loss:
                    pos_result = "LOST"
                    save_result(csv, file_name, pos_result)
                    return index

                if float(candlesticks[index][3]) <= take_profit:
                    pos_result = "WON"
                    save_result(csv, file_name, pos_result)
                    return index

            if index < len(candlesticks) - 1:
                index += 1
            else:
                return index

        except Exception as e:
            return index

csv文件的第10行示例。index变量指示我们读取的是哪一行

代码语言:javascript
复制
1631836800000,3.4311,3.4348,3.4128,3.4185,21701,1631837099999,74267.7900,412,11886,40673.5353,0
1631837100000,3.4191,3.4232,3.4122,3.4150,12947,1631837399999,44237.8548,272,3766,12865.3188,0
1631837400000,3.4167,3.4490,3.4153,3.4328,50973,1631837699999,175120.1143,498,22415,76932.7275,0
1631837700000,3.4329,3.4334,3.4239,3.4255,16417,1631837999999,56271.7909,231,11105,38067.8802,0
1631838000000,3.4243,3.4270,3.4159,3.4160,11105,1631838299999,37977.3639,174,4705,16092.3815,0
1631838300000,3.4160,3.4160,3.3871,3.3871,38991,1631838599999,132518.2692,369,3073,10450.5525,0
1631838600000,3.3872,3.4027,3.3852,3.3899,24696,1631838899999,83753.1764,311,11916,40391.6030,0
1631838900000,3.3907,3.3957,3.3861,3.3878,10555,1631839199999,35781.3135,213,5688,19278.9597,0
1631839200000,3.3872,3.3898,3.3758,3.3879,27350,1631839499999,92532.8729,296,20280,68618.0346,0
1631839500000,3.3886,3.3974,3.3858,3.3923,26058,1631839799999,88381.9005,236,15578,52830.4907,0

行中的值表示下列值

代码语言:javascript
复制
[
  [
    1499040000000,      // Open time
    "0.01634790",       // Open
    "0.80000000",       // High
    "0.01575800",       // Low
    "0.01577100",       // Close
    "148976.11427815",  // Volume
    1499644799999,      // Close time
    "2434.19055334",    // Quote asset volume
    308,                // Number of trades
    "1756.87402397",    // Taker buy base asset volume
    "28.46694368",      // Taker buy quote asset volume
    "17928899.62484339" // Ignore.
  ]
]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-24 21:22:24

在没有运行任何代码或调试的情况下,冒着得到大量减号的风险,我看到的是这种情况:

代码语言:javascript
复制
if index < len(candlesticks) - 1:

它以相同的缩进存在两次。当它的计算结果为true时,它会将index递增两次。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69321001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档