indiTraders - Forum for the Active Indian Trader  

Go Back   indiTraders - Forum for the Active Indian Trader > Tools > Software > Matlab

FXPrimus
Reply
 
Bookmark and Share LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 20-08-2010, 07:39 PM
Oxymoron's Avatar
Oxymoron Oxymoron is offline
Super Moderator
 
Join Date: Mar 2009
Location: Chennai
Posts: 1,275
Thanks: 4,471
Thanked 4,830 Times in 1,156 Posts
Oxymoron has a reputation beyond repute
Oxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond repute
Default Matlab Code : A Simple Stock Rotation Model

It's not exactly the most elegant of codes but I'm still a beginner!

Just change the code in
1) Load data section
2) Input section - no of stocks and no of long positions to take at a time
3) Create as many variables in the 'individual stock return, position and long return section

Code:
% Simple Stock Rotation Model
% Ranks stocks based on previous period return and goes long on 'LongCutOff' number of stocks

%load data
load RotationData.mat;

%Inputs
NoOfStocks = 6;
LongCutOff = 2;
EquityPerStock = 100000;

%Log Transform and First Difference
LData = log(data);
LDataDiff = LData - lagmatrix(LData,1);

%Individual Stock Returns
StockALRet = LDataDiff(:,1);
StockBLRet = LDataDiff(:,2);
StockCLRet = LDataDiff(:,3);
StockDLRet = LDataDiff(:,4);
StockELRet = LDataDiff(:,5);
StockFLRet = LDataDiff(:,6);

%Rank Stocks According to Strength over Last Month
[s x]=sort(LDataDiff,2,'descend');    % Sorts it matlab style
[J I]=ndgrid(1:size(x,1),1:size(x,2));% creates a grid from 1:c & 1:r
x(sub2ind(size(x),J,x))=I;

clear s J I

 % First row of position is a NaN
x(1,:) = NaN;

%Lag Position by one period
Position = lagmatrix(x,1);

%Long Position
L = Position < (LongCutOff + 1);

%Individual Stock Position
StockAPos = L(:,1);
StockBPos = L(:,2);
StockCPos = L(:,3);
StockDPos = L(:,4);
StockEPos = L(:,5);
StockFPos = L(:,6);

[m1 m2] = size(Position);

clear L x Position m2

%Individual Stock Long Return
ReturnA = EquityPerStock*exp(sum(StockALRet(StockAPos)));
ReturnB = EquityPerStock*exp(sum(StockBLRet(StockBPos)));
ReturnC = EquityPerStock*exp(sum(StockCLRet(StockCPos)));
ReturnD = EquityPerStock*exp(sum(StockDLRet(StockDPos)));
ReturnE = EquityPerStock*exp(sum(StockELRet(StockEPos)));
ReturnF = EquityPerStock*exp(sum(StockFLRet(StockFPos)));

format bank;

AccountValue = ReturnA + ReturnB + ReturnC + ReturnD + ReturnE + ReturnF
AccountProfit = AccountValue - EquityPerStock*NoOfStocks
PcttRetPerPeriod =100*((AccountValue/(EquityPerStock*NoOfStocks))^(1/m1) - 1)

clear StockALRet StockBLRet StockCLRet StockDLRet StockELRet StockFLRet
clear LData LDataDiff LongCutOff ShortCutOff EquityPerStock
clear StockAPos StockBPos StockCPos StockDPos StockEPos StockFPos
clear ReturnA ReturnB ReturnC ReturnD ReturnE ReturnF m1
Returns
1) Final Account Value
2) Percentage return over the whole period
3) Percentage return per period of study (geometric)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following 6 Users Say Thank You to Oxymoron For This Useful Post:
  #2 (permalink)  
Old 21-08-2010, 10:53 AM
Oxymoron's Avatar
Oxymoron Oxymoron is offline
Super Moderator
 
