Back to Community
new here - RSI/MACD inaccurate, how to test or fix?

n00b here. doing nothing fancy, just testing out quantopian and python. i am just trying to calculate the RSI/MACD (real simple) and the values are so off. I am comparing it to both yahoo finance & thinkorswim (my trading platform), quantopian is way off. so my questions (in order of importance)

  1. why is it so off? how can i fix it?
  2. typically RSI you can choose the average type (simple, exp, weighted, etc), can i choose a specific one somehow?
  3. same as #2 but for MACD - typically MACD you can choose the average type (simple, exp, weighted, etc), can i choose a specific one somehow?
  4. as a first time quantopian, everytime i make some type of code change, it seems like i have to backtest it to see the changes. is this normal? because i have like 100+ backtests cause every little change i have to test to see results
  5. side question - how do i prevent going margin? i want to pretend i have a cash account and can never go above or below the cash bank. what do i need to set and check before every order?

thanks everyone!!

Clone Algorithm
12
Loading...
Backtest from to with initial capital
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
# Backtest ID: 55dd272dde87430daffc89e2
There was a runtime error.
9 responses

The custom chart does smoothing.
You can move the sliders to zoom in and see if that does the trick or for precision that you know you can count on ...

Try logging and pls let us know if that clears it up:

log.info('m {}  s {}  h {}'.format(macd, signal, hist))  

Edit: Was too much of a rush, more like this with -1:

log.info('macd {}'.format(macd[symbol('SPY')][-1]))  

Also, possibly faster:

macd_val  = talib.MACD(prices[symbol('SPY')], 12, 26, 9)[0][-1]  
#                                0 to return only macd --^ and -1 for only last val  
log.info('macd {}'.format(macd_val))  

hi garyha - have zoomed in sliders, doesn't help, shows same numbers. i added your logging, it doesn't help. it just gives me an odd array full of nulls and some other values, but values are still way off

anyone else?

Your moving average of the price and RSI is causing the scale to be a lot higher and thus it looks like the MACD isn't moving much.

To fix that, just scale everything proportionally or remove the moving average and RSI.

but the values are way off. why is it so much different from yahoo + professional trading platforms? i can't create an algo if the values are so off, i need it to match (or at least be somewhat similar) to reality

What values are 'way off'? MACD is a subtraction from two MAs, MA of the price is expected to be high, RSI ranges between 0 and 100. Keep in mind that they are plotted on the same axis scale..

MACD/RSI are way off...like a solid 20%+ difference from yahoo and thinkorswim

@Day Trader

We get this question a lot about why results folks find here are different than on other web sites. Basically it comes down to two things: computation and data.

In regards to computation you can come up with a different results for the same computation based on the kinds of assumptions you make in your calculation. What window length, how many standard deviations, what did you use for the risk free rate, log returns or raw returns, are just a few examples of assumptions made explicitly or implicitly when making computations. Furthermore you can see that even financial data providers like Yahoo or Google have discrepancies between their metrics. See this thread for some information (albeit old) related specifically to MACD calculations on Quantopian, Yahoo, Google.

It is probably worth mentioning that another major difference is the type of data we use, vs others. Straight from the FAQ...

Quantopian uses the last traded price as the close price for the
security. Depending on the data source, others may use end-of-day
(EOD) prices. For example, Yahoo is an EOD datasource. Yahoo and other
EOD data providers get their price and volume data from the official
exchange record. Quantopian's data is generated by the actual trades,
regardless of what exchange the trade was made on. The EOD sources
rarely exactly match data derived from intraday data. For instance,
the official close for a NYSE stock is the last trade of the day for
the stock on NYSE. But if the stock also trades on Chicago, Pacific or
another regional exchange, the last trade on one of those exchanges
could be our close.

You can read more here. That should shine some light onto why you are seeing different results.

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

daytrader,

The following should help you learn the ropes. I'm not saying its perfect but it should be a step in the right direction.

Happy coding

Clone Algorithm
14
Loading...
Backtest from to with initial capital
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
# Backtest ID: 55de1d512b96440d93922937
There was a runtime error.

Change
Prices = history(50, '1d', 'price')

to collect 250 days of data
Prices = history(250, '1d', 'price')

Thank me later =) ...or let me know if it doesn't work out. I know when I applied this change it matches the T.A. in ThinkOrSwim