美文网首页
简单的策略

简单的策略

作者: SimpleLiang | 来源:发表于2016-01-28 20:35 被阅读0次

概要

下面的策略获取了指定Instrument过往的bars,用来确定订单的方向。如果收盘价大于开盘价,创建多单。否则创建空单。每个盈利的交易-----------

定义可配置参数

为了给我们的策略定义可配置的策略,我们需要使用@Configurable注解。

下面的代码片段定义了4个可配置参数:Instrument, Amount, Take profit, Stop loss并设置了默认值。

字段enginehistory用来从各自的历史数据中拉取bars。

public class SimpleTpSlStrategy implements IStrategy {    
    // Configurable parameters    
    @Configurable("Instrument") 
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("Amount")    
    public double amount = 0.001;    
    @Configurable("Stop loss")    
    public int slPips = 10;    
    @Configurable("Take profit on loss")    
    public int tpPipsOnLoss = 10;    
    @Configurable("Take profit on profit")  
    public int tpPipsOnProfit = 5;   
     
    private IEngine engine;    
    private IHistory history;    
    private IConsole console;    
    private IContext context;    
    private IOrder order;

onStart方法

onStart方法在策略开始的时候被调用。
我们使用它来创建初始订单。下面的代码片段从历史数据中拉取了之前的日bar,标示了初始订单的方向并提交了订单。

方法submitOrder(double amount, OrderCommand orderCmd, double takeProfitPips)计算了合适的止损和止盈价格并创建一个新的订单:

public void onStart(IContext context) throws JFException {
    this.engine = context.getEngine();
    this.history = context.getHistory();
    this.console = context.getConsole();
    this.context = context;
    // subscribe the instrument that we are going to work with
    context.setSubscribedInstruments(java.util.Collections.singleton(instrument));
    // Fetching previous daily bar from history
    IBar prevDailyBar = history.getBar(instrument, Period.DAILY, OfferSide.ASK, 1);
    // Identifying the side of the initial order
    OrderCommand orderCmd = prevDailyBar.getClose() > prevDailyBar.getOpen() 
            ? OrderCommand.BUY 
            : OrderCommand.SELL;
    // submitting the order with the specified amount, command and take profit
    submitOrder(amount, orderCmd, tpPipsOnLoss);
}

private void submitOrder(double amount, OrderCommand orderCmd, double tpPips) throws JFException {
    double slPrice, tpPrice;
    ITick lastTick = history.getLastTick(instrument);
    // Calculating stop loss and take profit prices
    if (orderCmd == OrderCommand.BUY) {
        slPrice = lastTick.getAsk() - slPips * instrument.getPipValue();
        tpPrice = lastTick.getAsk() + tpPips * instrument.getPipValue();
    } else {
        slPrice = lastTick.getBid() + slPips * instrument.getPipValue();
        tpPrice = lastTick.getBid() - tpPips * instrument.getPipValue();
    }
    // Submitting the order for the specified instrument at the current market price
    order = engine.submitOrder(orderCmd.toString() + System.currentTimeMillis(), instrument, orderCmd, amount, 0, 20, slPrice, tpPrice);
}

onMessage方法

onMessage方法在每次收到新消息的时候被调用,在我们的例子里,就是每次订单关闭的时候。我们用它来创建新的订单。

public void onMessage(IMessage message) throws JFException {
    if (message.getType() != Type.ORDER_CLOSE_OK
            || !message.getOrder().equals(order) //only respond to our own order close
        ) {
        return;
    }
    console.getInfo().format("%s closed with P/L %.1f pips", order.getLabel(), order.getProfitLossInPips()).println();
    if (message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_TP)) {
        // on close by TP we keep the order direction
        submitOrder(amount, order.getOrderCommand(), tpPipsOnProfit);
    } else if (message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_SL)) {
        //  on close by SL we change the order direction and use other TP distance
        OrderCommand orderCmd = order.isLong() ? OrderCommand.SELL : OrderCommand.BUY;
        submitOrder(amount, orderCmd, tpPipsOnLoss);
    } else {
        //on manual close or close by another strategy we stop our strategy
        console.getOut().println("Order closed either from outside the strategy. Stopping the strategy.");
        context.stop();
    }
}

public void onStop() throws JFException {
    if(order.getState() == IOrder.State.FILLED || order.getState() == IOrder.State.OPENED){
        order.close();
    }
}

下载

SimpleTpSIStrategy.java

相关文章

  • 简单的策略

    概要 下面的策略获取了指定Instrument过往的bars,用来确定订单的方向。如果收盘价大于开盘价,创建多单。...

  • 简单的策略

    简单,是用来留给有智慧的人。这样的人,热爱简单。 复杂,则是常人的需要,如不复杂,总觉得,不够威猛,不受重视。 在...

  • 简单的策略

    简单,是用来留给有智慧的人。 这样的人,热爱简单。 复杂,则是常人的需要,如不复杂,总觉得,不够威猛,不受重视。 ...

  • 简单的策略

    林海峰作品 简单,是用来留给有智慧的人。这样的人,热爱简单。 复杂,则是常人的需要,如不复杂,总觉得,不够威猛,不...

  • 简单的策略

    简单的策略 简单,是用来留给有智慧的人。这样的人,热爱简单。 复杂,则是常人的需要,如不复杂,总觉得,不够威猛,不...

  • 简单的策略

    工作中如何妥善处理各类关系让人头疼,最近看了部小说,深受启发,对我大有醍醐灌顶之意。我应该找到了痛点,决意...

  • 12.10 最简单的安全投资策略是什么?

    最简单的安全投资策略是什么? 最简单安全的投资策略:定投策略 即:定期等额购买某一支(或几支)成长型股票。 这里重...

  • 简单策略的好处

    成功可能是偶然,但是偶然的背后必定存在着某种必然。有些时候简单的策略可能比复杂的策略更有效。就拿定投基金来说,就只...

  • iOS图片加载策略的简单实现

    iOS图片加载策略的简单实现

  • 网格策略(含风控)

    在分享了套利程序后,有朋友问我使用的套利策略,其实策略很简单,属于网格策略的变更,分享给大家:网格变异策略: 发起...

网友评论

      本文标题:简单的策略

      本文链接:https://www.haomeiwen.com/subject/efcjkttx.html