Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad


Python programa para economistas basico, Apuntes de Economía

herramienta para economistas en sus primeros ciclos

Tipo: Apuntes

2019/2020

Subido el 28/10/2020

ana-milagros-chiroque-monja
ana-milagros-chiroque-monja 🇵🇪

10 documentos

1 / 6

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
5 minutos
September 2, 2020
[1]: ###############################################################################
# 1. Importación de bibliotecas #
###############################################################################
# Para crear objetos de fecha y hora
import datetime
# Para tomar datos de stock
import yfinance as fyf
from pandas_datareader import data as pdr
fyf.pdr_override() # <-- Aquí está la solución
C:\Users\CALIPSO\AppData\Local\Programs\Python\Python38\lib\site-
packages\pandas_datareader\compat\__init__.py:7: FutureWarning:
pandas.util.testing is deprecated. Use the functions in the public API at
pandas.testing instead.
from pandas.util.testing import assert_frame_equal
[2]: ###############################################################################
# 2. Leer Datos #
###############################################################################
# Almacenar datos
df =pdr.get_data_yahoo(# or pdr.get_data_yahoo(...
# tickers list or string as well
# Bitcoin Cash USD (BCH-USD)
# Coffee Dec 20 (KC=F)
# LTC / USD (LTCUSD = X)
tickers ="GC=F",
# use "period" instead of start/end
# valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
# (optional, default is '1mo')
period ="5d",
# fetch data by interval (including intraday if period < 60 days)
# valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo
1
pf3
pf4
pf5

Vista previa parcial del texto

¡Descarga Python programa para economistas basico y más Apuntes en PDF de Economía solo en Docsity!

5 minutos

September 2, 2020

[1]:

_# 1. Importación de bibliotecas # ###############################################################################

Para crear objetos de fecha y hora_

import datetime

# Para tomar datos de stock

import yfinance as fyf from pandas_datareader import data as pdr fyf.pdr_override() # <-- Aquí está la solución

C:\Users\CALIPSO\AppData\Local\Programs\Python\Python38\lib\site- packages\pandas_datareader\compat_init_.py:7: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead. from pandas.util.testing import assert_frame_equal

2. Leer Datos

###############################################################################_

# Almacenar datos df = pdr.get_data_yahoo( _# or pdr.get_data_yahoo(...

tickers list or string as well

Bitcoin Cash USD (BCH-USD)

Coffee Dec 20 (KC=F)

LTC / USD (LTCUSD = X)_

tickers = "GC=F",

_# use "period" instead of start/end

valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max

(optional, default is '1mo')_

period = "5d",

_# fetch data by interval (including intraday if period < 60 days)

valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo_

# (optional, default is '1d') interval = "5m")

[*********************100%***********************] 1 of 1 completed

[3]: df = df.reset_index()

4: Datetime Open High Low 0 2020-08-23 18:00:00-04:00 1947.900024 1949.000000 1944. 1 2020-08-23 18:05:00-04:00 1944.900024 1944.900024 1938. 2 2020-08-23 18:10:00-04:00 1940.000000 1940.800049 1938. 3 2020-08-23 18:15:00-04:00 1939.000000 1941.800049 1939. 4 2020-08-23 18:20:00-04:00 1941.199951 1943.900024 1940.

Close Adj Close Volume 0 1944.900024 1944.900024 344 1 1939.699951 1939.699951 1155 2 1939.099976 1939.099976 815 3 1941.300049 1941.300049 501 4 1943.000000 1943.000000 453

0.1 Calculo del MACD

[8]: exp1 = df.Close.ewm(span=12, adjust= False ).mean() exp2 = df.Close.ewm(span=26, adjust= False ).mean()

macd = exp1-exp

exp3 = macd.ewm(span=9, adjust= False ).mean()

0.2 Calculo de la Medias Moviles Exponenciales

[9]: ewm20 = df.Close.ewm(span=20, adjust= False ).mean() ewm50 = df.Close.ewm(span=50, adjust= False ).mean() ewm200 = df.Close.ewm(span=200, adjust= False ).mean()

x = df.index, y = ewm50, name = 'EWM_50', line = dict(color = '#6B0DB3'), opacity = 0.8) EWM_200 = go.Scatter( x = df.index, y = ewm200, name = 'EWM_200', line = dict(color = '#FFF326'), opacity = 0.8) datos = [Close, EWM_20, EWM_50, EWM_200] py.iplot(datos)

[13]: import plotly.graph_objects as go

fig = go.Figure(data=[go.Candlestick(x = df.index, open = df['Open'], high = df['High'], low = df['Low'], close = df['Close'], increasing_line_color = 'rgb(66,133,244)', decreasing_line_color= 'rgb(244,160,0)',␣ ↪→name="PRICE"), ]) fig.add_trace(go.Scatter(x=df.index, y=ewm20, name="EWM20")) fig.add_trace(go.Scatter(x=df.index, y=ewm50, name="EWM50")) fig.add_trace(go.Scatter(x=df.index, y=ewm200, name="EWM200")) fig.update_yaxes(tickprefix="$") fig.update_layout(xaxis_rangeslider_visible= False ) fig.show()

[18]: import plotly.offline as py from plotly.graph_objs import *

trace1 = { "mode": "lines", "name": "PRICE", "type": "scatter",

"x": df.index, "y": df.Close, "yaxis": "y3" } trace2 = { "mode": "lines", "name": "RSI", "type": "scatter", "x": df.index, "y": df['RSI'] } trace3 = { "name": "MACD", "type": "scatter", "x": df.index, "y": macd, "yaxis": "y2" } trace4 = { "name": "SIGNAL", "type": "scatter", "x": df.index, "y": exp3, "yaxis": "y2" } trace5 = { "mode": "lines", "name": "EWM200", "type": "scatter", "x": df.index, "y": ewm200, "yaxis": "y3" } trace6 = { "mode": "lines", "name": "EWM50", "type": "scatter", "x": df.index, "y": ewm50, "yaxis": "y3" } trace7 = { "mode": "lines", "name": "EWM20", "type": "scatter", "x": df.index, "y": ewm20,