API Reference
Namespaces
Classes
Events
Global
Externals

Class: Account

CIQ. Account


<abstract> new Account()

Account object used by CIQ.TFC.

This will contain all of the data related to this account, including initial configuration and account configuration.

Derive an account object from this basic template and ensure that each of the functions works correctly and that the data is stored in the specified format.

For proper functionality, this object should only be pre-set with the desired CIQ.Account.Config and currency. Let let CIQ.Account.Balances, CIQ.Account.OpenOrders, CIQ.Account.Positions and CIQ.Account.Trades be loaded using their corresponding fetch methods and not set manually in the Account object.

For example:

CIQ.Account.Demo = function() {
    this.currency = "EUR";
    this.config = {
        oto: false,
        oco: false,
        closeAll: true,
        tradeActions: true,
        vsp: "M"
    }
}

See the Trade From Chart Introduction for implementation details. See plugins/tfc/tfc-demo.js for a full demo account sample implementation.

Example

Sample data format for the Trade From Chart CIQ.Account object.

// After balances, openOrders, positions and trades are loaded using their corresponding fetch methods.
this.currency = "USD";
this.balances = {
    liquidity: 100000,
    unsettledCash: 0,
    cash: 100000,
    profitLoss: 0,
    buyingPower: 200000
};
this.positions = {
    "IBM":{quantity:1000,basis:126.13, price:129.13, prevClose:123.13, currency:"USD"},
    "GE":{quantity:100,basis:26.11, price:24.11, prevClose:26.11, currency:"USD"},
    "SPY":{quantity:-1000,basis:187.11, price:187.11, prevClose:190.11, currency:"USD"},
    "MSFT":{quantity:-100,basis:230, price:58, prevClose:240, currency:"USD"}
};
this.trades = {
  "IBM":
     [
       {id:"IBM001", time:1366206180000, quantity:300, basis:124.13, price:129.13, currency:"USD", protect:{limit:165, stop:135}},
       {id:"IBM002", time:1366910520000, quantity:600, basis:127.13, price:129.13, currency:"USD"},
       {id:"IBM003", time:1407181680000, quantity:100, basis:126.13, price:129.13, currency:"USD"}
     ],
  "GE":
     [
       {id:"GE001", time:1433779740000, quantity:100, basis:26.11, price:24.11, currency:"USD", protect:{limit:30, stop:25}}
     ],
  "SPY":
     [
       {id:"SPY001", time:1419262080000, quantity:-700, basis:190.45, price:187.11, currency:"USD"},
       {id:"SPY002", time:1419262380000, quantity:-300, basis:179.32, price:187.11, currency:"USD"}
     ],
  "MSFT":
     [
       {id:"MSFT001", time:1420740540000, quantity:-100, basis:230, price:58, currency:"USD"}
     ]
};
this.openOrders = {
  "IBM":
     [
       {id:"1", action:"sell", quantity:500, limit:197, tif:"GTC", currency:"USD"},
       {id:"2", action:"sell", quantity:500, limit:196, tif:"GTC", currency:"USD"},
       {id:"9", tradeid:"IBM001", action:"sell", quantity:300, limit:165, tif:"GTC", currency:"USD", oco:"10"},
       {id:"10", tradeid:"IBM001", action:"sell", quantity:300, stop:135, tif:"GTC", currency:"USD", oco:"9"}
     ],
  "TSLA":
     [
       {id:"3", action:"buy", quantity:10, limit:170, tif:"DAY", currency:"USD"}
     ],
  "GE":
     [
       {id:"4", tradeid:"GE001", action:"sell", quantity:100, limit:30, tif:"GTC", currency:"USD", oco:"5"},
       {id:"5", tradeid:"GE001", action:"sell", quantity:100, stop:25, tif:"GTC", currency:"USD", oco:"4"}
     ],
  "MSFT":
     [
       {id:"6", action:"buy", quantity:100, limit:61, tif:"DAY", currency:"USD", oto:
         [
           {id:"7", action:"sell", quantity:100, limit:61, tif:"GTC", currency:"USD", oco:"8"},
           {id:"8", action:"sell", quantity:100, stop:61, tif:"GTC", currency:"USD", oco:"7"}
         ]
       },
       {id:"9", action:"buy", quantity:100, limit:61, tif:"DAY", currency:"USD", oto:
         [
           // If only one leg, set oco to true.
           {id:"10", action:"sell", quantity:100, limit:61, tif:"GTC", currency:"USD", oco:true}
         ]
       },
     ]
};
this.config = {
    oto: true,
    oco: true,
    closeAll: true,  // Set to true to enable close all capability.
    disableModifyOrderQuantity: false,
    gtcOnly: false,
    tradeActions: true,  // Set to true to enable to protection/actions tab in the enhanced view.
    reducePosition: true,
    hedging: false,
    vsp: ""
};

