Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving average strategy
#1
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.
Reply


Messages In This Thread
Moving average strategy - by irina_shubina - Nov-26-2021, 03:48 PM
RE: Moving average strategy - by paulyan - Jul-31-2022, 05:02 PM
RE: Moving average strategy - by paulyan - Jul-31-2022, 05:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  strategy to troubleshoot what pyinstaller misses hammer 0 992 May-23-2022, 01:05 AM
Last Post: hammer
  Best strategy for creating .exe for windows hammer 4 1,558 Apr-05-2022, 12:47 AM
Last Post: hammer
  Strategy on updating edits back to data table and object variables hammer 0 1,235 Dec-11-2021, 02:58 PM
Last Post: hammer
  calculate daily return in percent in forex as to some strategy? alen 1 2,261 Mar-12-2021, 10:03 AM
Last Post: buran
  Python learning strategy IluvPython 6 3,329 Nov-04-2019, 07:41 PM
Last Post: buran
  What is the strategy for working with class variables? AlekseyPython 3 3,083 Feb-24-2019, 05:34 AM
Last Post: AlekseyPython
  Python and strategy pattern tanc 5 3,491 Nov-30-2018, 08:55 AM
Last Post: Gribouillis
  best parallelisation strategy on python simona 1 2,274 Apr-19-2018, 01:51 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020