abaixo, nao testado, deve estar bem proximo do q vc precisa. b
loop 1:
y[,4] = ifelse(y[,2] == 10, 'NC', 'C')
loop 2:
n = nrow(y)
y[-1, 4] = ifelse(diff(y[,3]) == 0 & y[-n, 4] == 'NC', 'NC', y[-1, 4])
loop 3:
dy = diff(y[,3])
y[-1, 4] = ifelse(y[-1, 4] == 'C' & dy > 0, 'DOWNTICK', y[-1, 4])
y[-1, 4] = ifelse(y[-1, 4] == 'C' & dy < 0, 'UPTICK', y[-1, 4])
y[-1, 4] = ifelse(y[-1, 4] == 'C' & dy == 0 & y[-n, 4] %in% c('UPTICK', 'ZERO.UPTICK'), 'ZERO.UPTICK', y[-1, 4])
y[-1, 4] = ifelse(y[-1, 4] == 'C' & dy == 0 & y[-n, 4] %in% c('DOWNTICK', 'ZERO.DOWNTICK'), 'ZERO.DOWNTICK', y[-1, 4])
2011/6/20 Luciano Ramos Gonçalves
<lrg.financas@gmail.com>
Colaboradores
do grupo, realizei um teste de financas em R, conhecido como
TICKTEST, porém meu código ficou muito lento. Talvez alguém
possa ajudar a torná-lo mais ágil.
Grato,
Luciano
Resumidamente:
1 - Prentende-se classificar mudanças de preço em uma ação.
2 - O primeiro negócio do dia não é classificado. Os negócios
subsequentes em que haja estabilidade no preço tb não são
classificados.
3 - Classifica-se as restantes conforme o movimento do preço. Se
sobe, UPTICK. Se desce, DOWNTICK. Se é estável e a última
variação foi positiva, ZERO.UPTICK. Se é estável e a
última variação foi positiva, ZERO.DOWNTICK.
Um exemplo dos dados:
Data |
Operação |
Preço |
Classificação |
1/1/2011 |
10 |
$10.00 |
NC |
1/1/2011 |
20 |
$9.90 |
DOWNTICK |
1/1/2011 |
30 |
$9.90 |
ZERO.DOWNTICK |
1/1/2011 |
40 |
$10.10 |
UPTICK |
1/1/2011 |
50 |
$10.20 |
UPTICK |
1/2/2011 |
10 |
$9.80 |
NC |
1/2/2011 |
20 |
$9.80 |
NC |
1/2/2011 |
30 |
$9.90 |
UPTICK |
1/2/2011 |
30 |
$9.90 |
ZERO.UPTICK |
O CÓDIGO EM SI:
# Abre o arquivo de dados, cria um novo objeto
"y" com uma coluna vazia.
x <-
read.table("dados.txt",sep=";",header=F,skip=1)
y
<- cbind(x,(rep(NA,nrow(x))))
# 1º Loop: Se a operação foi nº 10 não
classifica, o resto classifica.
z
<- 1:nrow(x)
for
(t in z)
{
if (y[t,2]==10)
y[t,4]<-"NC"
else y[t,4] <-"C"
}
# 2º Loop: Se a operação anterior não foi
classificada e o preço não mudou na atual, desclassifica esta
também.
w
<- 1:(nrow(x)-1)
for
(i in w)
{
if (y[i,4]=="NC" & y[i,3]==y[i+1,3])
y[i+1,4]<-"NC"
}
# 3º Loop: divide as operações classificadas
em quatro classes:
for
(i
in w)
{
# Se o preço diminuiu,
DOWNTICK;
if (y[i+1,4]=="C" & y[i,3]>y[i+1,3])
y[i+1,4]<-"DOWNTICK"
# Se o preço aumentou,
UPTICK;
if (y[i+1,4]=="C" & y[i,3]<y[i+1,3])
y[i+1,4]<-"UPTICK"
#
Se o preço permaneceu constante, mas a ultima variação foi
positiva, ZERO.UPTICK;
if (y[i+1,4]=="C" & y[i,3]==y[i+1,3] &
(y[i,4]=="UPTICK" | y[i,4]=="ZERO.UPTICK"))
y[i+1,4]<-"ZERO.UPTICK"
# Se o preço permaneceu
constante, mas a ultima variação foi negativa, ZERO.DOWNTICK;
if (y[i+1,4]=="C" & y[i,3]==y[i+1,3] &
(y[i,4]=="DOWNTICK" |
y[i,4]=="ZERO.DOWNTICK"))
y[i+1,4]<-"ZERO.DOWNTICK"
}
_______________________________________________
R-br mailing list
R-br@listas.c3sl.ufpr.br
https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e forneça código mínimo reproduzível.
--
Successful people ask better questions, and as a result, they get better answers. (Tony Robbins)