cpp_Processor: C++ Processor class

Description Arguments Usage Public Members and Methods Execution Model Ticks Candles Orders Trades Summary Daily Performance Options See Also Examples

Description

C++ class documentation

Arguments

timeFrame

candle timeframe in seconds

latencySend, latencyReceive

latency in seconds

Usage

Processor( int timeFrame, double latencySend, double latencyReceive )

Public Members and Methods

Name Return Type Description
onCandle( Candle candle ) std::function called on new candle event
onTick( Tick tick ) std::function called on new tick event
onMarketOpen() std::function called on trading hours start
onMarketClose() std::function called on trading hours end
onIntervalOpen() std::function called on intervals start
onIntervalClose() std::function called on intervals end
Feed( Tick tick ) void process by individual tick
Feed( Rcpp::DataFrame ticks ) void batch process, see 'Ticks' section
SendOrder( Order* order ) void send order to exchange
SetCost( Cost cost ) void set trading costs
SetCost( Rcpp::List cost ) void see 'cost' in 'Options' section
SetStop( Rcpp::List stop ) void see 'stop' in 'Options' section
SetStartTradingTime( double t ) void see 'trade_start' in 'Options' section
SetLatencyReceive( double x ) void see 'latency_receive' in 'Options' section
SetLatencySend( double x ) void see 'latency_send' in 'Options' section
SetLatency( double x ) void see 'latency' in 'Options' section
SetTradingHours( double start, double end ) void see 'trading_hours' in 'Options' section
SetPriceStep( double priceStep ) void see 'price_step' in 'Options' section
SetExecutionType( ExecutionType executionType ) void see 'execution_type' in 'Options' section
SetExecutionType( std::string executionType ) void see 'execution_type' in 'Options' section
SetIntervals( std::vector<double> starts, std::vector<double> ends ) void see 'intervals' in 'Options' section
AllowLimitToHitMarket() void see 'allow_limit_to_hit_market' in 'Options' section
AllowExactStop() void see 'allow_exact_stop' in 'Options' section
SetOptions( Rcpp::List options ) void see 'Options' section
StopTrading() void if called trading stop triggered. See 'stop' in 'Options' section
CanTrade() bool check if trading not stopped
IsTradingHoursSet() bool check if trading hours set
CancelOrders() void cancel active orders
GetCandle() Candle get current candle
GetPosition() int total executed position, positive means long, negative means short
GetPositionPlanned() int total number of orders processing ( not executed or cancelled yet )
GetMarketValue() double total portfolio percent value ( initial value is 0 )
GetCandles() Rcpp::List candles history, see 'Candles' section
GetOrders() Rcpp::List orders history, see 'Orders' section
GetTrades() Rcpp::List trades history, see 'Trades' section
GetSummary() Rcpp::List trades summary, see 'Summary' section
GetOnCandleMarketValueHistory() std::vector<double> vector of portfolio value history recalculated on candle complete
GetOnCandleDrawDownHistory() std::vector<double> vector of portfolio drawdown history recalculated on candle complete
GetOnDayClosePerformanceHistory() Rcpp::List daily performance history, see 'Daily Performance' section
Reset() void resets to initial state

Execution Model

System sends new order and after latencySend seconds it reaches exchange. System receives confirmation of order placement latencyReceive seconds later. When execution conditions met on exchange - order is executed and system receives execution confirmation latencyReceive seconds later.
When system sends cancel request to exchange and after latencySend seconds when exchange receives cancel request if order is not executed yet it is cancelled and cancellation confirmation is received by system after latencyReceive seconds.
Two execution types supported trade(default) and bbo. trade type processes orders using tick prices and bbo processes orders using preceding tick bid and ask values. Market orders in bbo mode executed at worst price: at bid for sells and at ask for buys, in trade mode at current tick price. Buy limit orders executed when ask goes under order price and sell orders executed when bid goes above order price. In case limit order is placed in the market it is executed as market order if allow_limit_to_hit_market set to TRUE (default is FALSE).

Ticks

Ticks must be a data.frame/data.table with at least the following columns:

Name Description
time time
price price
volume volume

tick id is ticks row number.

Candles

Candles returned as data.table with the following columns:

Name Description
time time when formed
open first tick price
high maximum tick price
low minimum tick price
close last tick price
volume total volume traded
id tick id when formed ( first tick after time formed )

Orders

Orders returned as data.table with the following columns:

Name Description
id_trade trade id
id_sent tick id when order was sent to exchange
id_processed tick id when enter order execution or cancelled confirmation was received ( first tick after time_processed )
time_sent time when order was sent to exchange
time_processed time when order execution or cancelled confirmation was received
price_init initial price
price_exec execution price
side buy/sell
type limit/market/stop/trail
state new/registered/executed/cancelling/cancelled
comment comment

Trades

Two orders are combined into trade by trade id. The first and the second orders are called enter and exit respectively.
Trade side is long if enter order is buy and short if enter order is sell.
Orders must be buy and sell only. Two buys or two sells not allowed. Trade can be

Trades returned as data.table with the following columns:

