Friends, I am attaching the matlab files for the filtering algorithm I used to track the Tesla mid-price. I will post some notes in the next post. Please note that this program is different from the one used to create above graphs.
.
.
function [] = PlayWithDataNewWilmott()
%copyright: Ahsan Amin, CEO of Infiniti Derivatives Technologies
%IF you would like me to develop far better and advanced versions of this program for you, please
% email me at anan2999(at)yahoo(dot)com
%please enjoy the filtering program below
%You will have to assign appropriate file paths for data and date files.
pathstring="C:\Users\Ahsan\Documents\MATLAB\ReadDates0.txt";
T = readtable(pathstring);
StockDateSeries(1:41)=table2array(T(1:41,1));
StockSymbol0(1)="TSLA"
for nn=2:41
StockDate0=StockDateSeries(nn);
if(nn>1)
StockDate1=StockDateSeries(nn-1);
Stock1=StocksClassW(StockSymbol0,StockDate1);
%Returns Variance from every previous day is fed to the filtering function using Stock1 object.
end
Stock0=StocksClassW(StockSymbol0,StockDate0);
StockDate0
%str=input('Look at date');
if(nn>1)
[Profit(nn),NoOfTrades(nn)]=SDEParamsFilterAndDoubleStrategyNew02A4paramsOptimMid02LargeW(Stock0.LogReturns,Stock0.StockPrice0Series,Stock1.LRVar,StockDate0);
AvgSpread(nn)=0.0;
for mm=2:1560
AvgSpread(nn)=AvgSpread(nn)+(Stock0.StockAskSeries(mm,1)-Stock0.StockBidSeries(mm,1))/1560;
end
AvgSpread(nn)
end
TotalProfitYet=sum(Profit(2:nn))
end
TotalProfit=sum(Profit(2:41))
TotalTrades=sum(NoOfTrades(2:41))
TradingProfit=TotalProfit
TotalCommissions=TotalTrades*.0035*2
TotalSpreadEarned=sum(NoOfTrades(:) .* AvgSpread(:))*.5
AvgSpread
end
.
.
function [Profit,NoOfTrades] = SDEParamsFilterAndDoubleStrategyNew02A4paramsOptimMid02LargeW(LogRSeries,PriceSeries,VarLogR,Date0)
Price0=PriceSeries;
a1(1:4)=.000005; %a1-a4 are autoregressive parameters for lag 1-4 periods.
a2(1:4)=.000005;
a3(1:4)=.000005;
a4(1:4)=.000005;
P_a1(1:4)=0.0; %P_a1-Pa4 is filter variance covariance matrix.
%It is considered diagonal in this prototype.That is why all calculations are linear.
P_a2(1:4)=0.0;
P_a3(1:4)=0.0;
P_a4(1:4)=0.0;
w_Zt=1.0*sqrt(VarLogR);
w_Zt0=1.0*sqrt(VarLogR);
w_ZtP=w_Zt;
Zt(1:1561)=0.0;
%Z1(1:1561)=0.0;
LogPrice(1:1561)=0.0;
%for tt0=2:2+Lag-1
for tt0=2:5
LogPrice(tt0)=log(PriceSeries(tt0,1));
end
Sell=0;
Buy=0;
Profit=0;
Trade=0;
Profit0(1:1561)=0.0;
NoOfTrades=0;
bb=1;
ss=1;
SellTimes(1)=0;
BuyTimes(1)=0;
PositionVolume=0;
for tt=5:1560
%LogPrice(tt)=LogPrice(tt-1)+LogRSeries(tt,1);
LogPrice(tt)=log(PriceSeries(tt,1));
if(tt>50)
w_a1=.0075;%.005 earlier
w_a2=.0035;%.0035
w_a3=.00125;
w_a4=.0005;
end
if(tt<=50)
w_a1=.006*2;%.005 earlier
w_a2=.004*2;%.0035
w_a3=.002*2;
w_a4=.001*2;
end
a1(tt)=a1(tt-1);
a2(tt)=a2(tt-1);
a3(tt)=a3(tt-1);
a4(tt)=a4(tt-1);
P_a1(tt)=P_a1(tt-1)+w_a1^2;
P_a2(tt)=P_a2(tt-1)+w_a2^2;
P_a3(tt)=P_a3(tt-1)+w_a3^2;
P_a4(tt)=P_a4(tt-1)+w_a4^2;
Kk0=( LogRSeries(tt-1,1).*P_a1(tt).*LogRSeries(tt-1,1) + ...
LogRSeries(tt-2,1).*P_a2(tt).*LogRSeries(tt-2,1) + ...
LogRSeries(tt-3,1).*P_a3(tt).*LogRSeries(tt-3,1) + ...
LogRSeries(tt-4,1).*P_a4(tt).*LogRSeries(tt-4,1) +w_ZtP.^2).^(-1);
Xnew=( LogRSeries(tt-1,1).*a1(tt) + LogRSeries(tt-2,1).*a2(tt)+ LogRSeries(tt-3,1).*a3(tt)+ LogRSeries(tt-4,1).*a4(tt));% + ...
%Z1(tt)=LogRSeries(tt,1)-( LogRSeries(tt-1,1).*a1(tt) + LogRSeries(tt-2,1).*a2(tt)+ LogRSeries(tt-3,1).*a3(tt)+ LogRSeries(tt-4,1).*a4(tt));% + ...
a1Prev(tt)=a1(tt);
a2Prev(tt)=a2(tt);
a3Prev(tt)=a3(tt);
a4Prev(tt)=a4(tt);
a1(tt)=a1(tt)+ LogRSeries(tt-1,1).*P_a1(tt).*Kk0.*(LogRSeries(tt,1)-Xnew);
adj=LogRSeries(tt-1,1).*(a1(tt)-a1Prev(tt));
%adj=0;
a2(tt)=a2(tt)+ LogRSeries(tt-2,1).*P_a2(tt).*Kk0.*(LogRSeries(tt,1)-Xnew-adj);
adj=adj+LogRSeries(tt-2,1).*(a2(tt)-a2Prev(tt));
%adj=0;
a3(tt)=a3(tt)+ LogRSeries(tt-3,1).*P_a3(tt).*Kk0.*(LogRSeries(tt,1)-Xnew-adj);
adj=adj+LogRSeries(tt-3,1).*(a3(tt)-a3Prev(tt));
%adj=0;
a4(tt)=a4(tt)+ LogRSeries(tt-4,1).*P_a4(tt).*Kk0.*(LogRSeries(tt,1)-Xnew-adj);
Zt(tt)=LogRSeries(tt,1)-( LogRSeries(tt-1,1).*a1(tt) + LogRSeries(tt-2,1).*a2(tt)+ LogRSeries(tt-3,1).*a3(tt)+ LogRSeries(tt-4,1).*a4(tt));% + ...
if(tt>=5)
w_ZtP2=w_ZtP^2;
end
sigma0=.98; %TSLA
sigma1=+.15;
sigma2=+.04;
sigmab0=.5*1.5; %TSLA
sigmab1=+.15/2*1.5;
sigmab2=+.04/2*1.5;
alpha0=.02;
sigma0=.96; %TSLA
sigma1=+.15;
sigma2=+.04;
sigmab0=.5*1.5; %TSLA
sigmab1=+.15/2*1.5;
sigmab2=+.04/2*1.5;
alpha0=.04;
%w_ZtP2=alpha0*w_ZtP2+sigma0*abs(Zt(tt)).^2+sigma1*abs(Zt(tt-1)).^2+sigma2*abs(Zt(tt-2)).^2;%+ ...
w_ZtP2=alpha0*w_ZtP2+sigmab0*LogRSeries(tt,1).^2+sigmab1*LogRSeries(tt-1,1).^2+sigmab2*LogRSeries(tt-2,1).^2+ ...
+sigma0*abs(Zt(tt)).^2+sigma1*abs(Zt(tt-1)).^2+sigma2*abs(Zt(tt-2)).^2;
%sigma3*abs(Zt(tt-3)).^2;
w_ZtP=sqrt(w_ZtP2) ;
%w_ZtP=w_Zt0;
P_a1(tt)=(1-LogRSeries(tt-1,1).*P_a1(tt).*Kk0.*LogRSeries(tt-1,1)).*P_a1(tt);
P_a2(tt)=(1-LogRSeries(tt-2,1).*P_a2(tt).*Kk0.*LogRSeries(tt-2,1)).*P_a2(tt);
P_a3(tt)=(1-LogRSeries(tt-3,1).*P_a3(tt).*Kk0.*LogRSeries(tt-3,1)).*P_a3(tt);
P_a4(tt)=(1-LogRSeries(tt-4,1).*P_a4(tt).*Kk0.*LogRSeries(tt-4,1)).*P_a4(tt);
%P_b2(tt)=(1-Zt(tt-2).*P_b2(tt).*Kk0.*Zt(tt-2)).*P_b2(tt);
Fp(tt)=a1(tt)*LogRSeries(tt,1)+a2(tt)*LogRSeries(tt-1,1)+a3(tt)*LogRSeries(tt-2,1)+a4(tt)*LogRSeries(tt-3,1);
Mul=.110/1.8;%/1.6;
%Mul=.110/1.7;
Up1=w_Zt0*Mul;
Down1=-w_Zt0*Mul;
Up0=w_Zt0*Mul;
Down0=-w_Zt0*Mul;
PProfitLimit=.00045;%Optimal .00045 for MSFT
PStopLimit=-.0005;
PProfit=0;
if((Sell==1) && (Trade==1))
PProfit=log(SellPrice)-log(Price0(tt));
end
if((Buy==1) && (Trade==1))
PProfit=log(Price0(tt))-log(BuyPrice);
end
if((Buy==1)&&(Sell==1)&&(Trade==1))
tt
str=input('There is an error');
end
if(((Fp(tt)>Up1)||(PProfit>=PProfitLimit)||(PProfit<=PStopLimit))&& ((Sell==1) && (Buy==0)&&(Trade==1))) %
Buy=0;
Sell=0;
Trade=0;
BuyPrice=Price0(tt);
Profit=Profit+(SellPrice)-(BuyPrice);
NoOfTrades=NoOfTrades+1;
if(BuyTimes(bb)<tt)
bb=bb+1;
BuyTimes(bb)=tt;
Pricebb(bb)=Price0(tt);
end
PositionVolume=PositionVolume+1;
end
if(((Fp(tt)<Down1)||(PProfit>=PProfitLimit)||(PProfit<=PStopLimit)) && ((Sell==0) && (Buy==1)&&(Trade==1))) %
Sell=0;
Buy=0;
Trade=0;
SellPrice=Price0(tt);
Profit=Profit+(SellPrice)-(BuyPrice);
NoOfTrades=NoOfTrades+1;
if(SellTimes(ss)<tt)
ss=ss+1;
SellTimes(ss)=tt;
Pricess(ss)=Price0(tt);
end
PositionVolume=PositionVolume-1;
end
if(((Fp(tt)<Down0)) && (Sell==0) && (Buy==0) && (Trade==0))%&&(WSum0(tt,1)>Up00)
Buy=0;
Sell=1;
Trade=1;
SellPrice=Price0(tt);
if(SellTimes(ss)<tt)
ss=ss+1;
SellTimes(ss)=tt;
Pricess(ss)=Price0(tt);
end
PositionVolume=PositionVolume-1;
end
if(((Fp(tt)>Up0)) && (Sell==0) && (Buy==0) &&(Trade==0))%&&(WSum0(tt,1)<Down00)
Sell=0;
Buy=1;
Trade=1;
BuyPrice=Price0(tt);
if(BuyTimes(bb)<tt)
bb=bb+1;
BuyTimes(bb)=tt;
Pricebb(bb)=Price0(tt);
end
PositionVolume=PositionVolume+1;
end
Profit0(tt)=Profit;
PositionVolume0(tt)=PositionVolume;
end
clf;
plot((5:1560),PositionVolume0(5:1560),'g')
%str=input('Look at stock positions'); %should remain between -1 and +1 since a new trade is not started until previous trade is closed.
yyaxis right
plot((5:1560),Profit0(5:1560),'k')
yyaxis left
plot((5:1560),Price0(5:1560),'g')
hold on
yyaxis left
plot(BuyTimes(2:bb),Pricebb(2:bb),'b+','MarkerSize',3);
yyaxis left
plot(SellTimes(2:ss),Pricess(2:ss),'ro','MarkerSize',2);
hold off
StockDate=datetime(Date0,'InputFormat','dd\MM\yyyy');
str1=string(StockDate, "yyyy-MM-dd");
yyaxis left
title(sprintf('Profit And Tesla Stock Mid-Price Graph on %s',str1));
xlabel('Time in units of 15 seconds')
ylabel('Mid-Price(Green)')
yyaxis right
ylabel('Trading Profit on Mid Price(Black)')
Profit
NoOfTrades
%str=input('Look at graph');
end
.
.
.
classdef StocksClassW
properties
StockSymbol=0;
StockDate;
StockPriceSeries(1561,1)=0;
StockPrice0Series(1561,1)=0;
StockBidSeries(1561,1)=0;
StockAskSeries(1561,1)=0;
StockVolumeSeries(1561,1)=0;
StockReturnSeries(1561,1)=0;
LogReturns(1561,1)=0;
TotalVolume=0;
LRMean;
LRVar;
LRSD;
end
methods
% function obj=StocksClass()
% obj.StockSymbol=StockSymbol0;
% end
function obj=StocksClassW(StockSymbol0,StockDate0)
StockSymbol0
obj.StockSymbol=StockSymbol0;
obj.StockDate=datetime(StockDate0,'InputFormat','dd\MM\yyyy');
str1=string(obj.StockDate, "yyyy_MM_dd");
str0="C:\Users\Ahsan\Documents\Project0\DailyData\";
pathstring=str0+ str1 + '\' + str1 + '_' + string(obj.StockSymbol);
T = readtable(pathstring);
obj.StockBidSeries(1:1561,1)=table2array(T(1:1561,4));
%obj.StockAskSeries(1:1561,1)=table2array(T(1:1561,3));
obj.StockAskSeries(1:1561,1)=table2array(T(1:1561,5));
obj.StockPrice0Series(1:1561,1)=.5*obj.StockBidSeries(1:1561,1)+.5*obj.StockAskSeries(1:1561,1);
obj.StockVolumeSeries(1:1561,1)=table2array(T(1:1561,6));
obj.LogReturns(1)=0;
for nn=2:1561
obj.LogReturns(nn,1)=log(obj.StockPrice0Series(nn,1))-log(obj.StockPrice0Series(nn-1,1));
end
obj.TotalVolume=sum(obj.StockVolumeSeries(:,1));
obj.LRMean=sum(obj.LogReturns(:,1))/1560;
obj.LRVar=sum((obj.LogReturns(:,1).^2-obj.LRMean.^2))/1560;
obj.LRSD=sqrt(obj.LRVar);
end
end
end
.
.
.
For files below, add them to special directories and use the calling routine accordingly in the program where these files are called.
You will also need the 41 text files of Tesla Stock data of market prices, bid prices, ask prices, volume all 15 secs apart for 41 different dates in a zip file.
You will also need the following date file to read relevant dates in the program to fetch the data from files to computer memory.
When you run this program, the following should be the final output
TotalProfitYet =
3.933149999999953e+02
TotalProfit =
3.933149999999953e+02
TotalTrades =
22422
TradingProfit =
3.933149999999953e+02
TotalCommissions =
1.569540000000000e+02
TotalSpreadEarned =
2.127602830128225e+03
AvgSpread =
Columns 1 through 3
0 0.185461538461541 0.185173076923080
Columns 4 through 6
0.181967948717951 0.171044871794874 0.141076923076926
Columns 7 through 9
0.175474358974355 0.163942307692309 0.215878205128208
Columns 10 through 12
0.241987179487182 0.180538461538463 0.198416666666668
Columns 13 through 15
0.195685897435900 0.167794871794873 0.199076923076925
Columns 16 through 18
0.173820512820515 0.157724358974360 0.172102564102566
Columns 19 through 21
0.178910256410257 0.182532051282054 0.150647435897440
Columns 22 through 24
0.173532051282053 0.203500000000002 0.181474358974359
Columns 25 through 27
0.212910256410258 0.196057692307696 0.184333333333334
Columns 28 through 30
0.254365384615386 0.271557692307694 0.221467948717949
Columns 31 through 33
0.180846153846156 0.222769230769233 0.193557692307694
Columns 34 through 36
0.243602564102567 0.155814102564104 0.160942307692309
Columns 37 through 39
0.178942307692311 0.169506410256412 0.162455128205131
Columns 40 through 41
0.157891025641026 0.134775641025641
You think life is a secret, Life is only love of flying, It has seen many ups and downs, But it likes travel more than the destination. Allama Iqbal