-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatbot.m
174 lines (160 loc) · 5.16 KB
/
matbot.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
%% MatLab Bet Bot
%% preprocessor
PROCESSTICKETSMIN=30;
switch (1)
case 1
%matbot development
DEVELOPMENTFLAG =1;
WRITETICKETSFLAG=1;
EXECUTEPLACEBETS=0;
SENDMAILMINS =0;
USERNUM =1;
STRATEGIESTABNUM=1;
LOGFILENAME ='matbot';
DATAFILENAME ='mbdata';
SCALGFILENAME='mscale';
case 2
%matbot production
DEVELOPMENTFLAG =0;
WRITETICKETSFLAG=1;
EXECUTEPLACEBETS=1;
SENDMAILMINS =12*60;
USERNUM =1;
STRATEGIESTABNUM=1;
LOGFILENAME ='mbot';
DATAFILENAME ='mbdata';
SCALGFILENAME='mscale';
end
%% logfile & datafile
timestamp=datestr(now,initvalues.timestamplogfile);
if DEVELOPMENTFLAG,
workpath=initvalues.workpathdev;
logfilepath=initvalues.logfilepathdev;
else
workpath=initvalues.workpathprod;
logfilepath=initvalues.logfilepathprod;
end
logfile=fullfile(logfilepath,[LOGFILENAME,timestamp,'.log']);
datafile=fullfile(workpath,[DATAFILENAME,timestamp,'.mat']);
scalingfile=fullfile(workpath,[SCALGFILENAME,'.mat']);
cd(workpath);
diary(logfile);
%% mail setup
if SENDMAILMINS,
subtxt=LOGFILENAME;
if DEVELOPMENTFLAG,
[mailaddress,mailpwd]=betmat.decrypt(initvalues.mailusertab,USERNUM);
setupgmail(mailaddress,mailpwd);
else
mailaddress=betmat.decrypt(initvalues.mailusertab,USERNUM);
pwd=getpassword;
if strcmp(pwd,''),
SENDMAILMINS=0;
else
setupgmail(mailaddress,pwd);
sendmail(mailaddress,subtxt,'Happy boting!');
end
end
end
%% api
if DEVELOPMENTFLAG,
[user,pwd]=betmat.decrypt(initvalues.usertab,USERNUM);
else
user=betmat.decrypt(initvalues.usertab,USERNUM);
pwd=getpassword;
end
betfairobj=betfair(user,pwd);
%% real time scheduler
rtobj=rtcontrol(betfairobj,datafile,scalingfile,STRATEGIESTABNUM);
%% environment
% keyboard virtual keys
VK_MENU=hex2dec('12'); % Alt Key
VK_LSHIFT=hex2dec('A0'); % Left Shift
VK_LCONTROL=hex2dec('A2'); % Left Control
% console
format('compact');
%% rt object data
rtobj.loadscaling; % load scaling data
rtobj.staticread; % read new static data
%% account funds
accountstart=betfairobj.getaccountfunds;
if WRITETICKETSFLAG,
rtobj.processtickets; % scale strategies
end
%% infinite loop - until user break (CTRL-C) or keystates
disptext=[];
loopcounter=1;
t1=tic; % time the loop
while ~(keystate(VK_LCONTROL) && keystate(VK_LSHIFT) && keystate(VK_MENU)), % keys currently pressed
% measure looptime
t2=tic;
% save execution
try
% check api environment and try to reconnect if necessary
if ~betfairobj.keepaliveandrelog(user,pwd),
%disptext=[sprintf('API not available!\n'),disptext]; %#ok<AGROW>
fprintf('API not accessible!\n');
else
% update ticket pnl and scaling before display
if WRITETICKETSFLAG && (mod(loopcounter,PROCESSTICKETSMIN)==0), % empty scheduler or scheduled cycle reached
rtobj.processtickets; % get pnl for tickets and scale strategies
end
% read and schedule static market data on lower refresh cycle
if rtobj.getstaticread, % empty scheduler or scheduled cycle reached
rtobj.checkdata; % check internal data integrity
rtobj.staticread; % read new static data
rtobj.save(true); % save checked static and dynamic data
end
% display status
disptext=rtobj.dispaccount(toc(t1),accountstart);
disp(disptext);rtobj.disp(false);%fprintf('\n');
if ~rtobj.getstaticempty,
% execute tickets
if WRITETICKETSFLAG,
rtobj.dynamicwrite(EXECUTEPLACEBETS);
end
% read and write dynamic market data
rtobj.dynamicread;
end
end
% send email every x minutes
if mod(loopcounter,SENDMAILMINS)==0,
t3=tic;
fprintf('Sending mail...');
sendmail(mailaddress,[subtxt,'#',num2str(loopcounter)],disptext);
fprintf('%.2fs\n',toc(t3));
end
catch exception
% throw error as warning
warning(exception.identifier,['iBot:ErrorHandler:',exception.identifier,':',exception.message,'!']);
%error(exception.identifier,[exception.identifier,':',exception.message,'!']);
% try to save
try
%rtobj.checkdata; % check internal data integrity
rtobj.save; % save cleaned static and dynamic data
catch exception2
% throw error as warning
warning(exception2.identifier,['iBot:ErrorHandler:Save:',exception2.identifier,':',exception2.message,'!']);
end
end
% delay
t2=toc(t2);
pausetime=max(0,initvalues.delaytime-t2); % should not be negative
fprintf('Loop %5.2fs/%5.2fs...\n',t2,pausetime);
pause(pausetime);
% counter & clear display
loopcounter=loopcounter+1;
clc;
end
%% save environment & quit
rtobj.checkdata; % check internal data integrity
rtobj.save; % save cleaned static and dynamic data
fprintf('ok!\n');
%if SENDMAILMINS,
% sendmail(mailaddress,subtxt,'Happy ending!');
%end
diary off;
if ~DEVELOPMENTFLAG,
clear classes; % destroy objs and logout
quit;
end