I've made a very simple algorithm that shorts the VXX only. The rules of the algorithm are as follows
- If current price is 1% over the 10 day moving average then short it.
- Once the current price is 1% over my cost basis then I close positon.
What I need to fix in this is the closing and opening algorithm. I noticed that the algo does terribly if there is a huge spike in volatility. Ideally I'm looking to adjust the entry rules where I wouldn't enter under extreme volatility as it appears thats where the majority of the drawdowns occur.
I appreciate all constructive feedback. This is my first algo on this forum.
|Returns||1 Month||3 Month||6 Month||12 Month|
|Alpha||1 Month||3 Month||6 Month||12 Month|
|Beta||1 Month||3 Month||6 Month||12 Month|
|Sharpe||1 Month||3 Month||6 Month||12 Month|
|Sortino||1 Month||3 Month||6 Month||12 Month|
|Volatility||1 Month||3 Month||6 Month||12 Month|
|Max Drawdown||1 Month||3 Month||6 Month||12 Month|
def initialize(context): # In our example, we're looking at Apple. If you re-type # this line you'll see the auto-complete popup after `sid(`. context.security = sid(38054) # Specify that we want the 'rebalance' method to run once a day schedule_function(rebalance, date_rule=date_rules.every_day()) """ Rebalance function scheduled to run once per day (at market open). """ def rebalance(context, data): # To make market decisions, we're calculating the stock's # moving average for the last 5 days. # We get the price history for the last 5 days. price_history = data.history( context.security, fields='price', bar_count=10, frequency='1d' ) # Then we take an average of those 5 days. average_price = price_history.mean() # We also get the stock's current price. current_price = data.current(context.security, 'price') # If our stock is currently listed on a major exchange if data.can_trade(context.security): # If the current price is 1% above the 5-day average price, # we open a long position. If the current price is below the # average price, then we want to close our position to 0 shares. costBasis = context.portfolio.positions[context.security].cost_basis; openPositon = len(context.portfolio.positions) if current_price > (1.01 * average_price): # Place the buy order (positive means buy, negative means sell) order_target_percent(context.security, -1) log.info("Open Short %s" % ((costBasis * 1.10))) elif current_price > (costBasis * 1.01): # Sell all of our shares by setting the target position to zero order_target_percent(context.security, 0) log.info("Close Short %s" % ((costBasis * 1.01))) # Use the record() method to track up to five custom signals. # Record Apple's current price and the average price over the last # five days. record(current_price=current_price, average_price=average_price)