gap adjustment in time series data
2 次查看(过去 30 天)
显示 更早的评论
I've only just started using Matlab, and I already have a question with a little thing I'm working on for fun. I have open high low close data for a stock, and I'm trying to make a function that outputs a new time series that closes the morning gaps (shifts the whole time series so that all the open prices of every bar are equal to the previous bars close). Here's my code:
%gapadjust
%[newOpen, newHigh, newLow, newClose] = gapadjust(open,high,low,close,time)
%this function forward adjusts a time series for gaps
%(in case you don't have any overnight exposure)
function [newOpen, newHigh, newLow, newClose] = gapadjust(open,high,low,close,time)
if nargin<5
disp('need more arguments');
end
for index=2:size(time,1)
if abs(open(index)-close(index-1))>.01
x=open(index)-close(index-1);
shiftVector = [zeros((index-1),1); x*ones(size(time,1)-(index-1),1)];
newOpen=open-shiftVector;
newHigh=high-shiftVector;
newLow=low-shiftVector;
newClose=close-shiftVector;
end
end
Thanks very much for any help.
1 个评论
the cyclist
2011-4-1
So, what's the question? Is the code failing to execute? Are you getting an error or a warning? Is it producing incorrect results?
Also, could you use the code markup tools to improve the layout of the code, to make it more readable?
采纳的回答
更多回答(2 个)
Jarrod Rivituso
2011-4-2
Here's a little example that has helped me in similar situations.
>> x1 = [1 2 3 4 5]
>> x2 = [5 4 3 2 1]
>> xtot = [x1 x2]
>> conditionForRemoval = diff(xtot) == 0
>> xtot(conditionForRemoval) = []
The main steps in that code are
- Create array with overlapping data next to each other
- Define a logical array that indicates which elements should be removed
- Use logical indexing to set those elements to "empty"
Hope this helps!
0 个评论
Taylor
2011-4-3
1 个评论
Jarrod Rivituso
2011-4-3
Hi Taylor,
It seems I have misunderstood your question. I'm still not sure I completely understand the data inputs you have and the desired output.
Is it something like this...
open - [1.0,3.1,4.2,5.4]
close - [3.0,4.2,5.1,5.8]
If so, what are your desired output arrays? Is it this...?
open - [1.0,3.0,4.2,5.1]
close - [3.0,4.2,5.1,5.8]
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!