Warning pandas, 2 loopings!

1

I have a problem with this looping, because it is a situation that I can not solve with np.where , why have to run DataFrame to find the value of the gain or loss from another condition, whichever comes first .

It's working but I'm getting this warning:

C:\Users\Jair\Anaconda3\lib\site-packages\ipykernel_launcher.py:14: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
def estrategia_2_alta(candles):

    for i in range(1,len(candles)):
        if candles['Twintower'][i]== 1:
            loop2 = i
            for twin in range(loop2+1, len(candles)):
                if candles['askclose'][twin]>=candles['Gain'][i]:

                    candles['Resultado'][i] = candles['Tamanho_Twin'][i]*GainC
                    break

                elif candles['askhigh'][twin]>=candles['Gain'][i]:

                    candles['Resultado'][i] = candles['Tamanho_Twin'][i]*GainC
                    break

                elif candles['askclose'][twin]<=candles['Loss'][i]:
                    candles['Resultado'][i] = candles['Tamanho_Twin'][i]*(-StopC)
                    break

                elif candles['asklow'][twin]<=candles['Loss'][i]:

                    candles['Resultado'][i] = candles['Tamanho_Twin'][i]*(-StopC)
                    break

    
asked by anonymous 22.12.2018 / 02:16

1 answer

0

Attempts to change the commands within the second if and elifs to:

candles.loc[i, 'Resultado'] = candles.loc[i, 'Tamanho_Twin']*GainC
break

(this would be for the first if internal; change the elifs following the same pattern)

Edit1:

df = pd.DataFrame({'askopen'         : np.random.sample(10) + 1,
                   'askhigh'         : np.random.sample(10) + 1,
                   'asklow'          : np.random.sample(10) + 1,
                   'askclose'        : np.random.sample(10) + 1,
                   'fechamento'      : np.random.choice(['alta','baixa'], 10),
                   'tamanho_candle'  : np.random.sample(10)/100 - 0.002,
                   'sombra'          : np.random.sample(10)/100 - 0.002,
                   'twintower'       : np.random.sample(10)/100 - 0.002,
                   'tamanho_twin'    : np.random.sample(10)/100 - 0.002,
                   'gain'            : np.full(10, np.nan),
                   'loss'            : np.full(10, np.nan),
                   'resultado'       : np.full(10, np.nan),
                   'soma_cumulativa' : np.full(10, np.nan),
                   'MA20'            : np.full(10, np.nan),
                   'MA200'           : np.full(10, np.nan),
                   })

df.index = pd.date_range('2017-05-26 00:00:00', freq='H', periods=10)


def estrategia_2_alta(candles):

    N = len(candles)

    GainC = 1.2
    StopC = 1.2

    for i in range(1,N):
        if candles['twintower'][i]== 1:

            for twin in range(i+1, N):
                if candles['askclose'][twin]>=candles['gain'][i]:

                    candles['resultado'][i] = candles['tamanho_twin'][i]*GainC
                    break

                elif candles['askhigh'][twin]>=candles['gain'][i]:

                    candles['resultado'][i] = candles['tamanho_twin'][i]*GainC
                    break

                elif candles['askclose'][twin]<=candles['loss'][i]:
                    candles['resultado'][i] = candles['tamanho_twin'][i]*(-StopC)
                    break

                elif candles['asklow'][twin]<=candles['loss'][i]:

                    candles['resultado'][i] = candles['tamanho_twin'][i]*(-StopC)
                    break

    return candles



x = estrategia_2_alta(df)  # para mim, roda ser dar nenhum Warning. No entanto nada é alterado no DataFrame.

Nothing happened since I initialized the twintower columns with NaN's, so the code does not go into the first if . Could you put an example of your DataFrame before executing the function? (without the columns being filled with NaN)

    
22.12.2018 / 02:33