Hello,
I'm pretty new to python and brand new to Quantopian.
I'm looking for someone to help me with the framework of the trading the algorithm.
The trading theory combines mean reversion and days to cover. Below is more details – I'm asking for help with building the framework so I can backtest, changing the variables described below:
""" Combining mean reversion strategy and days to cover (short ratio interest)
- Mean Reversion Strategy - What goes up must come down, what goes down must come up
- Days to cover Strategy - If a stock price is moving upwards, then the price could be driven up further by short interest (investors closing out their positions).
Combined Strategy Rules
- If the 10 day SMA of a stock is greater than the 30 day LMA be long on the position if the days to cover is equal to/greater than 8 days, else be short
- If the 10 day SMA of a stock is less than the 30 day LMA be short on the position if the days to cover is less than 8 days, else be long
Factors to play around with:
1. Market cap --- smaller caps are more prone to squeeze ?
2. Industry --- maybe tech stocks ? solar? something that swings based on technology speculation?
3. Play around with SMA and LMA windows and days to cover threshold
"""
import numpy as np
import pandas as pd
def initialize(context):
# As mentioned above, think the best securities would be small cap, tech companies, can this be called dynamically via pipeline that has sector specific capabilities?
context.security = ["""sids to be entered and tested"""]
# Rebalance every Monday (or the first trading day if it's a holiday)
# at market open.
schedule_function(rebalance,
date_rules.week_start(days_offset=0),
time_rules.market_open())
# Record variables at the end of each day.
schedule_function(record_vars,
date_rules.every_day(),
time_rules.market_close())
schedule_function(define_positions,
date_ruls.every_day(),
time_rules.market_close())
## Find short or long assignment
def define_positions(context, data):
define the long moving averages and the short moving averages windows
hist = data.history(context.security, 'price', 30, '1d')
LMAVG = pd.rolling_mean(hist, window=30)
SMAVG = pd.rolling_mean(hist, window=10)
define the direction that the SMAVG is moving relative to the LMAVG
Trend = LMAVG - SMAVG
trend_up_list = context.security[Trend < 0]
trend_down_list = context.security[Trend >0]
Not sure how to call and get avg trading volume data or short volume data. The below fields ('avg_vol' and 'short_vol') are just examples and I don't think data.history can get this requested data. If not, maybe calling a CSV with this data via a global variable at the beginning could work instead
Avg_volume = data.history(context.security, 'avg_vol')
Short_volume = data.history(context.security, 'short_vol')
define days to cover (DTC)
DTC = Short_volume / Avg_volume
squeeze lists or no squeeze lists
squeeze_list = context.security[DTC > 8]
no_squeeze_list = context.security[DTC < 8]
defining the long or short securities to trade, not sure if "or" is the correct syntax for lists in python?
longs = [val for val in trend_up_list if val in squeeze_list] or [val for val in trend_down_list if val in squeeze_list]
shorts = [val for val in trend_up_list if val in no_squeeze_list] or [val for val in trend_down_list if val in no_squeeze_list]
def rebalance(context, data):
def record_vars(context, data):