Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
The Investment Strategy Based on Attention--Zirong You

% Monthly Data  
% Construct the coming SCI monthly returns based on previous SCI monthly returns
clc  
clear  
[NUMERIC,TXT,RAW]=xlsread('\path\IDX_Idxtrdmth.xlsx','Shanghai Composite Index');
[a b]=size(NUMERIC);
for m=1:72  
    NUMERIC1=NUMERIC(:,7);  
    NUMERIC2(m,:)=NUMERIC1(randint(1,1,[1 a]));  
    % We randomly choose a previous SCI monthly return between -1 and 1 to be the coming SCI monthly return.  
    if NUMERIC1(randint(1,1,[1 a]))<1 & NUMERIC1(randint(1,1,[1 a]))>-1  
       NUMERIC2(m,:)=NUMERIC1(randint(1,1,[1 a]));  
    end  
    NUMERIC3=[NUMERIC1; NUMERIC2];  
end  
% Find the data which satisfies the two requirements of 'dual reversal' timing strategy model.
for n=185:a  
    if mean(NUMERIC(n-2:n,7))<0 & NUMERIC(n,9)<nanmean(NUMERIC(:,9))  
        for k=1:24  
            ALLRET(n,k)=sum(NUMERIC3(n+1:n+3*k,:));  
        end  
    end  
end  
[c,d]=size(ALLRET);
% Calculate all the discription statistics.
for j=1:1:d  
    Neg(:,j)=length(find(ALLRET(:,j)<0));  
    Pos(:,j)=length(find(ALLRET(:,j)>0));  
    Zero(:,j)=c-Neg(:,j)-Pos(:,j);  
    Max(:,j)=max(ALLRET(:,j));  
    Min(:,j)=min(ALLRET(:,j));  
    Sum(:,j)=sum(ALLRET(:,j));  
    Mean(:,j)=sum(ALLRET(:,j))/(Pos(:,j)+Neg(:,j));  
    Std(:,j)=std(ALLRET(:,j));  
    Sharpe(:,j)=Mean(:,j)/Std(:,j);  
    Success(:,j)=Pos(:,j)/(Pos(:,j)+Neg(:,j));  
end  
% Calculate the average SCI monthly return and the monthly maximum loss. 
for l=1:24  
Monthly(:,l)=Mean(:,l)/(3*l);  
Monthloss(:,l)=Min(:,l)/(3*l);  
end  
% Collect all the results into the table named 'Results'.
Results=[ALLRET;Neg;Pos;Zero;Max;Min;Monthloss;Sum;Mean;Monthly;Std;Sharpe;Success];

% Daily Data (based on 5-day time window)
% Construct the coming SCI daily returns based on previous SCI daily returns.
% Determine the lagged periods of both previous SCI daily return and Baidu Index based on R-square.
clc  
clear  
[NUMERIC,TXT,RAW]=xlsread('\path\IDX_Idxtrdmth.xlsx','Daily Data');
x1=[];x2=[];Y=[];  
[a b]=size(NUMERIC);
for i=1:12  
    for j=1:12  
        k=max(i,j);  
for n=1:a-k  
    x1(n,:)=mean(NUMERIC(k-i+1:k+n-1,1));  
    x2(n,:)=log(mean(NUMERIC(k-j+1:k+n-1,2)))/100;  
Y(n,:)=NUMERIC(k+n,2);  
end  
X=[x1 x2];  
stats = regstats(Y,X);  
beta=stats.beta';  
t=stats.tstat.t';  
R2=stats.rsquare;  
R20(:,j)=stats.rsquare;  
Results0(j,:)=[beta NaN t NaN R2];  
    end  
Results1(i,:)=R20;% R-square  
Results(13*i-12:13*i-1,:)=Results0; %beta, t-statistic, R-square  
end

% Construct 5-day time window 'dual momentum' timing strategy model.
% The code of 10-day time window 'dual momentum' timing strategy model are the same. Here we just present 5-day model.
clc  
clear  
% Find the data which satisfies the two requirements of 'dual momentum' timing strategy model.
[NUMERIC,TXT,RAW]=xlsread('\path\IDX_Idxtrdmth.xlsx','Daily Data');
[a b]=size(NUMERIC);
for n=10:a-10  
    if mean(NUMERIC(n-4:n,1))>0 & NUMERIC(n,3)>nanmean(NUMERIC(:,3))  
        for k=1:10  
            ALLRET(n+1,k)=sum(NUMERIC(n+1:n+k,1));  
        end  
    end  
