indiTraders - Forum for the Active Indian Trader  

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

GFDL
Reply
 
Bookmark and Share LinkBack (5) Thread Tools Display Modes
  5 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 28-12-2010, 10:35 AM
Oxymoron's Avatar
Oxymoron Oxymoron is offline
Super Moderator
 
Join Date: Mar 2009
Location: Chennai
Posts: 1,275
Thanks: 4,471
Thanked 4,828 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 Basket Optimization in Matlab

Data Arrangement : Closing price of N stocks, arranged as T*N matrix (name this matrix as 'data' and save it as MABasketOptData.mat if you don't want to modify the code below).
Example System : MA-Price crossover
Customizable Parameters: Initial Capital, Execution Lag, Starting Optimization Value, Increments, Ending Optimization Value, Transaction Cost
Optimization Objective: Maximize slope of equity curve, calculated via least squares regression (without intercept term), with respect to time (1,2...T)

Results Image:


Code:
load MABasketOptData.mat
tic
format bank

%Inputs
InitialCapital = 100000;
ExecutionLag = 0; %In days
BeginOpt = 10;
EndOpt = 100;
Increments = 20;
MAPds = BeginOpt:Increments:EndOpt;
TransactionCost = .3; % In Percentage


[~,StocksInPortfolio] = size(data);

BSFinalEquityValue = zeros(StocksInPortfolio,1);
BSSharpeRatio = zeros(StocksInPortfolio,1);
BSPercentProfitable = zeros(StocksInPortfolio,1);
BSDrawdown = zeros(StocksInPortfolio,1);
BSBeta = zeros(StocksInPortfolio,1);
BSBestMAInput = zeros(StocksInPortfolio,1);
Trades = zeros(StocksInPortfolio,1);

for StockNumber = 1:StocksInPortfolio
Input = data(:,StockNumber);

[rows,~] = size(Input);
[~,columns] = size(MAPds);
Time = cumsum(ones(rows,1));

%Variables
MovAvg = zeros(rows,1);
position = zeros(rows,1);
returns = zeros(rows,1);
FinalEquityValue = zeros(columns,1);
SharpeRatio = zeros(columns,1);
PercentProfitable  = zeros(columns,1);
Drawdown  = zeros(columns,1);
Beta  = zeros(columns,1);

for j = 1:columns
    for i = MAPds(1,j):rows
        
        %Calculation of Moving Average
        MovAvg(i,1) = sum(Input(i-(MAPds(1,j)-1):i,1))/MAPds(1,j);
        
        %Calculation of Position Direction
        if Input(i,1) > MovAvg(i,1)
            position(i,1) = 1;
        else
            position(i,1) = 0;
        end
        
        %Calculation of returns
        returns(i,1) = Input(i,1)/Input(i-1,1);
    end
    
    %Calculation of Percent Profitable
    EntryPrice = Input(position > lagmatrix(position,1));
    ExitPrice  = Input(position < lagmatrix(position,1));
    [NoOfTrades,~] = size(ExitPrice);
    
    PositionProfit = EntryPrice(1:NoOfTrades) < ExitPrice(1:NoOfTrades);
    
    Signal = or(position > lagmatrix(position,1),position < lagmatrix(position,1));
    TransactionHit = Signal.*TransactionCost/100;
    
    %Time Series Statistics
    positionLag = lagmatrix(position,ExecutionLag+1);
    PositionalReturns = 1 + positionLag.*(returns - 1) - TransactionHit;
    PositionalReturns(isnan(PositionalReturns)) = 1;
    EquityCurve = InitialCapital*cumprod(PositionalReturns);
    
    %Specific Statistics
    FinalEquityValue(j,1) = EquityCurve(end);
    SharpeRatio(j,1) = (EquityCurve(end) - InitialCapital)/std(EquityCurve);
    PercentProfitable(j,1) = sum(PositionProfit)/NoOfTrades;
    Drawdown(j,1) = maxdrawdown(EquityCurve);
    Beta(j,1) = regress(log(EquityCurve),Time);
end
%%%%%%%%%%%%%%%%%%%%%%%%%End of Optimization Stage%%%%%%%%%%%%%%%%%%%%%%%%%

%Find the best combination
[j,~] = find(Beta == max(Beta));
j = j(1,1);

for i = MAPds(1,j):rows
    
    %Calculation of Moving Average
    MovAvg(i,1) = sum(Input(i-(MAPds(1,j)-1):i,1))/MAPds(1,j);
    
    %Calculation of Position Direction
    if Input(i,1) > MovAvg(i,1)
        position(i,1) = 1;
    else
        position(i,1) = 0;
    end
    
    %Calculation of returns
    returns(i,1) = Input(i,1)/Input(i-1,1);
end

%Calculation of Percent Profitable
EntryPrice = Input(position > lagmatrix(position,1));
ExitPrice  = Input(position < lagmatrix(position,1));
[NoOfTrades,~] = size(ExitPrice);

PositionProfit = EntryPrice(1:NoOfTrades) < ExitPrice(1:NoOfTrades);

Signal = or(position > lagmatrix(position,1),position < lagmatrix(position,1));
TransactionHit = Signal.*TransactionCost/100;

%Time Series Statistics
positionLag = lagmatrix(position,ExecutionLag+1);
PositionalReturns = 1 + positionLag.*(returns - 1) - TransactionHit;
PositionalReturns(isnan(PositionalReturns)) = 1;
EquityCurve = InitialCapital*cumprod(PositionalReturns);

%Specific Statistics
BSFinalEquityValue(StockNumber,1) = EquityCurve(end);
BSSharpeRatio(StockNumber,1) = (EquityCurve(end) - InitialCapital)/std(EquityCurve);
BSPercentProfitable(StockNumber,1) = 100*sum(PositionProfit)/NoOfTrades;
BSDrawdown(StockNumber,1) = maxdrawdown(EquityCurve)*100;
BSBeta(StockNumber,1) = regress(log(EquityCurve),Time);
BSBestMAInput(StockNumber,1) = MAPds(1,j);
Trades(StockNumber,1) = NoOfTrades;

end

StockNumber = cumsum(ones(StocksInPortfolio,1));

Statistics = [StockNumber BSBestMAInput,BSFinalEquityValue,BSSharpeRatio,Trades,BSPercentProfitable,BSDrawdown];
disp('        StockNo    BestMAInput   EquityVal     SharpeRatio      Trades      %Profitable     Drawdown%');
disp(Statistics);

clear PositionalReturns EntryPrice ExitPrice ExecutionLag TransactionHit TransactionCost Signal i position positionLag
clear rows returns PositionProfit MovAvg BeginOpt Beta Drawdown EndOpt EquityCurve FinalEquityValue Increments MAPds NoOfTrades
clear Time columns j StockNumber SharpeRatio PercentProfitable Input StockNumber  BSBestMAInput  BSFinalEquityValue  
clear BSSharpeRatio  BSPercentProfitable  BSDrawdown  StocksInPortfolio
toc
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following 4 Users Say Thank You to Oxymoron For This Useful Post:
  #2 (permalink)  
Old 28-12-2010, 02:29 PM
uasish's Avatar
uasish uasish is offline
Super Moderator
 
Join Date: Mar 2009
Location: Kolkata,India.
Posts: 7,141
Thanks: 8,455
Thanked 14,341 Times in 4,989 Posts
uasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond repute
uasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond reputeuasish has a reputation beyond repute
Default

I wonder how this Whizkid masters these things so fast Hats Off.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following User Says Thank You to uasish For This Useful Post:
Reply

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


Tags
basket optimization, efficient coding, equity curve, matlab, optimization, regression

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


LinkBacks (?)
LinkBack to this Thread: http://www.inditraders.com/matlab/4191-basket-optimization-matlab.html
Posted By For Type Date
Basket Optimization in Matlab - indiTraders - Forum for the Active... | Share on LinkedIn This thread Refback 03-01-2011 10:06 PM
Basket Optimization in Matlab - indiTraders - Forum for the Active... | Share on LinkedIn This thread Refback 28-12-2010 03:37 PM
Basket Optimization in Matlab - indiTraders - Forum for the Active... | Share on LinkedIn This thread Refback 28-12-2010 03:36 PM
Basket Optimization in Matlab - indiTraders - Forum for the Active... | Share on LinkedIn This thread Refback 28-12-2010 03:29 PM
Basket Optimization in Matlab - indiTraders - Forum for the Active... | Share on LinkedIn This thread Refback 28-12-2010 02:56 PM

GFDL

All times are GMT +5.5. The time now is 07:26 AM.


vBulletin Copyright by vBulletin

Content Relevant URLs by vBSEO 3.3.2