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 log.info the data it doesnt match with other references.
Can someone help?
Thanks

Clone Algorithm
0
Loading...
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
import pandas as pd
import talib

def initialize(context):
    schedule_function(DPO, 
                      date_rules.every_day(),
                      time_rules.market_open(hours = 0, minutes = 30)) 
pass
       
      
def DPO(context, data):
    context.security = sid(8554)
    DPO_period = 20
  #  Lookback_offset = ((DPO_period/2) + 1)
    price_current = data.history(context.security, 'close', DPO_period,'1d')
  #  DPO_SMA_current = DPO_price_current.mean()
  #  Lookback_price = data.history(context.security, '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
    
    log.info(price_current[-1])
  #  record(DPO_VV = DPO_VV)
    
    
def handle_data(context, data):
    pass
    
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.