I want to prevent my strategy from trading too frequently, by imposing a minimum number of bars between trades. I have done this:
CODE:
Please log in to see this code.
This does not work, I suspect because my trades work on the next bar, e.g.
CODE:
Please log in to see this code.
Therefore, if I set up a trade at bar N, it is planned to execute at bar N+1, and when the code loops through bar N+1 apparently that trade has not executed so the above code does not detect it as the most recent trade. Is there a workaround, or hopefully some simpler way to accomplish this (this meaning limit trading frequency to more then every X bars)?
I considered setting an integer value for the latest trade bar very time my code does (or plans) a trade, but I think that is an invitation to bugs, because there are many different places in my code that do trades under different conditions (e.g. open a single long or short position, CloseAllLongPositions(), close all positions that meet certain criteria, etc). and keeping the logic consistent among these different areas will be challenging. Better to have it in single place at a lower level.
Thanks,
Ron