Methods


cancelOrder(tfc, order, cb)

Abstract for Canceling an order

Parameters:
Name Type Description
tfc CIQ.TFC

The TFC object

order object

The order to cancel, in native TFC format. The abstract interface is responsible for converting this order into the format required by the broker interface.

cb function

Callback function fc(err)


closeAllPositions(tfc, cb)

Abstract for Closing all positions

Parameters:
Name Type Description
tfc CIQ.TFC

The TFC object

cb function

Callback function fc(err)


closePosition(tfc, position, cb)

Abstract for Closing a position

Parameters:
Name Type Description
tfc CIQ.TFC

The TFC object

position object

position to close

cb function

Callback function fc(err)


closeTrade(tfc, trade, cb)

Abstract for Closing a trade

Parameters:
Name Type Description
tfc CIQ.TFC

The TFC object

trade object

trade to close

cb function

Callback function fc(err)


confirmOrder(tfc, order, cb)

Confirm an order before placing it. This is optional and only for firms that support a server-side order confirmation (Are you sure) process. If not supported, then simply call the callback.

Parameters:
Name Type Description
tfc CIQ.TFC

The TFC object

order object

The order to confirm

cb Confirmation

The callback when confirmed with Confirmation object


disconnect()

Disconnect an account when done using it. This should be called when detaching an account. If a Poller exists and is polling for quote updates, should stop the polling. See tfc-demo.js for example of a Poller.

Since:
  • 7.0.0


fetchBalances(cb)

Function used for fetching balances. Your implementation should set this.balances inside this function. The server fetch callback function should return data in CIQ.Account.Balances format, or you must format it once received.

Parameters:
Name Type Description
cb funcion

Required Callback function to track data fetch progress. Do not remove.


fetchOpenOrders(cb)

Function used for fetching Open Orders. Your implementation should set this.openOrders inside this function. The server fetch callback function should return data in CIQ.Account.OpenOrders format, or you must format it once received.

Parameters:
Name Type Description
cb funcion

Required Callback function to track data fetch progress. Do not remove.


fetchPositions(cb)

Function used for fetching Positions. Your implementation should set this.positions inside this function. The server fetch callback function should return data in CIQ.Account.Positions format, or you must format it once received

Parameters:
Name Type Description
cb funcion

Required Callback function to track data fetch progress. Do not remove.


fetchTrades(cb)

Function used for fetching Trades. Your implementation should set this.trades inside this function. The server fetch callback function should return data in CIQ.Account.Trades format, or you must format it once received

Parameters:
Name Type Description
cb funcion

Required Callback function to track data fetch progress. Do not remove.


getPoller()

The Poller object factory function invoked by the CIQ.Account constructor. The return value of this function is assigned to the Poller property of CIQ.Account instances. CIQ.Account provides the prototype object of derived custom account objects such as CIQ.Account.Demo. The Poller property can then be accessed on the prototype object, for example:

CIQ.Account.Demo = function () {...};
CIQ.inheritsFrom(CIQ.Account.Demo, CIQ.Account);
CIQ.Account.Demo.prototype.Poller.intervals = {...};

The poller updates the account at intervals you specify in the Poller.intervals object. The object should contain two properties:

  • quotes — The number of milliseconds between updates of the bid and ask data and all pricing in the account holdings
  • account — The number of milliseconds between updates of the holdings themselves

Set the polling intervals as follows:

CIQ.Account.Demo.prototype.Poller.intervals = {
    "quotes": {timer: null, poll: 5000},  // Five-second poll of the quotes.
    "account": {timer: null, poll: 20000}  // 20-second poll of the account data.
};

See the tfc-demo.js file in the plugins/tfc folder for an example account implementation.

Since:
  • 8.1.0 Replaced the CIQ.Account.prototype.Poller property definition.

Returns:

A poller of the form:

{
    tfcs: [], // TFC instances associated with this account.
    intervals: {}, // Polling intervals.
    startPolling: function(tfc) {...}, // Adds the TFC parameter to this account and starts the quotes and account polling.
    stopPolling: function() {...}, // Stops the quotes and account polling.
    update: function(tfc, updateType) {...}, // Updates quote or account information for this account's TFC instances.
    updateQuote: function() {...}, // Updates quotes on this account's TFC instances.
    updateAccount: function() {...}, // Updates positions and trades on this account's TFC instances.
    removeInstance: function(tfc) {...} // Removes the TFC parameter instance from this account.
}
Type
object

isForex(symbol)

