这是数据帧并使用它我正在进行逻辑操作,抛出错误如下。我怎样才能克服这个问题?
Traceback (most recent call last):
in <module>
if( eoddf['High'][Open] > linebreakvalue):
File "", line , in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
逻辑低于表格:
eoddf :(数据帧)
Symbol Series Prev Close Open High Low Last Close \
Date
2015-01-01 SBIN EQ 311.85 312.45 315.00 310.70 314.00 314.00
2015-01-02 SBIN EQ 314.00 314.35 318.30 314.35 315.60 315.25
2015-01-05 SBIN EQ 315.25 316.25 316.80 312.10 312.80 312.75
2015-01-06 SBIN EQ 312.75 310.00 311.10 298.70 299.90 299.90
2015-01-07 SBIN EQ 299.90 300.00 302.55 295.15 301.40 300.15
2015-01-08 SBIN EQ 300.15 305.00 306.50 302.35 305.25 304.85
2015-01-09 SBIN EQ 304.85 306.70 307.85 302.00 303.00 303.20
2015-01-12 SBIN EQ 303.20 304.15 307.80 301.10 306.90 307.10
2015-01-13 SBIN EQ 307.10 308.15 310.75 304.15 305.25 305.10
2015-01-14 SBIN EQ 305.10 304.00 307.00 302.25 305.00 304.70
2015-01-15 SBIN EQ 304.70 319.90 323.70 314.00 318.40 320.30
2015-01-16 SBIN EQ 320.30 320.00 320.30 313.10 315.25 315.45
2015-01-19 SBIN EQ 315.45 316.55 317.95 312.50 313.20 313.15
2015-01-20 SBIN EQ 313.15 314.00 319.80 314.00 318.00 318.15
2015-01-21 SBIN EQ 318.15 319.90 327.60 319.00 326.00 326.20
2015-01-22 SBIN EQ 326.20 326.90 327.60 321.80 325.20 324.65
2015-01-23 SBIN EQ 324.65 328.25 332.55 324.65 327.05 327.45
2015-01-27 SBIN EQ 327.45 329.40 332.15 322.60 331.45 330.05
2015-01-28 SBIN EQ 330.05 330.40 336.00 328.20 333.40 334.60
代码:
for Open in range(1, len(eoddf)):
if( eoddf['High'][Open] > linebreakvalue):
eoddf['LBHigh']{Open]=eoddf['High'][Open]
eoddf['LBLow'][Open] =eoddf['Low'][Open]
linebreakvalue = eoddf['LBHigh'][Open]
if( eoddf['Low'][Open] < linebreakvalue):
eoddf['LBHigh'][Open]=eoddf['Low'][Open]
eoddf['LBLow'][Open] =eoddf['High'][Open]
linebreakvalue = eoddf['LBLow'][Open]
我正在尝试将此MQ4代码写入Python以满足我的需要。代码参考位于:http://www.35pip.com/metatrader-indicators/indicators/3LineBreak.mq4
我还想运行这个循环,直到Open列的最后一个条目为止,或者在这里可以使用其他方法吗?
基本上我正在尝试将这段代码转换为Python
OLDSwing=Swing;
VALUE1=High[Highest(NULL,0,MODE_HIGH,Lines_Break,shift+1)];
VALUE2= Low[Lowest(NULL,0,MODE_LOW,Lines_Break,shift+1)];
if (OLDSwing==1 && Low[shift]<VALUE2) Swing=-1;
if (OLDSwing==-1 && High[shift]>VALUE1 ) Swing=1;
if (Swing==1)
{ HighBuffer[shift]=High[shift]; LowBuffer[shift]=Low[shift]; }
if (Swing==-1)
{ LowBuffer[shift]=High[shift]; HighBuffer[shift]=Low[shift]; }
这个规则是:
这样做的最佳方式是什么?
答案 0 :(得分:3)
这就是你所寻求的:
eoddf['LBHigh'] = df.apply(lambda x: x['High'] if x['High'] > linebreakvalue else x['Low'], axis=1)
eoddf['LBLow'] = df.apply(lambda x: x['Low'] if x['Low'] < linebreakvalue else x['High'], axis=1)
答案 1 :(得分:2)
我相信你需要加倍numpy.where
,但如果相等则条件返回第二个值:
linebreakvalue = 320
m1 = eoddf['High'] > linebreakvalue
m2 = eoddf['Low'] > linebreakvalue
eoddf['LBHigh']= np.where(m1, eoddf['High'], eoddf['Low'])
eoddf['LBLow'] = np.where(m2, eoddf['Low'], eoddf['High'])
print (eoddf)
Symbol Series Prev Close Open High Low Last Close \
2015-01-01 SBIN EQ 311.85 312.45 315.00 310.70 314.00 314.00
2015-01-02 SBIN EQ 314.00 314.35 318.30 314.35 315.60 315.25
2015-01-05 SBIN EQ 315.25 316.25 316.80 312.10 312.80 312.75
2015-01-06 SBIN EQ 312.75 310.00 311.10 298.70 299.90 299.90
2015-01-07 SBIN EQ 299.90 300.00 302.55 295.15 301.40 300.15
2015-01-08 SBIN EQ 300.15 305.00 306.50 302.35 305.25 304.85
2015-01-09 SBIN EQ 304.85 306.70 307.85 302.00 303.00 303.20
2015-01-12 SBIN EQ 303.20 304.15 307.80 301.10 306.90 307.10
2015-01-13 SBIN EQ 307.10 308.15 310.75 304.15 305.25 305.10
2015-01-14 SBIN EQ 305.10 304.00 307.00 302.25 305.00 304.70
2015-01-15 SBIN EQ 304.70 319.90 323.70 314.00 318.40 320.30
2015-01-16 SBIN EQ 320.30 320.00 320.30 313.10 315.25 315.45
2015-01-19 SBIN EQ 315.45 316.55 317.95 312.50 313.20 313.15
2015-01-20 SBIN EQ 313.15 314.00 319.80 314.00 318.00 318.15
2015-01-21 SBIN EQ 318.15 319.90 327.60 319.00 326.00 326.20
2015-01-22 SBIN EQ 326.20 326.90 327.60 321.80 325.20 324.65
2015-01-23 SBIN EQ 324.65 328.25 332.55 324.65 327.05 327.45
2015-01-27 SBIN EQ 327.45 329.40 332.15 322.60 331.45 330.05
2015-01-28 SBIN EQ 330.05 330.40 336.00 328.20 333.40 334.60
LBHigh LBLow
2015-01-01 310.70 315.00
2015-01-02 314.35 318.30
2015-01-05 312.10 316.80
2015-01-06 298.70 311.10
2015-01-07 295.15 302.55
2015-01-08 302.35 306.50
2015-01-09 302.00 307.85
2015-01-12 301.10 307.80
2015-01-13 304.15 310.75
2015-01-14 302.25 307.00
2015-01-15 323.70 323.70
2015-01-16 320.30 320.30
2015-01-19 312.50 317.95
2015-01-20 314.00 319.80
2015-01-21 327.60 327.60
2015-01-22 327.60 321.80
2015-01-23 332.55 324.65
2015-01-27 332.15 322.60
2015-01-28 336.00 328.20
但如果确实需要循环:
for i, x in eoddf.iterrows():
if(eoddf.loc[i, 'High'] > linebreakvalue):
eoddf.loc[i, 'LBHigh']=eoddf.loc[i,'High']
eoddf.loc[i,'LBLow'] =eoddf.loc[i,'Low']
if(eoddf.loc[i,'Low'] < linebreakvalue):
eoddf.loc[i,'LBHigh']=eoddf.loc[i,'Low']
eoddf.loc[i,'LBLow'] =eoddf.loc[i,'High']