Here are the results (with image):
Return: +13.36% (rounded)
PLEASE DO NOT TRADE ON THESE RESULTS.
As I mentioned above, this should not be used from a trading perspective for several reasons:
1) It is run on far too few data points. Perhaps it just happened to work perfectly for this interval. I have no clue if this holds. (If you would like to donate historical data, please let me know!)
2) This is done during earnings, and, as you would see if you ran this for other stocks, could be hit or miss, especially with overnight holding (and, again, especially through earnings).
3) There are plenty of stocks this does not work for. Some with similar sized returns, although negative.
4) My data could be flawed, my routine should be suspect and you should always perform tests read on the internet for yourself!
5) I am sure there are 10 more reasons.
Now, here is the "backtest" code in function form. It's for MATLAB and requires that you have some data in a MySQL database (very easy if you use my IB Historical Data Downloader/Importer):
function sum_log_change = vwap_backtest(symbol_string)
clear close_mat
clear volume_mat
clear vwap
clear vwap_2
clear buy_sell_matrix
clear trade
clear trade_prices
clear sum_log_change
vwap_length = 100;
vwap_length_2 = 500;
format long
symbol_string
%connect to the database CHANGE PASSWORD FROM ***** TO WHATEVER YOUR PASSWORD IS!!!
conn = database('stocks','root','****','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/stocks');
%query the symbols in the database
str_query_close = ['SELECT close, volume FROM stock_prices_minute WHERE symbol = ''',symbol_string, ''''];
close_cell = fetch(conn, str_query_close);
close_mat = cell2mat(close_cell(:,1));
volume_mat = cell2mat(close_cell(:,2));
close_times_volume = close_mat .* volume_mat;
for i = vwap_length:length(close_times_volume)
volume_total = 0;
volume_price_sum = 0;
for j = 0:(vwap_length - 1)
volume_total = volume_total + volume_mat(i-j);
volume_price_sum = volume_price_sum + volume_mat(i-j)*close_mat(i-j);
end
vwap(i-(vwap_length - 1)) = volume_price_sum / volume_total;
end
for i = vwap_length_2:length(close_times_volume)
volume_total = 0;
volume_price_sum = 0;
for j = 0:(vwap_length_2 - 1)
volume_total = volume_total + volume_mat(i-j);
volume_price_sum = volume_price_sum + volume_mat(i-j)*close_mat(i-j);
end
vwap_2(i-(vwap_length_2 - 1)) = volume_price_sum / volume_total;
end
%do comparing
for i = 1:(length(close_times_volume) - (vwap_length_2 - 1))
if vwap_2(i) < vwap(i+(vwap_length_2 - vwap_length))
buy_sell_matrix(i) = 1;
else
buy_sell_matrix(i) = -1;
end
end
buy_sell_matrix;
%calculate trade prices
for i = 1:(length(close_times_volume) - (vwap_length_2 - 1))
if i == 1
trades(i) = close_mat(i + vwap_length_2);
else
if buy_sell_matrix(i) ~= buy_sell_matrix(i-1)
trades(i) = close_mat(i + vwap_length_2);
elseif i == (length(close_times_volume)-(vwap_length_2 - 1))
trades(i) = close_mat(length(close_mat));
else
trades(i) = 0;
end
end
end
trades;
j = 1;
%matrix of prices trades took place at
for i = 1:length(trades)
if trades(i) ~= 0
trade_prices(j) = trades(i);
j = j + 1;
end
end
trade_prices;
log_change = 0;
sum_log_change = 0;
%now you can calculate lognormal return
for i = 1:(length(trade_prices) - 1)
if buy_sell_matrix(1) == 1
if mod(i,2) == 0
log_change = -(log(trade_prices(i+1)) - log(trade_prices(i)));
else
log_change = log(trade_prices(i+1)) - log(trade_prices(i));
end
sum_log_change = sum_log_change + log_change;
else
if mod(i,2)==0
log_change = log(trade_prices(i+1)) - log(trade_prices(i));
else
log_change = -(log(trade_prices(i+1)) - log(trade_prices(i)));
end
sum_log_change = sum_log_change + log_change;
end
end
sum_log_change;
x = [1:length(close_mat)];
x2 = [(vwap_length):length(close_mat)];
x3 = [(vwap_length_2):length(close_mat)];
plot(x, close_mat)
hold on
plot(x2, vwap, '-r')
hold on
plot(x3, vwap_2, '-g')
Link to previous article: MATLAB VWAP (Part I)
No comments:
Post a Comment