📄 smoothedroc.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 the S&P 500 E-mini futures (ES) using smoothed
* rate of change as an indicator. When SmoothedROC is positive, it buys.
* Otherwise, it sells short.
*/
public class SmoothedRoC extends Strategy {
private final int maLength = 3;
private final int lookBackLength = 65;
private double smoothedROC;
private final IndicatorHistory smoothedROCHistory;
public SmoothedRoC() throws JSystemTraderException {
// add strategy-specific headers to standard headers
strategyLogHeaders.add("smoothedROC");
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; // show indicator on a separate subchart
smoothedROCHistory = new IndicatorHistory("SmoothedROC", subChart);
addIndicatorHistory(smoothedROCHistory);
// no trading interval defined: strategy may hold positions overnight
//addTradingInterval("9:35", "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 (smoothedROC > 0) {
decision = DECISION_LONG;
}
if (smoothedROC < 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 += "smoothedROC: " + nf4.format(smoothedROC) + "<br>";
eventLog.write(msg, "Info", 1);
strategyLogColumns.add(nf4.format(smoothedROC));
strategyLog.write(strategyLogColumns, getCalendar(), "Info", 1);
}
/**
* Called from the super class
*/
public void updateIndicators() throws IndicatorCalculationException {
long date = quoteHistory.getLastPriceBar().getDate();
SmoothedROC smoothedROCInd = new SmoothedROC(date, quoteHistory, maLength, lookBackLength);
smoothedROC = smoothedROCInd.calculate();
smoothedROCHistory.addIndicator(smoothedROCInd);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -