Trade Smarter with Automation

Positions

This document will cover working with positions in Alta5. It assumes the reader has basic programming skills and has reviewed the Strategy Basics guide.

Overview

Entering

When the onSeek method of a strategy finds a position it wants to open, it initiates the entry of a new position by calling the Trader's enter method.

enter

Mixed security, String memo, Number quantity
Initiates a trade to enter a new position. By default, enter will create a trade for the maximum quantity of the passed Security or Spread this Trader has funds to enter. You can override that behavior by passing a custom quantity. securityMixed Any Equity, Index, Option or Spread. memoString A memo to include on the created trade and position. quantityNumber A custom quantity for the trade. Using a custom quantity overrides Alta5's built-in risk controls.

Example


onSeek: function(target, data){
    var bar = data.chart.lastBar,
        rsi = bar.indicator('rsi');

    if(rsi <= data.overboughtSignal){
        var option = data.chain.firstSeries.getCallAbove(target.last);
        this.enter(option, 'RSI Overbought: {0}'.format(rsi));
    }
}

Automation

Following the call to enter, the entire workflow for opening a position - quantity, pricing, bid/ask gap, routing and trade events - is automated based on the configurations set for the strategy or Trader.

Managing

After a position has successfully been entered, the onManage method of the strategy will be called on every Market tick to give the Trader an opportunity to close the position.

onManage

Security target, Position position
Called every Market tick when the Trader is ready and has an active position. Generally scans available market data for specific exit conditions. targetSecurity The target security for this Trader (this.target). positionPosition The active position for this Trader. dataObject An object containing properties with parameter values and any data loaded by workers. The property name for values is the id of the parameter or worker.

Example


onManage: function(target, position, data){
    var bar = data.chart.lastBar,
        macd = bar.indicator('macd'),
        signal = bar.indicator('macd', 'signal'),
        rsi = bar.indicator('rsi');

    // macd cross under
    if(macd < signal){
        return this.exit(position, 'MACD cross under');
    }

    // rsi overbought
    if(rsi >= this.get('overboughtSignal')){
        return this.exit(position, 'RSI overbought');
    }
}

The onSeek method of the strategy will not be called while a Trader has a position.

Exiting

When the onManage method of a strategy determines it wants to close a position, it initiates the exit of the position by calling the Trader's exit method.

Like opening a position, the entire workflow for closing a position is automated based on the configurations set for the strategy or Trader.

exit

Position position, String memo, mixed waitTime
Initiates a trade to exit a position. By default, exit will create the appropriate type of trade to exit the type of Position passed as the first argument. positionPosition Any type of Position. memoString A memo to include on the created trade and position. waitTimeMixed Override the default stratgey waitTime setting. Controls how long to wait before seeking a new position. A String duration to wait, i.e. '15m' or '1h' or '2d' 'tomorrow' - next market open. 'nextweek' - first market open next week, generally monday. A Date object in the future

Example


onManage: function(target, position, data){
    var bar = data.chart.lastBar,
        rsi = bar.indicator('rsi');

    // rsi overbought
    if(rsi >= data.overboughtSignal){
        return this.exit(position, 'RSI overbought: {0}'.format(rsi));
    }
}

Position API

Positions in Alta5 expose properties to help a strategy determine if the position is profitable, calculate risk metrics, track commissions, manage expiration, etc. All calculations are based on the current market ask or bid prices. In a live trade, prices will generally fill at a better price somewhere in between those prices (mark price).

Properties labeled real-time are bound to the real-time properties of the Security and are always up-to-date with the latest market value.

All Positions

The following properties are available on all positions:

cost

Number
The total cost to open the position including commissions.

enterCommission

Number
The broker commission paid to open the position.

enterDate

Date
The date the position was opened.

enterInfo

String
The memo provided when the position was opened.

enterPrice

Number
The price per share to open the position.

expired

Boolean
True if this position is expired.

gain

