<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.
currencyis a simplestring. See CIQ.convertCurrencyCode and CIQ.Account#tradesLikeForex for more details on labeling}
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 tfcCIQ.TFC The TFC object
orderobject 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.
cbfunction Callback function fc(err)
-
closeAllPositions(tfc, cb)
-
Abstract for Closing all positions
Parameters:
Name Type Description tfcCIQ.TFC The TFC object
cbfunction Callback function fc(err)
-
closePosition(tfc, position, cb)
-
Abstract for Closing a position
Parameters:
Name Type Description tfcCIQ.TFC The TFC object
positionobject position to close
cbfunction Callback function fc(err)
-
closeTrade(tfc, trade, cb)
-
Abstract for Closing a trade
Parameters:
Name Type Description tfcCIQ.TFC The TFC object
tradeobject trade to close
cbfunction 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 tfcCIQ.TFC The TFC object
orderobject The order to confirm
cbConfirmation 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.balancesinside 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 cbfuncion Required Callback function to track data fetch progress. Do not remove.
-
fetchOpenOrders(cb)
-
Function used for fetching Open Orders. Your implementation should set
this.openOrdersinside 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 cbfuncion Required Callback function to track data fetch progress. Do not remove.
-
fetchPositions(cb)
-
Function used for fetching Positions. Your implementation should
set this.positionsinside this function. The server fetch callback function should return data in CIQ.Account.Positions format, or you must format it once receivedParameters:
Name Type Description cbfuncion Required Callback function to track data fetch progress. Do not remove.
-
fetchTrades(cb)
-
Function used for fetching Trades. Your implementation should
set this.tradesinside this function. The server fetch callback function should return data in CIQ.Account.Trades format, or you must format it once receivedParameters:
Name Type Description cbfuncion 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
Pollerproperty of CIQ.Account instances. CIQ.Account provides the prototype object of derived custom account objects such asCIQ.Account.Demo. ThePollerproperty 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.intervalsobject. 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 holdingsaccount— 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.Pollerproperty 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 symbolstring 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 tfcCIQ.TFC The TFC object
orderobject 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 typestring "order" (as opposed to "replace")
symbolstring The security symbol
actionstring "buy","sell", “short", “cover"
quantitynumber The quantity to trade
limitnumber <optional>
The limit price, optional (if no limit or stop then the order is a market order)
stopnumber <optional>
The stop price, optional.
marketIfTouchednumber <optional>
The market if touched price, optional.
tifstring "GTC" or "DAY"
otoarray <optional>
Optional OTO array, each array element contains an order in this same format
cbfunction Callback function fc(err)
-
replaceOrder(tfc, order, cb)
-
Abstract for Modifying an order (cancel/replace)
Parameters:
Name Type Description tfcCIQ.TFC The TFC object
orderobject 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 typestring "replace" (as opposed to "order")
symbolstring The security symbol
idstring The ID of the order being modified
actionstring "buy", "sell", “short", “cover"
limitobject Limit price (if one exists)
Properties
Name Type Description oldnumber Old limit price if there was one
newnumber New limit price if there is one
stopobject Stop price (if one exists)
Properties
Name Type Description oldnumber Old stop price if there was one
newnumber New stop price if there is one
marketIfTouchedobject Market If Touched price (if one exists)
Properties
Name Type Description oldnumber Old market if touched price if there was one
newnumber New market if touched price if there is one
quantityobject Quantity tuple
Properties
Name Type Description oldnumber Old quantity
newnumber New quantity
tifobject TIF tuple
Properties
Name Type Description oldstring Old TIF
newstring New TIF
otoobject <optional>
Optional tuple containing old and new oto
Properties
Name Type Argument Description oldobject <optional>
Old OTO
newobject <optional>
New OTO
cbfunction Callback function fc(err)
-
setProtection(tfc, array, cb)
-
Abstract for protecting a trade
Parameters:
Name Type Description tfcCIQ.TFC The TFC object
arrayarray of orders constituting protection (the bracket)
cbfunction 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 symbolstring Symbol to check
cbTradability Callback with tradability status
-
tradesLikeForex(symbol)
-
Function used for determining labeling throughout. Your implementation should check symbol properties
Parameters:
Name Type Description symbolstring 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 balancesobject A balances object
Properties
Name Type Description liquiditynumber The liquidation value for the account
cashnumber Trading cash in the account
profitLossnumber Gain or loss in the account
unsettledCashnumber Unsettled cash for the account
buyingPowernumber 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.gtcOnlyboolean <optional>
Set to true if no DAY TIF supported
config.ocoboolean <optional>
Set to true if oco orders are supported
config.otoboolean <optional>
Set to true if oto orders are supported
config.closeAllboolean <optional>
Set to true to allow closing all positions
config.disableModifyOrderQuantityboolean <optional>
Set to true if order modify does not support quantity change
config.tradeActionsboolean <optional>
Set to true to allow actions on the trades (lots) such as protect and close
config.reducePositionsboolean <optional>
Set to false to disallow actions which reduce positions (such as the sell/cover order form)
config.hedgingboolean <optional>
Set to true to allow hedging actions (stepping into a long and short position in the same security)
config.vspstring <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.showOpenOrdersWhenTFCClosedboolean <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 commissionnumber <optional>
The commission amount if available
feesnumber <optional>
The fee amount if available
totalnumber <optional>
Total amount of trade
errorsarray <optional>
Any errors
warningsarray <optional>
Any warnings
-
OpenOrders(openOrders)
-
OpenOrders object required by Account.OpenOrders
Parameters:
Name Type Description openOrdersobject 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 uniqueid.
An optionalocofield should reference the id of a linked order.
An optionalotofield contains an array of orders that will be triggered on execution.
An optionaltradeidfield 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 positionsobject 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 tradableboolean True if the symbol can be traded
shortableboolean True if the symbol can be shorted
marketableboolean True if the symbol can be traded as a market order
-
Trades(trades)
-
Trades object required by Account.Trades
Parameters:
Name Type Description tradesobject 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 uniqueid.
An optionalprotectfield 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" }, ], };
