I have this code.
import yfinance as yfimport numpy as npimport pandas as pddf = yf.download('TSLA', start = '1991-01-01')data = df.Close.loc[(df.index>='1991-01-01')]data=data.to_frame()sma_s = 50sma_l = 100data['sma_s'] = data.Close.rolling(sma_s).mean()data['sma_l'] = data.Close.rolling(sma_l).mean()data['day'] = data.index.daydata.dropna(inplace=True)data['position_1_-1'] = np.where(data['sma_s']>data['sma_l'], 1, -1)data['position_1_0'] = np.where(data['sma_s']>data['sma_l'], 1, 0)data['position_0_-1'] = np.where(data['sma_s']>data['sma_l'], 0, -1)data Close sma_s sma_l day position_1_-1 position_1_0 position_0_-1Date 2010-11-17 1.966000 1.476147 1.399193 17 1 1 02010-11-18 1.992667 1.488387 1.403193 18 1 1 02010-11-19 2.066000 1.502813 1.407967 19 1 1 02010-11-22 2.226667 1.519720 1.415593 22 1 1 02010-11-23 2.304667 1.537653 1.425840 23 1 1 0
This is a Simple Moving Average strategy and the columns beginning with position
indicates our Long/Short position such that position_1_-1
is both Long and Short. position_1_0
is Long Biased. And position_0_-1
is short biased. I want to use the day
column to determine start and end of a trend within the month. And plot Gantt Charts.
import altair as altimport pandas as pdsource = pd.DataFrame([ {"task": "A", "start": 1, "end": 3}, {"task": "B", "start": 3, "end": 8}, {"task": "C", "start": 8, "end": 10}])alt.Chart(source).mark_bar().encode( x='start', x2='end', y='task')
The start and end has to be monthwise. What is the best way to do this?