Hey folks,

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.

Cheers..

13
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 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 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):
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)

There was a runtime error.
2 responses

I suspect there may be a flaw in your logic here. There is no proper entry, exit, re-entry mechanism.

f current_price > (1.01 * average_price):


OK so far: you enter.
But then:

lif current_price > (costBasis * 1.01)


This can happen in a nano-second with an instrument of this volatility.
What happens next? The stock continues up and you simply re-enter the next day. Perhaps at a higher price.

Perhaps that is what was intended. And of course if the stock goes sharply down after your exit or overnight, well great.

But....perhaps you should think about the profit target bit. Is that what you really mean to happen? In a rising market do you want to keep enering and exiting?

No real comment from me. Just a query

It looks like you've developed an evil negative skew system. A negative skew system is one that frequently wins (lots of small wins), but when it loses it loses big. And you're asking for guidance on how to make it "not lose big" when it loses.

Have you ever considered that the two go hand-in-hand? The reason why you're able to collect all of those small wins is because you're willing to bear the risk of eating those huge losses from time to time? And if you were to implement rules that eliminate the huge losses from your results, it might also alter (or even eliminate) all of those small wins in the process?

I hate to say it, but there is no free lunch when it comes to this stuff. I wish I had the "magic bullet" for you, but I don't.

Consider why the strategy should work in the first place. What is the economic justification for it to work? What risk premiums is it capturing? And then consider if trying to eliminate the "big losses" would be violating the underlying basis for the strategy in the first place.