2014年1月23日木曜日

C言語で構造体を使って配列を関数に渡すサンプル

C言語の関数で、動的配列を渡すのに、構造体を使ったサンプルコード。

struct ints {
int len;
int *array;
};

void test(struct ints is) {
int i;
for (i = 0; i < is.len; i++) {
printf("%d, ", is.array[i]);
}
printf("\n");
}

int main(int argc, char *argv[]) {
int is[] = { 1, 2, 3, 4, 5 };
struct ints ints1;
ints1.len = 5;
ints1.array = is;
test(ints1);

return 0;
}

2013年9月23日月曜日

Client action on order fill and close

Client action on order fill and close
オーダー記入とクローズする上でのクライアントアクション

Consider that from the program you wish to do some extra logging on both order fill and close.
オーダー記入とクローズの両方において、拡張的ロギングが必要ならば、プログラム上で行うことを検討して欲しい。
And also you wish to set order stop loss:
また、あなたはストップロスオーダーを望むだろう:
client.startStrategy(new StrategyPublicMethods(new StrategyPublicMethods.ClientActions() {
                       
    @Override
    public void onOrderFill(IOrder order) {
        LOGGER.info("Order filled, execute here some logic on client side, say set stop loss if the order is long");
        if(order.isLong()){
            try {
                order.setStopLossPrice(order.getOpenPrice() - order.getInstrument().getPipValue() * 10);
            } catch (JFException e) {
                e.printStackTrace();
            }
        }
    }
                       
    @Override
    public void onOrderClose(IOrder order) {
        LOGGER.info("Order closed, execute here some logic on client side");                                  
    }
}));
Consider introducing an interface in a strategy which would serve as an order fill and close listener.
オーダー記入とクローズの助けとして、ストラテジの中でインタフェイスの導入を検討して欲しい。
Implementation of the interface would get passed from the IClient program:
インタフェイスの実装はIClientプログラムから渡されるだろう。
public class StrategyPublicMethods implements IStrategy {
       
    private IConsole console;
    private IEngine engine;
    private StrategyPublicMethods.ClientActions clientActions;
   
    public interface ClientActions {
        void onOrderClose(IOrder order);
        void onOrderFill(IOrder order);
    }
   
    //for the launch from standalone
    public StrategyPublicMethods (StrategyPublicMethods.ClientActions clientActions){
        this.clientActions = clientActions;
    }

    //...
}
Then on every order fill and close execute the logic that has been passed from the IClient program:
そして、全てのオーダー記入とクローズ時、IClientプログラムから渡されたロジックが実行される。 
@Override
public void onMessage(IMessage message) throws JFException {
    if(message.getType() == IMessage.Type.ORDER_FILL_OK){
        clientActions.onOrderFill(message.getOrder());
    }
    if(message.getType() == IMessage.Type.ORDER_CLOSE_OK){
        clientActions.onOrderClose(message.getOrder());
    }
}

2013年9月21日土曜日

Program communicating with a strategy

Program communicating with a strategy
ストラテジとしてのプログラム・コミュニケイティング

By defining a listener (in the means of public interface) within a strategy one can implement some client side logic on strategy events.
ストラテジの中でリスナー (パブリックインターフェイスの意味で)を定義することにより、一つのストラテジ・イベントにいくつかのクライアント側のロジックを実装することができる。



2013年9月18日水曜日

Change chart theme

Change chart theme
チャートテーマの変更

Note: Available with JForex-API 2.7.9
注意:JForex-API 2.7.9にて有効
IChartTheme represents a chart theme, it can be retreived and set to a chart by using the getTheme and setTheme methods of the IClientChartPresentationManager interface.
IChartThemeはチャートテーマを表示するものであり、IClientChartPresentationManagerインタフェイスのgetThemeメソッドとsetThemeメソッドの使用により取り出され、チャートにセットすることができる。
Consider changing chart's background and tick/candle colors:
チャートの背景やティック/ローソク足の色の変更を検討して欲しい。
chartPresentationManager.setTheme(
    chartPresentationManager.getTheme()
        .setName("Custom tick/candle color theme")
        .setColor(ColoredElement.BACKGROUND, new Color(254, 244, 214))
        .setColor(ColoredElement.CANDLE_BEAR, Color.CYAN)
        .setColor(ColoredElement.CANDLE_BULL, Color.ORANGE)
        .setColor(ColoredElement.ASK, Color.YELLOW.darker())
        .setColor(ColoredElement.BID, Color.PINK.darker())
);
One can retrieve a predefined theme by calling the IClientChartPresentationManager.getPredefinedTheme method.
IClientChartPresentationManager.getPredefinedThemeメソッドのコールにより、あらかじめ定義されたテーマを取り出すことができる。
Consider setting a predefined theme to a chart:
あらかじめ定義されたテーマをチャートにセッティングすることを検討して欲しい。
chartPresentationManager.setTheme(chartPresentationManager.getPredefinedTheme(IChartTheme.Predefined.BLUE_BLACK_ON_GRAY));
For full usage example look up the cmbChartTheme combo box in:
全ての使用方法の例をcmbChartThemeコンボボックスで検索できる。 

