When working with JForex and creating indicators, you may often want to access historical price data beyond the indices provided in the calculate method. However, the calculate method is typically constrained to the range specified by the startIndex and endIndex parameters. If you want to access additional historical data, there are a few approaches you can take:
1. Using IIndicators Methods
JForex provides various methods through the IIndicators interface that allow you to fetch historical data. You can often use the IIndicators instance to retrieve additional bars or historical data points.
Here’s how you can do this:
import com.dukascopy.api.indicators.IIndicators;
import com.dukascopy.api.IHistory;
public class MyCustomIndicator implements IIndicator {
private IIndicators indicators;
private IHistory history;
public MyCustomIndicator(IIndicators indicators, IHistory history) {
this.indicators = indicators;
this.history = history;
}
@Override
public void calculate(int startIndex, int endIndex, double[] prices) {
for (int i = startIndex; i <= endIndex; i++) {
double currentValue = prices[i];
// Access data farther back
double previousValue = 0;
int backIndex = i - 10; // Example: get data 10 periods back
if (backIndex >= 0) { // Ensure it is not out of bounds
previousValue = prices[backIndex];
} else {
// Handle cases where backIndex is less than zero, e.g., use a default value
}
// Do your calculations...
double result = currentValue + previousValue; // Example calculation
setValue(i, result);
}
}
private void setValue(int index, double value) {
// Logic to set the computed value in your output
}
// Implement other required methods...
}
2. Fetching Historical Data
If you need access to bars or candles for your calculations and you require a longer historical view (in situations where the provided indices are not sufficient), you can utilize the IHistory interface. This allows you to request historical price data programmatically.
Here's how to obtain historical data beyond the current index range:
import com.dukascopy.api.IHistory;
public class MyCustomIndicator implements IIndicator {
private IHistory history;
public MyCustomIndicator(IHistory history) {
this.history = history;
}
@Override
public void calculate(int startIndex, int endIndex, double[] prices) {
// Example of fetching historical data
int additionalBarsBack = 20; // Number of bars you want to go back
int additionalBarsToRequest = additionalBarsBack + endIndex + 1; // or whatever range needed
// Ensure that you request enough historical data
if (startIndex >= additionalBarsBack) {
double[] oldPrices = history.getClosePrices("EUR/USD", Period.ONE_MIN, additionalBarsToRequest);
// Use the fetched prices for your calculations
}
// Your classic calculations...
for (int i = startIndex; i <= endIndex; i++) {
// Existing calculations here
}
}
}
Notes:
- Ensure Load: Remember that fetching more historical data can impact performance, especially if you are working with a very high number of bars. Be mindful of the volume of data you are requesting.
- Handling Out Of Bounds: Always handle the potential for out-of-bounds errors when accessing array elements. Use appropriate checks before accessing indices.
- Use Efficiently: Only pull back data that you need for your current calculation to avoid overloading your indicator with unnecessary data.
In summary, to access more price data beyond the indices provided, you should leverage the IIndicators and IHistory interfaces that JForex offers. Make sure to request and manage the historical data efficiently, keeping performance considerations in mind.