Uruchom kod MATLAB w Python
import matlab.engine
eng = matlab.engine.start_matlab()
eng.mscriptname(nargout=0)
Light Ladybird
import matlab.engine
eng = matlab.engine.start_matlab()
eng.mscriptname(nargout=0)
% 1 minute data on GLD-USO
load('inputData_ETF', 'tday', 'syms', 'cl');
idxG=find(strcmp('GLD', syms));
idxU=find(strcmp('USO', syms));
x=cl(:, idxG);
y=cl(:, idxU);
lookback=20; % Lookback set arbitrarily short
hedgeRatio=NaN(size(x, 1), 1);
for t=lookback:size(hedgeRatio, 1)
regression_result=ols(y(t-lookback+1:t), [x(t-lookback+1:t) ones(lookback, 1)]);
hedgeRatio(t)=regression_result.beta(1);
end
y2=[x y];
yport=sum([-hedgeRatio ones(size(hedgeRatio))].*y2, 2); % The net market value of the portfolio is same as the "spread"
hedgeRatio(1:lookback)=[]; % Removed because hedge ratio is indterminate
yport(1:lookback)=[];
y2(1:lookback, :)=[];