Name Description
id_trade trade id
id_sent tick id when enter order was sent to exchange
id_enter tick id when enter order execution confirmation was received ( first tick after enter time_executed )
id_exit tick id when exit order execution confirmation was received ( first tick after exit time_executed )
time_sent time when enter order sent to exchange
time_enter time when enter order execution confirmation was received
time_exit time when exit order execution confirmation was received
side side long/short
price_enter enter order execution price
price_exit exit order execution price
pnl trade pnl net
mtm mark-to-market
mtm_min min mark-to-market
mtm_max max mark-to-market
cost absolute trading cost
pnl_rel trade pnl net in basis points
mtm_rel mark-to-market in basis points
mtm_min_rel min mark-to-market in basis points
mtm_max_rel max mark-to-market in basis points
cost_rel relative trading cost in basis points
state new/opened/closed

Summary

Back test summary statistics:

Name Description
from first tick time
to last tick time
days_tested number of trading days tested
days_traded number of trading days traded ( at least one order was executed )
n_per_day number of trades per day
n number of trades
n_long number of long trades
n_short number of short trades
n_win number of winning trades
n_loss number of loosing trades
pct_win percent of winning trades
pct_loss percent of loosing trades
avg_win average winning trade in basis points
avg_loss average loosing trade in basis points
avg_pnl average trade pnl in basis points
win total won in percent
loss total lost in percent
pnl total pnl in percent
max_dd maximum drawdown in percent
max_dd_start time the maximum drawdown started
max_dd_end time the maximum drawdown recovered
max_dd_length number of calendar days in the maximum drawdown period
sharpe annualized Sharpe ratio calulated on daily returns
sortino annualized Sortino ratio calulated on daily returns
r_squared R Squared calulated on daily PnL values
avg_dd average drawdown calulated on daily drawdown history

Daily Performance

Back test daily performance history:

Name Description
date date
return return
pnl cumulative pnl
drawdown drawdown
n_per_day number of closed trades
avg_pnl average trade pnl

Options

List of following elements. All options are optional.

cost

list or data.table with items identical to Cost C++ class.
E.g. if set to data.table( tradeAbs = -0.01, shortRel = -0.05 / 360 ) means you pay -$0.01 per executed order and -5% p.a. overnight short.

stop

list or data.table with at least one item:

drawdown

Trading stops when drawdown exceeds set value. E.g. if set to -0.02 then when drawdown exceeds 2% trading stops.

loss

Trading stops when market value (P&L) is lower set value. E.g. if set to -0.05 then when market value (P&L) is lower than -5% trading stops.

If stop rule triggered no orders sent to exchange and opened trades closed by market orders.

trade_start

POSIXct timestamp. All orders ignored until specified time. Useful to 'warm-up' strategy.

latency_send, latency_receive, latency

numeric value. Latency can be set by send/receive or overall. 'latency' sets send and receive latency as x / 2. See 'Execution Model' section.

trading_hours

numeric vector of length two. Sets trading hours start and end according to formula:
hours + minutes / 60 + seconds / 3600.
If set onMarketOpen, onMarketClose events are executed at corresponding times.
E.g. if set to c( 10.25, 17.5 ) means onMarketOpen event called every day at '10:15' and onMarketClose event called every day at '17:30'.
For convenience IsTradingHoursSet() method can be used to check wether trading hours are set.

allow_limit_to_hit_market

if TRUE, limit order execution price set to market price if executed on same tick as registered.

allow_exact_stop

if TRUE, stop order executed at set price.

price_step

if positive, limit order init price rounded to price_step down for buy orders and up for sell orders before placement. if negative, limit order init price rounded to price_step up for buy orders and down for sell orders before placement.

execution_type

trade or bbo.

intervals

sorted multi row data.table with POSIXct timestamps columns start, end. Represents time intervals. At time start onIntervalOpen called and at time end onIntervalClose called.

See Also

Other backtesting classes: Candle, Cost, Indicator, Order, Tick

Other C++ classes: BBands, Candle, Cost, Crossover, Ema, Indicator, Order, RollLinReg, RollPercentRank, RollRange, RollSd, RollVolumeProfile, Rsi, Sma, Stochastic, Tick

Examples

  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
######################################
## Simple Moving Averages Crossover ##
######################################

# load tick data
data( 'ticks' )

# define strategy
strategy_source = system.file( package = 'QuantTools', 'examples/sma_crossover.cpp' )
# compile strategy
Rcpp::sourceCpp( strategy_source )

# set strategy parameters
parameters = data.table(
  period_fast = 50,
  period_slow = 30,
  timeframe   = 60
)

# set options, see 'Options' section
options = list(
  cost    = list( tradeAbs = -0.01 ),
  latency = 0.1 # 100 milliseconds
)

# run test
test_summary = sma_crossover( ticks, parameters, options, fast = TRUE )
print( test_summary )

# run test
test = sma_crossover( ticks, parameters, options, fast = FALSE )

