From: db on 13 May 2010 04:20 I have question if I can use if statement on my logic. I created variable called position upon my condition using "position(condition) = -1". I was wondering if there is a way to create my position variable using if statement instead ? Thanks clear all; price =[10 15 12 12 14 17 18 15 15 18 19 16 10 13 15 15 18 19 16 15]'; hi=[13 14 15 13 16 16 16 18 20 17 18 16 14 14 14 20 17 18 16 14]'; lo=[10 9 13 12 12 13 10 10 16 10 15 13 10 10 10 16 10 15 13 10]'; position= zeros(size(price(2:end))); %position= zeros(size(price)); % lag the rows to match up with next rows pr_previous = price(1:end-1); hi_previous = hi(1:end-1); lo_previous = lo(1:end-1); pr_next = price(2:end); hi_next = hi(2:end); lo_next = lo(2:end); % if previous row price < previous row hi && current(next row) price >= current(next row) hi then position = -1 position((pr_previous < hi_previous) & (pr_next >= hi_next)) = -1 % if previous row price > previous row lo && current(next row) price <= current lo then position = 1 position((pr_previous > lo_previous) & (pr_next <= lo_next)) = 1 % Is there a way to use if statement to create position variable instead of using above logic "position (condition) = -1 " ? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % if (pr_previous < hi_previous) & (pr_next >= hi_next) % position = -1; % end; % % if (pr_previous > lo_previous) & (pr_next <= lo_next) % postion = 1; % end; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
From: dpb on 13 May 2010 08:58 db wrote: > I have question if I can use if statement on my logic. > I created variable called position upon my condition using > "position(condition) = -1". I was wondering if there is a way to > create my position variable using if statement instead ? Thanks > > clear all; > price =[10 15 12 12 14 17 18 15 15 18 19 16 10 13 15 15 18 19 16 15]'; > hi=[13 14 15 13 16 16 16 18 20 17 18 16 14 14 14 20 17 18 16 14]'; > lo=[10 9 13 12 12 13 10 10 16 10 15 13 10 10 10 16 10 15 13 10]'; > > position= zeros(size(price(2:end))); > %position= zeros(size(price)); > > % lag the rows to match up with next rows > pr_previous = price(1:end-1); > hi_previous = hi(1:end-1); > lo_previous = lo(1:end-1); > > pr_next = price(2:end); > hi_next = hi(2:end); > lo_next = lo(2:end); > > > % if previous row price < previous row hi && current(next row) price >> = current(next row) hi then position = -1 > position((pr_previous < hi_previous) & (pr_next >= hi_next)) = -1 > > % if previous row price > previous row lo && current(next row) price > <= current lo then position = 1 > position((pr_previous > lo_previous) & (pr_next <= lo_next)) = 1 > > % Is there a way to use if statement to create position variable > instead of using above logic "position (condition) = -1 " ? > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% > % if (pr_previous < hi_previous) & (pr_next >= hi_next) > % position = -1; > % end; > % > % if (pr_previous > lo_previous) & (pr_next <= lo_next) > % postion = 1; > % end; > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
From: Lior Cohen on 13 May 2010 09:29 dpb <none(a)non.net> wrote in message <hsgt57$9q9$1(a)news.eternal-september.org>... > db wrote: > > I have question if I can use if statement on my logic. > > I created variable called position upon my condition using > > "position(condition) = -1". I was wondering if there is a way to > > create my position variable using if statement instead ? Thanks > > > > clear all; > > price =[10 15 12 12 14 17 18 15 15 18 19 16 10 13 15 15 18 19 16 15]'; > > hi=[13 14 15 13 16 16 16 18 20 17 18 16 14 14 14 20 17 18 16 14]'; > > lo=[10 9 13 12 12 13 10 10 16 10 15 13 10 10 10 16 10 15 13 10]'; > > > > position= zeros(size(price(2:end))); > > %position= zeros(size(price)); > > > > % lag the rows to match up with next rows > > pr_previous = price(1:end-1); > > hi_previous = hi(1:end-1); > > lo_previous = lo(1:end-1); > > > > pr_next = price(2:end); > > hi_next = hi(2:end); > > lo_next = lo(2:end); > > > > > > % if previous row price < previous row hi && current(next row) price > >> = current(next row) hi then position = -1 > > position((pr_previous < hi_previous) & (pr_next >= hi_next)) = -1 > > > > % if previous row price > previous row lo && current(next row) price > > <= current lo then position = 1 > > position((pr_previous > lo_previous) & (pr_next <= lo_next)) = 1 > > > > % Is there a way to use if statement to create position variable > > instead of using above logic "position (condition) = -1 " ? > > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% > > % if (pr_previous < hi_previous) & (pr_next >= hi_next) > > % position = -1; > > % end; > > % > > % if (pr_previous > lo_previous) & (pr_next <= lo_next) > > % postion = 1; > > % end; > > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% you can do it by for loop and if statement on every single element, but I really do not see the point of this. The vectorized logical index you use is better. Lior
From: dpb on 13 May 2010 09:36 Lior Cohen wrote: > dpb <none(a)non.net> wrote in message .... > you can do it by for loop and if statement on every single element, but > I really do not see the point of this. The vectorized logical index you > use is better. > Lior yes, sorry for empty post. Agree; seems logical way to approach the question... :) I'll ask OP straight out--why _not_ as you wrote it? --
From: db on 13 May 2010 12:29 Thank you all for putting your thought ! I happened to see a logic that use if end statement for stock backtesting logic and I couldn't get it to work with if end statement since I am a nebie to matlab.. function [postion] = get_entry_signal(obj,bar) position = 0; [c] = (obj.close(1:bar) % [movavg,uppr,lowr] = bollinger(obj.close(1:bar),151); if(c(end) >= uppr(end)) postion = 1; end if(c(end) <= lowr(end)) position = -1; end end On May 13, 6:36 am, dpb <n...(a)non.net> wrote: > Lior Cohen wrote: > > dpb <n...(a)non.net> wrote in message > > ... > > > you can do it by for loop and if statement on every single element, but > > I really do not see the point of this. The vectorized logical index you > > use is better. > > Lior > > yes, sorry for empty post. > > Agree; seems logical way to approach the question... :) > > I'll ask OP straight out--why _not_ as you wrote it? > > --
|
Next
|
Last
Pages: 1 2 Prev: code operating speed is very slow Next: Different answers on different CPUs? |