A little while ago, I shared John Ehlers' MAMA and FRAMA indicators.
I decided to play a little with it, and show you the possibilities when you account for neutral fluctuations in the market.
So I present you the MAMA and FRAMA indicators, applied to AAPL over the period 2005-2015. Clearly, if you trade aggressively with all your funds on the very short term, your equity will increase the most. However, such strategies are not always replicable in real-life due to transaction costs and slippage/illiquidity issues. So just for the purpose of motivation/entertainment, here is an algorithm that turns 10k initial capital into more than 1 million in the course of 10 years. So really, this is a little (overexaggerated) demonstration to show you that you can improve your strategy if you take the cyclical behaviour of the markets into account. In order to view the resulting graph, clone the algorithm and run the backtest.
The number of trades is too high to generate a summary of the backtest, so you would need to perform backtests on individual years to judge its power statistically. In a more serious manner, if you plan on using (part) of this strategy, you should adjust the algorithm to trade less frequently. One simple way of doing so is changing the consolidation period. Right now, this is:
int _consolidated_minutes = 10
You can change this to a larger number of minutes, for example, 60 minutes, to decrease the trading frequency.
In any case, I hope you enjoy this little example.
Keep dreaming and relish your coffee ;)
JP B
JP B
JP B
Michael Handschuh
LukeI
Michael Handschuh
if (quantity > 0) Order(_ticker, _trend_dir*quantity); _oldprice = _price;
It should look like the following to include the oldprice=price assignment in the if block:if (quantity > 0) { Order(_ticker, _trend_dir*quantity); _oldprice = _price; }
JP B
// Order logic / (simple) risk management if (Portfolio[_ticker].IsShort == true) { pps = ((_oldprice - _price)/_oldprice)*100; if (pps <= -2.5M || pps >= 2.5M || _trend_dir != _old_dir) { // End position Liquidate(_ticker); } } else { pps = ((_price - _oldprice)/_oldprice)*100; if (pps <= -2.5M || _trend_dir != _old_dir) { // if direction is wrong // End position Liquidate(_ticker); } }
And this yields the profits from before (>1 million). So what do we learn from this? That it's smart to take profits early on short positions! Apparently shorts have the tendency to go from being profitable to being non-profitable; so liquidating when the position is in-the-money is a smart move. Just for consistency, I've also tested this with long positions but there it doesn't hold: it's more profitable to let the long positions 'run' instead of taking profits early. So thank you again for scrutinizing my code as we have now learned that there is a profit-taking anomaly between long and short positions. Take profits early on shorts, let long positions run! :) See my adjusted version of your code below (Sharpe of 1.45!):JP B
Jose Sobrinho
Hello guys, First time in here and still trying to digest a lot of data and good comments you guys put together... If someone can help with this idea... how can we buy per example, SQQQ everytime AAPL(part of this code) was supposed to be liquidated... and every time was supposed to buy AAPL as per the code, to sell the SQQQ... basically to use AAPL as the indicator but the trade the ETN SQQQ on the other direction... can someone please let me know? So, I tried to invert the operation to sell the inverse ETN (SQQQ) when was time to buy APPL and Buy this ETN when was time to sell APPL... for some reason, I am running out of the money... doing something wrong :( ... in case someone can take a look.
Stephen Oehler
For what its worth, I'm having the same issue on a different algorithm. Just encountered it today. It seems that when I apply SetHoldings("SPY", 1.0m), which is a long position, it buys 1x worth of orders. However when I quickly try to go directly to a short position (SetHoldings("SPY", -1.0m), it buys -2x worth of orders (ostensibly to sell the ones I have, which is -1x, and then again to fully short it, which is another -1x). My thinking is there's a problem calculating how much the portfolio is worth at that point in time when you are requesting to short the position. Still looking into it.
Jared Broad
Welcome @Jose! @Stephen, does it rejects the order to go short? Please send me your project id to support@quantconnect.com and I'll dig into it.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Stephen Oehler
Ok so I found out that it was indeed rejecting orders to go short. I worked around this issue by doing the following when switching directly from a fully long to a fully short position (or vice versa): What I was doing: SetHoldings("SPY", -1.0m); // Attempt to directly take full short position from full long position (this results in many invalid orders) What I've put in place: SetHoldings("SPY", 0.0m); // Sell all first SetHoldings("SPY", -1.0m); // Then take short position In making this change, I went from having multiple invalid orders, to having every single order successfully filled. So this looks like a good workaround. Jared, attached is a project I was knocking around if you'd like to dig around it.
Stephen Oehler
Can't vet for how this would affect trading fees, however. The workaround I mentioned above would effectively double the trades, right?
Jose Sobrinho
Jared and Stephen, Thanks for your input.. @jared and Stephen I am still not understanding how to analize one stock and trade other. In order to keep it simple, I cloned your project Jared, that is checking the MA for the SPY and I tried to change to buy the SQQQ/QQQ ETN pair but the code is not trading... I think is related to the fact that, when adding these securities to the collection, they interfere on the numbers... So just to summarize, I was looking to keep everything as it is but just buy and sell these ETNs whenever the code was doing the same for the SPY... you will see on the code. Thanks for your help https://www.quantconnect.com/terminal/#open/206378
JP B
Jose Sobrinho
I will try to understand the LEAN one... but funny or strange that nobody had needed such thing yet? To use one stock and trade other... thanks
Michael Handschuh
@Jose, I think you just need to uncomment the lines that subscribe to QQQ and SQQQ data. In LEAN you can't trade a security that you don't have price data for.
Stephen Oehler
JP B, This is a really neat algo! Just for kicks, though, I applied it to a few other stocks and it didn't perform as well. I'm wondering if the indicators require some tuning per the underlying asset? Not to sound antagonistic, but I also wonder how much of the gain we're seeing is due to buy-and-hold-Apple having stellar performance these past few years? -Stephen
JP B
Stephen Oehler
Great discussion! Thanks very much for taking the time to write that. It is VERY impressive to have pulled in 11,000% total return as opposed to 840%. And with such low deviation on top of that. Does John Ehler discuss any techniques for calibrating the algorithm to the underlying asset? In particular, which parameters need to be calibrated? Thanks again, and I apologize if I'm pestering you with questions. This is fascinating stuff.
JP B
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!