📄 optimizedroc.java
字号:
package com.jsystemtrader.strategy;
import com.ib.client.*;
import com.jsystemtrader.indicator.*;
import com.jsystemtrader.platform.*;
import com.jsystemtrader.util.*;
public class OptimizedRoC extends Strategy {
private final int longPeriodLength = 72, shortPeriodLength = 36;
private final IndicatorHistory rateOfChangeLongHistory, rateOfChangeShortHistory;
private double rateOfChangeLong, rateOfChangeShort;
public OptimizedRoC() throws JSystemTraderException {
// add strategy-specific headers to standard headers
strategyLogHeaders.add("RoC-Long");
strategyLogHeaders.add("RoC-Short");
Contract contract = ContractFactory.makeContract("ES", "FUT", "GLOBEX", MostLiquidContract.getMostLiquid(), null);
setStrategy(contract, PriceBar.BAR_5_MINUTE, true, 1);
/*
Create indicator histories, so that they can be shown on the
strategy performance chart for the subsequent analysis.
*/
int subChart = 1; // separate subchart from the price chart
rateOfChangeLongHistory = new IndicatorHistory("RoC-Long", subChart);
rateOfChangeShortHistory = new IndicatorHistory("RoC-Short", subChart);
addIndicatorHistory(rateOfChangeLongHistory);
addIndicatorHistory(rateOfChangeShortHistory);
addTradingInterval("9:40", "15:55", 5);
}
/**
* 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 (rateOfChangeLong > 0 && rateOfChangeShort > 0) {
decision = DECISION_LONG;
} else if (rateOfChangeLong < 0 && rateOfChangeShort < 0) {
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 += "RoC-Long: " + nf4.format(rateOfChangeLong) + " RoC-Short: " + nf4.format(rateOfChangeShort) + "<br>";
eventLog.write(msg, "Info", 1);
strategyLogColumns.add(nf4.format(rateOfChangeLong));
strategyLogColumns.add(nf4.format(rateOfChangeShort));
strategyLog.write(strategyLogColumns, getCalendar(), "Info", 1);
}
/**
* Called from the super class
*/
public void updateIndicators() throws IndicatorCalculationException {
PriceBar lastPriceBar = quoteHistory.getLastPriceBar();
long date = lastPriceBar.getDate();
// calculate long and short rate of change indicators
ROC longROC = new ROC(date, quoteHistory, longPeriodLength);
ROC shortROC = new ROC(date, quoteHistory, shortPeriodLength);
rateOfChangeLong = longROC.calculate();
rateOfChangeShort = shortROC.calculate();
rateOfChangeLongHistory.addIndicator(longROC);
rateOfChangeShortHistory.addIndicator(shortROC);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -