import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use ('fivethirtyeight')`
import yfinance as yf
# Set the start and end date and cash
start_date = '2010-01-01'
end_date = '2020-01-01'
# Set the ticker
ticker = 'SPY'
# Get the data
df = yf.download(ticker, start_date, end_date)
def SMA10(data, period=10, column='Close'):
return data [column].rolling(window=period).mean()
def SMA30(data, period=30, column='Close'):
return data [column].rolling(window=period).mean()
# Set strategie
def strategy(df):
buy = []
sell = []
flag = 0
buy_price = 0
for i in range(0, len(df)):
if df['SMA10'] [i] > df['SMA30'] [i] and flag == 0:
buy.append(df['Close'][i])
sell.append(np.nan)
buy_price = df['Close'][i]
flag = 1
elif df['SMA10'] [i] < df['SMA30'] [i] and flag == 1:
sell.append(df['Close'][i])
buy.append(np.nan)
buy_price = 0
flag = 0
else:
sell.append(np.nan)
buy.append(np.nan)
return (buy, sell)Hi, can you help me to testing my strategy, for example: add a cash = 10.000, portfolio, and quantity of Stocks ( Order Size=quantity=(10000/df(Adj.Close)(i) Thanks a lot.
Larz60+ write Nov-26-2021, 06:22 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
