Back to Community
Problem with closing prices

Hi, I have been trying to create a DPO signal, to realize that the closing prices that I get for SPY and Tesla are not the same as in Yahoo finance or charting service. Is there something that I do wrong?
I added comments to my code only to keep the current_price data.
When I the data it doesnt match with other references.
Can someone help?

Clone Algorithm
Total Returns
Max Drawdown
Benchmark Returns
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
import pandas as pd
import talib

def initialize(context):
                      time_rules.market_open(hours = 0, minutes = 30)) 
def DPO(context, data): = sid(8554)
    DPO_period = 20
  #  Lookback_offset = ((DPO_period/2) + 1)
    price_current = data.history(, 'close', DPO_period,'1d')
  #  DPO_SMA_current = DPO_price_current.mean()
  #  Lookback_price = data.history(, 'close', DPO_period + Lookback_offset+1,'1d')
  #  SMA_Lookback_offset = Lookback_price[:DPO_period+1].mean()
  #  DPO_VV= SMA_Lookback_offset -  DPO_price_current[-1]
  #  DPO = DPO_price_current[-Lookback_offset-1] - DPO_SMA_current
  #  DPO3 = DPO_price_current[-1] - SMA_Lookback_offset[-1])
  #  record(DPO_VV = DPO_VV)
def handle_data(context, data):
There was a runtime error.
3 responses

You didn't show the actual problem, i.e. the difference between the data you see on Yahoo finance and the prices you log with your script.

At first look it seems you're printing out a running value (the close price of a day 30 minutes into the trading day) so you end with a difference at the end of the day.

Either log the previous day's price by indexing [-2] or move that log call into before_trading_start().

Thanks a lot, I moved the logic inside before trading start and it worked.
thank you !!

You're welcome, as future reference I suggest checking this link about asking questions.