ティックデータを取得する方法は以下の2種類です。
同期と非同期の両方で取得する事が可能です。
同期取得と非同期取得の違いについてはヒストリーバーのページを参照して下さい。
ラストティック
最後に受信したティックデータを取得します。バックテスト時には現在の時刻として使われます。
getLastTickメソッドで呼び出してITickオブジェクトにセットし、ITickオブジェクトのgetTimeメソッドで時間を取得します。
long currentTime = history.getLastTick(instrument).getTime();
最後に受信したAsk価格を取得するには、getAskメソッドを使います。
double bestAskPrice = history.getLastTick(instrument).getAsk();
シフトによるティックデータ取得
getTickメソッドはシフト指定によるITickオブジェクトを返します。
シフトに0を設定した場合はラストティックを取得し、
シフトに1を設定した場合は1つ前のティックを取得し、
シフトに2を設定した場合は2つ前のティックを取得します。
以下例は、ラストティックと1つ前のティックを取得します。
ITick tick0 = history.getTick(Instrument.EURUSD, 0);
ITick tick1 = history.getTick(Instrument.EURUSD, 1);
console.getOut().println(String.format("[シフト指定]最後に受信したティック : %s", tick0));
console.getOut().println(String.format("[シフト指定]一つ前に受信したティック: %s", tick1));
サンプルソースコード:
HistoryTicksSynch.java
タイムインターバルによるティックデータ取得
getTickメソッドは指定されたタイムインターバルのITickオブジェクトリストを返します。
指定した取得開始時刻と取得終了時刻がティック時刻に一致すると結果リストを返します。
以下例は、ラストティック〜テストティックから10秒前の間のティックを取得します。
ITick lastTick = history.getLastTick(Instrument.EURUSD);
List<ITick> ticks = history.getTicks(Instrument.EURUSD, lastTick.getTime() - 10 * 1000, lastTick.getTime());
int last = ticks.size() - 1;
console.getOut().println(String.format(
"[タイムインターバル指定]取得したティック数=%s; 最後(最新)に受信したティックのBid価格=%.5f, 時刻=%s",
ticks.size(),
ticks.get(0).getBid() , DateUtils.format(ticks.get(0).getTime())
));
console.getOut().println(String.format(
"[タイムインターバル指定]取得したティック数=%s; 最も古い時間に取得したティックのBid価格=%.5f, 時刻=%s",
ticks.size(),
ticks.get(last).getBid(), DateUtils.format(ticks.get(last).getTime())
));
サンプルソースコード:
HistoryTicksSynch.java
ティックデータレベルでの判定
以下例は、各アクティブオーダー毎に、オーダー約定から現在までの間に一定価格レベルをティックレベルで何回上抜け又は下抜けしたかを調べます。
enum Location {
ABOVE,
BELOW
}
private Location priceLocation;
private double priceLevel;
@Override
public void onStart(IContext context) throws JFException {
history = context.getHistory();
console = context.getConsole();
engine = context.getEngine();
context.setSubscribedInstruments(java.util.Collections.singleton(Instrument.EURUSD), true);
ITick lastTick = history.getLastTick(instrument);
for(IOrder order : engine.getOrders(instrument)){
int breachCount = 0;
int fallCount = 0;
priceLevel = order.getOpenPrice() + instrument.getPipValue() * 5;
List<ITick> ticks = history.getTicks(instrument, order.getFillTime(), lastTick.getTime());
if(ticks == null || ticks.size() == 0){
console.getErr().println("オーダー約定された時刻〜ラストティック時刻の間にティックがありませんでした。");
continue;
}
priceLocation = ticks.get(0).getBid() >= priceLevel
? Location.ABOVE
: Location.BELOW;
ticks.remove(0);
for(ITick tick : ticks){
if(priceLocation == Location.ABOVE && tick.getBid() < priceLevel){
priceLocation = Location.BELOW;
fallCount++;
} else if(priceLocation == Location.BELOW && tick.getBid() > priceLevel){
priceLocation = Location.ABOVE;
breachCount++;
}
}
console.getOut().println(String.format(
"%sに約定された%sラベルのオーダーは、価格レベル%.5fを%s回上抜けし、%s回下抜けしました。",
DateUtils.format(order.getFillTime()),order.getLabel(), priceLevel, breachCount, fallCount
));
}
}
サンプルソースコード:
PriceBreachAccordingToOrder.java