Outlier Detection after Detrending a Time Series With Missing Values or NaN Goal

Substitute outliers in a time series by most recent valid data

Problem

The time series (end-of-day stock prices) has several 'uncomfortable' properties:


It is non-stationary and can have components of low and high frequency (trends and sudden price moves)
There can be missing data for some days, typically single occurrences, but sometimes a whole consecutive segment is missing
Some values are erroneous (implausibly high or low i.e. due to misplaced decimal sign). These are the real outliers and don't belong to the series.


There is an additional caveat: The substitution algorithm must not look into the future of the time series since it will be used in a machine learning scenario.

Strategy

What I thought of so far: 


Detrending the series. Maybe rolling linear detrending by the last n values. Will still be a problem with very short term moves.
Replacing the missing values by extrapolating the trend identified in (1)
Calculating the z-score and replacing values with z &gt; 6 by extrapolating the trend identified in (1)


As the tags suggest, I will realise this in python / scipy. However I don't have much experience with time series analysis. I have no idea if this is an appropriate approach or if I'm missing something important. Any help is appreciated. Thanks.
Problem
  
  The time series (end-of-day stock prices) has several 'uncomfortable' properties:
  
  
  It is non-stationary and can have components of low and high frequency (trends and sudden price moves)
  


These are not outliers though. These are valid signal behaviour.


  
  There can be missing data for some days, typically single occurrences, but sometimes a whole consecutive segment is missing
  Some values are erroneous (implausibly high or low i.e. due to misplaced decimal sign). These are the real outliers and don't belong to the series.
  


To reduce the effect of implausibly high or low values you can use a rolling median filter at a suitable time scale (e.g. depending on your sampling frequency, you might want to get the median of a day's transactions).

Missing data is a more challenging problem. A few missing values might be "patched" with some interpolation scheme but if you have almost a day's worth transactions missing, it is probably better to break the analysis into segments too. Otherwise, you would be assuming a lot for a part of the signal that you don't really know what happened in reality.

Hope this helps.