The
It stores, updates, and indexes candles
It automatically calculates derived properties of each candle such as the high-low range and the median price
It detects changes such as the start of a new candle, and fires events which you can hook into as a simple way of detecting types of change in the store
It can automatically update a list of technical analysis calculations
For example, an automated trading algorithm might look broadly as follows:
// The "hello world" of algo trading: constantly trade the direction of a fast
// moving average versus a slow moving average. When a new bar starts,
// it checks the averages at the end of the bar which has just closed and
// modifies its position if necessary. To make it trade intra-bar, instead of
// when a bar closes, change OnNewCandle to OnUpdate, and read the current
// values of the moving averages using GetValue(0) rather than GetValue(1)
var instrumentId = Framework.Account.defaultInstrumentId;
var timeframe = 3600; // H1
var fastPeriod = 20;
var slowPeriod = 50;
var myCandleStore = new FXB.CandleStore({
// Automatically calculate two moving averages
ta: [
new FXB.ta.EMA({period: fastPeriod}),
new FXB.ta.EMA({period: slowPeriod})
],
// Called whenever a new candle starts
OnNewCandle: function(store) {
// Get the two moving averages
var fast = store.ta[0].GetValue(1);
var slow = store.ta[1].GetValue(1);
// Should we be long?
var shouldBeLong = (fast >= slow), shouldTrade = true;
// Is there an existing open trade?
if (Framework.Orders.length) {
// Get the open trade
var order = Framework.Orders.values()[0];
// Do we need to trade? Are we currently in the opposite direction
// to what we want?
if (FXB.OrderTypes.IsLong(order.orderType)) {
shouldTrade = !shouldBeLong;
} else {
shouldTrade = shouldBeLong;
}
}
// Can either be (a) first candle, on which we place our first trade,
// or (b) a need to switch direction
if (!shouldTrade) {
// Already in the correct direction. No action.
} else {
// Need to trade. Issue an instruction to close the existing trade (if any)
// and place a new one in the require direction
if (shouldBeLong) {
Framework.SendOrder([
{tradingAction: FXB.OrderTypes.FLATTEN},
{instrumentId: instrumentId, tradingAction: FXB.OrderTypes.BUY, volume: 1000}
]);
} else {
Framework.SendOrder([
{tradingAction: FXB.OrderTypes.FLATTEN},
{instrumentId: instrumentId, tradingAction: FXB.OrderTypes.SELL, volume: 1000}
]);
}
}
}
});
// Use Framework.RequestCandles to load (streaming) data into the store, and set things moving
Framework.RequestCandles({instrumentId: instrumentId, timeframe: timeframe}, myCandleStore);