Function used for determining FOREX. Your implementation should check symbol properties

Parameters:
Name Type Description
symbol string

Symbol to test

Since:
  • 2015-11-1

Returns:

true if Forex

Type
boolean

placeOrder(tfc, order, cb)

Abstract for Placing an order

Parameters:
Name Type Description
tfc CIQ.TFC

The TFC object

order object

The order, in native TFC format. The abstract interface is responsible for converting this order into the format required by the broker interface.

Properties
Name Type Argument Description
type string

"order" (as opposed to "replace")

symbol string

The security symbol

action string

"buy","sell", “short", “cover"

quantity number

The quantity to trade

limit number <optional>

The limit price, optional (if no limit or stop then the order is a market order)

stop number <optional>

The stop price, optional.

marketIfTouched number <optional>

The market if touched price, optional.

tif string

"GTC" or "DAY"

oto array <optional>

Optional OTO array, each array element contains an order in this same format

cb function

Callback function fc(err)


replaceOrder(tfc, order, cb)

Abstract for Modifying an order (cancel/replace)

Parameters:
Name Type Description
tfc CIQ.TFC

The TFC object

order object

The modification order, in native TFC format. The abstract interface is responsible for converting this order into the format required by the broker interface.

Properties
Name Type Argument Description
type string

"replace" (as opposed to "order")

symbol string

The security symbol

id string

The ID of the order being modified

action string

"buy", "sell", “short", “cover"

limit object

Limit price (if one exists)

Properties
Name Type Description
old number

Old limit price if there was one

new number

New limit price if there is one

stop object

Stop price (if one exists)

Properties
Name Type Description
old number

Old stop price if there was one

new number

New stop price if there is one

marketIfTouched object

Market If Touched price (if one exists)

Properties
Name Type Description
old number

Old market if touched price if there was one

new number

New market if touched price if there is one

quantity object

Quantity tuple

Properties
Name Type Description
old number

Old quantity

new number

New quantity

tif object

TIF tuple

Properties
Name Type Description
old string

Old TIF

new string

New TIF

oto object <optional>

Optional tuple containing old and new oto

Properties
Name Type Argument Description
old object <optional>

Old OTO

new object <optional>

New OTO

cb function

Callback function fc(err)


setProtection(tfc, array, cb)

Abstract for protecting a trade

Parameters:
Name Type Description
tfc CIQ.TFC

The TFC object

array array

of orders constituting protection (the bracket)

cb function

Callback function fc(err)


tradability(symbol, cb)

Determines the tradability of the requested symbol. This includes whether it's tradable at all and whether it can be shorted. Override this with your own firm's logic and query.

Parameters:
Name Type Description
symbol string

Symbol to check

cb Tradability

Callback with tradability status


tradesLikeForex(symbol)

Function used for determining labeling throughout. Your implementation should check symbol properties

Parameters:
Name Type Description
symbol string

Symbol to test

Since:
  • 2015-11-1

Returns:

true if using Forex labeling (pips, amount, units) vs equity labeling (points, dollars, shares)

Type
boolean

Type Definitions


Balances(balances)

Balances object required by Account.Balances

Parameters:
Name Type Description
balances object

A balances object

Properties
Name Type Description
liquidity number

The liquidation value for the account

cash number

Trading cash in the account

profitLoss number

Gain or loss in the account

unsettledCash number

Unsettled cash for the account

buyingPower number

Buying power for the account. Null if not a margin account.

Example
this.balances={
			liquidity: 100000,
			unsettledCash: 0,
			cash: 100000,
			profitLoss: 0,
			buyingPower: 200000
		};

Config()

Config object required by Account.Config

Parameters:
Name Type Argument Description
config.gtcOnly boolean <optional>

Set to true if no DAY TIF supported

config.oco boolean <optional>

Set to true if oco orders are supported

config.oto boolean <optional>

Set to true if oto orders are supported

config.closeAll boolean <optional>

Set to true to allow closing all positions

config.disableModifyOrderQuantity boolean <optional>

Set to true if order modify does not support quantity change

config.tradeActions boolean <optional>

Set to true to allow actions on the trades (lots) such as protect and close

config.reducePositions boolean <optional>

Set to false to disallow actions which reduce positions (such as the sell/cover order form)

config.hedging boolean <optional>

Set to true to allow hedging actions (stepping into a long and short position in the same security)

config.vsp string <optional>

PROPOSED Set to a combination of M/L/S to allow vsp orders (reducing trades) of market, limit and stop order type, respectively

config.showOpenOrdersWhenTFCClosed boolean <optional>

Set to true if you want open orders markers to display when the TFC side bar is closed


