PureBytes Links
Trading Reference Links
|
Hello All,
I am stuck on what was supposed to be some very simple code,
and grew into a problem. I wonder if someone would be kind
enough to give me some advise. Details are below, but to make a long story
short:
Here is what I am trying to code (simple moving average
reversal - buy when average turns up):
buy = today's MA > yesterday's MA
AND
yesterday's MA < MA of most recent day
before yesterday whose MA was not equal to yesterday's
Here is what I have so far:
buy = ma(close,30) > ref(ma(close,30),-1)
AND
ref(ma(close,30),-1) < ? (this is the tough
part)
Tomasz says that 90% (or whatever) of things can be coded
without using scripts. Can this one be done in AFL? It looks like I need to
iterate the database, right?
OK, here comes the whole sad story for anyone who is
interested:
I originally thought this would be very simple to code,and I
coded it as follows:
buy = ma(close,30) > ref(ma(close,30),-1)
AND
ref(ma(close,30),-1) <
ref(ma(close,30),-2)
After I ran this, and was looking at the resulting trade arrows on the
chart, it was obvious that I was missing some trades (and some BIG price
moves). I did a quick exploration to debug it, which revealed that (for
many stocks, over a long period) there are MANY instances where the moving
average does not change for 2, 3, even 10 or more days in a row. It
remains the same right out to 9 decimal places! One reason for this appears
to be that for some stocks, even though they trade frequently enough now, they
would sometimes go for days without a trade in their early days. There may be
other reasons too, I didnt follow up on all of them. So this brings me to my
current situation - my original simple code only works correctly if the MA
changes every day, which sometimes it doesnt. In order to make this work
properly, I need to look back, day-by-day, starting with the
day-before-yesterday, until I come to a day whose moving average is different
than yesterday's, and then use that days MA in my formula. Any advise is greatly
appreciated. Thanks very much!
Steve
|