mercredi 28 juillet 2021

if in pandas data frame : TypeError: '<' not supported between instances of 'NoneType' and 'int'

I am lost in some simple pandas op.

I am getting the error: TypeError: '<' not supported between instances of 'NoneType' and 'int'

When evauating the following statement:

self.df.sma_s.iloc[-1] < self.df.sma_l.iloc[-1]

The weird thing is that my dataframe actually contains floats in those fiels.

Trying to debug, I did the following:

    self.df['sma_s'] = self.df.close.rolling(sma_s).mean()
    self.df['sma_l'] = self.df.close.rolling(sma_l).mean()

    self.current_position = self.check_positions()

    print('gonna check for trades')

    print(self.df.sma_s.iloc[-1])
    print(type(self.df.sma_s.iloc[-1]))

    print(self.df.sma_l.iloc[-1])
    print(type(self.df.sma_l.iloc[-1]))

    print('='*50)
    print(self.df.tail(5))

    over = self.df.sma_s.iloc[-1] > self.df.sma_l.iloc[-1]
    print(f'Over: {over}')

    under = self.df.sma_s.iloc[-1] < self.df.sma_l.iloc[-1]
    print(f'Under: {under}')

    if over and np.sign(self.current_position) != 1:
        self.close_position()
        trade = self.buy(self.qty)
        
    elif under and np.sign(self.current_position) != -1:
        self.close_position()
        trade = self.sell(self.qty)

The results are:

gonna check for trades
 1.1847966666666665 
<class 'numpy.float64'>
 1.1848311111111112 
<class 'numpy.float64'>
 
  date                      sma_s     sma_l   
2021-07-28 16:23:00-04:00  1.184852  1.184856
2021-07-28 16:24:00-04:00  1.184860  1.184863
2021-07-28 16:25:00-04:00  1.184847  1.184853
2021-07-28 16:26:00-04:00  1.184820  1.184837
2021-07-28 16:27:00-04:00  1.184797  1.184831
Over: False 
Under: True

But it still gives me the error:

TypeError: '<' not supported between instances of 'NoneType' and 'int'
    176 
--> 177         elif under and np.sign(self.current_position) != -1:
    178             self.close_position()
    179             trade = self.sell(self.qty)

TypeError: '<' not supported between instances of 'NoneType' and 'int'

How come, when I ask it to print,self.df.sma_s.iloc[-1] is a float64 and self.df.sma_s.iloc[-1] > self.df.sma_l.iloc[-1] it is a boolean, but when I evaluate it inside the "if" I get that error? It does not make any sense to me! any clues?

Thanks!!

Aucun commentaire:

Enregistrer un commentaire