The following code shows why I like to swing trade ETFs that track the stock market. Historically the stock market has never NOT recovered. Investors buy aRead More
The following code shows why I like to swing trade ETFs that track the stock market. Historically the stock market has never NOT recovered. Investors buy and hold their stocks forever so I simply combine that train of thought to swing trade the stock market as a whole.
If I was going to buy and hold forever anyway I might as will swing trade the small dips in the market along the way and make more than the dividends pay out. Worse case is, I hold until the market recovers which is what I would have done if I were investing for the long run.
This code will show what would happen if you were to buy a single share of a stock everyday from January 1, 2020 until May 29, 2020.
import pandas as pd
import sys
import matplotlib.pyplot as plt
import studies
import mykeys
api_key = mykeys.api_key
ticker = sys.argv[1]
api_url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=' + ticker + '&apikey=' + api_key + '&outputsize=full&datatype=csv'
data = pd.read_csv(api_url)
stock_data = pd.DataFrame(index=range(0,len(data)),columns=['timestamp', 'open','high','low','close','volume'])
stock_data = stock_data.reindex(index=stock_data.index[::-1])
for i in range(0,len(data)):
if "2020" in data['timestamp'][i]:
ts = data['timestamp'][i]
stock_data['timestamp'][i] = data['timestamp'][i]
stock_data['close'][i] = float(data['close'][i])
stock_data['high'][i] = float(data['high'][i])
stock_data['low'][i] = float(data['low'][i])
stock_data['open'][i] = float(data['open'][i])
stock_data['volume'][i] = int(data['volume'][i])
stock_data.index = stock_data.timestamp
stock_data = stock_data.dropna()
timestamp = stock_data['timestamp']
close = stock_data['close']
high = stock_data['high']
low = stock_data['low']
open = stock_data['open']
volume = stock_data['volume']
share_count = 0
stock_price_sum = 0
last_price = close[len(close)-1]
net_values = []
spent_values = []
for now in range(0,len(close)):
share_count = share_count + 1
stock_price_sum = round(stock_price_sum + close[now],2)
net_value = round(share_count * close[now],2)
net_values.append(net_value)
spent_values.append(stock_price_sum)
print(timestamp[now],close[now]," - Total Shares:", share_count, " Spent: ", stock_price_sum, " Net Value: ", net_value)
plt.rcParams.update({
"lines.color": "white",
"patch.edgecolor": "white",
"text.color": "white",
"axes.facecolor": "black",
"axes.edgecolor": "lightgray",
"axes.labelcolor": "white",
"xtick.color": "white",
"ytick.color": "white",
"grid.color": "lightgray",
"figure.facecolor": "black",
"figure.edgecolor": "black",
"savefig.facecolor": "black",
"savefig.edgecolor": "black"})
#Define our chart with 2 subcharts and a size of 1200x1200.
fig, axs = plt.subplots(2, 1,figsize=(18,9))
####Sub Graph 1
axs[0].set_title('Stock Chart For: ' + ticker,color='black')
axs[0].set_facecolor('#000000')
axs[0].plot(timestamp,close)
axs[0].set_xticks([0,len(close)-1], minor=False)
axs[0].set_ylabel(ticker + ' CLOSE',fontsize=18,color='white')
axs[0].tick_params(axis='x', colors='red')
axs[0].tick_params(axis='y', colors='green')
axs[0].grid(True,color='#292b2e')
axs[0].spines['bottom'].set_visible(False)
axs[0].spines['top'].set_visible(False)
axs[0].spines['right'].set_visible(False)
axs[0].spines['left'].set_visible(False)
axs[0].set_xticklabels([])
axs[0].set_yticklabels([])
fig.tight_layout()
####Sub Graph 2
axs[1].set_title('Relative Strength Index',color='black')
axs[1].set_facecolor('#000000')
axs[1].plot(timestamp,net_values)
axs[1].plot(timestamp,spent_values)
axs[1].set_xticks([0, len(close)-1], minor=False)
#axs[1].set_yticks([0, 30,50, 70, 100], minor=False)
axs[1].tick_params(axis='x', colors='red')
axs[1].tick_params(axis='y', colors='green')
axs[1].grid(True,color='#292b2e')
axs[1].set_ylabel('NET VALUE v.s. NET SPENT',fontsize=18,color='white')
axs[1].spines['bottom'].set_visible(False)
axs[1].spines['top'].set_visible(False)
axs[1].spines['right'].set_visible(False)
axs[1].spines['left'].set_visible(False)
axs[1].set_xticklabels([])
axs[1].set_yticklabels([])
print("Done.")
plt.show()