2013年9月15日日曜日

Add chart objects

Add chart objects
チャートオブジェクトの追加

One can not only plot chart objects from within a strategy, but also from the program running IClient.
ストラテジの中からチャートオブジェクトをプロットできるだけではなく、IClient実行中のプログラムからも可能である。
Consider creating a button panel which receives an IChart from IClientGUI.getChart():
IClientGUI.getChart()からIChartを受信するボタンパネルの作成を検討して欲しい。 

@SuppressWarnings("serial")
private class ChartObjectPanel extends JPanel {
   
    private final IChart chart;
    private ChartObjectPanel(IChart chart){
        this.chart = chart;
        addButtons();
    }
   
    private void addButtons(){          
        JButton btnVLine = new JButton("Add VLine");
        btnVLine.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                //draw the line at the time of the last drawn feed element on the chart
                ITimedData[] chartData = chart.getLastRequestedData();
                long time = chartData[chartData.length - 1].getTime();
                IChartObject obj = chart.getChartObjectFactory().createVerticalLine("vLine", time);
                obj.setColor(Color.RED);
                chart.add(obj);
            }});
        add(btnVLine);          
        JButton btnHLine = new JButton("Add HLine");
        btnHLine.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {  
                //draw the line in the chart price axis midpoint
                double price = chart.getMinPrice() + (chart.getMaxPrice() - chart.getMinPrice()) / 2;
                System.out.println(String.format("%.5f", price));                  
                IChartObject obj = chart.getChartObjectFactory().createHorizontalLine("hLine", price);
                obj.setColor(Color.GREEN);
                chart.add(obj);
            }});
        add(btnHLine);
    }
   
}

Handle IContext.openChart

Handle IContext.openChart
IContext.openChartのハンドル

Consider a program which opens a chart whenever the strategy calls IContext.openChart and closes a chart whenever the strategy call IContext.closeChart:
ストラテジがIContext.openChartをコールするごとにチャートを開くか、ストラテジがIContext.closeChartをコールするごとにチャートを閉じるか、ということをプログラムとして検討して欲しい。

client.addClientGUIListener(new IClientGUIListener() {  
    @Override
    public void onOpenChart(final IClientGUI clientGUI) {
        LOGGER.info("Chart opened from a startegy " + clientGUI.getChart().getFeedDescriptor());
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ChartFrame frame = new ChartFrame(clientGUI, client.getSubscribedInstruments());
                chartFrameMap.put(clientGUI.getChart(), frame);
                //Handle manual close - we need to call IClient.closeChart for strategy to know that the chart is no more there
                frame.addWindowListener(new WindowAdapter(){
                    public void windowClosing(WindowEvent e) {
                        LOGGER.info("Chart manually closed, removing the chart from the strategy context");
                        client.closeChart(clientGUI.getChart());
                        updateOnClose(clientGUI.getChart());
                    }
                });
            }
        });
    }
 
    @Override
    public void onCloseChart(IChart chart) {
        LOGGER.info("Chart closed from a startegy " + chart.getFeedDescriptor());
        //we need to take care of closing the frame ourselves in gui
        ChartFrame frame = chartFrameMap.get(chart);
        frame.dispose();
        updateOnClose(chart);
    }
 
    private void updateOnClose(IChart chart){
        chartFrameMap.remove(chart);
        if(chartFrameMap.isEmpty()){
            LOGGER.info("All charts closed, stopping the program");
            System.exit(0);
        }
    }
});

2013年9月14日土曜日

Open a chart from IClient

Open a chart from IClient
IClientからチャートを開く。

Note: Available with JForex-API 2.7.1
注意:JForex-API 2.7.1として有効。
Consider opening multiple charts - for each instrument in an array instrArr:
複数チャートを開く場合を検討 - instrArr配列の中でfor each命令ループを使用。
for(Instrument instrument : instrArr){
    IFeedDescriptor feedDescriptor = new TicksFeedDescriptor(instrument);
    feedDescriptor.setOfferSide(OfferSide.BID);// need to set due to platform requirements
    IChart chart = client.openChart(feedDescriptor);
    final IClientGUI clientGUI = client.getClientGUI(chart);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            ChartFrame frame = new ChartFrame(clientGUI, client.getSubscribedInstruments());
            chartFrameMap.put(clientGUI.getChart(), frame);
            //Handle manual close - we need to call IClient.closeChart for strategy to know that the chart is no more there
            frame.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    LOGGER.info("Chart manually closed, removing the chart from the strategy context");
                    client.closeChart(clientGUI.getChart());
                    chartFrameMap.remove(clientGUI.getChart());
                    if(chartFrameMap.isEmpty()){
                        LOGGER.info("All charts closed, stopping the program");
                        System.exit(0);
                    }
                }
            });
        }
    });
}