All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xeiam.xchange.examples.btcchina.fix.BTCChinaClient Maven / Gradle / Ivy

Go to download

Provides a set of examples that demonstrate how to use XChange in client applications

The newest version!
package com.xeiam.xchange.examples.btcchina.fix;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

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

import com.xeiam.xchange.btcchina.service.fix.BTCChinaApplication;
import com.xeiam.xchange.btcchina.service.fix.fix44.BTCChinaMessageFactory;
import com.xeiam.xchange.currency.CurrencyPair;
import com.xeiam.xchange.dto.account.AccountInfo;
import com.xeiam.xchange.dto.marketdata.Ticker;

import quickfix.ConfigError;
import quickfix.DoNotSend;
import quickfix.FileLogFactory;
import quickfix.FileStoreFactory;
import quickfix.Initiator;
import quickfix.LogFactory;
import quickfix.MessageFactory;
import quickfix.MessageStoreFactory;
import quickfix.SessionID;
import quickfix.SessionNotFound;
import quickfix.SessionSettings;
import quickfix.SocketInitiator;

public class BTCChinaClient {

  private static final Logger log = LoggerFactory.getLogger(BTCChinaClient.class);

  public static void main(String args[]) throws ConfigError, DoNotSend, IOException, SessionNotFound, InterruptedException {

    final String accessKey = args[0];
    final String secretKey = args[1];

    BTCChinaApplication app = new BTCChinaApplication() {
      @Override
      public void onLogon(quickfix.SessionID sessionId) {
        super.onLogon(sessionId);
        this.requestSnapshot(CurrencyPair.BTC_CNY, sessionId);
        this.requestSnapshotAndUpdates(CurrencyPair.BTC_CNY, sessionId);
      }

      @Override
      protected void onTicker(Ticker ticker) {

        log.info("ticker: {}", ticker);
      }

      @Override
      protected void onAccountInfo(String accReqId, AccountInfo accountInfo) {

        log.info("accReqId: {}, accountInfo: {}", accReqId, accountInfo);
      };

    };

    InputStream inputStream = BTCChinaClient.class.getResourceAsStream("client.cfg");
    SessionSettings settings = new SessionSettings(inputStream);
    MessageStoreFactory storeFactory = new FileStoreFactory(settings);
    LogFactory logFactory = new FileLogFactory(settings);
    MessageFactory messageFactory = new BTCChinaMessageFactory();
    Initiator initiator = new SocketInitiator(app, storeFactory, settings, logFactory, messageFactory);
    initiator.start();

    while (!initiator.isLoggedOn()) {
      TimeUnit.SECONDS.sleep(1);
    }

    log.info("logged on.");
    SessionID sessionId = initiator.getSessions().get(0);

    // account info request: U1000
    app.requestAccountInfo(accessKey, secretKey, UUID.randomUUID().toString(), sessionId);
    log.info("account info request sent.");

    TimeUnit.SECONDS.sleep(30);
    log.info("exiting...");
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy