📄 rangebreaker.java
字号:
package com.jsystemtrader.strategy;
import com.ib.client.*;
import com.jsystemtrader.indicator.*;
import com.jsystemtrader.platform.*;
import com.jsystemtrader.util.*;
/**
* This sample strategy trades Dow E-Mini futures using a range breakout system.
* When the last bar high is above the 180-minute period high, it buys.
* When the last bar low is below the 180-minute period low, it sells short.
*/
public class RangeBreaker extends Strategy {
private final int periodLength = 180;
private final IndicatorHistory periodLowHistory, periodHighHistory;
private double periodHigh, periodLow;
private boolean isHighBroken, isLowBroken;
public RangeBreaker() throws JSystemTraderException {
// add strategy-specific headers to standard headers
strategyLogHeaders.add("Period High");
strategyLogHeaders.add("Period Low");
Contract contract = ContractFactory.makeContract("YM", "FUT", "ECBOT", MostLiquidContract.getMostLiquid(), null);
setStrategy(contract, PriceBar.BAR_1_MINUTE, true, 1);
/*
Create indicator histories, so that they can be shown on the
strategy performance chart for the subsequent analysis.
*/
int subChart = 0; // same subchart as the price chart
periodLowHistory = new IndicatorHistory("PeriodLow", subChart);
periodHighHistory = new IndicatorHistory("PeriodHigh", subChart);
addIndicatorHistory(periodLowHistory);
addIndicatorHistory(periodHighHistory);
// see javadocs for the TradingInterval class
addTradingInterval("9:35", "15:55", 15);
}
/**
* This method is invoked when a new bar is completed.
*/
public void onBar() {
// First, let the super strategy decide if we can trade at all
super.onBar();
if (decision == DECISION_NONE) { // the super strategy has no objections
if (isHighBroken) {
decision = DECISION_LONG;
}
if (isLowBroken) {
decision = DECISION_SHORT;
}
}
}
/**
* This method is invoked when a new tick comes in.
*/
public void onTick() {
super.onTick();
double lastTick = quoteHistory.getLast();
// do something
}
/**
* Instance of NumberFormat is shared by multiple threads,
* so the access must be synchronized.
*/
public void updateState() {
super.updateState();
String msg = this.getName() + ": state updated" + "<br>";
msg += "Last PriceBar: " + quoteHistory.getLastPriceBar() + "<br>";
msg += "periodHigh: " + nf4.format(periodHigh) + " periodLow: " + nf4.format(periodLow) + "<br>";
eventLog.write(msg, "Info", 1);
strategyLogColumns.add(nf4.format(periodHigh));
strategyLogColumns.add(nf4.format(periodLow));
strategyLog.write(strategyLogColumns, getCalendar(), "Info", 1);
}
/**
* Called from the super class
*/
public void updateIndicators() throws IndicatorCalculationException {
int lastBar = quoteHistory.size() - 1;
long date = quoteHistory.getLastPriceBar().getDate();
// Have the period high or period low been broken?
int periodEnd = lastBar - 1;
int periodStart = periodEnd - periodLength;
PeriodHigh periodHigh = new PeriodHigh(date, quoteHistory, periodStart, periodEnd);
PeriodLow periodLow = new PeriodLow(date, quoteHistory, periodStart, periodEnd);
double lastHigh = quoteHistory.getLastPriceBar().getHigh();
isHighBroken = (lastHigh > periodHigh.calculate());
double lastLow = quoteHistory.getLastPriceBar().getLow();
isLowBroken = (lastLow < periodLow.calculate());
periodLowHistory.addIndicator(periodLow);
periodHighHistory.addIndicator(periodHigh);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -