Back to Community
Please HELP ME: SMA value delay

Hello every expert :-)
Sorry for the mistake,
The code attached is showing the SMA 10& SMA 30 for USEquityprice.close, but I found out every SMA 10/30 values are delayed by 1 day.

e.g. on 4/19, the SMA 10 is showing the value which is actually calculated from 4/18, even though I schedule the function is running at the market close spot.
I check the SMA 10 in tradingview as well, the SMA 10(AEY) on 4/19 should be 1.820, SMA 10(AEY) on 4/18 should be 1.824,SMA 10(AEY) on 4/17 should be 1.831

here is the backtest log:

2017-04-18 15:59  PRINT                       sma_10    sma_30  
Equity(21 [AAME])   3.876926  3.834567  
Equity(117 [AEY])   1.832000  1.879833  
Equity(157 [AEG])   4.946000  5.337833  
Equity(225 [AHPI])  1.988600  1.963414  
Equity(392 [AMS])   4.582400  4.243300  
2017-04-19 15:59  PRINT                      sma_10    sma_30  
Equity(21 [AAME])   3.86386  3.838785  
Equity(117 [AEY])   1.82400  1.877500  
Equity(157 [AEG])   4.91550  5.309667  
Equity(225 [AHPI])  1.98460  1.965069  
Equity(392 [AMS])   4.56500  4.254967  

I think this is because the algo assumes to run 1 min before the market close, so I can't get the current SMA value.
Does anyone can help me to get the current SMA value

Clone Algorithm
0
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
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import SimpleMovingAverage

def initialize(context):

    # Create and attach an empty Pipeline.
    pipe = Pipeline()
    pipe = attach_pipeline(pipe, name='my_pipeline')

    # Construct Factors.
    sma_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10)
    sma_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30)

    # Construct a Filter.
    prices_under_5 = (sma_10 < 5)

    # Register outputs.
    pipe.add(sma_10, 'sma_10')
    pipe.add(sma_30, 'sma_30')

    # Remove rows for which the Filter returns False.
    pipe.set_screen(prices_under_5)

    schedule_function(
        func = print_out,
        date_rule = date_rules.every_day(),
        time_rule = time_rules.market_close()
    )
        
def print_out(context,data):
    # Access results using the name passed to `attach_pipeline`.
    results = pipeline_output('my_pipeline')
    print results.head(5)

    # Store pipeline results for use by the rest of the algorithm.
    context.pipeline_results = results
    
There was a runtime error.