File size: 27,205 Bytes
4565b50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486

#import streamlit as st
import pandas as pd
from sklearn.ensemble import RandomForestRegressor

def rank( X, y):
    """
    Gets a rank of relation of features to target
    The output is a list of combination of features order by relevance by 'RF rank'
    
    :param X: Train data without target
    :param y: Target
    
    """

    X = X
    y = y
    # estimators
    rank = pd.DataFrame({'features': X.columns})


    rfr = RandomForestRegressor(n_jobs=-1, n_estimators=50, verbose=3)
    rfr.fit(X, y)
    rank['RFR'] = (rfr.feature_importances_ * 100)

    #print(rank.sort_values('RFR', ascending=False))

    # opções de listas de features selecionadas para cada estimador
    lista_comb_feat_RFR = []
    #for x in range(2, 11):
    for x in range(2, len(rank)):
        lista_comb_feat_RFR.append(rank.sort_values('RFR', ascending=False).head(x)['features'].tolist())

    return lista_comb_feat_RFR , rank.sort_values('RFR', ascending=False)

#UTILIZA LISTA COM NOMES ÚNICOS DAS AÇÕES, DATAFRAME DA B3, DADOS HISTÓRICOS E A TABELA DE INPUT COM UMA LINHA PARA CADA AÇÃO
def inputer_train(lista, df, data, df_filled):

    for i in range(len(lista)):
        if df.loc[(df['Código de Negociação'].str.contains(lista[i]))].sort_values('Tipo de Movimentação')[-1:]['Tipo de Movimentação'].item() == 'Venda':
            
            #Construção das variáveis
            preco_medio_compra = df.loc[(df['Código de Negociação'].str.contains(lista[i])) & (df['Tipo de Movimentação'] == 'Compra' )]['Preço'].mean()
            quantidade_comprada = df.loc[(df['Código de Negociação'].str.contains(lista[i])) & (df['Tipo de Movimentação'] == 'Compra' )]['Quantidade'].sum()
            preco_medio_vendido = df.loc[(df['Código de Negociação'].str.contains(lista[i])) & (df['Tipo de Movimentação'] == 'Venda' )]['Preço'].mean()
            quantidade_vendida = df.loc[(df['Código de Negociação'].str.contains(lista[i])) & (df['Tipo de Movimentação'] == 'Venda' )]['Quantidade'].sum()
            
            data_compra_1 = df.loc[(df['Código de Negociação'].str.contains(lista[i]))].sort_values('Data do Negócio')[-1:]['Data do Negócio'].item()
            Ganho_total = (preco_medio_vendido - preco_medio_compra) * quantidade_vendida
            rendimento_total = round(((preco_medio_vendido - preco_medio_compra) / preco_medio_vendido) * 100,2)
            
            #dados históricos ticker
            dados_acao = pd.DataFrame(data.loc[ : , (['Open','High','Low','Close','Adj Close','Volume'],lista[i]+".SA")])
            #dados_acao_filtrado = dados_acao.loc[dados_acao.index <= data_compra_1]
            dados_acao_filtrado = dados_acao.loc[dados_acao.index <= pd.to_datetime(data_compra_1)]               
            
            #RENDIMENTO ULTIMOS X DIAS (ONTEM X DIA COMPARADO)
            
            #valor da ultima cotação
            cotacao_last = dados_acao_filtrado['Close'][-1:][lista[i]+".SA"][0]
            #valor cotação x dias atras
            try:
                cotacao_7 = dados_acao_filtrado['Close'][-7:-6][lista[i]+".SA"][0]
                cotacao_14 = dados_acao_filtrado['Close'][-14:-13][lista[i]+".SA"][0]
                cotacao30 = dados_acao_filtrado['Close'][-30:-29][lista[i]+".SA"][0]
                cotacao_60 = dados_acao_filtrado['Close'][-60:-59][lista[i]+".SA"][0]
                cotacao_90 = dados_acao_filtrado['Close'][-90:-89][lista[i]+".SA"][0]
                #% da queda ou aumento ultimos x dias
                crescimento_7 = round(((cotacao_last - cotacao_7) / cotacao_7) * 100,2)
                crescimento_14 = round(((cotacao_last - cotacao_14) / cotacao_14) * 100,2)
                crescimento_30 = round(((cotacao_last - cotacao30) / cotacao30) * 100,2)
                crescimento_60 = round(((cotacao_last - cotacao_60) / cotacao_60) * 100,2)
                crescimento_90 = round(((cotacao_last - cotacao_90) / cotacao_90) * 100,2)
            
            except:
                exit

            #CRESCIMENTO VOLUME ULTIMOS X DIAS (ONTEM X DIA COMPARADO)
            
            #volume do dia anterior a compra
            volume_last = dados_acao_filtrado['Volume'][-1:][lista[i]+".SA"][0]
            #valor cotação x dias atras
            try:
                volume_7 = dados_acao_filtrado['Volume'][-7:-6][lista[i]+".SA"][0]
                volume_14 = dados_acao_filtrado['Volume'][-14:-13][lista[i]+".SA"][0]
                volume30 = dados_acao_filtrado['Volume'][-30:-29][lista[i]+".SA"][0]
                volume_60 = dados_acao_filtrado['Volume'][-60:-59][lista[i]+".SA"][0]
                volume_90 = dados_acao_filtrado['Volume'][-90:-89][lista[i]+".SA"][0]
                #% da queda ou aumento ultimos x dias
                crescimento_vol_7 = round(((volume_last - volume_7) / volume_7) * 100,2)
                crescimento_vol_14 = round(((volume_last - volume_14) / volume_14) * 100,2)
                crescimento_vol_30 = round(((volume_last - volume30) / volume30) * 100,2)
                crescimento_vol_60 = round(((volume_last - volume_60) / volume_60) * 100,2)
                crescimento_vol_90 = round(((volume_last - volume_90) / volume_90) * 100,2)
            
            except:
                exit
                
            #RSI
            try:
                delta = dados_acao_filtrado['Close'][-90:].diff()
                up, down = delta.copy(), delta.copy()
                up[up < 0] = 0
                down[down > 0] = 0
                period = 14
                rUp = up.ewm(com=period - 1,  adjust=False).mean()
                rDown = down.ewm(com=period - 1, adjust=False).mean().abs()
                delta['RSI'] = 100 - 100 / (1 + rUp / rDown).fillna(0)
                
                rsi_0 = delta['RSI'][-1:][0]
                rsi_7 = delta['RSI'][-7:-6][0]
                rsi_14 = delta['RSI'][-14:-13][0]
                rsi_30 = delta['RSI'][-30:-29][0]
                rsi_60 = delta['RSI'][-60:-59][0]

                #% da queda ou aumento ultimos x dias
                cresc_rsi_7 = round(((rsi_0 - rsi_7) / rsi_7) * 100,2)
                cresc_rsi_14 = round(((rsi_0 - rsi_14) / rsi_14) * 100,2)
                cresc_rsi_30 = round(((rsi_0 - rsi_30) / rsi_30) * 100,2)
                cresc_rsi_60 = round(((rsi_0 - rsi_60) / rsi_60) * 100,2)

            except:
                exit
                
            #BOLINGER
            
            try:
                bolinger = dados_acao_filtrado.copy()
                bolinger['MA20'] = dados_acao_filtrado['Close'].rolling(20).mean()
                bolinger['20 Day STD'] = bolinger['Close'].rolling(window=20).std()
                bolinger['Upper Band'] = bolinger['MA20'] + (bolinger['20 Day STD'] * 2)
                bolinger['Lower Band'] = bolinger['MA20'] - (bolinger['20 Day STD'] * 2)

                boolinger_up_0 = bolinger['Upper Band'][-1:][0]
                boolinger_down_0 = bolinger['Lower Band'][-1:][0]
                boolinger_up_7 = bolinger['Upper Band'][-7:-6][0]
                boolinger_down_7 = bolinger['Lower Band'][-7:-6][0]

                delta_bolinger_0 = round((boolinger_up_0 - boolinger_down_0) / boolinger_down_0 * 100,2)
                cresc_bolinger_up_7 = round((boolinger_up_0 - boolinger_up_7) / boolinger_up_7 * 100,2)
                cresc_bolinger_down_7 = round((boolinger_down_0 - boolinger_down_7) / boolinger_down_7 * 100,2)
                
            except:
                exit
                
            
            #MÉDIAS MOVEIS
            try:
                time = dados_acao_filtrado.copy()
                rolling_9  = time['Close'].rolling(window=9)
                rolling_mean_9 = rolling_9.mean().round(1)

                rolling_20  = time['Close'].rolling(window=20)
                rolling_mean_20 = rolling_20.mean().round(1)

                rolling_72  = time['Close'].rolling(window=72)
                rolling_mean_72 = rolling_72.mean().round(1)
                time['MM9'] = rolling_mean_9.fillna(0)
                time['MM20'] = rolling_mean_20.fillna(0)
                time['MM72'] = rolling_mean_72.fillna(0)
                time['cruzamento'] =  time['MM9'] - time['MM72']
                buy = time.tail(50).loc[(time.tail(50)['cruzamento']==0)]

                if buy.empty == False:
                    cruzou_mm = 1
                else:
                    cruzou_mm = 0         

                if time['MM72'].iloc[-1] < time['MM9'].iloc[-1]:
                    direcao_cruzada_cima = 1
                else:
                    direcao_cruzada_cima = 0
                    

                mm9_0 = time['MM9'][-1:][0]
                mm9_7 = time['MM9'][-7:-6][0]
                mm9_14 = time['MM9'][-14:-13][0]
                mm9_30 = time['MM9'][-30:-29][0]
                mm9_60 = time['MM9'][-60:-59][0]
                
                mm20_0 = time['MM20'][-1:][0]
                mm20_7 = time['MM20'][-7:-6][0]
                mm20_14 = time['MM20'][-14:-13][0]
                mm20_30 = time['MM20'][-30:-29][0]
                mm20_60 = time['MM20'][-60:-59][0]
    
                mm72_0 = time['MM72'][-1:][0]
                mm72_7 = time['MM72'][-7:-6][0]
                mm72_14 = time['MM72'][-14:-13][0]
                mm72_30 = time['MM72'][-30:-29][0]
                mm72_60 = time['MM72'][-60:-59][0]
                
                #% da queda ou aumento ultimos x dias
                cresc_mm9_7 = round(((mm9_0 - mm9_7) / mm9_7) * 100,2)
                cresc_mm9_14 = round(((mm9_0 - mm9_14) / mm9_14) * 100,2)
                cresc_mm9_30 = round(((mm9_0 - mm9_30) / mm9_30) * 100,2)
                cresc_mm9_60 = round(((mm9_0 - mm9_60) / mm9_60) * 100,2)
                
                #% da queda ou aumento ultimos x dias
                cresc_mm20_7 = round(((mm20_0 - mm20_7) / mm20_7) * 100,2)
                cresc_mm20_14 = round(((mm20_0 - mm20_14) / mm20_14) * 100,2)
                cresc_mm20_30 = round(((mm20_0 - mm20_30) / mm20_30) * 100,2)
                cresc_mm20_60 = round(((mm20_0 - mm20_60) / mm20_60) * 100,2)
                
                #% da queda ou aumento ultimos x dias
                cresc_mm72_7 = round(((mm72_0 - mm72_7) / mm72_7) * 100,2)
                cresc_mm72_14 = round(((mm72_0 - mm72_14) / mm72_14) * 100,2)
                cresc_mm72_30 = round(((mm72_0 - mm72_30) / mm72_30) * 100,2)
                cresc_mm72_60 = round(((mm72_0 - mm72_60) / mm72_60) * 100,2)
                
            except:
                exit
                
            #try:
            #    pfizer = yf.Ticker(lista[i])
            #    info = pfizer.info 

            #    setor = info['sector']
            #    atividade = info['industry']
                
            #except:
            #    exit

            try:
            #Atribuições
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'data_compra_1'] = data_compra_1
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'Preço_médio_comprado'] = preco_medio_compra
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'Preço_médio_vendido'] = preco_medio_vendido
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'Ganho_total'] = Ganho_total

                #df_filled.loc[df_filled['name'].str.contains(lista[i]),'Setor'] = setor
                #df_filled.loc[df_filled['name'].str.contains(lista[i]),'Atividade'] = atividade

                df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_total_%'] = rendimento_total
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_ultimos_7_dias'] = crescimento_7
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_ultimos_14_dias'] = crescimento_14
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_ultimos_30_dias'] = crescimento_30
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_ultimos_60_dias'] = crescimento_60
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_ultimos_90_dias'] = crescimento_90

                df_filled.loc[df_filled['name'].str.contains(lista[i]),'crescimento_vol_ultimos_7_dias'] = crescimento_vol_7
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'crescimento_vol_ultimos_14_dias'] = crescimento_vol_14
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'crescimento_vol_ultimos_30_dias'] = crescimento_vol_30
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'crescimento_vol_ultimos_60_dias'] = crescimento_vol_60
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'crescimento_vol_ultimos_90_dias'] = crescimento_vol_90

                df_filled.loc[df_filled['name'].str.contains(lista[i]),'rsi'] = rsi_0
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_rsi_ultimos_7_dias'] = cresc_rsi_7
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_rsi_ultimos_14_dias'] = cresc_rsi_14
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_rsi_ultimos_30_dias'] = cresc_rsi_30
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_rsi_ultimos_60_dias'] = cresc_rsi_60

                df_filled.loc[df_filled['name'].str.contains(lista[i]),'delta_bolinger_0'] = delta_bolinger_0
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_bolinger_up_7'] = cresc_bolinger_up_7
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_bolinger_down_7'] = cresc_bolinger_down_7

                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cruzou_mm'] = cruzou_mm
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'direcao_cruzada_mm_cima'] = direcao_cruzada_cima

                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm9_ultimos_7_dias'] = cresc_mm9_7
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm9_ultimos_14_dias'] = cresc_mm9_14
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm9_ultimos_30_dias'] = cresc_mm9_30
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm9_ultimos_60_dias'] = cresc_mm9_60

                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm20_ultimos_7_dias'] = cresc_mm20_7
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm20_ultimos_14_dias'] = cresc_mm20_14
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm20_ultimos_30_dias'] = cresc_mm20_30
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm20_ultimos_60_dias'] = cresc_mm20_60        

                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm72_ultimos_7_dias'] = cresc_mm72_7
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm72_ultimos_14_dias'] = cresc_mm72_14
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm72_ultimos_30_dias'] = cresc_mm72_30
                df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm72_ultimos_60_dias'] = cresc_mm72_60
            
            except:
                exit

