Back to Posts
Listen to Thread

I love all the work done on this site, and when it does offer integrated broker features i will defiantly sign up. However, I do not know how to code, i was wondering if anyone was willing to help me write a very very simple code by my definitions. If you can please leave a message at the bottom and we can work together. the code will just focus on momentum and momentums moving avg.

Hi James I'm also new here so it'd be awesome if we could work together! What's your idea?

ok, here is its pretty simplified, you can do it on just the SPY to test, we would buy stock when momentum and its SMA is equal to or less than 1
and buy when momentum and its SMA is equal to or greater than 1
its very simple, but i cant find out how to write it properly
after it is finished then we could try multiple securities

Hi James,

This is the code by your definitions, hope that it could help you. But I still have some confusions:

You gave us two buy orders without sell order. I am not sure whether your meaning is "sell stock when less than 1 and buy when greater than 1". Tell me if I misunderstand your definitions, so that I can correct it for you.

Another question is about your investment cycle. You may find the fluctuation in the test is extremely high. This is because the data is updated per minite and your investment cycle might be one minite too.

Clone Algorithm
22
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

sorry about the confusion, i want the momentum and SMA momentum to sell when they are greater than 1
is it also possible it invest lets say 20% of the portfolio instead of betting the entire thing? im fine with it trading in the minute mark, i would like it to run like that preferably.

i was looking at the actuall code of the algo, now i dont know much about this stuff like i said before, but did you use regular moving avg?

Thanks Weiyi for the code :) Yeah you're right, the full backtest uses minute bar data.
@James: if you want to sell, just change order(context.spy, +100) to order(context.spy, -100) for the (sma<price) case. And for the 20% portfolio idea, what you can do is to change the initial capital from $1,000,000 to, say, $200,000, or order less shares, say 20 shares instead of 100. Another strategy that I can think of is to order more shares when the volatility is low and less when that's high. For that you can use the standard deviation (stddev) as an indicator to volatility.

yeah the method mavg(3) returns the moving avg for 3 days and gives that value to sma.

Hi James,

I have changed to "sell when greater than 1" now. And it is possible to invest only 20% of the portfolio.

Taibo has provided a way, which is to limit your used capital between $200,000 and -$200,000. But I think, the condition that you actually want to add might be ## used capital <= 0.2 * (used capital + cash) ##. That's the algorithm I used in this code.

Clone Algorithm
22
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Hi Taibo, happy to see you here.

thanks guys, i realize they dont have momentum as a study, and hope they add it, you did a lot of work thanks again!

James,

That's ok. It's my pleasure.

Besides, I recommend you to change the strategy as "sell when SMA <= 1 and buy when SMA >= 1", according to some of my experience and related research. See http://en.wikipedia.org/wiki/Momentum_%28finance%29. Just a piece of advice.

Log in to reply to this thread.
Not a member? Sign up!