The current loader's static setPush()
method must have been called in advance to ensure the correct functioning of
this generator.
Use a Controller object to add the generator to a chart:
const tradingGenerator = controller.addGenerator('Trading');
Next, using the setPortfolio
method, an object containing the following needs to be passed to the generator:
portfolioId
: the portfolio IDsession
: bg-trading.Session
convertCurrencyIsoToSymbol
: function to convert an ISO currency (EUR) to its symbol (€)subscribeUnderlyingQuoteEstimateByProductQuote
: function to subscribe to underlying quotesinitKoCalculation
: function to initialize KO conversion calculationsubscribeQuotes
: function to subscribe to KO quotes needed for conversionselectedExchangeStore
: store to enforce same exchange selection as used by the platform/// [...]
tradingGenerator.setPortfolio({
portfolioId: portfolioId,
session: session,
convertCurrencyIsoToSymbol: convertCurrencyIsoToSymbol,
subscribeUnderlyingQuoteEstimateByProductQuote: subscribeUnderlyingQuoteEstimateByProductQuote,
initKoCalculation: initKoCalculation,
subscribeQuotes: subscribeQuotes,
});
type KoCalculations = {
subscriptions: {
prodAsk: number,
prodBid: number,
underlying: string,
crossRate1?: number,
crossRate2?: number
},
estimateProduct(param: {
currentQuotes: {
prodAsk: Quote,
prodBid: Quote,
underlying: Quote
},
underlyingQuote: number
}): { prodAsk: number, prodBid: number, bidTooOld: boolean, askTooOld: boolean, rest?: Array<any> },
estimateUnderlying(param: {
currentQuotes: {
prodAsk: Quote,
prodBid: Quote,
underlying: Quote
},
prodAsk?: number,
prodBid?: number
}): {
underlyingQuote: number,
bidTooOld: boolean,
askTooOld: boolean,
rest?: Array<any>
},
}
type SubscribeKoCalculationObject = {
entry: string,
takeProfit: string,
stopLoss: string,
instrumentId: string,
underlyingSubscription?: string,
maxL?: string,
amount?: string
};
type subscribeUnderlyingQuoteEstimateByProductQuoteObject = {
instrumentId: string,
productExchangeId: string,
bid: number,
ask: null,
underlyingSubscription?: string
} | {
instrumentId: string,
bid: null,
ask: number,
underlyingSubscription?: string
}
The generator emits several different events:
Type
interface Trading {
emit(type: 'createOrder', order: Omit<bg-trading.Order, 'portfolioId', 'instrumentId', 'exchangeId', 'direction', 'size'> & {
portfolioId: string,
orderModel: bg-trading.OrderModel,
instrumentId: string,
exchangeId: string
direction: bg-trading.Direction,
size: string
});
}
Details
Currently only emitted for online brokers when the Positionmarker is clicked
Example
//example:
tradingGenerator.on('createOrder', (order) => {
/* order:
* {
* portfolioId: '0987654321',
* orderModel: 'market',
* instrumentId: '133962'
* exchangeId: '24'
* direction: 'sell',
* size: '1'
* }
*/
});
Type
interface Trading {
emit(type: 'selectOrder', order: null | Omit<bg-trading.Order, 'id', 'portfolioId', 'isEditable', 'isCancellable'> & {
id: string,
portfolioId: string,
isExecuted: boolean,
isEditable?: boolean,
isCancellable?: boolean
});
}
Details
emitted when
* **Example**
//example
tradingGenerator.on('selectOrder', (order) => {
/* order:
* {
* id: '1234567890',
* portfolioId: '0987654321',
* orderModel: 'market',
* isExecuted: true,
* isEditable: true,
* IsCancellable: false
* }
*/
});
tradingGenerator.on('selectOrder', (order) => {
/* order:
* null
*/
});
Type
interface Trading {
emit(type: 'changeOrder', order: Omit<bg-trading.Order, 'id', 'portfolioId', 'direction'> & {
id: string,
portfolioId: string,
direction: string
});
}
Details
Emitted when an Ordermarker is moved via drag and drop
Example
//example:
trading.on('changeOrder', (order) => {
/* order
* {
* id: '1234567890',
* portfolioId: '0987654321',
* orderModel: 'limit',
* limit: '45.474',
* direction: 'buy'
* }
*/
});
Type
interface Trading {
emit(type: 'cancelOrder', order: Omit<bg-trading.Order, 'id', 'portfolioId'> & {
id: string,
portfolioId: string,
exchangeId: string
});
}
Details
Emitted when the user selects Cancel order
in the context action menu
Example
//example:
trading.on('cancelOrder', (order) => {
/* order
* {
* id: '1234567890',
* portfolioId: '0987654321',
* orderModel: 'market'
* }
*/
});
Type
interface Trading {
emit(type: 'revertOrder', order: Omit<bg-trading.Order, 'id', 'portfolioId'> & {
id: string,
portfolioId: string,
exchangeId: string
});
}
Details
Emitted when the user selects Revert order
in the context action menu. Only available after an order has been edited.
Example
//example:
trading.on('revertOrder', (order) => {
/* order
* {
* id: '1234567890',
* portfolioId: '0987654321',
* orderModel: 'market'
* }
*/
});
Type
interface Trading {
emit(type: 'cancelStopLoss', order: Omit<bg-trading.Order, 'id', 'portfolioId'> & {
id: string,
portfolioId: string,
exchangeId: string
});
}
Details
Emitted when the user selects Delete StopLoss
in the right-click context-menu. Only available if the order has a StopLoss value set.
Example
//example:
trading.on('cancelStopLoss', (order) => {
/* order
* {
* id: '1234567890',
* portfolioId: '0987654321',
* orderModel: 'market'
* }
*/
});
Type
interface Trading {
emit(type: 'cancelTakeProfit', order: Omit<bg-trading.Order, 'id', 'portfolioId'> & {
id: string,
portfolioId: string,
exchangeId: string
});
}
Details
Emitted when the user selects Delete TakeProft
in the right-click context menu. Only available if the order has a TakeProfit value set.
Example
//example:
trading.on('cancelTakeProfit', (order) => {
/* order
* {
* id: '1234567890',
* portfolioId: '0987654321',
* orderModel: 'market'
* }
*/
});
Type
interface Trading {
emit(type: 'differingCurrencies', exchanges: Array<string>);
}
Details
Emitted when the user has orders/positions for the current instrument in their portfolio with a currency that differs from the current chart.
Example
//example:
trading.on('differingCurrencies', (exchanges) => {
/* exchanges
* [
* '125',
* '4',
* '2'
* ]
*/
});
Type
interface Trading {
emit(type: 'distance', quoteSource: {
high: number;
low: number;
});
}
Details
Emitted when the bounds of the chart change and a temporary tool is available. Supplies an object with 'high'|'low' keys to get a 15% chart-height distance from the quotesource
Example
trading.on('distance', (value) => {
/* values
* {
* low: 17.1891
* high: 20.1981
* }
*/
});
Type
interface Trading {
emit(type: 'showKOSearch');
}
Details
Emitted when the button in the settings is clicked. Has no parameters.
Example
trading.on('showKOSearch', () => {});
Type
interface Trading {
emit(type: 'outdatedQuoteData', outdated: boolean);
}
Details
Emitted when the quote data for a knockout certificate is outdated and the user needs to be warned. True if the warning needs to be displayed, false if it can be removed.
Example
trading.on('outdatedQuoteData', (outdated) => {
/* outdated
* true
*/
});
Type
interface Trading {
emit(type: 'instrumentChanged', newInstrumentId: number);
}
Details
Emitted when the underlying instrument for the generator changes. Not fired for exchange/quotetype changes.
Example
trading.on('instrumentChanged', (newInstrumentId) => {
/* newInstrumentId:
* 133962
*/
});
The generator has several public methods available.
Type
interface Trading {
setPortfolio({
portfolioId: string,
session: bg-trading.Session,
convertCurrencyIsoToSymbol: (currencyString: string) => string,
subscribeUnderlyingQuoteEstimateByProductQuote: (params: subscribeUnderlyingQuoteEstimateByProductQuoteObject, cb: (err: Error | null, data: {
underlyingEstimate: number,
bidTooOld: boolean,
askTooOld: boolean
}) => void)
=> { unbind: Function }
initKoCalculation: (param: {
prod: string,
underlying: string,
},
cb: (err: Error | null, data: KoCalculations) => void
) => { unbind: () => void },
subscribeQuotes(subscriptions: KoCalculations['subscriptions'],
cb: (quotes: { prodAsk: Quote; prodBid: Quote; underlying: Quote; crossRate1?: number | undefined; crossRate2?: number | undefined; }) => void): { unbind: Function },
}): void;
}
Details
This needs to be called after adding the generator to the chart. As long as the portfolioId or the session is not set, the generator will not be able to display anything. The third key in the object is a helper function to convert currency strings (e.g. EUR) to its corresponding ISO symbol (e.g. €).
Type
interface Trading {
setTemporaryOrder(order: bg-trading.Order | null, hidePrevious?: boolean): void;
}
Details
Temporary orders, i.e. orders that haven't yet been submitted to the broker and are still drafts, need to be passed to the generator directly. If the order doesn't have an ID, temp_order_hadc
will be used.
Orders that are being edited will also need to be set as temporary. This can be used in combination with the selectOrder
event.
If null
is used, the order is either finalized (and therefore available through a bg-trading subscription) or canceled by the user. It should no longer be displayed in the chart. The second optional parameter hides the previously selected Order even if the temporary tool is reset. This is useful for the delay between sending an order to a broker and waiting for a confirmation/error. The generator will set itself into a loading state, which will be cleared either when new data arrives, or when setTemporaryOrder
is called again.
Type
interface Trading {
getTradingFeatures(): {
canStop: boolean,
canLimit: boolean
};
}
Details
Calling this informs the application which features are supported on the current instrumentId/exchangeId combination. This can be useful to e.g. avoid showing trading-related items in a context menu when trading is not possible at all.
Type
interface Trading {
isCFD(): boolean | null;
}
Details
Returns whether the current broker session is a CFD type or an online broker.
Type
interface Trading {
getPrecision(): number;
}
Details
Sets the KO product price for the specified KO subscription (must be identical to one from 'requiredKOSubscriptions'-event), which will then be displayed as a label in the open orders of that product.
To retrieve all customized user settings (settings the user has changed from its default values) the serialize
method can be used.
const settings = trading.serialize();
/*
* {
* foldAll: true,
* showDistanceAbs: true
* }
*/
Passing this object to the generator through the deserialize method will re-initialize all saved settings.
trading.deserialize(settings);