-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2025新策略宏观反欺诈算法.R
More file actions
225 lines (174 loc) · 6.84 KB
/
Copy path2025新策略宏观反欺诈算法.R
File metadata and controls
225 lines (174 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
monthlyReturn()
quarterlyReturn()
weeklyReturn()
yearlyReturn()
start_date<-Sys.Date()-days(1460)
##获取股票的反弹数据
get_log_returns<-function(data){
data%>%
mutate(Log.Returns=dailyReturn(as.xts(as.numeric(Close),order.by=Date),subset=NULL,type='arithmetic',leading=TRUE),
Month.Returns=monthlyReturn(as.xts(as.numeric(Close),order.by=Date),subset=NULL,type='arithmetic',leading=TRUE),
Quarter.Returns=quarterlyReturn(as.xts(as.numeric(Close),order.by=Date),subset=NULL,type='arithmetic',leading=TRUE),
Year.Returns=yearlyReturn(as.xts(as.numeric(Close),order.by=Date),subset=NULL,type='arithmetic',leading=TRUE))%>%
select(Date,Log.Returns,Month.Returns,Quarter.Returns,Year.Returns)%>%
as_tibble() }
##### 年线级别投资机会
get_log_returns<-function(data){
data%>%
mutate(Log.Returns=dailyReturn(as.xts(as.numeric(Close),order.by=Date),subset=NULL,type='arithmetic',leading=TRUE))%>%
select(Date,Log.Returns)%>%
as_tibble() }
stocks_name<-df_all%>%
filter(industry_4 %in% c("港口","航运"))
## 2024年12月19号探索长城人寿的战略布局;
stocks_name<-df_all%>%
filter(name %in% c("赣粤高速","五洲交通","上海能源","昊华能源","江南水务","城发环境","中原高速","楚天高速","淮河能源","创业环保","无锡银行","金融街",
"东莞控股","浙江交科","秦港股份","绿色动力"))
stocks_name
library(purrr)
china_stocks <- stocks_name%>%
mutate(
stock.prices = map(ts_code,
function(.x) get_stock_prices(.x)
),
log.returns = map(stock.prices,
function(.x) get_log_returns(.x)),
mean.log.returns = map_dbl(log.returns, ~ mean(.$Log.Returns)),
sd.log.returns = map_dbl(log.returns, ~ sd(.$Log.Returns)),
n.trade.days = map_dbl(stock.prices, nrow)
)%>%as_tibble()
china_stocksdata<-china_stocks%>%
unnest(stock.prices)%>%
mutate(Time=as.numeric(today()-ymd(list_date)))
################################## 针对某个板块异常特定的走势图
china_stocks %>%
unnest(stock.prices)%>%
arrange(desc(Date))%>%
ggplot(aes(x=desc(Date),y=as.numeric(Close)),color=name)+
geom_line()+
facet_wrap(~name,ncol=3,scales = "free_y")+
labs(x="日期",y="价格",title="长城人寿中部崛起合纵与能源连横大战略")+
theme_avatar(title.font = "Slayer",
text.font = "Slayer",
title.size = 14)
#theme_hildaDay(ticks = TRUE,
# legend.position = "none")
library(plotly)
#subplot出现了BUG
china_stocks %>%
unnest(stock.prices)%>%
select(Date,Close,name)%>%
group_by(name) %>%
do(p = plot_ly(., x = ~desc(ymd(Date)), y = ~Close,name=~name)%>%
add_lines) %>%
subplot(nrows =6, shareX = TRUE)%>%
layout(title = "2019~2024年港口与航运集团股价走势")
china_stocks %>%
unnest(stock.prices)%>%
select(Date,Close,name)%>%
group_by(name) %>%
do(p = plot_ly(., x = ~desc(ymd(Date)), y = ~Close,name=~name)%>%
add_lines) %>%
subplot(nrows =6, shareX = TRUE)%>%
layout(title = "2019~2024年港口与航运集团股价走势")
################################聚类算法
library(corrplot)
library(gplots)
library(RColorBrewer)
library(heatmaply)
library(d3heatmap)
library(purrr)
library(dplyr)
library(tidyverse)
####异常公司聚类
heatmapdata<-china_stocks%>%
unnest(stock.prices)%>%
select(Date,name,Close)%>%
mutate(Close=as.numeric(Close))%>%
spread(key=name, value=Close)%>%
select(-Date)
d3heatmap(heatmapdata,scale = "column",
show_grid = TRUE, anim_duration = 500,
distfun = dist, hclustfun = hclust,height = 750,width=1350,main="天网作战聚类算法可视化")%>%
hmLegend(show = TRUE, title = "物以类聚人以群分", location = "tl")
#Heatmap3D(heatmapdata)
#############################################反欺诈算法过滤可疑对象
library(tidyr)
library(tidyverse)
library(timetk)
library(purrr)
library(dplyr)
anoma_data<-china_stocks %>%
unnest(stock.prices)%>%
select(Date,Volume,name)%>%
group_by(name) %>%
tk_anomaly_diagnostics(Date,Volume)%>%
filter(Date>ymd(today()-10))
library(DT)
datatable(anoma_data)
head(anoma_data)
anoma_name<-anoma_data%>%filter(anomaly=="Yes")
anoma_data%>%filter(anomaly=="Yes")%>%select(name,anomaly,Date)%>%knitr::kable()
head(df_all)
###########异常交易数据
anoma_data%>%
filter(anomaly=="Yes")%>%
select(name,anomaly,Date)%>%
left_join(df_all)%>%
select(name,anomaly,Date,close,pct_chg)%>%
arrange(desc(pct_chg))%>%
knitr::kable()
anoma_namedata<-anoma_data%>%
filter(anomaly=="Yes")%>%
select(name,anomaly,Date)%>%
left_join(df_all)%>%
select(name,anomaly,Date,close,pct_chg)%>%
arrange(desc(pct_chg))
#################################防欺诈算法异常数据表格
library(DT)
colnames(anoma_namedata)<-c('公司','是否异常','时间','收盘价','涨跌幅')
datatable(anoma_namedata,caption = htmltools::tags$caption(
style = 'caption-side: top; text-align: center;',
'反欺诈表: ', htmltools::em('主力异常行为数据')),extensions = 'Buttons',
options = list(dom = 'Bfrtip',pageLength = 20,
buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': 'lightpink', 'color': 'brown'});",
"$(this.api().table().body()).css({'background-color': 'gray', 'color': 'red'});",
"}"),searchHighlight = TRUE, filter = 'top'
))%>%
formatStyle('涨跌幅',
color = styleInterval(4, c('red', 'brown')),
backgroundColor = styleInterval(2, c('yellow', 'lightblue')))%>%
formatStyle(
'是否异常',
transform = 'rotateX(15deg) rotateY(1deg) rotateZ(1deg)',
backgroundColor = styleEqual(
unique(anoma_namedata$是否异常), c('lightgreen')
) )
#'background-color': '#000', 'color': '#fff'
plot_ly(anoma_namedata) %>%
add_table()%>%
layout(title='反欺诈算法数据')
library(dplyr)
library(purrr)
library(timetk)
china_data<-china_stocks %>%
unnest(stock.prices)
#####################################OK的
library(showtext)
showtext_auto(enable=TRUE)
a_plot1<-china_stocks %>%
unnest(stock.prices)%>%
select(Date,Volume,name)%>%
filter(name %in% anoma_namedata$公司)%>%
group_by(name) %>%
plot_anomaly_diagnostics(desc(Date), Volume,
.message = FALSE,
.facet_ncol = 3,
.ribbon_alpha = 0.25,
.interactive = FALSE)
a_plot1
ggplotly(a_plot1)%>%layout(title = "天网作战地图之反欺诈算法")
colnames(anoma_data)<-c("名称","日期","观察次数","周期性","趋势性","提示信号","季节调整","一级水平","二级水平","异常","一级维度","二级维度")
datatable(anoma_data)