トップ  >  リファレンス  >  サンプルソースコード  >  SDK-バックテスト  >  GUIModeChartControls.java
GUIModeChartControls.java

注意事項:
サンプルソースコードには実際にオーダーを発注するものがあります。
サンプルソースコードのストラテジーを起動する場合は、必ずデモ口座で行ってください。



// Copyright (c) 2009 Dukascopy (Suisse) SA. All Rights Reserved.
package singlejartest;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dukascopy.api.DataType;
import com.dukascopy.api.IChart;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.LoadingProgressListener;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.system.ISystemListener;
import com.dukascopy.api.system.ITesterClient;
import com.dukascopy.api.system.TesterFactory;
import com.dukascopy.api.system.tester.ITesterChartController;
import com.dukascopy.api.system.tester.ITesterExecution;
import com.dukascopy.api.system.tester.ITesterExecutionControl;
import com.dukascopy.api.system.tester.ITesterGui;
import com.dukascopy.api.system.tester.ITesterIndicatorsParameters;
import com.dukascopy.api.system.tester.ITesterUserInterface;
import com.dukascopy.api.system.tester.ITesterVisualModeParameters;
import com.dukascopy.charts.view.drawingstrategies.IVisualisationDrawingStrategy;

//Dukascopyテスターを初期化し、GUIモードでストラテジーを開始します。

@SuppressWarnings("serial")
public class GUIModeChartControls extends JFrame implements ITesterUserInterface, ITesterExecution {
    private static final Logger LOGGER = LoggerFactory.getLogger(GUIModeChartControls.class);

    private final int frameWidth = 1000;
    private final int frameHeight = 600;
    private final int controlPanelHeight = 100;
    private final int controlPanelMaxHeight = 150;
    
    private JPanel                  currentChartPanel = null;
    private ITesterExecutionControl executionControl  = null;
    
    private JPanel  controlPanel            = null;
    private JButton startStrategyButton     = null;
    private JButton pauseButton             = null;
    private JButton continueButton          = null;
    private JButton cancelButton            = null;
    private JPeriodComboBox jPeriodComboBox = null;
    
    private Map<IChart, ITesterGui> chartPanels = null;
    
    //url of the DEMO jnlp
    private static String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
    //user name
    private static String userName = "username";
    //password
    private static String password = "password";
    
    public GUIModeChartControls(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    }
    
    @Override
    public void setChartPanels(Map<IChart, ITesterGui> chartPanels) {
        this.chartPanels = chartPanels;
        this.jPeriodComboBox.setChartPanels(chartPanels);
        
        if(chartPanels != null && chartPanels.size() > 0){
            
            IChart chart = chartPanels.keySet().iterator().next();
            Instrument instrument = chart.getInstrument();
            setTitle(instrument.toString() + " " + chart.getSelectedOfferSide() + " " + chart.getSelectedPeriod());
            
            JPanel chartPanel = chartPanels.get(chart).getChartPanel();
            addChartPanel(chartPanel);
        }
    }

    @Override
    public void setExecutionControl(ITesterExecutionControl executionControl) {
        this.executionControl = executionControl;
    }
    
    public void startStrategy() throws Exception {
        // IClientインターフェースのインスタンスを取得
        final ITesterClient client = TesterFactory.getDefaultInstance();
        // システムイベントを受け取るリスナーをセット
        client.setSystemListener(new ISystemListener() {
            @Override
            public void onStart(long processId) {
                LOGGER.info("ストラテジー起動: " + processId);
                updateButtons();
            }

            @Override
            public void onStop(long processId) {
                LOGGER.info("ストラテジー停止: " + processId);
                resetButtons();
                
                File reportFile = new File("C:\\report.html");
                try {
                    client.createReport(processId, reportFile);
                } catch (Exception e) {
                    LOGGER.error(e.getMessage(), e);
                }
                if (client.getStartedStrategies().size() == 0) {
                    //Do nothing
                }
            }

            @Override
            public void onConnect() {
                LOGGER.info("接続完了");
            }

            @Override
            public void onDisconnect() {
                // テスターは切断されません
            }
        });

        LOGGER.info("接続中…");
        // jnlp, user , passwordでサーバーに接続します
        // データをダウンロードするにはサーバー接続する必要があります
        client.connect(jnlpUrl, userName, password);

        // 接続待ち
        int i = 10; // 最大10秒待ち
        while (i > 0 && !client.isConnected()) {
            Thread.sleep(1000);
            i--;
        }
        if (!client.isConnected()) {
            LOGGER.error("デューカスコピーのサーバーに接続出来ませんでした");
            System.exit(1);
        }

        // テストする通貨ペアをセット
        final Set<Instrument> instruments = new HashSet<Instrument>();
        instruments.add(Instrument.EURUSD);
        
        LOGGER.info("通貨ペア登録…");
        client.setSubscribedInstruments(instruments);
        // 初期資金設定
        client.setInitialDeposit(Instrument.EURUSD.getSecondaryCurrency(), 50000);
        // データロード
        LOGGER.info("データのダウンロード中");
        Future<?> future = client.downloadData(null);
        // ダウンロード完了待ち
        future.get();
        // ストラテジー開始
        LOGGER.info("ストラテジー開始");

        // IndicatorParameterBeanの実装 
        final class IndicatorParameterBean implements ITesterIndicatorsParameters {
            @Override
            public boolean isEquityIndicatorEnabled() {
                return true;
            }
            @Override
            public boolean isProfitLossIndicatorEnabled() {
                return true;
            }
            @Override
            public boolean isBalanceIndicatorEnabled() {
                return true;
            }
        }
        // TesterVisualModeParametersBeanの実装
        final class TesterVisualModeParametersBean implements ITesterVisualModeParameters {
            @Override
            public Map<Instrument, ITesterIndicatorsParameters> getTesterIndicatorsParameters() {
                Map<Instrument, ITesterIndicatorsParameters> indicatorParameters = new HashMap<Instrument, ITesterIndicatorsParameters>();
                IndicatorParameterBean indicatorParameterBean = new IndicatorParameterBean();
                indicatorParameters.put(Instrument.EURUSD, indicatorParameterBean);
                return indicatorParameters;
            }
        }
        // TesterVisualModeParametersBean作成
        TesterVisualModeParametersBean visualModeParametersBean = new TesterVisualModeParametersBean();
   
        // ストラテジー開始
        client.startStrategy(
            new MA_Play(),
            new LoadingProgressListener() {
                @Override
                public void dataLoaded(long startTime, long endTime, long currentTime, String information) {
                    LOGGER.info(information);
                }

                @Override
                public void loadingFinished(boolean allDataLoaded, long startTime, long endTime, long currentTime) {
                }

                @Override
                public boolean stopJob() {
                    return false;
                }
            },
            visualModeParametersBean,
            this,
            this
        );
        // 動作中
    }
    
    // 画面中央にフレームウインドウ表示 

    private void centerFrame(){
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setSize(screenWidth / 2, screenHeight / 2);
        setLocation(screenWidth / 4, screenHeight / 4);
    }
    
    // フレームにチャートパネル追加
    //@param panel

    private void addChartPanel(JPanel chartPanel){
        removecurrentChartPanel();
        
        this.currentChartPanel = chartPanel;
        chartPanel.setPreferredSize(new Dimension(frameWidth, frameHeight - controlPanelHeight));
        chartPanel.setMinimumSize(new Dimension(frameWidth, 200));
        chartPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
        getContentPane().add(chartPanel);
        this.validate();
        chartPanel.repaint();
    }
    
    private ITesterChartController getChartController() {
        if (chartPanels == null || chartPanels.size() == 0) {
            return null;
        }
        IChart chart = chartPanels.keySet().iterator().next();
        ITesterGui gui = chartPanels.get(chart);
        ITesterChartController chartController = gui.getTesterChartController();
        return chartController;
    }

    // start/pause/continue/cancelボタンと他ボタンを追加

    private void addControlPanel(){
        
        controlPanel = new JPanel();
        FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
        controlPanel.setLayout(flowLayout);
        controlPanel.setPreferredSize(new Dimension(frameWidth, controlPanelHeight));
        controlPanel.setMinimumSize(new Dimension(frameWidth, controlPanelHeight));
        controlPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, controlPanelMaxHeight));

        startStrategyButton = new JButton("Start strategy");
        startStrategyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startStrategyButton.setEnabled(false);
                Runnable r = new Runnable() {
                    public void run() {
                        try {
                            startStrategy();
                        } catch (Exception e2) {
                            LOGGER.error(e2.getMessage(), e2);
                            e2.printStackTrace();
                            resetButtons();
                        }
                    }
                };
                Thread t = new Thread(r);
                t.start();
            }
        });
        
        pauseButton = new JButton("Pause");
        pauseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(executionControl != null){
                    executionControl.pauseExecution();
                    updateButtons();
                }
            }
        });
        
        continueButton = new JButton("Continue");
        continueButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(executionControl != null){
                    executionControl.continueExecution();
                    updateButtons();
                }
            }
        });
        
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(executionControl != null){
                    executionControl.cancelExecution();
                    updateButtons();
                }
            }
        });
        
        jPeriodComboBox = new JPeriodComboBox(this);
        
        List<JButton> chartControlButtons = new ArrayList<JButton>();
        
        chartControlButtons.add(new JButton("Add Indicators") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().addIndicators();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Add Price Marker") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().activatePriceMarker();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Add Time Marker") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().activateTimeMarker();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Chart Auto Shift") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().setChartAutoShift();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Add Percent Lines") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().activatePercentLines();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Add Channel Lines") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().activateChannelLines();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Add Poly Line") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().activatePolyLine();
                }
            });
        }});        
        
        chartControlButtons.add(new JButton("Add Short Line") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().activateShortLine();
                }
            });
        }});
                
        chartControlButtons.add(new JButton("Add Long Line") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().activateLongLine();
                }
            });
        }});
                
        chartControlButtons.add(new JButton("Add Ray Line") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().activateRayLine();
                }
            });
        }});        
        
        chartControlButtons.add(new JButton("Add Horizontal Line") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().activateHorizontalLine();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Add Vertical Line") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().activateVerticalLine();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Add Text") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().activateTextMode();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Zoom In") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().zoomIn();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Zoom Out") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().zoomOut();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("add OHLC Index") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().addOHLCInformer();
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Bid") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().switchOfferSide(OfferSide.BID);
                }
            });
        }});
        
        chartControlButtons.add(new JButton("Ask") {{
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                
                    getChartController().switchOfferSide(OfferSide.ASK);
                }
            });
        }});
        
        controlPanel.add(startStrategyButton);
        controlPanel.add(pauseButton);
        controlPanel.add(continueButton);
        controlPanel.add(cancelButton);
        
        controlPanel.add(jPeriodComboBox);
        
        for(JButton btn : chartControlButtons){
            controlPanel.add(btn);
        }
        
        getContentPane().add(controlPanel);
        
        pauseButton.setEnabled(false);
        continueButton.setEnabled(false);
        cancelButton.setEnabled(false);
        
    }

    private void updateButtons(){
        if(executionControl != null){
            startStrategyButton.setEnabled(executionControl.isExecutionCanceled());
            pauseButton.setEnabled(!executionControl.isExecutionPaused() && !executionControl.isExecutionCanceled());
            cancelButton.setEnabled(!executionControl.isExecutionCanceled());
            continueButton.setEnabled(executionControl.isExecutionPaused());
            
        }
    }

    private void resetButtons(){
        startStrategyButton.setEnabled(true);
        pauseButton.setEnabled(false);
        continueButton.setEnabled(false);
        cancelButton.setEnabled(false);
    }
    
    private void removecurrentChartPanel(){
        if(this.currentChartPanel != null){
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        GUIModeChartControls.this.getContentPane().remove(GUIModeChartControls.this.currentChartPanel);
                        GUIModeChartControls.this.getContentPane().repaint();
                    }
                });             
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    }

    public void showChartFrame(){
        setSize(frameWidth, frameHeight);
        centerFrame();
        addControlPanel();
        setVisible(true);
    }
    
    public static void main(String[] args) throws Exception {
        GUIModeChartControls testerMainGUI = new GUIModeChartControls();
        testerMainGUI.showChartFrame();
    }
}

@SuppressWarnings("serial")
class JPeriodComboBox extends JComboBox implements ItemListener {
    private JFrame mainFrame = null;
    private Map<IChart, ITesterGui> chartPanels = null;
    private Map<Period, DataType> periods = new LinkedHashMap<Period, DataType>();
    
    public void setChartPanels(Map<IChart, ITesterGui> chartPanels) {
        this.chartPanels = chartPanels;
        
        IChart chart = chartPanels.keySet().iterator().next();
        this.setSelectedItem(chart.getSelectedPeriod());
    }

    public JPeriodComboBox(JFrame mainFrame){
        this.mainFrame = mainFrame;
        this.addItemListener(this);
        
        periods.put(Period.THIRTY_SECS, DataType.TIME_PERIOD_AGGREGATION);
        periods.put(Period.FIVE_MINS, DataType.TIME_PERIOD_AGGREGATION);
        periods.put(Period.TEN_MINS, DataType.TIME_PERIOD_AGGREGATION);
        periods.put(Period.THIRTY_MINS, DataType.TIME_PERIOD_AGGREGATION);
        
        for(Period period: periods.keySet()){
            this.addItem(period);
        }
    }
    
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            if(chartPanels != null && chartPanels.size() > 0){
                IChart chart = chartPanels.keySet().iterator().next();
                ITesterGui gui = chartPanels.get(chart);
                ITesterChartController chartController = gui.getTesterChartController();

                Period period = (Period)e.getItem();
                DataType dataType = periods.get(period); 
                
                chartController.changePeriod(dataType, period);
                mainFrame.setTitle(chart.getInstrument().toString() + " " + chart.getSelectedOfferSide() + " " + chart.getSelectedPeriod());
            }
        }
    }
}








スポンサーリンク

スポンサーリンク
検索
リファレンスツリー


Copyright ©2016 JForexAPIで自動売買させ隊! All Rights Reserved.


Top

inserted by FC2 system