FlowscopeDocs

strategy reference

Strategy declaration, orders, position and results.

strategy(initialCapital: 100000.0, defaultQtyValue: 1.0) after the header turns a script into a strategy, backtested by a broker emulator that follows TradingView. Place orders from handlers with strategy.entry, strategy.exit, strategy.close and friends; orders fill from the next bar’s open, or at the signal bar’s close with processOrdersOnClose: true. Results appear in the Strategy Tester and as marks on the chart.

Functions

strategy.entry

Flowscope Script
strategy.entry(id: string, direction: Direction, qty: float? = null, limit:, stop:, ocaName:, ocaType:, comment:, alertMessage:)

Enters a position; an entry in the other direction reverses it.

A market order unless limit or stop is given. qty defaults to the declaration’s defaultQtyType and defaultQtyValue. Entries in the same direction are limited by pyramiding. Placing an entry with the id of a pending order replaces it.

ParameterTypeDefault
idstring—
directionDirection—
qtyfloat?null
limitfloat?null
stopfloat?null
ocaNamestring?null
ocaTypeOcaType?null
commentstring?null
alertMessagestring?null

strategy.order

Flowscope Script
strategy.order(id: string, direction: Direction, qty: float? = null, limit:, stop:, ocaName:, ocaType:, comment:, alertMessage:)

Places a raw order: buys or sells qty, ignoring pyramiding and without reversing.

Unlike strategy.entry, an order in the opposite direction reduces the position by its size.

ParameterTypeDefault
idstring—
directionDirection—
qtyfloat?null
limitfloat?null
stopfloat?null
ocaNamestring?null
ocaTypeOcaType?null
commentstring?null
alertMessagestring?null

strategy.exit

Flowscope Script
strategy.exit(id: string, fromEntry: string? = null, qty:, qtyPercent:, profit:, limit:, loss:, stop:, trailPrice:, trailPoints:, trailOffset:, ocaName:, comment:, commentProfit:, commentLoss:, commentTrailing:, alertMessage:)

Places take-profit, stop-loss and trailing-stop orders for an entry.

profit and loss are distances in ticks from the entry price, limit and stop absolute prices; the trailing stop activates at trailPrice (or trailPoints ticks in profit) and follows the price trailOffset ticks behind. Without fromEntry the exit applies to every open entry.

ParameterTypeDefault
idstring—
fromEntrystring?null
qtyfloat?null
qtyPercentfloat?null
profitfloat?null
limitfloat?null
lossfloat?null
stopfloat?null
trailPricefloat?null
trailPointsfloat?null
trailOffsetfloat?null
ocaNamestring?null
commentstring?null
commentProfitstring?null
commentLossstring?null
commentTrailingstring?null
alertMessagestring?null

strategy.close

Flowscope Script
strategy.close(id: string, qty:, qtyPercent:, comment:, alertMessage:, immediately: bool = false)

Closes the trades of entry id with a market order.

Closes all of them unless qty or qtyPercent is given. With immediately: true the order fills at the current close instead of the next open.

ParameterTypeDefault
idstring—
qtyfloat?null
qtyPercentfloat?null
commentstring?null
alertMessagestring?null
immediatelyboolfalse

strategy.closeAll

Flowscope Script
strategy.closeAll(comment:, alertMessage:, immediately: bool = false)

Closes the whole position with a market order.

ParameterTypeDefault
commentstring?null
alertMessagestring?null
immediatelyboolfalse

strategy.cancel

Flowscope Script
strategy.cancel(id: string)

Cancels the pending orders with this id.

ParameterType
idstring

strategy.cancelAll

Flowscope Script
strategy.cancelAll()

Cancels every pending order.

strategy.openTrade

Flowscope Script
strategy.openTrade(index: int) -> StrategyTrade?

The open trade at index, oldest first, or null.

ParameterType
indexint

Returns StrategyTrade?

strategy.closedTrade

Flowscope Script
strategy.closedTrade(index: int) -> StrategyTrade?

The closed trade at index, oldest first, or null.

ParameterType
indexint

Returns StrategyTrade?

Properties

strategy.positionSize

