简体   繁体   English

如何将在函数外部、循环中定义的变量输入到函数中?

[英]How do you input variables defined outside of a function, in a loop, into a function?

I'm backtesting some trading strategies in pandas and I defined the function:我正在回测熊猫中的一些交易策略,并定义了函数:

def TakeOutDuplicates(DataFrame):
    Buy = []
    Sell = []
    flag = -1
    for i in range(len(DataFrame)-1):
        if DataFrame[ (takeOutThisStrategys[x][Time][j+1]) ][i] == "Buy":
            Sell.append(np.nan)
            if flag != 1:
                Buy.append(DataFrame["Open"][i+1])
                flag=1
            else: 
                Buy.append(np.nan)
        elif DataFrame[ (takeOutThisStrategys[x][Time][j+1]) ][i] == "Sell":
            Buy.append(np.nan)
            if flag != 0:
                Sell.append(DataFrame["Open"][i+1])
                flag=0
            else: 
                Sell.append(np.nan)
        else:
            Buy.append(np.nan)
            Sell.append(np.nan)
    return(Buy, Sell)

Which searches through specific columns in my dataframe and reduces the buy and sell signals into a series so I can analyse what prices it bought at and what prices it sold at, rather than seeing it told me to buy 30 times in a row, it just records the buy price at the first time it said buy, and then says the sell price at the time it said to sell.它在我的数据框中搜索特定列并将买卖信号减少到一个系列中,这样我就可以分析它以什么价格购买以及以什么价格出售,而不是看到它告诉我连续购买 30 次,它只是记录第一次说买入时的买入价,然后记录说卖出时的卖出价。

The loop I'm running to call this function is:我正在运行以调用此函数的循环是:

for Time in TimePeriods:  # TimePeriods = [5, 15, 60, 240, 1440] (in minutes)
    for j in range(savedi-1):      # savedi = 15
        takeOutThisStrategys[x][Time][j+1] = f"Strategy {j+1} on {Time} Mins"
        replaces[x][Time][j+1] = TakeOutDuplicates(df)

As you can see I want to populate my dictionary replaces with the buy and sell prices as returned from the function TakeOutDuplicates.如您所见,我想用 TakeOutDuplicates 函数返回的买入和卖出价格填充我的字典。 Except when I run this and check my dictionary like the following:除非我运行它并检查我的字典,如下所示:

Checking the 5 minute timeframe:检查 5 分钟的时间范围:

replaces[x][5]

{1: ([14185.11,
   nan,
   nan,
   14185.03,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   14121.72,...])}

Now checking the 240 min (4 hour) time frame.现在检查 240 分钟(4 小时)的时间范围。

replaces[x][240]

{1: ([14185.11,
   nan,
   nan,
   14185.03,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   nan,
   14121.72,...])}

As you can see all the buy and sell signals are the same, which is not how it should be, implying that when i run my loop, it's not taking the variables for Time and j and then inputting them into my function, as I would have expected.正如你所看到的,所有的买入和卖出信号都是相同的,这不是它应该的样子,这意味着当我运行我的循环时,它没有像我那样获取 Time 和 j 的变量,然后将它们输入到我的函数中已经预料到。 How can I solve this issue?我该如何解决这个问题?

I managed to fix this issue, but the problem was in the fact that when I defined my strategies I forgot to multiply by my time periods:我设法解决了这个问题,但问题在于当我定义我的策略时,我忘记乘以我的时间段:

So I had this:所以我有这个:

df.loc[( (df[str(length*1) + " MA"] > df[str(l*1) + " MA"]) & (df["Close"] > df[str(l*1) + " MA"])), f"Strategy {i} on {Time} Mins"] = "Buy"

Instead of this:取而代之的是:

df.loc[( (df[str(length*Time) + " MA"] > df[str(l*Time) + " MA"]) & (df["Close"] > df[str(l*Time) + " MA"])), f"Strategy {i} on {Time} Mins"] = "Buy"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM