JForex Strategy

Discussion in 'General Topics on Brokers' started by BWD, Nov 20, 2010.

  1. BWD

    BWD

    本人正在试用JForex,总体感觉不错。不知道哪位高手能分享些简单的、用于交易的Strategy?或者提供几个学习、下载Strategy的网页,先谢了。
     
  2. BWD

    BWD

    多谢,不过好像没有现成的策略可以直接下载使用。
     
  3. http://www.dukascopy.com/wiki/index.php?title=Simple_Strategy
    以下是一个简单策略的模版
    import java.awt.KeyEventDispatcher;
    import java.awt.KeyboardFocusManager;
    import java.awt.event.KeyEvent;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Random;
    import java.util.concurrent.Callable;
    import com.dukascopy.api.*;

    public class MyQuickCalStrategy implements IStrategy {

    //define some constants that we will use
    private static final String DATE_FORMAT_NOW = "yyyyMMdd_HHmmssSSS";
    private static final String BUY = "BUY";
    private static final String SELL = "SELL";

    //this variable is used for generating random numbers
    private static Random random = new Random();

    private IEngine engine;
    private IConsole console;
    private IOrder order;

    //BUY order variables. Here you can set default parameters for BUY order
    private Instrument buyInstrument = Instrument.EURUSD;
    private IEngine.OrderCommand buyComand = IEngine.OrderCommand.BUY;
    private double buyAmount = 0.1;

    //SELL order variables. Here you can set default parameters for SELL order
    private Instrument sellInstrument = Instrument.EURUSD;
    private IEngine.OrderCommand sellComand = IEngine.OrderCommand.SELL;
    private double sellAmount = 0.1;

    public void onStart(final IContext context) throws JFException {
    this.engine = context.getEngine();
    this.console = context.getConsole();

    //create keyboard listener
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
    public boolean dispatchKeyEvent(final KeyEvent e) {
    boolean discardEvent = false;

    //define input mask. KeyEvent.CTRL_DOWN_MASK stands for button Ctrl is pressed down
    //66 is index of character "b", "s"=83, "c"=67 and "r"=82
    int buyMask = KeyEvent.CTRL_DOWN_MASK + 66; //Ctrl+b
    int sellMask = KeyEvent.CTRL_DOWN_MASK + 83; //Ctrl+s

    //check if the input on keyboard is correct for our buyMask
    if(((e.getModifiersEx()+e.getKeyCode()) == buyMask) && e.paramString().indexOf("KEY_PRESSED")>=0){
    //creates "buy" order task and executes it
    DoOrderTask task = new DoOrderTask(BUY);
    context.executeTask(task);
    }
    //check if the input on keyboard is correct for our selMask
    if(((e.getModifiersEx()+e.getKeyCode()) == sellMask) && e.paramString().indexOf("KEY_PRESSED")>=0){
    //creates "sell" order task and executes it
    DoOrderTask task = new DoOrderTask(SELL);
    context.executeTask(task);
    }
    return discardEvent;
    }
    });

    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    //switch off keyboard listener
    KeyboardFocusManager.setCurrentKeyboardFocusManager(null);
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }

    private class DoOrderTask implements Callable<Object> {
    String mode;
    //constructor, so that we could pass "what to create"
    public DoOrderTask(String mode) {
    this.mode = mode;
    }
    //This method places an order regardless off passed mode
    public Object call() {
    try{
    if (mode != null && mode.equals(BUY)){
    order = engine.submitOrder(getLabel(), buyInstrument, buyComand, buyAmount);
    }
    if (mode != null && mode.equals(SELL)){
    order = engine.submitOrder(getLabel(), sellInstrument, sellComand, sellAmount);
    }

    }catch(JFException jfe){
    console.getOut().println(jfe.getMessage());
    }
    return null;
    }
    }

    //gets unique label for order
    protected String getLabel() {
    String label = "";
    label = "I" + getCurrentTime() + generateRandom(2);
    if (order != null && order.getLabel().equals(label)) {
    label += generateRandom(3);
    }
    return label;
    }

    //used for order label generation
    private String getCurrentTime() {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
    return sdf.format(cal.getTime());
    }

    //used for order label generation
    private static int generateRandom(int n){
    return Math.abs(random.nextInt(n)) % n;
    }
    }
     
  4. 明天开始准备好好学习一下jforex的编程。发现很强大。
    先寻找一些简单通用的策略模版,比如双均线系统
     
  5. /**
    * MA Cross
    *
    * @author defc0n1
    * @version 0.1
    */
    package jforex;

    import java.util.concurrent.TimeUnit;

    import com.dukascopy.api.*;
    import com.dukascopy.api.IEngine.OrderCommand;
    import com.dukascopy.api.IIndicators.AppliedPrice;
    import com.dukascopy.api.IIndicators.MaType;

    public class MaCross implements IStrategy {
    @Configurable("Instrument") public Instrument instrument = Instrument.EURUSD;
    @Configurable("Amount") public double amount = 0.25;
    @Configurable("Slippage") public double slippage = 5;
    @Configurable("Time frame") public Period timeFrame = Period.TEN_MINS;
    @Configurable("MA1 type") public MaType ma1Type = MaType.SMA;
    @Configurable("MA1 time period") public int ma1TimePeriod = 20;
    @Configurable("MA2 type") public MaType ma2Type = MaType.SMA;
    @Configurable("MA2 time period") public int ma2TimePeriod = 50;
    @Configurable("MA filter") public Filter maFilter = Filter.WEEKENDS;
    @Configurable("Offer side") public OfferSide offerSide = OfferSide.ASK;
    @Configurable("Applied price") public AppliedPrice appliedPrice = AppliedPrice.CLOSE;



    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;

    private IOrder order = null;

    public void onStart(IContext context) throws JFException {
    this.engine = context.getEngine();
    this.console = context.getConsole();
    this.history = context.getHistory();
    this.context = context;
    this.indicators = context.getIndicators();
    this.userInterface = context.getUserInterface();
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {

    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    if(!instrument.equals(this.instrument) || period != this.timeFrame) return;

    double MaFast ;
    double MaFastprev;
    double MaFastnext;

    double MaSlow ;
    double MaSlowprev ;
    double MaSlownext;



    String EntryMa = "false" ;

    // previous bar
    int shift2 = 2 ;

    //actual bar
    int shift1 = 1 ;
    //next bar
    int shift0 = 0 ;

    MaFast = indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma1TimePeriod,this.ma1Type,shift1);
    MaFastprev= indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma1TimePeriod,this.ma1Type,shift2);
    MaFastnext = indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma1TimePeriod,this.ma1Type,shift0);




    MaSlow = indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma2TimePeriod,this.ma2Type,shift1);
    MaSlowprev = indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma2TimePeriod,this.ma2Type,shift2);
    MaSlownext = indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma2TimePeriod,this.ma2Type,shift0);



    if (MaFast > MaSlow && MaFastprev < MaSlowprev && MaFastnext > MaSlownext) {

    console.getOut().println("Long Entry");

    }



    else if (MaSlow > MaFast && MaSlowprev < MaFastprev && MaSlownext >MaFastnext){

    console.getOut().println("Short Entry");

    }



    }
    }
     
  6. 上面是双均线交叉的一个模版,不过还无法下单,只有信息显示。要完成下单:1,需要一个下单指令;2,需要建立一个label
     
  7. 官方论坛:http://www.dukascopy.com/swiss/english/forex/jforex/forum/

    jorex的WIKI:http://www.dukascopy.com/wiki/index.php?title=Main_Page

    如何编一个最简单的策略:http://www.dukascopy.com/wiki/index.php?title=Simple_Strategy

    如何在jforex创建和打开一个策略:http://www.dukascopy.com/swiss/chinese/forex/jforex/tutorial/?ibref=1697

    dukascopy论坛上各类现成的策略下载(dukascopy正进行每月的交易比赛,奖金丰厚)

    http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewforum.php?f=21

    策略可以直接在官方的服务器上运行,而非本地机子上Strategy running on the Dukascopy

    http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=31&t=23909

    MT4/JForex 客户连接(你也可以用MT上的策略在jforex运行)

    http://www.dukascopy.com/swiss/chinese/forex/jforex/third-party-solutions/mt4-jforex_bridge/

    ichart:http://www.dukascopy.com/swiss/docs/interfaces/IChart.htm
     
  8. http://www.quantisan.com/url]博主用jfo...p://www.quantisan.com/jforex-stopmanager-1-0/,
    配合jforex上的stop loss level功能(这个风险管理功能直接放在dukas的服务器上)
    就可以制作成一个风险完全可控的低成本的外汇自动交易
     
  9. 谢谢你啊! :)
     
  10. BWD

    BWD

    非常感谢。