Flowscope Script
strategy.positionSize: float

Position size: positive long, negative short, 0 flat.

Returns float

strategy.positionAvgPrice

Flowscope Script
strategy.positionAvgPrice: float?

Average entry price of the open position, or null when flat.

Returns float?

strategy.positionEntryName

Flowscope Script
strategy.positionEntryName: string?

Id of the entry that opened the position, or null when flat.

Returns string?

strategy.initialCapital

Flowscope Script
strategy.initialCapital: float

The declared initial capital.

Returns float

strategy.equity

Flowscope Script
strategy.equity: float

Initial capital plus net profit plus open profit.

Returns float

strategy.netProfit

Flowscope Script
strategy.netProfit: float

Sum of the closed trades’ profits after commission and funding.

Returns float

strategy.grossProfit

Flowscope Script
strategy.grossProfit: float

Sum of the winning trades’ profits.

Returns float

strategy.grossLoss

Flowscope Script
strategy.grossLoss: float

Sum of the losing trades’ losses, as a positive number.

Returns float

strategy.openProfit

Flowscope Script
strategy.openProfit: float

Unrealised profit of the open trades.

Returns float

strategy.openTrades

Flowscope Script
strategy.openTrades: int

Number of open trades.

Returns int

strategy.closedTrades

Flowscope Script
strategy.closedTrades: int

Number of closed trades.

Returns int

strategy.winTrades

Flowscope Script
strategy.winTrades: int

Number of closed trades with a profit.

Returns int

strategy.lossTrades

Flowscope Script
strategy.lossTrades: int

Number of closed trades with a loss.

Returns int

strategy.evenTrades

Flowscope Script
strategy.evenTrades: int

Number of closed trades at break-even.

Returns int

strategy.maxDrawdown

Flowscope Script
strategy.maxDrawdown: float

Largest equity drawdown so far, in account currency.

Returns float

strategy.maxRunup

Flowscope Script
strategy.maxRunup: float

Largest equity run-up so far, in account currency.

Returns float

strategy.maxContractsHeldAll

Flowscope Script
strategy.maxContractsHeldAll: float

Largest position held so far, either side.

Returns float

strategy.maxContractsHeldLong

Flowscope Script
strategy.maxContractsHeldLong: float

Largest long position held so far.

Returns float

strategy.maxContractsHeldShort

Flowscope Script
strategy.maxContractsHeldShort: float

Largest short position held so far.

Returns float

strategy.marginLiquidationPrice

Flowscope Script
strategy.marginLiquidationPrice: float?

Price at which a margin call would liquidate the position, or null.

Returns float?

Records

StrategyTrade

Flowscope Script
type StrategyTrade

A trade returned by strategy.openTrade and strategy.closedTrade.

FieldTypeHistoryDescription
entryIdstring—
entryPricefloat—
entryTimetime—
entryBarIndexint—
entryCommentstring?—
sizefloat—
exitIdstring?—
exitPricefloat?—
exitTimetime?—
exitBarIndexint?—
exitCommentstring?—
profitfloat—
profitPercentfloat—
commissionfloat—
maxRunupfloat—
maxDrawdownfloat—

Constants

ConstantTypeValueDescription
strategy.longDirection1Long direction for strategy.entry and strategy.order.
strategy.shortDirection2Short direction for strategy.entry and strategy.order.
strategy.fixedQtyType0defaultQtyType: defaultQtyValue contracts per order.
strategy.cashQtyType1defaultQtyType: as many contracts as defaultQtyValue of account currency buys.
strategy.percentOfEquityQtyType2defaultQtyType: defaultQtyValue percent of current equity per order.
strategy.commission.percentCommissionType0commissionType: percent of the order value.
strategy.commission.cashPerContractCommissionType1commissionType: money per contract traded.
strategy.commission.cashPerOrderCommissionType2commissionType: money per filled order.
strategy.oca.noneOcaType0No one-cancels-all behaviour.
strategy.oca.cancelOcaType1A fill cancels the other orders of the OCA group.
strategy.oca.reduceOcaType2A fill reduces the other orders of the OCA group by the filled size.