python assignments Flashcards

1
Q

Imports basics

A
import yfinance as yf
import datetime as dt
import pandas as pd
import numpy as np
start_date = dt.datetime(2015, 1, 2)
end_date = dt.datetime(2020, 4, 30)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

work out %

work out sma

A
df_MSFT["range"] = (df_MSFT["High"] - df_MSFT["Close"]) / df_MSFT["Close"]
df_MSFT["range"] = df_MSFT["range"].rolling(window=25).mean()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

work out on going returns

A

df_MSFT[‘cc_returns’].cumsum()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how to add data sheets portrait to new df

A

df = pd.concat( [x, y, z], axis=1 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to change column names

A

df.columns = [‘GLD’, ‘GDX’, ‘USO’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Change index to datetime

A

df.index = pd.to_datetime(df.index)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Work out percentages for df, then cumulative growth

A

df_pct = (df.pct_change()+1).cumprod()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

plot df

A

df_pct.plot(figsize=(16,8))

plt. ylabel(“Percentage Change”)
plt. show()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

work with module that allows estimation by ordinary least square

A

from statsmodels.api import OLS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Fit the model OLS to the first 90 rows,

A

model = OLS(df[“GLD”].iloc[:90], df[[“USO”,”GDX”]].iloc[:90])
model = model.fit()
print(‘The hedge ratio for GDX and USO are’)
model.params

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

placing long position

A

df[‘long_entry’] = df.spread < df.lower_band
df[‘positions_long’] = np.nan
df.loc[df.long_entry, ‘positions_long’] = 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

date for end and start

A
end_date = dt.date(2020, 4, 30)
start_date = dt.date(2015, 1, 2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Download using yfinance

A

import yfinance as yf

df = yf.download(“MSFT”, start=start_date, end=end_date)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Work out return

A

df[“return”] = np.log(df[“Adg Close”] /df[“Adg Close”].shift(1))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Find the day name

A

data[‘day’] = data.index.day_name()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

use a where to build up condition

A

data[‘signal’] = np.where((data[‘condition1’] == 1)
& (data[‘condition2’] == 1)
& (data[‘condition3’] == 1),
1, 0)

17
Q

How to use lambda on list

A

list ( map (lambda x, y: x + y, list_1, list_2 ) )

18
Q

What is the 20 rolling average mean

A

df2[‘20_day_sma’] = df2[‘Adj Close’].rolling(window=20, center=False).mean()

19
Q

Working out factorials

A

for i in range(1, num+1):

fact = fact * i