Number
The profit or loss as a decimal percent of the capital used to secure it, calculated using the user's Open P/L settings (markGain or marketGain).

id

String
The id of this position.

iid

String
The id of the Trader that opened this position.

isActive

Boolean
True if this position is open.

isShort

Boolean
True if this is a short position.

maintenance

Number
The amount of capital or margin held to secure this position.

marketGain

Number
The profit or loss as a decimal percent of the capital used to secure it, calculated using the marketPnl.

marketPnl

Number
The profit or loss for the position using marketValue. Includes commission fees.

markGain

Number
The profit or loss as a decimal percent of the capital used to secure it, calculated using the markPnl.

markPnl

Number
The profit or loss using markValue. Includes commission fees.

pnl

Number
The profit or loss using the user's Open P/L settings (markPnl or marketPnl). Includes commission fees.

quantity

Number
The quantity of shares, contracts or spreads for this position. The value will be negative for short positions.

security

String
A Security object for the security held by the position or the underlying security for multi-leg positions.

symbol

String
The symbol of the security held by the position.

This property will be undefined for multi-leg positions.

underlyingSymbol

String
The symbol of the underlying security.

Open Positions

The following properties are available on all open positions:

markValue

Number
The current value of the position using the mark price(s) midway between the bid/ask spread.

marketValue

Number
The current value of the position using the market asking price(s).

value

Number
The current value of the position using the user's Open P/L settings (either markValue or marketValue) or exitValue for closed positions.

Closed Positions

The following properties are available on all closed positions:

exitCommission

Number
The broker commission paid to close the position.

exitDate

Date
The date the position was closed.

exitInfo

String
The memo provided when the position was closed.

exitPrice

Number
The price per share to close the position.

exitValue

Number
The total amount to close the position.

Option Positions

The following properties are available on all Option positions:

breakeven

Number
The price of the underlying required for this position to breakeven.

days

Number
The number of days until expiration.

delta

Number
The delta greek for this option.

expiration

Date
The expiration date for this position.

expired

Boolean
True if this position is expired.

gamma

Number
The gamma greek for this option.

isCall

Boolean
True if this is a call option.

isPut

Boolean
True if this is a put option.

itm

Boolean
True if this position is in the money.

mdays

Number
The number of days until expiration, adjusted to only including trading days in the final week. If an option with 10 days until expiration expires on Saturday and the preceding Friday is a holiday, the days value would be adjusted 2 days and mdays would be 8.

strike

Number
The strike price for the option.

theta

Number
The theta greek for this option.

timeValue

Number
The portion of an option's premium that is attributable to the amount of time remaining until the expiration of the option contract.

vega

Number
The vega greek for this option.

Spread Positions

The following properties are available on all Spread positions:

breakeven

Number
The price of the underlying required for this position to breakeven.

days

Number
The number of days until expiration.

credit

Number
The total premium collected for a credit spread position.

creditPerShare

Number
The premium per share collected for a credit spread position.

delta

Number
The sum of the delta greek for all legs in the Spread.

expiration

Date
The expiration date of the short leg of the Spread.

expired

Boolean
True if this position is expired.

gamma

Number
The sum of the gamma greek for all legs in the Spread.

itm

Boolean
True if this position is in the money.
legs
Array
An array of containing the Option positions for the Spread. The first position is at index 0.

var firstLeg = position.legs[0];

longLeg

Position
The long option position for the Spread.

mdays

Number
The number of days until expiration, adjusted to only including trading days in the final week. If an option with 10 days until expiration expires on Saturday and the preceding Friday is a holiday, the days value would be adjusted 2 days and mdays would be 8.

premiumGain

Number
The percentage of the original credit received that would be profit/loss at current market prices.

shortLeg

Position
The short option position for the Spread.

spread

Spread
The original entry spread object created by the option chain worker.

strike

Number
The strike price for the option.

theta

Number
The sum of the theta greek for all legs in the Spread.

vega

Number
The sum of the vega greek for all legs in the Spread.