I am happy to share code with the other fans of this book but what I was hoping for was that we go over the rules and code together. The author is the first to admit there are many different ways to construct a momentum stock portfolio. The measures of momentum, risk, volatility, market regime etc can all have different definitions.
On top of that, with AmiBroker you can almost always arrive at the same end point via different routes. So perhaps different members could improve upon the code. At the end of the exercise maybe we will have a "better" code and an improved model. Or perhaps a couple of versions to choose from.
To Bruce, I have the paperback and in my book though the model is discussed the rules are summarized ("The Exact Trading Rules") beginning on page 100.
//////////////////////////////////////////////////
Trade one day a week, in the book he randomly says to trade every Wednesday. In a series of follow up emails and a Skype conversation (though the discussion was mostly unrelated to this book and strategy) Mr. Clenow thinks it might be simplified to trade every two weeks or even every month without a significant change in performance.
Certainly it is a simple thing to identify the first trading day of the week, so let's start with code for that. Here are two versions, choose your favorite or create a better one.
Credit to Aron P for this first one.
// Setting the day of the week for trading
function firstTradingDay( timeframe ) // this finds the first trading day of the timeframe
{
TimeFrameSet( timeframe ) ; // 'timeframe' I would commonly use are inDaily, inWeekly, inMonthly
lastday = 1;
TimeFrameRestore( timeframe );
lastday = TimeFrameExpand( lastday , timeframe , expandpoint ) ;
// expands time-compressed array from 'timeframe' (inWeekly) to base timeframe (daily)
// expandPoint - the resulting array gets not empty values only for the last bar within given period (all remaining bars are Null (empty))
// so this is finding the last day in the week (or selected 'timeframe') and setting it to lastday variable
return Ref( lastday , -1 );
}
Rotate = firstTradingDay( inweekly ) ;
Rotate = Nz( Rotate ); // Converts Null/Nan/Infinity values to zero
Credit to fxshrat for this second one.
function firstdayofweek() {
global dow;
dow = DayOfWeek();
SOW = dow < Ref( dow, -1 );
return SOW;
}
flag = Nz(firstdayofweek());
And I personally like the idea of rotation every 2nd week, so my code modification, (also see http://amibrokerforum.proboards.com/thread/75/start-period-recognition)
/////////////////////////////////////////////////
// Designed with a bi-weekly rotation system in mind
// First trading day of every other week identifier
/////////////////////////////////////////////////
function firstdayofweek() {
global dow;
dow = DayOfWeek();
SOW = dow < Ref( dow, -1 );
return SOW;
}
flag = Nz(firstdayofweek());
countflag=Cum(flag); // count firstdayofweek
// rotate only on firstdayofweek, every 2nd one
rotation=flag AND (countflag%2==0);//using the modulus we find days with remainder zero
// to explore the results
Filter = 1;//
AddColumn( C,"Price", 1.2, -1, -1 );
AddColumn(flag,"flag");
AddColumn( dow,"Weekday", 1, colorGrey50, IIf( flag, colorDarkGreen, colorDefault) );
AddColumn(countflag,"countflag");
AddColumn(rotation,"Rotation Day",1,colorBlue, IIf(rotation,colorYellow,colorDefault));
// rotate only on firstdayofweek, every 2nd one
rotation=flag AND (countflag%2==0);//using the modulus we find days with remainder zero
// to explore the results
Filter = 1;//
AddColumn( C,"Price", 1.2, -1, -1 );
AddColumn(flag,"flag");
AddColumn( dow,"Weekday", 1, colorGrey50, IIf( flag, colorDarkGreen, colorDefault) );
AddColumn(countflag,"countflag");
AddColumn(rotation,"Rotation Day",1,colorBlue, IIf(rotation,colorYellow,colorDefault));
Suggestions/criticisms/different ideas ? More rules to follow when I get time this weekend.
Larry
Attachment(s) from portfoliobuilder99@gmail.com [amibroker] | View attachments on the web
1 of 1 Photo(s)
Posted by: portfoliobuilder99@gmail.com
Reply via web post | • | Reply to sender | • | Reply to group | • | Start a New Topic | • | Messages in this topic (11) |
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
EmoticonEmoticon