我有一个包含3列的数据框:日期,股票交易代码(即金融工具)和价格。 我只想计算每个股票的回报。
一些数据可玩:
AsofDate = as.Date(c("2018-01-01","2018-01-02","2018-01-03","2018-01-04","2018-01-05",
"2018-01-01","2018-01-02","2018-01-03","2018-01-04","2018-01-05",
"2018-01-01","2018-01-02","2018-01-03","2018-01-04","2018-01-05"))
Tickers = c("Ticker1", "Ticker1", "Ticker1", "Ticker1", "Ticker1",
"Ticker2", "Ticker2", "Ticker2", "Ticker2", "Ticker2",
"Ticker3", "Ticker3", "Ticker3", "Ticker3", "Ticker3")
Prices =c(1,2,7,4,2,
6,5,7,9,12,
11,11,16,14,15)
df = data.frame(AsofDate, Tickers, Prices)
我的第一个想法是按(股票行情价格)订购价格,然后计算所有向量,并在第一天将其设置为NA ...
TTR::ROC(x=Prices)
它可以在Excel中运行,但我想要更漂亮的东西
所以我尝试了这样的事情:
require(dplyr)
ret = df %>%
select(Tickers,Prices) %>%
group_by(Tickers) %>%
do(data.frame(LogReturns=TTR::ROC(x=Prices)))
df$LogReturns = ret$LogReturns
但是在这里我得到了太多的值,看来该计算不是由Tickers完成的。
你能给我一个提示吗?
谢谢!!
答案 0 :(得分:1)
在dplyr
中,我们可以使用lag
来获取先前的Prices
library(dplyr)
df %>%
group_by(Tickers) %>%
mutate(returns = (Prices - lag(Prices))/Prices)
# AsofDate Tickers Prices returns
# <date> <fct> <dbl> <dbl>
# 1 2018-01-01 Ticker1 1 NA
# 2 2018-01-02 Ticker1 2 0.5
# 3 2018-01-03 Ticker1 7 0.714
# 4 2018-01-04 Ticker1 4 -0.75
# 5 2018-01-05 Ticker1 2 -1
# 6 2018-01-01 Ticker2 6 NA
# 7 2018-01-02 Ticker2 5 -0.2
# 8 2018-01-03 Ticker2 7 0.286
# 9 2018-01-04 Ticker2 9 0.222
#10 2018-01-05 Ticker2 12 0.25
#11 2018-01-01 Ticker3 11 NA
#12 2018-01-02 Ticker3 11 0
#13 2018-01-03 Ticker3 16 0.312
#14 2018-01-04 Ticker3 14 -0.143
#15 2018-01-05 Ticker3 15 0.0667
在基数R中,我们可以将ave
与diff
一起使用
df$returns <- with(df, ave(Prices, Tickers,FUN = function(x) c(NA,diff(x)))/Prices)
答案 1 :(得分:1)
谢谢! 您的解决方案是完美的:
df %>%
group_by(Tickers) %>%
mutate(returns = (Prices - lag(Prices))/Prices)
答案 2 :(得分:0)
我们可以使用<!--This is register.html which will render the form -->
{% extends "layout.html" %}
{% block content %}
<div class="content-section">
<form method="POST" action="">
<!--adds cross site request forgery token. Needed for secuity.-->
{{ form.hidden_tag() }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today</legend>
<div class="form-group">
{{ form.username.label(class="form-control-label") }}
{{ form.username(class="form-control form-control-lg") }}
</div>
<div class="form-group">
{{ form.email.label(class="form-control-label") }}
{{ form.email(class="form-control form-control-lg") }}
</div>
<div class="form-group">
{{ form.password.label(class="form-control-label") }}
{{ form.password(class="form-control form-control-lg") }}
</div>
<div class="form-group">
{{ form.confirmPassword.label(class="form-control-label") }}
{{ form.confirmPassword(class="form-control form-control-lg") }}
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
</div>
</form>
</div>
<div class="border-top pt-3">
<small class="text-muted">
<!-- url_for() uses the name of the route function -->
Already Have an account? <a class="ml-2" href="{{ url_for('register') }}" >Sign In</a>
</small>
</div>
{% endblock content %}
data.table