Trading Generator
Prerequisites:
The current loader's static setPush()
method must have been called in advance to ensure the correct functioning of
this generator.
Add to Chart:
Use a BG.charts.Controller object to add the generator to a chart:
const tradingGenerator = controller.addGenerator('TradingGenerator');
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 conversion
/// [...]
tradingGenerator.setPortfolio({
portfolioId: portfolioId,
session: session,
convertCurrencyIsoToSymbol: convertCurrencyIsoToSymbol,
subscribeUnderlyingQuoteEstimateByProductQuote: subscribeUnderlyingQuoteEstimateByProductQuote,
initKoCalculation: initKoCalculation,
subscribeQuotes: subscribeQuotes,
});
Types
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
}
Events:
The generator emits several different events:
#createOrder
-
Type
interface TradingGenerator {
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'
* }
*/
});
#selectOrder
-
Type
interface TradingGenerator {
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
- a Tipmarker is clicked
- an Ordermarker is clicked
- an order is selected through the Positionmarker when using cfd brokers
- an order is selected in the orderlist
- when an order is deselected (null)
* **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
*/
});
#changeOrder
-
Type
interface TradingGenerator {
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'
* }
*/
});
#cancelOrder
-
Type
interface TradingGenerator {
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'
* }
*/
});
#revertOrder
-
Type
interface TradingGenerator {
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'
* }
*/
});
#cancelStopLoss
-
Type
interface TradingGenerator {
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'
* }
*/
});
#cancelTakeProfit
-
Type
interface TradingGenerator {
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'
* }
*/
});
#differingCurrencies
-
Type
interface TradingGenerator {
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'
* ]
*/
});
#distance
-
Type
interface TradingGenerator {
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
* }
*/
});
#showKOSearch
-
Type
interface TradingGenerator {
emit(type: 'showKOSearch');
} -
Details
Emitted when the button in the settings is clicked. Has no parameters.
-
Example
trading.on('showKOSearch', () => {});
#outdatedQuoteData
-
Type
interface TradingGenerator {
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
*/
});
#instrumentChanged
-
Type
interface TradingGenerator {
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
*/
});
Public methods:
The generator has several public methods available.
#setPortfolio
-
Type
interface TradingGenerator {
tradingGenerator.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. €).
#setTemporaryOrder
-
Type
interface TradingGenerator {
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 whensetTemporaryOrder
is called again.
#getTradingFeatures
-
Type
interface TradingGenerator {
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.
#isCFD
-
Type
interface TradingGenerator {
isCFD(): boolean | null;
} -
Details
Returns whether the current broker session is a CFD type or an online broker.
#getPrecision
-
Type
interface TradingGenerator {
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.
Storing user settings
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);