def inputer_predict(data, df_filled):

    lista = list(df_filled['name'])
    for i in range(len(df_filled)):
        dados_acao = pd.DataFrame(data.loc[ : , (['Open','High','Low','Close','Adj Close','Volume'],lista[i]+".SA")])
        dados_acao_filtrado = dados_acao.copy()
        #RENDIMENTO ULTIMOS X DIAS (ONTEM X DIA COMPARADO)
        #valor da ultima cotação
        cotacao_last = dados_acao_filtrado['Close'][-1:][lista[i]+".SA"][0]
        #valor cotação x dias atras
        try:
            cotacao_7 = dados_acao_filtrado['Close'][-7:-6][lista[i]+".SA"][0]
            cotacao_14 = dados_acao_filtrado['Close'][-14:-13][lista[i]+".SA"][0]
            cotacao30 = dados_acao_filtrado['Close'][-30:-29][lista[i]+".SA"][0]
            cotacao_60 = dados_acao_filtrado['Close'][-60:-59][lista[i]+".SA"][0]
            cotacao_90 = dados_acao_filtrado['Close'][-90:-89][lista[i]+".SA"][0]
            #% da queda ou aumento ultimos x dias
            crescimento_7 = round(((cotacao_last - cotacao_7) / cotacao_7) * 100,2)
            crescimento_14 = round(((cotacao_last - cotacao_14) / cotacao_14) * 100,2)
            crescimento_30 = round(((cotacao_last - cotacao30) / cotacao30) * 100,2)
            crescimento_60 = round(((cotacao_last - cotacao_60) / cotacao_60) * 100,2)
            crescimento_90 = round(((cotacao_last - cotacao_90) / cotacao_90) * 100,2)
        except:
            exit
        #CRESCIMENTO VOLUME ULTIMOS X DIAS (ONTEM X DIA COMPARADO)
        #volume do dia anterior a compra
        volume_last = dados_acao_filtrado['Volume'][-1:][lista[i]+".SA"][0]
        #valor cotação x dias atras
        try:
            volume_7 = dados_acao_filtrado['Volume'][-7:-6][lista[i]+".SA"][0]
            volume_14 = dados_acao_filtrado['Volume'][-14:-13][lista[i]+".SA"][0]
            volume30 = dados_acao_filtrado['Volume'][-30:-29][lista[i]+".SA"][0]
            volume_60 = dados_acao_filtrado['Volume'][-60:-59][lista[i]+".SA"][0]
            volume_90 = dados_acao_filtrado['Volume'][-90:-89][lista[i]+".SA"][0]
            #% da queda ou aumento ultimos x dias
            crescimento_vol_7 = round(((volume_last - volume_7) / volume_7) * 100,2)
            crescimento_vol_14 = round(((volume_last - volume_14) / volume_14) * 100,2)
            crescimento_vol_30 = round(((volume_last - volume30) / volume30) * 100,2)
            crescimento_vol_60 = round(((volume_last - volume_60) / volume_60) * 100,2)
            crescimento_vol_90 = round(((volume_last - volume_90) / volume_90) * 100,2)
        except:
            exit
        #RSI
        try:
            delta = dados_acao_filtrado['Close'][-90:].diff()
            up, down = delta.copy(), delta.copy()
            up[up < 0] = 0
            down[down > 0] = 0
            period = 14
            rUp = up.ewm(com=period - 1,  adjust=False).mean()
            rDown = down.ewm(com=period - 1, adjust=False).mean().abs()
            delta['RSI'] = 100 - 100 / (1 + rUp / rDown).fillna(0)

            rsi_0 = delta['RSI'][-1:][0]
            rsi_7 = delta['RSI'][-7:-6][0]
            rsi_14 = delta['RSI'][-14:-13][0]
            rsi_30 = delta['RSI'][-30:-29][0]
            rsi_60 = delta['RSI'][-60:-59][0]

            #% da queda ou aumento ultimos x dias
            cresc_rsi_7 = round(((rsi_0 - rsi_7) / rsi_7) * 100,2)
            cresc_rsi_14 = round(((rsi_0 - rsi_14) / rsi_14) * 100,2)
            cresc_rsi_30 = round(((rsi_0 - rsi_30) / rsi_30) * 100,2)
            cresc_rsi_60 = round(((rsi_0 - rsi_60) / rsi_60) * 100,2)
        except:
            exit
        #BOLINGER
        try:
            bolinger = dados_acao_filtrado.copy()
            bolinger['MA20'] = dados_acao_filtrado['Close'].rolling(20).mean()
            bolinger['20 Day STD'] = bolinger['Close'].rolling(window=20).std()
            bolinger['Upper Band'] = bolinger['MA20'] + (bolinger['20 Day STD'] * 2)
            bolinger['Lower Band'] = bolinger['MA20'] - (bolinger['20 Day STD'] * 2)

            boolinger_up_0 = bolinger['Upper Band'][-1:][0]
            boolinger_down_0 = bolinger['Lower Band'][-1:][0]
            boolinger_up_7 = bolinger['Upper Band'][-7:-6][0]
            boolinger_down_7 = bolinger['Lower Band'][-7:-6][0]

            delta_bolinger_0 = round((boolinger_up_0 - boolinger_down_0) / boolinger_down_0 * 100,2)
            cresc_bolinger_up_7 = round((boolinger_up_0 - boolinger_up_7) / boolinger_up_7 * 100,2)
            cresc_bolinger_down_7 = round((boolinger_down_0 - boolinger_down_7) / boolinger_down_7 * 100,2)
        except:
            exit
        #MÉDIAS MOVEIS
        try:
            time = dados_acao_filtrado.copy()
            rolling_9  = time['Close'].rolling(window=9)
            rolling_mean_9 = rolling_9.mean().round(1)

            rolling_20  = time['Close'].rolling(window=20)
            rolling_mean_20 = rolling_20.mean().round(1)

            rolling_72  = time['Close'].rolling(window=72)
            rolling_mean_72 = rolling_72.mean().round(1)
            time['MM9'] = rolling_mean_9.fillna(0)
            time['MM20'] = rolling_mean_20.fillna(0)
            time['MM72'] = rolling_mean_72.fillna(0)
            time['cruzamento'] =  time['MM9'] - time['MM72']
            buy = time.tail(50).loc[(time.tail(50)['cruzamento']==0)]

            if buy.empty == False:
                cruzou_mm = 1
            else:
                cruzou_mm = 0         

            if time['MM72'].iloc[-1] < time['MM9'].iloc[-1]:
                direcao_cruzada_cima = 1
            else:
                direcao_cruzada_cima = 0
                
            mm9_0 = time['MM9'][-1:][0]
            mm9_7 = time['MM9'][-7:-6][0]
            mm9_14 = time['MM9'][-14:-13][0]
            mm9_30 = time['MM9'][-30:-29][0]
            mm9_60 = time['MM9'][-60:-59][0]

            mm20_0 = time['MM20'][-1:][0]
            mm20_7 = time['MM20'][-7:-6][0]
            mm20_14 = time['MM20'][-14:-13][0]
            mm20_30 = time['MM20'][-30:-29][0]
            mm20_60 = time['MM20'][-60:-59][0]

            mm72_0 = time['MM72'][-1:][0]
            mm72_7 = time['MM72'][-7:-6][0]
            mm72_14 = time['MM72'][-14:-13][0]
            mm72_30 = time['MM72'][-30:-29][0]
            mm72_60 = time['MM72'][-60:-59][0]

            #% da queda ou aumento ultimos x dias
            cresc_mm9_7 = round(((mm9_0 - mm9_7) / mm9_7) * 100,2)
            cresc_mm9_14 = round(((mm9_0 - mm9_14) / mm9_14) * 100,2)
            cresc_mm9_30 = round(((mm9_0 - mm9_30) / mm9_30) * 100,2)
            cresc_mm9_60 = round(((mm9_0 - mm9_60) / mm9_60) * 100,2)

            #% da queda ou aumento ultimos x dias
            cresc_mm20_7 = round(((mm20_0 - mm20_7) / mm20_7) * 100,2)
            cresc_mm20_14 = round(((mm20_0 - mm20_14) / mm20_14) * 100,2)
            cresc_mm20_30 = round(((mm20_0 - mm20_30) / mm20_30) * 100,2)
            cresc_mm20_60 = round(((mm20_0 - mm20_60) / mm20_60) * 100,2)

            #% da queda ou aumento ultimos x dias
            cresc_mm72_7 = round(((mm72_0 - mm72_7) / mm72_7) * 100,2)
            cresc_mm72_14 = round(((mm72_0 - mm72_14) / mm72_14) * 100,2)
            cresc_mm72_30 = round(((mm72_0 - mm72_30) / mm72_30) * 100,2)
            cresc_mm72_60 = round(((mm72_0 - mm72_60) / mm72_60) * 100,2)

        except:
            exit

        try:

        #Atribuições
            #df_filled.loc[df_filled['name'].str.contains(lista[i]),'data_compra_1'] = data_compra_1
            #df_filled.loc[df_filled['name'].str.contains(lista[i]),'Preço_médio_comprado'] = preco_medio_compra
            #df_filled.loc[df_filled['name'].str.contains(lista[i]),'Preço_médio_vendido'] = preco_medio_vendido
            #df_filled.loc[df_filled['name'].str.contains(lista[i]),'Ganho_total'] = Ganho_total

            #df_filled.loc[df_filled['name'].str.contains(lista[i]),'Setor'] = setor
            #df_filled.loc[df_filled['name'].str.contains(lista[i]),'Atividade'] = atividade

            #df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_total_%'] = rendimento_total
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_ultimos_7_dias'] = crescimento_7
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_ultimos_14_dias'] = crescimento_14
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_ultimos_30_dias'] = crescimento_30
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_ultimos_60_dias'] = crescimento_60
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'Rendimento_ultimos_90_dias'] = crescimento_90

            df_filled.loc[df_filled['name'].str.contains(lista[i]),'crescimento_vol_ultimos_7_dias'] = crescimento_vol_7
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'crescimento_vol_ultimos_14_dias'] = crescimento_vol_14
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'crescimento_vol_ultimos_30_dias'] = crescimento_vol_30
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'crescimento_vol_ultimos_60_dias'] = crescimento_vol_60
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'crescimento_vol_ultimos_90_dias'] = crescimento_vol_90

            df_filled.loc[df_filled['name'].str.contains(lista[i]),'rsi'] = rsi_0
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_rsi_ultimos_7_dias'] = cresc_rsi_7
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_rsi_ultimos_14_dias'] = cresc_rsi_14
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_rsi_ultimos_30_dias'] = cresc_rsi_30
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_rsi_ultimos_60_dias'] = cresc_rsi_60

            df_filled.loc[df_filled['name'].str.contains(lista[i]),'delta_bolinger_0'] = delta_bolinger_0
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_bolinger_up_7'] = cresc_bolinger_up_7
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_bolinger_down_7'] = cresc_bolinger_down_7

            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cruzou_mm'] = cruzou_mm
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'direcao_cruzada_mm_cima'] = direcao_cruzada_cima

            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm9_ultimos_7_dias'] = cresc_mm9_7
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm9_ultimos_14_dias'] = cresc_mm9_14
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm9_ultimos_30_dias'] = cresc_mm9_30
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm9_ultimos_60_dias'] = cresc_mm9_60

            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm20_ultimos_7_dias'] = cresc_mm20_7
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm20_ultimos_14_dias'] = cresc_mm20_14
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm20_ultimos_30_dias'] = cresc_mm20_30
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm20_ultimos_60_dias'] = cresc_mm20_60        

            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm72_ultimos_7_dias'] = cresc_mm72_7
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm72_ultimos_14_dias'] = cresc_mm72_14
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm72_ultimos_30_dias'] = cresc_mm72_30
            df_filled.loc[df_filled['name'].str.contains(lista[i]),'cresc_mm72_ultimos_60_dias'] = cresc_mm72_60

            
        except:
            exit
        
    return df_filled