Exit All Units on Close

Discussions about the testing and simulation of mechanical trading systems using historical data and other methods. Trading Blox Customers should post Trading Blox specific questions in the Customer Support forum.
Post Reply
HWG
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Fri Jun 04, 2010 10:21 am
Location: Omaha, NE
Contact:

Exit All Units on Close

Post by HWG »

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?
fab1usa1
Roundtable Knight
Roundtable Knight
Posts: 383
Joined: Sat May 21, 2011 8:30 pm
Location: New York

Re: Exit All Units on Close

Post by fab1usa1 »

Hi HWG, I don't think you can accomplish that with end-of-day 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.
zug7
Full Member
Full Member
Posts: 16
Joined: Fri Dec 24, 2004 3:34 am
Location: New Zealand

Re: Exit All Units on Close

Post by zug7 »

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 20-day 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.
HWG
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Fri Jun 04, 2010 10:21 am
Location: Omaha, NE
Contact:

Re: Exit All Units on Close

Post by HWG »

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?
Tim Arnold
Site Admin
Site Admin
Posts: 9015
Joined: Tue Apr 06, 2004 1:41 pm
Location: Boston, MA
Contact:

Re: Exit All Units on Close

Post by Tim Arnold »

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 n-1 days closes )
Post Reply