Sentiment Index
The IDataService interface method getFXSentimentIndex provides Sentiment index data.
Sentiment Index bars over interval
Consider a strategy which retrieves a List of IFXSentimentIndexBar for the given instrument and its both currencies by using the getFXSentimentIndex method. Note that the to time is inclusive, so in order to retrieve a single bar, pass parameters such that to = from.
@Override
public void onStart(IContext context) throws JFException {
console = context.getConsole();
history = context.getHistory();
dataService = context.getDataService();
long lastTickTime = history.getTimeOfLastTick(instr);
long from = history.getTimeForNBarsBack(period, lastTickTime, barCount);
long to = history.getTimeForNBarsBack(period, lastTickTime, 1);
printIndices(dataService.getFXSentimentIndex(instr, period, from, to), instr);
printIndices(dataService.getFXSentimentIndex(
instr.getPrimaryJFCurrency(), period, from, to), instr.getPrimaryJFCurrency());
printIndices(dataService.getFXSentimentIndex(
instr.getSecondaryJFCurrency(), period, from, to), instr.getSecondaryJFCurrency());
}
private void printIndices(List<IFXSentimentIndexBar> sentimentBars, Object key){
for (IFXSentimentIndexBar sentimentBar : sentimentBars) {
console.getOut().println(
key.toString() + " - " + sentimentBar + ", close price: " + sentimentBar.getClose());
}
}
Call Sentiment Index as indicator
Consider doing the same value retrieval by using the Sentiment Index indicator:
@Override
public void onStart(IContext context) throws JFException {
console = context.getConsole();
history = context.getHistory();
indicators = context.getIndicators();
long lastTickTime = history.getTimeOfLastTick(instrument);
long from = history.getTimeForNBarsBack(period, lastTickTime, barCount);
long to = history.getTimeForNBarsBack(period, lastTickTime, 1);
Object[] values2 = indicators.calculateIndicator(instrument, period,
new OfferSide[] { OfferSide.ASK }, "SentimentBar",
new AppliedPrice[] { IIndicators.AppliedPrice.CLOSE },
new Object[] {
SentimentIndexBarIndicator.SentimentMode.CHART_PERIOD.ordinal(),
SentimentIndexBarIndicator.DataSource.INSTRUMENT.ordinal(),
SentimentIndexBarIndicator.MAX_BARS_DEFAULT
}, from, to);
IFXSentimentIndexBar[] sentimentBars =
Arrays.copyOf((Object[]) values2[0], ((Object[]) values2[0]).length, IFXSentimentIndexBar[].class);
for (IFXSentimentIndexBar sentimentBar : sentimentBars) {
console.getOut().println(sentimentBar + ", close price: " + sentimentBar.getClose());
}
}
Single instrument and currency
Consider a strategy which prints sentiment index for:
- EUR/USD instrument,
- USD currency, both the latest and the 24h old one.
@Override
public void onStart(IContext context) throws JFException {
console = context.getConsole();
history = context.getHistory();
dataService = context.getDataService();
long time = history.getLastTick(Instrument.EURUSD).getTime();
IFXSentimentIndex eurUsdIndex = dataService.getFXSentimentIndex(Instrument.EURUSD, time);
print("%s sentiment index at %s is %.5f with tendency %.5f",
Instrument.EURUSD,
sdf.format(eurUsdIndex.getIndexTime()),
eurUsdIndex.getIndexValue(),
eurUsdIndex.getIndexTendency());
IFXSentimentIndex usdIndex = dataService.getFXSentimentIndex(JFCurrency.getInstance("USD"), time);
print("USD sentiment index at %s is %.5f with tendency %.5f",
sdf.format(usdIndex.getIndexTime()),
usdIndex.getIndexValue(),
usdIndex.getIndexTendency());
usdIndex = dataService.getFXSentimentIndex(JFCurrency.getInstance("USD"), time - Period.DAILY.getInterval());
print("USD sentiment index at %s is %.5f with tendency %.5f",
sdf.format(usdIndex.getIndexTime()),
usdIndex.getIndexValue(),
usdIndex.getIndexTendency());
}
Multiple instruments and currencies
Consider a strategy which prints the latest sentiment indices for:
- set of Instruments,
- array of Currencies.
One has to mind that instrument index retrieval requires instrument to be subscribed. However, currency index retrieval does not.
The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.