⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 forexroc.java

📁 网上期货交易的外挂原码,可实现自动交易功能,自动添加模块
💻 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 EUR.USD cash on IDEALPRO using rate of change
 * as an indicator. If both short term and long term rates of change are positive,
 * it buys. If both short term and long term rates of change are negative,
 * it sells short.
 */
public class ForexRoC extends Strategy {
    private final int longPeriodLength = 72, shortPeriodLength = 36;
    private final IndicatorHistory rateOfChangeLongHistory, rateOfChangeShortHistory;
    private double rateOfChangeLong, rateOfChangeShort;


    public ForexRoC() throws JSystemTraderException {
        // add strategy-specific headers to standard headers
        strategyLogHeaders.add("RoC-Long");
        strategyLogHeaders.add("RoC-Short");

        Contract contract = ContractFactory.makeContract("EUR", "CASH", "IDEALPRO", null, "USD");
        setStrategy(contract, PriceBar.BAR_5_MINUTE, false, 25000);

        /*
         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);

        // see javadocs for the TradingInterval class
        addTradingInterval("2:05", "11:00", 5); // trades about 9 hours/day
    }

    /**
     * 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 + -