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

📄 forexantitrend.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 moving average
 * crossover as an indicator. When the fast EMA is above the slow EMA, it sells
 * short. Otherwise, it buys. This is an anti-trend type of a system.
 * The moving averages are calculated from the priceBar history of the 1-minute bars.
 */
public class ForexAntiTrend extends Strategy {
    private final int maFastLength = 45, maSlowLength = 180;
    private final IndicatorHistory fastEMAHistory, slowEMAHistory;
    private double fastEMA, slowEMA;

    public ForexAntiTrend() throws JSystemTraderException {
        // add strategy-specific headers to standard headers
        strategyLogHeaders.add("fastEMA");
        strategyLogHeaders.add("slowEMA");

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

        /*
         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
        fastEMAHistory = new IndicatorHistory("Fast EMA", subChart);
        slowEMAHistory = new IndicatorHistory("Slow EMA", subChart);
        addIndicatorHistory(fastEMAHistory);
        addIndicatorHistory(slowEMAHistory);

        // see javadocs for the TradingInterval class
        addTradingInterval("1:00", "23:50", 5); // trades 23 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 (fastEMA > slowEMA) {
                decision = DECISION_SHORT;
            }
            if (fastEMA < slowEMA) {
                decision = DECISION_LONG;
            }
        }
    }

    /**
     * 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 += "fastEMA: " + nf4.format(fastEMA) + " slowEMA: " + nf4.format(slowEMA) + "<br>";
        eventLog.write(msg, "Info", 1);

        strategyLogColumns.add(nf4.format(fastEMA));
        strategyLogColumns.add(nf4.format(slowEMA));
        strategyLog.write(strategyLogColumns, getCalendar(), "Info", 1);
    }

    /**
     * Called from the super class
     */
    public void updateIndicators() throws IndicatorCalculationException {

        long date = quoteHistory.getLastPriceBar().getDate();

        // calculate fast and slow EMAs
        EMA fastEMAInd = new EMA(date, quoteHistory, maFastLength);
        EMA slowEMAInd = new EMA(date, quoteHistory, maSlowLength);
        fastEMA = fastEMAInd.calculate();
        slowEMA = slowEMAInd.calculate();

        fastEMAHistory.addIndicator(fastEMAInd);
        slowEMAHistory.addIndicator(slowEMAInd);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -