Get Historical Prices for an Instrument

Brief

The article teaches how to receive historical prices for an instrument.

Details

How to receive historical prices for an instrument

In this article you will learn how to use the API to receive historical prices for an instrument.
Before you can get historical data, you need to:

Step 1: Create a callback class for receiving price history.

The first step is to create a callback class that will handle the response from the API when requesting price history.
You can use the PriceHistoryManagerCallback interface to define the methods of this class.

Below you can see some example code for creating a callback class for receiving price history:

PriceHistoryManagerCallback Hide

typescriptjavascript
class PriceHistoryManagerCallback implements FXConnectLite.IPriceHistoryManagerCallback { resolve: any; app: Application; onSuccess(responseObject: FXConnectLite.IPriceHistoryResponse) { PriceHistoryPrinter.printAll(responseObject, this.app); this.resolve(); } onError(errorObject: FXConnectLite.IFXConnectLiteError) { Printer.print(errorObject.getMessage()); this.resolve(); } constructor(resolve: any, app: Application) { this.resolve = resolve; this.app = app; } }

For more information about IPriceHistoryManagerCallback, see the API reference.

Step 2: Get the price history of an instrument.

The next step is to get the price history of a specific instrument. You can use the getPriceHistory method of the API to request the price history of an instrument by its name.
This method takes a PriceHistoryManagerCallback object as an argument and calls its onSuccess or onError methods depending on the result.

Below, you can see some example code for getting the price history of an instrument:

Get the price history Hide

typescriptjavascript
let manager = session.getPriceHistoryManager(); this.timeframe = FXConnectLite.Timeframe.create(FXConnectLite.TimeframeUnit.Minute, 30); manager.getPrices(this.instrument, this.timeframe, this.from, this.to, -1, new PriceHistoryManagerCallback(resolve, this));

For more information about getPrices method, see the API reference.

Step 3: Check if the price history contains an ask price.

The final step is to check if the price history of an instrument contains an ask price.
The ask price is the price at which you can buy an instrument. You need to know the ask price to correctly place limit buy orders.
However, not all instruments have an ask price in their history.

To check if the price history of an instrument contains an ask price, you can use the hasAskPrice method of the API.
This method takes the name of the instrument as an argument and returns a boolean value.
The method will return true if the instrument's history has an ask price, and false if it does not.

Below, you can see some example code to check if the price history contains an ask price:

hasAskPrice Hide

typescriptjavascript
manager = session.getPriceHistoryManager(); let hasAskPrice = manager.hasAskPrice("EUR/USD");

See also IPriceHistoryManager

Conclusion:

You have learned how to use the API to receive historical prices for an instrument. Before you can get historical data, you need to create a callback class for receiving price history, get the price history of an instrument, and check if the price history contains an ask price. You have also learned how to create a callback class using the PriceHistoryManagerCallback interface and how to get the price history of an instrument using the getPriceHistory method of the API. You can now use this knowledge to retrieve historical prices for instruments on the platform.

Download the sample TypeScript, JavaScript.

back