Confirmation( [commission] [, fees] [, total] [, errors] [, warnings])

Parameters:
Name Type Argument Description
commission number <optional>

The commission amount if available

fees number <optional>

The fee amount if available

total number <optional>

Total amount of trade

errors array <optional>

Any errors

warnings array <optional>

Any warnings


OpenOrders(openOrders)

OpenOrders object required by Account.OpenOrders

Parameters:
Name Type Description
openOrders object

An open orders object. Contains a field for each security symbol. Each symbol contains an array of open orders.
It is assumed that each open order is referenced by a unique id.
An optional oco field should reference the id of a linked order.
An optional oto field contains an array of orders that will be triggered on execution.
An optional tradeid field contains the id of a protected trade.

Example
this.openOrders={
		"IBM":
			[
				{id:"1", action:"sell", quantity:500, limit:197, tif:"GTC", currency:"USD"},
				{id:"2", action:"sell", quantity:500, limit:196, tif:"GTC", currency:"USD"},
				{id:"9", tradeid:"IBM001", action:"sell", quantity:300, limit:165, tif:"GTC", currency:"USD", oco:"10"},
				{id:"10", tradeid:"IBM001", action:"sell", quantity:300, stop:135, tif:"GTC", currency:"USD", oco:"9"}
			],
		"TSLA":
			[
				{id:"3", action:"buy", quantity:10, limit:170, tif:"DAY", currency:"USD"}
			],
		"GE":
			[
				{id:"4", tradeid:"GE001", action:"sell", quantity:100, limit:30, tif:"GTC", currency:"USD", oco:"5"},
				{id:"5", tradeid:"GE001", action:"sell", quantity:100, stop:25, tif:"GTC", currency:"USD", oco:"4"}
			],
		"MSFT":
			[
		        {id:"6", action:"buy", quantity:100, limit:112, tif:"DAY", currency:"USD", oto:
		        	[
						{id:"7", action:"sell", quantity:100, limit:130, tif:"GTC", currency:"USD", oco:"8"},
						{id:"8", action:"sell", quantity:100, stop:110, tif:"GTC", currency:"USD", oco:"7"}
					]
		        },
				{id:"9", action:"buy", quantity:100, limit:112, tif:"DAY", currency:"USD", oto:
					[
					 {id:"10", action:"sell", quantity:100, limit:130, tif:"GTC", currency:"USD", oco:true}		// if only one leg, set oco to true.
					]
		        },
			]
	};

Positions(positions)

Positions object required by Account.Positions

Parameters:
Name Type Description
positions object

A positions object. Contains a field for each security symbol.

Example
this.positions={
		"IBM":{quantity:1000,basis:126.13, price:129.13, prevClose:123.13, currency:"USD"}, // "basis" is the (current, cumulative) cost-basis for the position
		"GE":{quantity:100,basis:26.11, price:24.11, prevClose:26.11, currency:"USD"},
		"SPY":{quantity:-1000,basis:187.11, price:187.11, prevClose:190.11, currency:"USD"}, // Use negative values for short positions
		"MSFT":{quantity:-100,basis:230, price:186, prevClose:240, currency:"USD"}
	};

Tradability(tradable, shortable, marketable)

Parameters:
Name Type Description
tradable boolean

True if the symbol can be traded

shortable boolean

True if the symbol can be shorted

marketable boolean

True if the symbol can be traded as a market order


Trades(trades)

Trades object required by Account.Trades

Parameters:
Name Type Description
trades object

A trades object. Contains a field for each security symbol. Each symbol contains an array of trades.
It is assumed that each trade is referenced by a unique id.
An optional protect field should outline the protection limit and stop prices, if any.

Example
this.trades={
		"IBM":
			[
				{id:"IBM001", time:1366206180000, quantity:300, basis:124.13, price:129.13, currency:"USD", protect:{limit:165, stop:135}},
				{id:"IBM002", time:1366910520000, quantity:600, basis:127.13, price:129.13, currency:"USD"},
				{id:"IBM003", time:1407181680000, quantity:100, basis:126.13, price:129.13, currency:"USD"}
			],
		"GE":
			[
				{id:"GE001", time:1433779740000, quantity:100, basis:26.11, price:24.11, currency:"USD", protect:{limit:30, stop:25}}
			],
		"SPY":
			[
				{id:"SPY001", time:1419262080000, quantity:-700, basis:190.45, price:187.11, currency:"USD"},
				{id:"SPY002", time:1419262380000, quantity:-300, basis:179.32, price:187.11, currency:"USD"}],
		"MSFT":
			[
				{id:"MSFT001", time:1420740540000, quantity:-100, basis:230, price:58, currency:"USD"}
			]
};