This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MultiSignalStarterSystem: | |
''' | |
Upgraded Start System using multiple entry rules. Adapted from Rob Carver's | |
Leveraged Trading: https://amzn.to/3C1owYn | |
''' | |
def __init__(self, ticker: str, signals: dict, target_risk: float = 0.12, | |
stop_loss_gap: float = 0.5, starting_capital: float = 1000, | |
margin_cost: float = 0.04, short_cost: float = 0.001, | |
interest_on_balance: float = 0.0, start: str = '2000-01-01', | |
end: str = '2020-12-31', shorts: bool = True, weights: list = [], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MultiSignalStarterSystem available here: | |
# https://gist.github.com/raposatech/2d9f309e2a54fc9545d44eda821e29ad#file-starter_system_mult_sigs-py | |
class ContinuousStarterSystem(MultiSignalStarterSystem): | |
''' | |
Carver's Starter System without stop losses and multiple entry rules. | |
Adapted from Rob Carver's Leveraged Trading: https://amzn.to/3C1owYn | |
''' | |
def __init__(self, ticker: str, signals: dict, target_risk: float = 0.12, | |
starting_capital: float = 1000, margin_cost: float = 0.04, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ForecastStarterSystem(ContinuousStarterSystem): | |
''' | |
Carver's Starter System without stop losses, multiple entry rules, and | |
a forecast for position sizing and rebalancing. | |
Adapted from Rob Carver's Leveraged Trading: https://amzn.to/3C1owYn | |
ContinuousStarterSystem class defined here: | |
https://gist.github.com/raposatech/49ccc66f5c312f939f8826251c55a676 | |
''' | |
def __init__(self, ticker: str, signals: dict, target_risk: float = 0.12, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DiversifiedStarterSystem(MultiSignalStarterSystem): | |
''' | |
Carver's Starter System without stop losses, multiple entry rules, | |
a forecast for position sizing and rebalancing, and multiple instruments. | |
Adapted from Rob Carver's Leveraged Trading: https://amzn.to/3C1owYn | |
Code for MultiSignalStarterSystem available here: | |
https://gist.github.com/raposatech/2d9f309e2a54fc9545d44eda821e29ad | |
''' | |
def __init__(self, tickers: list, signals: dict, target_risk: float = 0.12, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AlpacaStarterSystem(DiversifiedStarterSystem): | |
''' | |
Carver's Starter System without stop losses, multiple entry rules, | |
a forecast for position sizing and rebalancing, and multiple instruments. | |
Adapted from Rob Carver's Leveraged Trading: https://amzn.to/3C1owYn | |
Allows live trading via the Alpaca API. | |
DiversifiedStarterSystem found here: | |
https://gist.github.com/raposatech/d3f10df41c8745b00cb608bd590a986d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from argparse import ArgumentParser | |
from decouple import config | |
from starter_system import AlpacaStarterSystem | |
# Import Keys | |
KEY = config('ALPACA_PAPER_API_KEY') | |
SECRET = config('ALPACA_PAPER_SECRET') | |
URL = config('ALPACA_PAPER_URL') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A few helper functions | |
def calcReturns(df): | |
# Helper function to avoid repeating too much code | |
df['returns'] = df['Close'] / df['Close'].shift(1) | |
df['log_returns'] = np.log(df['returns']) | |
df['strat_returns'] = df['position'].shift(1) * df['returns'] | |
df['strat_log_returns'] = df['position'].shift(1) * \ | |
df['log_returns'] | |
df['cum_returns'] = np.exp(df['log_returns'].cumsum()) - 1 | |
df['strat_cum_returns'] = np.exp( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
class PSAR: | |
''' | |
Calculates the Parabolic Stop-and-Reverse (PSAR) indicator for trading. | |
Find the full explanation here: | |
https://raposa.trade/blog/the-complete-guide-to-calculating-the-parabolic-sar-in-python/ | |
''' | |
def __init__(self, init_af=0.02, max_af=0.2, af_step=0.02): |