使用 Pandas 计算的价格指标在哪里应该是 'put'?
Where is a price indicator calculated using Pandas supposed to be 'put'?
首先感谢您对像我这样的新人的所有爱。
现在开始编写代码。
我有一段看似简单的代码,有人写了这段代码来计算价格的移动平均线,但要理解它并遵循它,我需要回答一个关于 where[ 的定义的问题=27=].
def ma(Data, lookback, what, where):
for i in range(len(Data)):
try:
Data[i, where] = (Data[i - lookback + 1:i + 1, what].mean())
except IndexError:
pass
return Data
#1 The Data variable is the OHLC array
#2 The what variable is the closing price column
#3 The where variable is where to put the indicator
#4 The lookback variable is the moving average's period
但是说到#3,我不明白。 将指标放在哪里是什么意思?我想计算数据,并使用该数据。我不想把指标放在任何地方,只是为了参考。
这是我想用作数据的数据框:
open high low close
hr
2021-02-01 01:00 32835.839519 33438.836060 32835.839519 33389.974454
2021-02-01 02:00 33400.422170 33663.000232 33260.343246 33569.816646
2021-02-01 03:00 33581.206956 33633.454861 33437.081539 33466.178320
2021-02-01 04:00 33473.881292 33680.926267 33473.881292 33512.310031
2021-02-01 05:00 33496.550836 33692.662741 33442.771302 33692.295982
2021-02-01 06:00 33674.363254 33931.304243 33372.640454 33372.640454
2021-02-01 07:00 33466.277250 33718.499099 33410.173005 33543.580113
2021-02-01 08:00 33691.622420 34155.668712 33577.491467 34155.668712
2021-02-01 09:00 34541.989582 34541.989582 34246.591606 34255.313422
2021-02-01 10:00 34254.311054 34318.042807 34009.930110 34043.516502
2021-02-01 11:00 34102.209535 34181.236131 33960.033182 34181.236131
2021-02-01 12:00 34196.688991 34196.688991 33677.872302 33677.872302
2021-02-01 13:00 33658.073761 33831.043558 33616.263612 33616.263612
2021-02-01 14:00 33506.144521 33698.288934 33298.355558 33458.379708
2021-02-01 15:00 33449.472954 33540.119075 33137.424801 33137.424801
2021-02-01 16:00 33129.661923 33444.487621 33129.661923 33444.487621
2021-02-01 17:00 33427.510914 33966.758995 33328.452664 33966.758995
2021-02-01 18:00 33883.647812 33883.647812 33692.664177 33753.657270
2021-02-01 19:00 33568.840250 33904.003721 33568.840250 33817.161851
2021-02-01 20:00 33716.826906 33857.417730 33663.339958 33757.230256
2021-02-01 21:00 33608.964863 33692.950108 33508.713475 33553.086495
2021-02-01 22:00 33475.494546 33629.985564 33423.225607 33423.225607
2021-02-01 23:00 33396.069559 33614.188165 33361.882597 33405.990357
2021-02-02 00:00 33423.242711 33912.467287 33423.242711 33626.927090
2021-02-02 01:00 33578.032588 33578.032588 33470.631802 33470.631802
where
指的是存储结果的列名。在 pandas 中,您通常不会 return 来自函数的列表,而只是将 column/Series 添加到 DataFrame 中,下游函数可以根据需要使用它们。
在本例中,我将结果保存到 where='close-3-day-avg'
列。请注意,我对函数 ma
进行了一些修改,因为我无法使其以以下形式工作:
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 2000)
def ma(Data, lookback, what, where):
Data[where] = 0.0
for i in range(len(Data)):
Data[where].loc[i] = Data[what].loc[i - lookback + 1:i].mean()
return Data
data = """ open high low close
hr
2021-02-01 01:00 32835.839519 33438.836060 32835.839519 33389.974454
2021-02-01 02:00 33400.422170 33663.000232 33260.343246 33569.816646
2021-02-01 03:00 33581.206956 33633.454861 33437.081539 33466.178320
2021-02-01 04:00 33473.881292 33680.926267 33473.881292 33512.310031
2021-02-01 05:00 33496.550836 33692.662741 33442.771302 33692.295982
2021-02-01 06:00 33674.363254 33931.304243 33372.640454 33372.640454
2021-02-01 07:00 33466.277250 33718.499099 33410.173005 33543.580113
2021-02-01 08:00 33691.622420 34155.668712 33577.491467 34155.668712
2021-02-01 09:00 34541.989582 34541.989582 34246.591606 34255.313422
2021-02-01 10:00 34254.311054 34318.042807 34009.930110 34043.516502
2021-02-01 11:00 34102.209535 34181.236131 33960.033182 34181.236131
2021-02-01 12:00 34196.688991 34196.688991 33677.872302 33677.872302
2021-02-01 13:00 33658.073761 33831.043558 33616.263612 33616.263612
2021-02-01 14:00 33506.144521 33698.288934 33298.355558 33458.379708
2021-02-01 15:00 33449.472954 33540.119075 33137.424801 33137.424801
2021-02-01 16:00 33129.661923 33444.487621 33129.661923 33444.487621
2021-02-01 17:00 33427.510914 33966.758995 33328.452664 33966.758995
2021-02-01 18:00 33883.647812 33883.647812 33692.664177 33753.657270
2021-02-01 19:00 33568.840250 33904.003721 33568.840250 33817.161851
2021-02-01 20:00 33716.826906 33857.417730 33663.339958 33757.230256
2021-02-01 21:00 33608.964863 33692.950108 33508.713475 33553.086495
2021-02-01 22:00 33475.494546 33629.985564 33423.225607 33423.225607
2021-02-01 23:00 33396.069559 33614.188165 33361.882597 33405.990357
2021-02-02 00:00 33423.242711 33912.467287 33423.242711 33626.927090
2021-02-02 01:00 33578.032588 33578.032588 33470.631802 33470.631802"""
rows = [line.split(' ') for line in data.split('\n')[2:]]
df = pd.DataFrame(rows, columns=['hr', 'open', 'high', 'low', 'close']) # .set_index('hr')
df[['open', 'high', 'low', 'close']] = df[['open', 'high', 'low', 'close']].apply(pd.to_numeric)
print(ma(df, lookback=3, what='close', where='close-3-day-avg'))
结果(注意结果在新列中):
hr open high low close close-3-day-avg
0 2021-02-01 01:00 32835.839519 33438.836060 32835.839519 33389.974454 33389.974454
1 2021-02-01 02:00 33400.422170 33663.000232 33260.343246 33569.816646 33479.895550
2 2021-02-01 03:00 33581.206956 33633.454861 33437.081539 33466.178320 33475.323140
3 2021-02-01 04:00 33473.881292 33680.926267 33473.881292 33512.310031 33516.101666
4 2021-02-01 05:00 33496.550836 33692.662741 33442.771302 33692.295982 33556.928111
5 2021-02-01 06:00 33674.363254 33931.304243 33372.640454 33372.640454 33525.748822
6 2021-02-01 07:00 33466.277250 33718.499099 33410.173005 33543.580113 33536.172183
7 2021-02-01 08:00 33691.622420 34155.668712 33577.491467 34155.668712 33690.629760
8 2021-02-01 09:00 34541.989582 34541.989582 34246.591606 34255.313422 33984.854082
9 2021-02-01 10:00 34254.311054 34318.042807 34009.930110 34043.516502 34151.499545
10 2021-02-01 11:00 34102.209535 34181.236131 33960.033182 34181.236131 34160.022018
11 2021-02-01 12:00 34196.688991 34196.688991 33677.872302 33677.872302 33967.541645
12 2021-02-01 13:00 33658.073761 33831.043558 33616.263612 33616.263612 33825.124015
13 2021-02-01 14:00 33506.144521 33698.288934 33298.355558 33458.379708 33584.171874
14 2021-02-01 15:00 33449.472954 33540.119075 33137.424801 33137.424801 33404.022707
15 2021-02-01 16:00 33129.661923 33444.487621 33129.661923 33444.487621 33346.764043
16 2021-02-01 17:00 33427.510914 33966.758995 33328.452664 33966.758995 33516.223806
17 2021-02-01 18:00 33883.647812 33883.647812 33692.664177 33753.657270 33721.634629
18 2021-02-01 19:00 33568.840250 33904.003721 33568.840250 33817.161851 33845.859372
19 2021-02-01 20:00 33716.826906 33857.417730 33663.339958 33757.230256 33776.016459
20 2021-02-01 21:00 33608.964863 33692.950108 33508.713475 33553.086495 33709.159534
21 2021-02-01 22:00 33475.494546 33629.985564 33423.225607 33423.225607 33577.847453
22 2021-02-01 23:00 33396.069559 33614.188165 33361.882597 33405.990357 33460.767486
23 2021-02-02 00:00 33423.242711 33912.467287 33423.242711 33626.927090 33485.381018
24 2021-02-02 01:00 33578.032588 33578.032588 33470.631802 33470.631802 33501.183083
首先感谢您对像我这样的新人的所有爱。
现在开始编写代码。
我有一段看似简单的代码,有人写了这段代码来计算价格的移动平均线,但要理解它并遵循它,我需要回答一个关于 where[ 的定义的问题=27=].
def ma(Data, lookback, what, where):
for i in range(len(Data)):
try:
Data[i, where] = (Data[i - lookback + 1:i + 1, what].mean())
except IndexError:
pass
return Data
#1 The Data variable is the OHLC array
#2 The what variable is the closing price column
#3 The where variable is where to put the indicator
#4 The lookback variable is the moving average's period
但是说到#3,我不明白。 将指标放在哪里是什么意思?我想计算数据,并使用该数据。我不想把指标放在任何地方,只是为了参考。
这是我想用作数据的数据框:
open high low close
hr
2021-02-01 01:00 32835.839519 33438.836060 32835.839519 33389.974454
2021-02-01 02:00 33400.422170 33663.000232 33260.343246 33569.816646
2021-02-01 03:00 33581.206956 33633.454861 33437.081539 33466.178320
2021-02-01 04:00 33473.881292 33680.926267 33473.881292 33512.310031
2021-02-01 05:00 33496.550836 33692.662741 33442.771302 33692.295982
2021-02-01 06:00 33674.363254 33931.304243 33372.640454 33372.640454
2021-02-01 07:00 33466.277250 33718.499099 33410.173005 33543.580113
2021-02-01 08:00 33691.622420 34155.668712 33577.491467 34155.668712
2021-02-01 09:00 34541.989582 34541.989582 34246.591606 34255.313422
2021-02-01 10:00 34254.311054 34318.042807 34009.930110 34043.516502
2021-02-01 11:00 34102.209535 34181.236131 33960.033182 34181.236131
2021-02-01 12:00 34196.688991 34196.688991 33677.872302 33677.872302
2021-02-01 13:00 33658.073761 33831.043558 33616.263612 33616.263612
2021-02-01 14:00 33506.144521 33698.288934 33298.355558 33458.379708
2021-02-01 15:00 33449.472954 33540.119075 33137.424801 33137.424801
2021-02-01 16:00 33129.661923 33444.487621 33129.661923 33444.487621
2021-02-01 17:00 33427.510914 33966.758995 33328.452664 33966.758995
2021-02-01 18:00 33883.647812 33883.647812 33692.664177 33753.657270
2021-02-01 19:00 33568.840250 33904.003721 33568.840250 33817.161851
2021-02-01 20:00 33716.826906 33857.417730 33663.339958 33757.230256
2021-02-01 21:00 33608.964863 33692.950108 33508.713475 33553.086495
2021-02-01 22:00 33475.494546 33629.985564 33423.225607 33423.225607
2021-02-01 23:00 33396.069559 33614.188165 33361.882597 33405.990357
2021-02-02 00:00 33423.242711 33912.467287 33423.242711 33626.927090
2021-02-02 01:00 33578.032588 33578.032588 33470.631802 33470.631802
where
指的是存储结果的列名。在 pandas 中,您通常不会 return 来自函数的列表,而只是将 column/Series 添加到 DataFrame 中,下游函数可以根据需要使用它们。
在本例中,我将结果保存到 where='close-3-day-avg'
列。请注意,我对函数 ma
进行了一些修改,因为我无法使其以以下形式工作:
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 2000)
def ma(Data, lookback, what, where):
Data[where] = 0.0
for i in range(len(Data)):
Data[where].loc[i] = Data[what].loc[i - lookback + 1:i].mean()
return Data
data = """ open high low close
hr
2021-02-01 01:00 32835.839519 33438.836060 32835.839519 33389.974454
2021-02-01 02:00 33400.422170 33663.000232 33260.343246 33569.816646
2021-02-01 03:00 33581.206956 33633.454861 33437.081539 33466.178320
2021-02-01 04:00 33473.881292 33680.926267 33473.881292 33512.310031
2021-02-01 05:00 33496.550836 33692.662741 33442.771302 33692.295982
2021-02-01 06:00 33674.363254 33931.304243 33372.640454 33372.640454
2021-02-01 07:00 33466.277250 33718.499099 33410.173005 33543.580113
2021-02-01 08:00 33691.622420 34155.668712 33577.491467 34155.668712
2021-02-01 09:00 34541.989582 34541.989582 34246.591606 34255.313422
2021-02-01 10:00 34254.311054 34318.042807 34009.930110 34043.516502
2021-02-01 11:00 34102.209535 34181.236131 33960.033182 34181.236131
2021-02-01 12:00 34196.688991 34196.688991 33677.872302 33677.872302
2021-02-01 13:00 33658.073761 33831.043558 33616.263612 33616.263612
2021-02-01 14:00 33506.144521 33698.288934 33298.355558 33458.379708
2021-02-01 15:00 33449.472954 33540.119075 33137.424801 33137.424801
2021-02-01 16:00 33129.661923 33444.487621 33129.661923 33444.487621
2021-02-01 17:00 33427.510914 33966.758995 33328.452664 33966.758995
2021-02-01 18:00 33883.647812 33883.647812 33692.664177 33753.657270
2021-02-01 19:00 33568.840250 33904.003721 33568.840250 33817.161851
2021-02-01 20:00 33716.826906 33857.417730 33663.339958 33757.230256
2021-02-01 21:00 33608.964863 33692.950108 33508.713475 33553.086495
2021-02-01 22:00 33475.494546 33629.985564 33423.225607 33423.225607
2021-02-01 23:00 33396.069559 33614.188165 33361.882597 33405.990357
2021-02-02 00:00 33423.242711 33912.467287 33423.242711 33626.927090
2021-02-02 01:00 33578.032588 33578.032588 33470.631802 33470.631802"""
rows = [line.split(' ') for line in data.split('\n')[2:]]
df = pd.DataFrame(rows, columns=['hr', 'open', 'high', 'low', 'close']) # .set_index('hr')
df[['open', 'high', 'low', 'close']] = df[['open', 'high', 'low', 'close']].apply(pd.to_numeric)
print(ma(df, lookback=3, what='close', where='close-3-day-avg'))
结果(注意结果在新列中):
hr open high low close close-3-day-avg
0 2021-02-01 01:00 32835.839519 33438.836060 32835.839519 33389.974454 33389.974454
1 2021-02-01 02:00 33400.422170 33663.000232 33260.343246 33569.816646 33479.895550
2 2021-02-01 03:00 33581.206956 33633.454861 33437.081539 33466.178320 33475.323140
3 2021-02-01 04:00 33473.881292 33680.926267 33473.881292 33512.310031 33516.101666
4 2021-02-01 05:00 33496.550836 33692.662741 33442.771302 33692.295982 33556.928111
5 2021-02-01 06:00 33674.363254 33931.304243 33372.640454 33372.640454 33525.748822
6 2021-02-01 07:00 33466.277250 33718.499099 33410.173005 33543.580113 33536.172183
7 2021-02-01 08:00 33691.622420 34155.668712 33577.491467 34155.668712 33690.629760
8 2021-02-01 09:00 34541.989582 34541.989582 34246.591606 34255.313422 33984.854082
9 2021-02-01 10:00 34254.311054 34318.042807 34009.930110 34043.516502 34151.499545
10 2021-02-01 11:00 34102.209535 34181.236131 33960.033182 34181.236131 34160.022018
11 2021-02-01 12:00 34196.688991 34196.688991 33677.872302 33677.872302 33967.541645
12 2021-02-01 13:00 33658.073761 33831.043558 33616.263612 33616.263612 33825.124015
13 2021-02-01 14:00 33506.144521 33698.288934 33298.355558 33458.379708 33584.171874
14 2021-02-01 15:00 33449.472954 33540.119075 33137.424801 33137.424801 33404.022707
15 2021-02-01 16:00 33129.661923 33444.487621 33129.661923 33444.487621 33346.764043
16 2021-02-01 17:00 33427.510914 33966.758995 33328.452664 33966.758995 33516.223806
17 2021-02-01 18:00 33883.647812 33883.647812 33692.664177 33753.657270 33721.634629
18 2021-02-01 19:00 33568.840250 33904.003721 33568.840250 33817.161851 33845.859372
19 2021-02-01 20:00 33716.826906 33857.417730 33663.339958 33757.230256 33776.016459
20 2021-02-01 21:00 33608.964863 33692.950108 33508.713475 33553.086495 33709.159534
21 2021-02-01 22:00 33475.494546 33629.985564 33423.225607 33423.225607 33577.847453
22 2021-02-01 23:00 33396.069559 33614.188165 33361.882597 33405.990357 33460.767486
23 2021-02-02 00:00 33423.242711 33912.467287 33423.242711 33626.927090 33485.381018
24 2021-02-02 01:00 33578.032588 33578.032588 33470.631802 33470.631802 33501.183083