TradingLogic

Exported


#### TradingLogic.perf_prom Pessimistic

return on margin marg.

For pror = true returns pessimistic rate of return (can be thought of as a more realistic profit factor).

Optional nbest_remove argument specifies how many best wins are dropped (to increase PROM strictness).

CAUTION -Inf returned if nbest_remove exceeds the total number of trades.

source: TradingLogic/src/performance.jl:292


#### TradingLogic.runtrading!

Event-driven backtesting / live trading.

source: TradingLogic/src/TradingLogic.jl:26


#### TradingLogic.tradepnlfinal

Final profit/loss for blotter provided as DateTime => (Qty::Int64, FillPrice::Float64) assoc. collection. Faster verision (minimizing memory allocation) to be used in e.g. parameter optimization workflow.

Returns: final profit/loss Float64 scalar.

source: TradingLogic/src/performance.jl:171


#### emptyblotter() Initialize empty

blotter as an associative collection DateTime => (Qty::Int64, FillPrice::Float64)

source: TradingLogic/src/types.jl:59


#### perf_prom(blotter::Dict{DateTime, Tuple{Int64, Float64}})

From blotter using completed trades.

source: TradingLogic/src/performance.jl:305


#### perf_prom(vtrpnl::Array{Float64, 1})

From profit/loss vector of completed trades.

source: TradingLogic/src/performance.jl:325


#### printblotter(io::IO, blotter::Dict{DateTime, Tuple{Int64,

Float64}}) Print blotter transactions. Resembles DataFrames.printtable.

source: TradingLogic/src/performance.jl:42


#### runbacktesttarg{M}(ohlc_ta::TimeSeries.TimeArray{Float64, 2, M},

ohlc_inds::Dict{Symbol, Int64}, fileout::Union{AbstractString, Void}, dtformat_out, pfill::Symbol, position_initial::Int64, targetfun::Function, strategy_args...) Similar to runbacktest but instead of performance metrics, current position and targets from the latest step are included in the output.

Input: same as runbacktest.

Return tuple components:

  • transaction blotter as an associative collection;
  • Int64 position as of the latest timestep;
  • Targ targeting tuple as of the latest timestep.

This function is useful to run through a recent historical period and determine the latest timestep actions.

source: TradingLogic/src/TradingLogic.jl:223


#### runtrading!(blotter::Dict{DateTime, Tuple{Int64, Float64}},

s_ohlc::Reactive.Input{Tuple{DateTime, Array{Float64, 1}}}, ohlc_inds::Dict{Symbol, Int64}, s_pnow::Reactive.Signal{Float64}, position_initial::Int64, targetfun::Function, strategy_args...) Backtesting or real-time order submission with status output.