Join Date: Mar 2009
Location: Chennai
Posts: 1,275
Thanks: 4,471
Thanked 4,830 Times in 1,156 Posts
Oxymoron has a reputation beyond repute
Oxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond repute
Default

Reworked the code in a more workable way and included short positions.
Just change the 'load data' section and 'input section'.

Code:
% Simple Stock Rotation Model
% Ranks stocks based on previous period return and goes long on 
% top 'LongCutOff' number of stocks and short on bottom
% 'ShortCutOff' number of stocks

%load data
load RotationData.mat;

%Inputs
NoOfStocks = 6;
LongCutOff = input('Enter LongCutOff:');
ShortCutOff = input('Enter ShortCutOff:');
Equity = 100000;

%Log Transform and First Difference
ROC = 1 + (data - lagmatrix(data,1))./lagmatrix(data,1);

%Rank Stocks According to Strength over Last Month
[s x]=sort(ROC,2,'descend');    % Sorts it matlab style
[J I]=ndgrid(1:size(x,1),1:size(x,2));% creates a grid from 1:c & 1:r
x(sub2ind(size(x),J,x))=I; 

clear s J I

 % First row of position is a NaN
x(1,:) = NaN;

%Lag Position by one period
Position = lagmatrix(x,1);

%Long Position
L = Position > (NoOfStocks - LongCutOff);

%Short Position
S = Position < (ShortCutOff + 1) ;

[m1 m2] = size(Position);

PeriodReturnMatrix = (ROC - 1).*L + (1-ROC).*S;
PeriodReturnMatrix(2,:) = NaN;
PeriodReturn = sum(PeriodReturnMatrix,2)/(LongCutOff + ShortCutOff);
PeriodReturn(isnan(PeriodReturn)) = 0;
PeriodReturn = 1 + PeriodReturn;
clear L m2 x Position

format bank; %verified and correct for longs

AccountGrowth = Equity*cumprod(PeriodReturn);
AccountProfit = AccountGrowth - Equity;
FinalAccountValue = Equity*prod(PeriodReturn)
PerPeriodPctReturn = 100*((FinalAccountValue/Equity)^(1/m1) - 1)

clear ROC LongCutOff ShortCutOff EquityPerStock m1 NoofStocks
clear NoOfStocks S Equity
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following 3 Users Say Thank You to Oxymoron For This Useful Post:
  #3 (permalink)  
Old 24-08-2010, 11:51 PM
kkseal kkseal is offline
Hip-py
 
Join Date: Apr 2009
Posts: 4,978
Thanks: 6,571
Thanked 9,571 Times in 4,123 Posts
kkseal has a reputation beyond reputekkseal has a reputation beyond repute
kkseal has a reputation beyond reputekkseal has a reputation beyond reputekkseal has a reputation beyond reputekkseal has a reputation beyond reputekkseal has a reputation beyond reputekkseal has a reputation beyond reputekkseal has a reputation beyond reputekkseal has a reputation beyond reputekkseal has a reputation beyond reputekkseal has a reputation beyond reputekkseal has a reputation beyond reputekkseal has a reputation beyond reputekkseal has a reputation beyond repute
Default

I use a Ami code to rank stocks on a no. of strength & performance parameters Attached top 100 (as on end of last wk) from F&O & a wider universe of 800 stocks (weak ones can be picked bottom-up from the same scan)
Attached Files
File Type: xls Top100-FnO.xls (16.0 KB, 30 views)
File Type: xls Top100-Margin.xls (5.6 KB, 26 views)
__________________
'Growth & Value are joined at the hip' - Warren buffet
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following 7 Users Say Thank You to kkseal For This Useful Post:
Reply

indiTraders - Forum for the Active Indian Trader > Tools > Software > Matlab


Tags
matlab, rotational trading, stock rotation

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


FXPrimus

All times are GMT +5.5. The time now is 03:38 PM.


vBulletin Copyright by vBulletin

Content Relevant URLs by vBSEO 3.3.2