# plot result
indicators = plot_dts(
test$indicators,
test$orders[ side == 'buy' , .( time_processed, buy  = price_exec ) ],
test$orders[ side == 'sell', .( time_processed, sell = price_exec ) ] )$
lines( c( 'sma_fast', 'sma_slow' ) )$
lines( c( 'buy', 'sell' ), type = 'p', pch = c( 24, 25 ), col = c( 'blue', 'red' ) )

performance = plot_dts( test$indicators[, .( time, pnl = pnl * 100, drawdown = drawdown * 100 ) ] )$
lines( c( 'pnl', 'drawdown' ), c( '% pnl', '% drawdown' ), col = c( 'darkolivegreen', 'darkred' ) )

interval = '2016-01-19 12/13'
par( mfrow = c( 2, 1 ), oma = c( 5, 4, 2, 4 ) + 0.1, mar = c( 0, 0, 0, 0 ) )
indicators $limits( tlim = interval )$style( time = list( visible = FALSE ) )
performance$limits( tlim = interval )
title( 'Simple Moving Averages Crossover', outer = TRUE )
par( mfrow = c( 1, 1 ), oma = c( 0, 0, 0, 0 ), mar = c( 5, 4, 4, 2 ) + 0.1 )




#####################
## Bollinger Bands ##
#####################

# load tick data
data( 'ticks' )

# define strategy
strategy_source = system.file( package = 'QuantTools', 'examples/bbands.cpp' )
# compile strategy
Rcpp::sourceCpp( strategy_source )

# set strategy parameters
parameters = data.table(
  n         = 100,
  k         = 0.5,
  timeframe = 60
)

# set options, see 'Options' section
options = list(
  cost    = list( tradeAbs = -0.01 ),
  latency = 0.1 # 100 milliseconds
)

# run test
test_summary = bbands( ticks, parameters, options, fast = TRUE )
print( test_summary )

# run test
test = bbands( ticks, parameters, options, fast = FALSE )

# plot result
indicators = plot_dts(
test$indicators,
test$orders[ side == 'buy' , .( time_processed, buy  = price_exec ) ],
test$orders[ side == 'sell', .( time_processed, sell = price_exec ) ] )$
lines( c( 'lower', 'sma', 'upper' ) )$
lines( c( 'buy', 'sell' ), type = 'p', pch = c( 24, 25 ), col = c( 'blue', 'red' ) )

performance = plot_dts( test$indicators[, .( time, pnl = pnl * 100, drawdown = drawdown * 100 ) ] )$
lines( c( 'pnl', 'drawdown' ), c( '% pnl', '% drawdown' ), col = c( 'darkolivegreen', 'darkred' ) )

interval = '2016-01-19 12/13'
par( mfrow = c( 2, 1 ), oma = c( 5, 4, 2, 4 ) + 0.1, mar = c( 0, 0, 0, 0 ) )
indicators $limits( tlim = interval )$style( time = list( visible = FALSE ) )
performance$limits( tlim = interval )
title( 'Bollinger Bands', outer = TRUE )
par( mfrow = c( 1, 1 ), oma = c( 0, 0, 0, 0 ), mar = c( 5, 4, 4, 2 ) + 0.1 )




##################################
## Bollinger Bands Market Maker ##
##################################

# load tick data
data( 'ticks' )

# define strategy
strategy_source = system.file( package = 'QuantTools', 'examples/bbands_market_maker.cpp' )
# compile strategy
Rcpp::sourceCpp( strategy_source )

# set strategy parameters
parameters = data.table(
  n         = 100,
  k         = 0.5,
  timeframe = 60
)

# set options, see 'Options' section
options = list(
  cost    = list( tradeAbs = -0.01 ),
  latency = 0.1, # 100 milliseconds
  allow_limit_to_hit_market = TRUE
)

# run test
test_summary = bbands_market_maker( ticks, parameters, options, fast = TRUE )
print( test_summary )

# run test
test = bbands_market_maker( ticks, parameters, options, fast = FALSE )

# plot result
indicators = plot_dts(
test$indicators,
test$orders[ side == 'buy' , .( time_processed, buy  = price_exec ) ],
test$orders[ side == 'sell', .( time_processed, sell = price_exec ) ] )$
lines( c( 'lower', 'sma', 'upper' ) )$
lines( c( 'buy', 'sell' ), type = 'p', pch = c( 24, 25 ), col = c( 'blue', 'red' ) )

performance = plot_dts( test$indicators[, .( time, pnl = pnl * 100, drawdown = drawdown * 100 ) ] )$
lines( c( 'pnl', 'drawdown' ), c( '% pnl', '% drawdown' ), col = c( 'darkolivegreen', 'darkred' ) )

interval = '2016-01-19 12/13'
par( mfrow = c( 2, 1 ), oma = c( 5, 4, 2, 4 ) + 0.1, mar = c( 0, 0, 0, 0 ) )
indicators $limits( tlim = interval )$style( time = list( visible = FALSE ) )
performance$limits( tlim = interval )
title( 'Bollinger Bands On Limit Orders', outer = TRUE )
par( mfrow = c( 1, 1 ), oma = c( 0, 0, 0, 0 ), mar = c( 5, 4, 4, 2 ) + 0.1 )

QuantTools documentation built on Oct. 23, 2020, 7:54 p.m.