-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic_regression.m
119 lines (96 loc) · 5.55 KB
/
logistic_regression.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function [model, profit, precision, recall] = logistic_regression( tsecs, prices, volumes)
train_start = 9 * 3600 + 30 * 60;
train_end = 10*3600 + 30 * 60;
[tsecs_train, prices_train] = range_data(tsecs, prices, train_start, train_end);
[~, volumes_train] = range_data(tsecs, volumes, train_start, train_end);
price_chart = parse_training_data(tsecs_train, prices_train);
volume_chart = parse_training_data(tsecs_train, volumes_train);
m = length(price_chart);
n = 6; % 12 features
train_perc_priceHigh = zeros(m - 1, 1);
train_perc_priceLow = zeros(m - 1, 1);
train_perc_priceOpen = zeros(m - 1, 1);
train_perc_volumeHigh = zeros(m - 1, 1);
train_perc_volumeLow = zeros(m - 1, 1);
train_perc_volumeOpen = zeros(m - 1, 1);
train_labels = zeros(m - 1, 1);
for ii = 2:m;
% build train_perc_priceHigh
train_perc_priceHigh(ii - 1) = (price_chart(ii, 1) - price_chart(ii - 1, 1)) / price_chart(ii - 1, 1);
% build train_perc_priceLow
train_perc_priceLow(ii - 1) = (price_chart(ii, 2) - price_chart(ii - 1, 2)) / price_chart(ii - 1, 2);
% build train_perc_priceOpen
train_perc_priceOpen(ii - 1) = (price_chart(ii, 3) - price_chart(ii - 1, 3)) / price_chart(ii - 1, 3);
% build train_perc_volumeHigh
train_perc_volumeHigh(ii - 1) = (volume_chart(ii, 1) - volume_chart(ii - 1, 1)) / volume_chart(ii - 1, 1);
% build train_perc_volumeLow
train_perc_volumeLow(ii - 1) = (volume_chart(ii, 2) - volume_chart(ii - 1, 2)) / volume_chart(ii - 1, 2);
% build train_perc_volumeOpen
train_perc_volumeOpen(ii - 1) = (volume_chart(ii, 3) - volume_chart(ii - 1, 3)) / volume_chart(ii - 1, 3);
%train_labels(ii - 1) = (price_chart(ii, 4) - price_chart(ii - 1, 4))/price_chart(ii - 1,4);
train_labels(ii - 1) = sign(price_chart(ii, 4) - price_chart(ii - 1, 4));
if train_labels(ii - 1) == -1 || train_labels(ii - 1) == 0;
train_labels(ii - 1) = 2;
end
end
% building feature matrix
%train_features = [train_perc_priceHigh train_perc_priceLow train_perc_priceOpen train_perc_volumeHigh train_perc_volumeLow train_perc_volumeOpen];
train_features = [train_perc_priceHigh train_perc_priceLow train_perc_priceOpen];
% logistic regression training
model = mnrfit(train_features, train_labels);
% Testing UWLR on TRAINING set
correct_predictions = 0;
total_predictions = 0;
positive_predictions = 0;
true_positives = 0;
profit = 0;
% Calculating results
train_start_time = tsecs_train(1) + 60 * 5;
for ii = 1:length(tsecs_train) - 1;
if tsecs_train(ii) >= train_start_time;
[ price_num_trades, price_high, price_low, price_open ] = minute_block( tsecs_train, prices_train, tsecs_train(ii) );
[ prev_price_num_trades, prev_price_high, prev_price_low, prev_price_open ] = minute_block( tsecs_train, prices_train, tsecs_train(ii) - 60 );
[ ~, volume_high, volume_low, volume_open ] = minute_block( tsecs_train, volumes_train, tsecs_train(ii) );
[ ~, prev_volume_high, prev_volume_low, prev_volume_open ] = minute_block( tsecs_train, volumes_train, tsecs_train(ii) - 60 );
if price_num_trades >= 5 && prev_price_num_trades >= 5;
% build feature vector
train_perc_priceHigh = (price_high - prev_price_high) / prev_price_high;
train_perc_priceLow = (price_low - prev_price_low) / prev_price_low;
train_perc_priceOpen = (price_open - prev_price_open) / prev_price_open;
train_perc_volumeHigh = (volume_high - prev_volume_high) / prev_volume_high;
train_perc_volumeLow = (volume_low - prev_volume_low) / prev_volume_low;
train_perc_volumeOpen = (volume_open - prev_volume_open) / prev_volume_open;
%train_feature = [1 train_perc_priceHigh train_perc_priceLow train_perc_priceOpen train_perc_volumeHigh train_perc_volumeLow train_perc_volumeOpen];
train_feature = [1 train_perc_priceHigh train_perc_priceLow train_perc_priceOpen];
% make prediction
prediction = exp(train_feature * model); % see wikipedia
total_predictions = total_predictions + 1;
%display(model.beta);
if prediction > 1;
if prices_train(ii + 1) > prices_train(ii);
positive_predictions = positive_predictions + 1;
true_positives = true_positives + 1;
correct_predictions = correct_predictions + 1;
elseif prices_train(ii + 1) < prices_train(ii);
profit = profit - 1;
end
else
if prices_train(ii + 1) > prices_train(ii);
true_positives = true_positives + 1;
elseif prices_train(ii + 1) < prices_train(ii);
correct_predictions = correct_predictions + 1;
end
end
end
end
end
display(profit);
display(correct_predictions);
display(total_predictions);
precision = correct_predictions / total_predictions;
display(precision);
display(positive_predictions);
display(true_positives);
recall = positive_predictions / true_positives;
display(recall);
end