I'm trying code up a really basic system and the criteria for entering/exiting a trade is based on if the current day's close is above below a certain Moving Average. I wanted to set it up so that if the close is clearly above the Moving Average then it would enter/exit on that day's closing price. This might be a feedback loop wherein it doesn't know the MA until the day closes so it can't enter that day but in practice if you know the MA from previous day assuming the lookback window is long enough then you should be reasonably sure the close is going to be above/below it.
When I used Enter/Exit on Close however it entered/exited on the NEXT day's close per Trading Blox description which funny enough improved performance quite a bit but is it possible to enter a trade on that day? I guess I could say use previous days MA which would give me a day lag for the entry. Thoughts?
Exit All Units on Close
Re: Exit All Units on Close
Hi HWG, I don't think you can accomplish that with endofday data. Unfortunately I don't have experience with Trading Blox and intraday data. Perhaps someone else can pick up this conversation with you. But may I suggest...
Why not set your alarm for 3:55pm (if you are trading stocks in the eastern time zone) and close your positions if it meets your exit criteria? I would suggest setting your Trading Blox Slippage above 0 so that your simulations approximate what happens in real life. Either that or close your positions at tomorrow's open, much easier.
Why not set your alarm for 3:55pm (if you are trading stocks in the eastern time zone) and close your positions if it meets your exit criteria? I would suggest setting your Trading Blox Slippage above 0 so that your simulations approximate what happens in real life. Either that or close your positions at tomorrow's open, much easier.
Re: Exit All Units on Close
This is what I have done:
Let's say this is a simple moving average of 20 bars.  I have done this for RSI with end of day data and it works just fine: Based on this example I would take the last 19 known closes and put it in a BPV series and use yesterday's close as the assumed close for tonight and calculate the 20day SMA. Let's say you want a higher close then the SMA  there are 2 outcomes after calculating the SMA with this 20 values: the assumed close is already higher or it is lower then the SMA.
IF this assumed close is lower then the SMA and you want it higher : You can then code a loop in which you take this assumed close, increase the close by one tick, calculate the SMA again and check if it is higher. You repeat that until you find the first assumed close which is higher then the SMA. That is the close you are looking for.  If the first assumed close is already higher then the SMA you run through a similar loop but you subtract one tick until you find the first value which is lower then the SMA. You will then use the previous value which was the last close above the SMA.
In order not to over load my code I have put this into a custom script.
I hope this all makes sense.
Let's say this is a simple moving average of 20 bars.  I have done this for RSI with end of day data and it works just fine: Based on this example I would take the last 19 known closes and put it in a BPV series and use yesterday's close as the assumed close for tonight and calculate the 20day SMA. Let's say you want a higher close then the SMA  there are 2 outcomes after calculating the SMA with this 20 values: the assumed close is already higher or it is lower then the SMA.
IF this assumed close is lower then the SMA and you want it higher : You can then code a loop in which you take this assumed close, increase the close by one tick, calculate the SMA again and check if it is higher. You repeat that until you find the first assumed close which is higher then the SMA. That is the close you are looking for.  If the first assumed close is already higher then the SMA you run through a similar loop but you subtract one tick until you find the first value which is lower then the SMA. You will then use the previous value which was the last close above the SMA.
In order not to over load my code I have put this into a custom script.
I hope this all makes sense.
Re: Exit All Units on Close
I think that makes sense to me but there is no way I can code that up. I don't have much experience with the looping function. Any chance you'd care to share? Understand if you don't want to.
Another issue is that some of my worst trades on the backtest are ones that are entered immediately when the test starts so Day 1 if my price is above where it needs to be it enters no matter how big the gap to the Moving Average. Is there a way to ignore that?
Another issue is that some of my worst trades on the backtest are ones that are entered immediately when the test starts so Day 1 if my price is above where it needs to be it enters no matter how big the gap to the Moving Average. Is there a way to ignore that?

 Site Admin
 Posts: 8559
 Joined: Tue Apr 06, 2004 1:41 pm
 Location: Boston, MA
 Contact:
Re: Exit All Units on Close
You can always compute the close at which the moving average will equal a set value x and place the stop close order at that price.
Or you could use the After Instrument Open script to place your order (for back testing only). In this script you have access to the current OHLC.
n = days in moving average
For an SMA: Next close = desired moving average value * n  ( sum of the last n1 days closes )
Or you could use the After Instrument Open script to place your order (for back testing only). In this script you have access to the current OHLC.
n = days in moving average
For an SMA: Next close = desired moving average value * n  ( sum of the last n1 days closes )