PDA

View Full Version : Timing issues with ref() and signals


dlucas
22nd August 2006, 07:46 AM
I am having a few difficulties in the timing of entry signals. I am looking at a system that when backtested with TradeSim gives solid (although not astonishing) results over 10-15 years of data and hundreds of stocks.

Initially I created this as a system that generates a signal based on the close and then also buys at the close. While in reality you could probably achieve something like this - by buying several minutes before the close if the signal had already been generated during the trading day - a more realistic and less demanding approach would be to test the system on the assumption that you buy at the open of the following day.

If this were a moving average system one way to implement it might be this:

EntryTrigger := Cross(Mov(C,10,S),Mov(C,20,S));
EntryPrice := Ref(OPEN,1);
ExitTrigger := Cross(Mov(C,20,S),Mov(C,10,S));
ExitPrice := Ref(OPEN,1);

As you can see, I am using Ref() to point into the "future" so that if the buy signal occurred during backtesting on 1 Jan 2002, the entry is on 2 Jan 2002. This approach gives results that are very similar to those of the "buy at close" system, which is what I would expect, because the open of the following day does not appear to be consistently higher or lower than the close of the day a signal is generated.

However, a more natural approach (to my mind anyway) would be to use Ref() on the signal, which in this example is the moving average, rather than on the entry price. So, I take the MAs for the previous day (yesterday) and see if they cross, then buy at the open of the next day (today). This is how I implemented it:

EntryTrigger:=Cross(Ref(Mov(C,10,S),-1),Ref(Mov(C,20,S),-1));
EntryPrice:= OPEN;
ExitTrigger:= Cross(Ref(Mov(C,20,S),-1),Ref(Mov(C,10,S),-1));
ExitPrice:= OPEN;

When I tried this latter formulation I got some very strange results. Basically, the system - which was profitable previously - was useless. Not only that but it generated an equity curve that, for the same data, seems to be the inverse of that generated by the profitable system. It looks almost as if the system is selling on buy signals. Yet when I look at the above syntax it still seems correct to me.

I suspect that there is an apparently small but fundamental mistake in the way I am approaching this that is causing the problem, but can anybody suggest what it might be? Failing that, can anybody reassure me that the first approach using ref(open,1) for the entry price is conceptually sound?

Regards
Dan

Jose
23rd August 2006, 01:52 AM
This is how I would delay the entry/exit signals:

delay:=1;

EntryTrigger:=
Ref(Cross(Mov(C,10,S),Mov(C,20,S)),-delay);
EntryPrice:= OPEN;
ExitTrigger:=
Ref(Cross(Mov(C,20,S),Mov(C,10,S)),-delay);
ExitPrice:= OPEN;

{ Signal test }
{EntryTrigger-ExitTrigger}


It's always good to plot the signals as a MetaStock indicator, so that they can be verified.

It's amazing how quickly profits evaporate once realistic trade parameters are introduced, such as brokerage, slippage (specially in volatile trade periods), and signals triggered after market hours.


jose '-)

dlucas
23rd August 2006, 08:07 AM
This is how I would delay the entry/exit signals:

delay:=1;

EntryTrigger:=
Ref(Cross(Mov(C,10,S),Mov(C,20,S)),-delay);
EntryPrice:= OPEN;
ExitTrigger:=
Ref(Cross(Mov(C,20,S),Mov(C,10,S)),-delay);
ExitPrice:= OPEN;



Many thanks for this Jose. This syntax, when applied to the system, does give very poor results, just like my own effort.

On the other hand, this confuses me even more. I tried to use this to duplicate the "successful" system. To do so I changed the exit and entry prices to Ref(C,-delay) so that the system is now using your syntax but it is looking for a cross on the previous day and entering the trade at the close of that same day. That is...

EntryTrigger:=
Ref(Cross(Mov(C,10,S),Mov(C,20,S)),-delay);
EntryPrice:= Ref(C,-delay);
ExitTrigger:=
Ref(Cross(Mov(C,20,S),Mov(C,10,S)),-delay);
ExitPrice:= Ref(C,-delay);

The "original" version was like this, not looking back in time but finding and entering on the same day...

EntryTrigger := Cross(Mov(C,10,S),Mov(C,20,S));
EntryPrice := CLOSE;
ExitTrigger := Cross(Mov(C,20,S),Mov(C,10,S));
ExitPrice := CLOSE;

Surely these two versions are effectively the same...? They both refer to catching a signal and entering on the same day and therefore they should both give the same or similar results - but they don't!

Dan

Jose
23rd August 2006, 01:44 PM
Dan, the easiest way to find out why the two delayed versions are different (and they really are different), is to plot the code as a visual indicator, on/below a chart. A chart is worth a thousand words...


jose '-)

dlucas
23rd August 2006, 07:32 PM
A chart is worth a thousand words...
Fair enough, I will start using my eyes!

Dan