Skip to content

Instantly share code, notes, and snippets.

@raposatech
raposatech / psar.py
Created February 18, 2022 16:18
PSAR Calculation Class
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):
@raposatech
raposatech / finance_helper_funcs.py
Created February 18, 2022 16:01
Helper functions to calculate backtest returns and strategy stats.
# 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(
@raposatech
raposatech / run_alpaca_system.py
Created December 8, 2021 18:24
Run Starter System with Alpaca
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')
@raposatech
raposatech / starter_system_alpaca.py
Last active December 25, 2023 00:03
Starter System with Alpaca Integration
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
@raposatech
raposatech / starter_system_mult_instruments.py
Last active January 16, 2022 07:03
Starter System with Multiple Instruments
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,
@raposatech
raposatech / starter_system_forecast.py
Created November 12, 2021 17:45
Starter System with Forecast
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,
@raposatech
raposatech / starter_system_continuous.py
Last active December 25, 2023 00:01
Continuous Starter System
# 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,
@raposatech
raposatech / starter_system_mult_sigs.py
Last active January 29, 2023 17:43
Starter System with Multiple Entry Signals
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 = [],