Input:

  • blotter (could be initially empty) to write transactions to, as an associative collection DateTime => (Qty::Int64, FillPrice::Float64)`;
  • backtest is Bool, live trading performed if false;
  • s_ohlc is tuple-valued (DateTime, Vector-ohlc) signal;
  • ohlc_inds provides index correspondence in Vector-ohlc;
  • s_pnow is instantaneous price signal;
  • position_initial corresponds to the first timestep;
  • targetfun is the trading strategy function generating (poschg::Int64, Vector[limitprice, stopprice] signal;
  • additional arguments ... to be passed to targetfun: these would most commonly be trading strategy parameters.

In-place modifies blotter (adds transactions to it).

Returns tuple-signal with:

  • the overall status of the trading system (false if problems are detected);
  • current cumulative profit/loss since the signals were initiated (i.e. since the beginning of the trading session).

See orderhandling! for the PnL details.

source: TradingLogic/src/TradingLogic.jl:72


#### tradeperf(blotter::Dict{DateTime, Tuple{Int64, Float64}})

Trade analysis for blotter provided as DateTime => (Qty::Int64, FillPrice::Float64) assoc. collection. Input metrics specifies what to calculate (PnL included already - others). Returns: tuple ( DateTime (ordered) array , assoc. collection of perf metrics ). Basic transaction info is also included (quantity, fill price).

CAUTION: PnL and drawdown are calculated here based on the transaction blotter only, not the price history. Hence, price swing effects while holding an open position are not showing up in the results. Use orderhandling! output if performance metrics over the whole price history are needed (as typically done when analyzing PnL and drawdown).

source: TradingLogic/src/performance.jl:95


#### tradeperf(blotter::Dict{DateTime, Tuple{Int64, Float64}},

metrics::Array{Symbol, 1}) Trade analysis for blotter provided as DateTime => (Qty::Int64, FillPrice::Float64) assoc. collection. Input metrics specifies what to calculate (PnL included already - others). Returns: tuple ( DateTime (ordered) array , assoc. collection of perf metrics ). Basic transaction info is also included (quantity, fill price).

CAUTION: PnL and drawdown are calculated here based on the transaction blotter only, not the price history. Hence, price swing effects while holding an open position are not showing up in the results. Use orderhandling! output if performance metrics over the whole price history are needed (as typically done when analyzing PnL and drawdown).

source: TradingLogic/src/performance.jl:95


#### tradeperfcurr(s_status::Reactive.Signal{Tuple{Bool, Float64}})

Selected performance metrics from runtrading! signal output.

Output tuple-signal components:

  • Float64 cumulative maximum PnL;
  • Float64 maximum drawdown over the entire trading session hisotry.

NOTE: Use this function only if needed, otherwise save resources; it is not required for running the trading session.

source: TradingLogic/src/performance.jl:213


#### tradepnlfinal(blotter::Dict{DateTime, Tuple{Int64, Float64}})

Based on blotter only, ending at the last transaction timestamp.

source: TradingLogic/src/performance.jl:174


#### tradepnlfinal(blotter::Dict{DateTime, Tuple{Int64, Float64}},

pnow::Float64) Adding current price as the last timestamp.

source: TradingLogic/src/performance.jl:177


#### vtradespnl(blotter::Dict{DateTime, Tuple{Int64, Float64}})

Selected metrics for completed trades out of transactions blotter.

Return tuple contains:

  • Vector{Float64} profit/loss for each completed trade;
  • Int64 number of winning trades;
  • Float64 average winning trade profit;
  • Int64 number of loosing trades;
  • Float64 average loosing trade loss.

source: TradingLogic/src/performance.jl:226


#### writeblotter(filename::AbstractString, blotter::Dict{DateTime,

Tuple{Int64, Float64}}) Write blotter transactions to file.

source: TradingLogic/src/performance.jl:72

Internal


#### TradingLogic.query_orderstatus

Get order status by order ID string. Returns Symbol in line with Order-type options for status-slot.

source: TradingLogic/src/exchange.jl:26


#### TradingLogic.submit_ordercancel

Cancel order request. Returns Bool request result.

source: TradingLogic/src/exchange.jl:66


#### TradingLogic.submit_ordernew

Submit new order. Returns order ID string or FAIL-string

source: TradingLogic/src/exchange.jl:47


#### apnlcum(blotter::Dict{DateTime, Tuple{Int64, Float64}})

Cumulative position, profit/loss, last fill price for blotter.

source: TradingLogic/src/performance.jl:139


#### emptyorder() Empty order: no

quantity

source: TradingLogic/src/types.jl:27


#### fsigchange(prev, x) Signal value

change function to be used with foldl; use with (Bool, signal_t=0) tuple as initial fold value

source: TradingLogic/src/sigutils.jl:9


#### getorderposchg(orde::TradingLogic.Order)

Signed position change in the Order object

source: TradingLogic/src/types.jl:39


#### goldencrossmktstate(mafast::Float64, maslow::Float64)

Market state in goldencross strategy.

source: TradingLogic/src/strategies/goldencross.jl:2


#### goldencrossposlogic(mktstate::Symbol, targetqty::Int64,

position_actual_mut::Array{Int64, 1}) Target position for goldencross strategy. This simplest form involves only market orders, long-side enter. ... Returns (poschg::Int64, Vector[limitprice, stopprice].

source: TradingLogic/src/strategies/goldencross.jl:22


#### goldencrosstarget(s_ohlc::Reactive.Input{Tuple{DateTime,

Array{Float64, 1}}}, ohlc_inds::Dict{Symbol, Int64}, position_actual_mut::Array{Int64, 1}, targetqty::Int64) Target signal for goldencross strategy.

source: TradingLogic/src/strategies/goldencross.jl:49


#### goldencrosstarget(s_ohlc::Reactive.Input{Tuple{DateTime,

Array{Float64, 1}}}, ohlc_inds::Dict{Symbol, Int64}, position_actual_mut::Array{Int64, 1}, targetqty::Int64, nsma_fast::Int64) Target signal for goldencross strategy.

source: TradingLogic/src/strategies/goldencross.jl:49


#### goldencrosstarget(s_ohlc::Reactive.Input{Tuple{DateTime,

Array{Float64, 1}}}, ohlc_inds::Dict{Symbol, Int64}, position_actual_mut::Array{Int64, 1}, targetqty::Int64, nsma_fast::Int64, nsma_slow::Int64) Target signal for goldencross strategy.

source: TradingLogic/src/strategies/goldencross.jl:49


#### initbuff(nbuff::Int64, xinit::Float64)

Initialization of nbuff-size float-elements buffer with NaNs and last element xinit.

source: TradingLogic/src/sigutils.jl:43


#### ispending(orde::TradingLogic.Order)

Check if order status is :pending

source: TradingLogic/src/types.jl:30


#### luxormktstate(mafast::Float64, maslow::Float64)

Market state in luxor strategy

source: TradingLogic/src/strategies/luxor.jl:2


#### luxorposlogic(mktstate::Symbol, mktchgh::Float64,

mktchgl::Float64, pthresh::Float64, targetqty::Int64, position_actual_mut::Array{Int64, 1}) Target position and stop, limit prices (if any) for luxor strategy. ... Returns (poschg::Int64, Vector[limitprice, stopprice].

source: TradingLogic/src/strategies/luxor.jl:30


#### luxortarget(s_ohlc::Reactive.Input{Tuple{DateTime, Array{Float64,

1}}}, ohlc_inds::Dict{Symbol, Int64}, position_actual_mut::Array{Int64, 1}, nsma_fast::Int64, nsma_slow::Int64, pthreshold::Float64, targetqty::Int64) Target signal for luxor strategy.

source: TradingLogic/src/strategies/luxor.jl:60


#### neworderid(trig::ASCIIString)

Generate oder ID string for a new order

source: TradingLogic/src/orderhandl.jl:4


#### orderhandling!(targ::Tuple{Int64, Array{Float64, 1}},

pnow::Float64, tnow::DateTime, position_actual_mut::Array{Int64, 1}, ordcurr::TradingLogic.Order, blotter::Dict{DateTime, Tuple{Int64, Float64}}, backtest::Bool) Order handling for backtesting and live trading. Input: - target targ as (poschg::Int64, Vector[limitprice, stopprice]; - current/instantaneous price pnow - current time tnow; for backtest, the time corresponding to targ (i.e. the current OHLC step/bar time).

In-place modifies:

  • position_actual_mut vector;
  • ordcurr object;
  • backtestblotter associative collection.

Returns tuple with:

  • Bool system status;
  • Float64 current cumulative profit/loss.

NOTE: As opposed to tradeperf function, here total PnL is updated at each price change time-point.

source: TradingLogic/src/orderhandl.jl:87


#### perf_pror_auxil(ppos::Float64, pneg::Float64)

Pessimistic rate of return with extreme case handling.

source: TradingLogic/src/performance.jl:295


#### plimitcheck(orde::TradingLogic.Order, pnow::Float64)

Backtesting helper function: check if limit-price is reached

source: TradingLogic/src/exchange.jl:12


#### printvecstring(io, vstring::Array{T, 1}, separator::Char,

quotemark::Char) Print a text line from string vector.

source: TradingLogic/src/performance.jl:25


#### query_orderstatus(orde::TradingLogic.Order, pnow::Float64)

Order status: backtesting version based on current price pnow

source: TradingLogic/src/exchange.jl:29


#### query_orderstatus(ordid::ASCIIString)

Order status: live version

source: TradingLogic/src/exchange.jl:41


#### runbacktestcore{M}(ohlc_ta::TimeSeries.TimeArray{Float64, 2, M},

s_ohlc::Reactive.Input{Tuple{DateTime, Array{Float64, 1}}}, s_status::Reactive.Signal{Tuple{Bool, Float64}}, s_perf::Reactive.Signal{Tuple{Float64, Float64}}, fileout::Union{AbstractString, Void}, dtformat_out) Core of the backtest run.

source: TradingLogic/src/TradingLogic.jl:255


#### schange{T}(s_inp::Reactive.Signal{T})

Bool change signal, true when input signal changes

source: TradingLogic/src/sigutils.jl:16


#### setcancelled!(orde::TradingLogic.Order)

Change order status to :cancelled

source: TradingLogic/src/types.jl:33


#### sighistbuffer!(buffer, valnew)

Buffer for storing previous signal values to be used with foldl when indicators are calculated based on signal history.

IMPORTANT: Initial value supplied to foldl determines buffer window size, i.e. how many past signal values are retained (rolling window size). In the case of e.g. SMA that would be moving average window. Specifying initial value may be tricky: see test/signals.jl.

In-place modifies buffer argument and returns updated one.

source: TradingLogic/src/sigutils.jl:33


#### submit_ordercancel(orde::TradingLogic.Order)

Cancel pending order backtest version

source: TradingLogic/src/exchange.jl:69


#### submit_ordercancel(ordid::ASCIIString)

Cancel order live version: provide order ID string ordid

source: TradingLogic/src/exchange.jl:79


#### submit_ordernew(orde::TradingLogic.Order, backtest::Bool)

New order submission: backtesting version.

source: TradingLogic/src/exchange.jl:50


#### submit_ordernew(orde::TradingLogic.Order,

position_actual::Int64) New order submission: live version

source: TradingLogic/src/exchange.jl:59


#### targ2order!(orde::TradingLogic.Order, targ::Tuple{Int64,

Array{Float64, 1}}, trig::ASCIIString, position_actual::Int64, backtest::Bool) Prepare new order from targ ((poschg::Int64, Vector[limitprice,stopprice]) and trigger-string trig. Note: this function prepares limit and market orders for submission. Stop-part of stoplimit orders is handled at the software level in orderhandling! (even for live trading), which calls targ2order! for limit order submission if stop-price of stoplimit order is reached. ... Overwrites orde and returns Bool request status.

source: TradingLogic/src/orderhandl.jl:20


#### tradeperffold(perfprev::Tuple{Float64, Float64},

statusnow::Tuple{Bool, Float64}) Performance metrics helper function for use in foldl.

source: TradingLogic/src/performance.jl:186


#### vapblotter(blotter::Dict{DateTime, Tuple{Int64, Float64}})

Amount Vector{Int64) and price Vector{Float64) from blotter in chronological order (returns vector tuple).

source: TradingLogic/src/performance.jl:10


#### vtblotter(blotter::Dict{DateTime, Tuple{Int64, Float64}})

Ordered timestamps from blotter associative collection.

source: TradingLogic/src/performance.jl:4


#### TradingLogic.Order Order type

source: TradingLogic/src/types.jl:5