A Simple Stock Rotation Model - Matlab, R project and Python | futures io social day trading
futures io futures trading


A Simple Stock Rotation Model
Updated: Views / Replies:2,954 / 2
Created: by Oxymoron Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

A Simple Stock Rotation Model

  #1 (permalink)
Just starting out...
Chennai
 
Futures Experience: Intermediate
Platform: Matlab, Multicharts
 
Posts: 7 since Apr 2010
Thanks: 10 given, 5 received

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)

Looking for reviews on the code accuracy


Last edited by Oxymoron; August 20th, 2010 at 10:51 AM.
Reply With Quote
The following 2 users say Thank You to Oxymoron for this post:
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Just starting out...
Chennai
 
Futures Experience: Intermediate
Platform: Matlab, Multicharts
 
Posts: 7 since Apr 2010
Thanks: 10 given, 5 received


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 = 2;
ShortCutOff = 2;
Equity = 100000;

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

%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);

%Short Position
S = Position > (NoOfStocks - ShortCutOff);

[m1 m2] = size(Position);

PeriodReturnMatrix = exp(LDataDiff).*L + exp(-LDataDiff).*S;
PeriodReturnMatrix(2,: ) = NaN;
PeriodReturn = sum(PeriodReturnMatrix,2)/(LongCutOff + ShortCutOff);
PeriodReturn(isnan(PeriodReturn)) = 1;

clear L m2 x Position

format bank;

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

clear LData LDataDiff LongCutOff ShortCutOff EquityPerStock m1 NoofStocks
clear NoOfStocks S Equity

Reply With Quote
The following user says Thank You to Oxymoron for this post:

Reply



futures io > > > > A Simple Stock Rotation Model

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need simple code for StochRSI please TraderTom NinjaTrader Programming 5 May 6th, 2014 06:36 PM
SMA Simple Trading websouth NinjaTrader 5 July 14th, 2012 09:56 AM
Simple RSI Strategy frostengine NinjaTrader Programming 2 October 30th, 2011 01:12 PM
Keeping it Simple Silver Dragon Trading Journals 26 September 19th, 2011 06:00 PM
Simple strategy vast NinjaTrader Programming 19 August 5th, 2010 06:39 PM


All times are GMT -4. The time now is 04:05 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts
Page generated 2017-12-11 in 0.07 seconds with 19 queries on phoenix via your IP 54.226.113.250