Checking Trading Signals for PSAR and Stochastic Oscillator

Course Name: Python For Trading!, Section No: 11, Unit No: 5, Unit type: Notebook

Hello, in this notebook we're checking crossover from Close and SAR and Stochastic fast K and slow D. However we should check if yesterday the crossover are in the oppossite situation to avoid situations where we're already in a bullish/bearish crossover.

https://i.imgur.com/sKJMBq8.png

My solution:

https://i.imgur.com/50PVcVV.png

Hi Daniel, will check this and get back to you.

Hello Daniel,



The choice to add the condition to check the previous day depends on the way the backtester is written.



In the notebook you mentioned, we used a vectorized approach, and the signal values are multiplied by the daily returns of the asset. So, we need the signal to be '1' or '-1' as long as the 'buy' or 'sell' conditions are met. Hence, we don't need to check the condition for the previous day.



To summarize, we are setting the value of 'signal' to '1' or '-1' for all the cases where the buy or sell condition is met. For this, we don't need to check the previous day's condition.



However, if you want to set the signal to '1' or '-1' only on the crossover day, then you need to check the condition on the previous day and also change the way you are using this signal value to backtest.



I hope this is clear, if you have any more doubts on this, feel free to share. 

You're right, in this way I'm buying or selling ONLY when there're crosses betweek fast_k and slow_d. You're approach it's better because we buy / sell only when these two indicators are in bullish / bearish signal.



This is how looks using your logic. Looks coherent:



Hello Daniel,

Good to know that it is clear to you now.

Incase you have any further queries, feel free to leave a comment.

Happy learning!