Beispiel #1
0
def fn_growth(df: pd.DataFrame, window_size):
    v = df[DF_ADJ_CLOSE]
    t = TrailingStats(v, window_size)
    return t.exp_growth


def fn_std_change(df: pd.DataFrame, window_size):
    v = df[DF_ADJ_CLOSE]
    t = TrailingStats(v, window_size)
    return t.exp_std_dev


yahoo = YahooData()

stocks = yahoo.get_symbol_names()
rs = np.random.RandomState(10)
rs.shuffle(stocks)
stocks = stocks[0:20]
stocks = stocks + ['SPY']


class Indicators:
    def __init__(self,
                 stocks: list[str],
                 func: Callable,
                 fargs=(),
                 fkwargs=None):
        self.stocks = stocks
        self.func = func
        self.fargs = fargs
Beispiel #2
0
from backtester.stockdata import YahooData
from backtester.exceptions import NotEnoughDataError
import datetime


def func1(window_size, df: pd.DataFrame):
    v = df[DF_ADJ_CLOSE]
    t = TrailingStats(v, window_size)
    volatility = t.exp_std_dev
    rate = t.exp_growth
    pdb.set_trace()
    return rate, volatility


yahoo_data = YahooData()
all_names = yahoo_data.get_symbol_names()
np.random.seed(0)
np.random.shuffle(all_names)
stocks1 = all_names[0 : 16]
stocks = []
for stock in stocks1:
    if len(yahoo_data[stock]) > 0:
        stocks.append(stock)
yahoo_data = YahooData(symbols=stocks)


class Strat1(Strategy):
    
    def init(self):
        # Build long and short rate metrics
        self.ind1 = self.indicator(func1, 301, name='rate')
Beispiel #3
0
from typing import Callable
from backtester import Strategy, Backtest
from backtester.indicators import TrailingStats
from backtester.definitions import DF_ADJ_CLOSE
from backtester.stockdata import YahooData, TableData, Indicators
from backtester.exceptions import NotEnoughDataError

from functools import cached_property
import datetime

import matplotlib.pyplot as plt
from dotenv import load_dotenv
load_dotenv()

yahoo = YahooData()
symbols = yahoo.get_symbol_names()
rs = np.random.default_rng(0)
rs.shuffle(symbols)

# STOCKS = symbols[0:100]
# STOCKS.append('SPY')
# STOCKS.append('VOO')
# STOCKS.append('GOOG')
# STOCKS.append('TSLA')
STOCKS = ['SPY']
STOCKS = np.array(STOCKS)


def post1(df: pd.DataFrame):
    series = df[DF_ADJ_CLOSE]
    ts = TrailingStats(series, 100)
Beispiel #4
0
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import backtester

from backtester.indicators import TrailingStats
from backtester.analysis import BuySell, avg_future_growth
from backtester.stockdata import YahooData
from backtester.definitions import DF_ADJ_CLOSE
from backtester import utils
from backtester.smooth import SmoothOptimize, TrailingSavGol

y = YahooData()
names = y.get_symbol_names()
np.random.seed(7)
np.random.shuffle(names)

symbol = 'MSFT'
date1 = np.datetime64('2005-01-01')
date2 = np.datetime64('2018-08-01')

df = y.get_symbol_before(symbol, date2)
ii = df.index.values >= date1
df = df.loc[ii]
close = df[DF_ADJ_CLOSE]

ts = TrailingStats(close, window_size=30)
growth = ts.exp_growth