12. R Basic
• a=0
• for (i in 1:100){a=a+1}
• a=0
• while (a<=100){a=a+1}
• a=0
• for (i in seq(1,99,by=2)){a=a+i}
2016/09 12
1+1+1+…+1=?
1+1+1+…+1=?
1+3+5+…+97+99=?
43. 交易的馬丁格爾
• 產生一個計算每次部位大小的向量
• PZ=setNames(rep(1,length(profit)),names(profit))
• 如果上次輸,下次加倍買
• if (sign(profit)[m-1]<0){PZ[m]=2*PZ[m-1]}
• 如果上次贏,下次買一張
• If (sign(profit)[m-1]<0){PZ[m]=1}
• 計算每一次的PZ
• for (m in 2:length(profit)){
if (sign(profit)[m-1]<0){PZ[m]=2*PZ[m-1]}
if (sign(profit)[m-1]>0){PZ[m]=1}
If (sign(profit)[m-1]==0){PZ[m]=PZ[m-1]}
}
2016/09 43
123. 2016/09
Total Profit: 3054
# of Win: 161
Win Rate: 39.07767 %
Maximum Draw Down: 1273
The Periods of MDD: 112 171 188 204 254
Profit Factor: 1.150466
Total Profit/MDD: 2.399057
Total Profit: 1140
# of Win: 161
Win Rate: 39.07767 %
Maximum Draw Down: 312
The Periods of MDD: 107 112 171 188 254
Profit Factor: 1.192763
Total Profit/MDD: 3.653846 123
152. 完整交易系統架構圖
Application
Order Manager
Market Data
Complex Event Processing
engine
Exchange
1
Storage
Application Server Exchange
Strategy
Settings
UI
State
Mgmt (PnL
+ Position)
Order /
Execution
Monitor
Within
application
TMS
Maths
Calc
OMS
Admin
Monitor
Exchange
2
F
I
X
F
I
X
Data
Normalizer
Order
Router
Back
office
record
MktData
Store
Event
History
Adaptor for
third party
apps – R,
Matlab, etc
Data
Retrieval
Data Vendor
Replay
of stored
data
Simulator
exchange
API
軟
體
6
157. IB Trader Workstation 系統資訊流
Establish
Connection
ibtws
Get Info
accounts
contractdetails
portfolio
Get Data
getdata
timeseries
history
realtime
marketdepth
Create Orders
orderid
createorder
orders
executions
Close
Connection
close
163. R 計量財務套件規劃圖
Cran Task View: Empirical Finance
https://cran.r-project.org/web/views/Finance.html
Big data
High Frequency
High Frequency
Sendrequeststheserver
Downloadandload
Data Mining
quant mod
Raw Data
A web application
Online
Shiny
Price Data
TTR
Techical Trading
Candlestick Chart
Additions of different
lines to the chart
* Moving Average
* Bollinger Bands
* Moving Average
Convergence
Divergence
Demonstration
framework for R
Statistics
* Downside risk
measures
* Risk-adjusted
performance
Plots
* Density plot
* Time series plot
* Drawdown plot
Return Data
Performance Analytics
Performance Analysis
Data Journcy
ExamplesofUsingRinFinanceExtensions
Datacleansing
Datatransformation
Webpages
Files
Database
RCurl
httr
rvest
17