end  
[c,d]=size(ALLRET);
% Calculate all the discription statistics.
for j=1:1:d  
    Neg(:,j)=length(find(ALLRET(:,j)<0));  
    Pos(:,j)=length(find(ALLRET(:,j)>0));  
    Zero(:,j)=c-Neg(:,j)-Pos(:,j);  
    Max(:,j)=max(ALLRET(:,j));  
    Min(:,j)=min(ALLRET(:,j));  
    Sum(:,j)=sum(ALLRET(:,j));  
    Mean(:,j)=sum(ALLRET(:,j))/(Pos(:,j)+Neg(:,j));  
    Std(:,j)=std(ALLRET(:,j));  
    Sharpe(:,j)=Mean(:,j)/Std(:,j);  
    Success(:,j)=Pos(:,j)/(Pos(:,j)+Neg(:,j));  
    for i=1:c  
        Asum(i,j)=sum(ALLRET(1:i,j)); % Calculate accumulative SCI daily returns. (5-day rolling)  
    end  
end  
for i=1:10  
    Max_Recall(1,i)=min(Min(:,1:i)); % Calculate maximum retracement.  
end  
for l=1:10  
Dayly(:,l)=Mean(:,l)/(l);  
Dayloss(:,l)=Min(:,l)/(l);  
end  
% Collect all the results into the table named 'Results'.
Results=[ALLRET;Neg;Pos;Zero;Max;Min;Dayloss;Sum;Mean;Dayly;Std;Sharpe;Success;Max_Recall];



> Blockquote

4 responses

Abstract
We analyze the relationship between investors’ attention and Shanghai Composite Index (SCI) to construct timing strategy based on limited attention theory of behavior finance. We use Baidu Index as proxy variable of investors’ attention. The result shows that the annual return of 15-month holding period is 14.34% depending on ‘dual reversal’ timing strategy. It also reveals that the annual return of 5-day time window and 3-day holding period is 27.12% based on ‘dual momentum’ timing strategy. In detail, the daily returns of ‘dual momentum’ timing strategy associated with 5-day time window are 5-day rolling average returns. Similarly, the annual return of 10-day time window and 5-day holding period is 25.96%.

Data
(1) We use Shanghai Composite Index (SCI) to represents the tendency of stock market in China. The data comes from China Stock Market Trading Database (CSMAR).

(2) Baidu Index is used to present investors’ attention, which is similar to Google Index. The Baidu website is listed as following: http://index.baidu.com. Using SCI code "000001" as key word to search in the Baidu website to get the attention index.

Zirong, this is a fascinating concept. Any plans to translate the Matlab code into python?

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

John, the methodology is presented as following:
‘Dual reversal’ timing strategy model (1) Sample period: June 2006 - October 2013
(2) The definition of ‘Dual reversal’timing strategy associated with 3-month holding period: Firstly, the monthly returns of SCI based on 3-month holding period ‘Dual reversal’ timing strategy are 3-month rolling average returns. In detail, we assume that we are in T month. Then the monthly return of SCI in T month is the average monthly return from (T-2) month to T month. Similarly, the monthly increase of Baidu Index are also 3-month rolling average increase.
Secondly, we calculate the sum of Baidu Index from (T-5) month to (T-3) month and name it X1. Then, we calculate the sum of Baidu Index from (T-2) month to T month and name it X2. Consequently, the variation of Baidu Index of those two period is X=(X2-X1)/X1.
Finally, if X in T month is less than all the X in previous months and the average monthly return of SCI from (T-2) month to T month is negative, we can take long position in SCI in (T+1) month. These are also the two conditions of ‘dual reversal’ timing strategy.

‘Dual momentum’ timing strategy model:5-day time window ‘Dual momentum’timing strategy model (1)Sample period: June 1st 2006 - October 31th 2013
(2)The definition of 5-day time window ‘Dual momentum’ timing strategy: The two requirements of ‘dual momentum’ timing strategy are that the increase of Baidu Indexis above average meanwhile the stock daily return is positive.
For the first requirement, we suppose that we are in T month, andthen we define X1 as total Baidu Index from the (T-9) month to (T-5) month and define X2 as total Baidu Index from (T-4) month to T month.Thus, we define the previous two periods variation of Baidu Indexas x=(X2-X1)/X1.
Besides, If the x in T month is less than the mean of x in all previous months while the daily returnof SCI from (T-4) month to T month is negative, then we can safely take long position in SCI at the beginning of (T+1) month.

Hi Zirong !
I know this is an old topic but I'm orking on a project using baidu and I can't find how to download the data from baidu index.
Could you please tell me how you did that ?
Thank you very much,
Pierre