2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【QuickFIX】14 口座情報要求 < AN > RequestForPositions

Last updated at Posted at 2017-05-11

口座情報の取得を < AN > RequestForPositions で要求します

send_AN_RequestForPositions.cpp
#include "config.h"

#include "Application.h"
#include "quickfix/Session.h"
#include <iostream>

/* AN */
void Application::RequestForPositions(
    /* 715  */ const std::string& clearingBusinessDate
  , /* 724  */ const int reqType
)
{
  FIX44::RequestForPositions message;

  /* 710  */ message.set( FIX::PosReqID( "PosReq_" + YmdHMSs() ));
  /* 263  */ message.set( FIX::SubscriptionRequestType(FIX::SubscriptionRequestType_SNAPSHOT_PLUS_UPDATES /* 1 */ ) );
  /* 724  */ message.set( FIX::PosReqType( reqType )); /* 0 Position, 1 Trade */
  /*              0=Position: < AP > Position Report
   *              1=Trades: All Margin Info, including
   *                        < AZ > Collateral Response,
   *                        < AP > Position Report,
   *                        < BA > Collateral Report,
   *                        < CG > Party Details List
   *
   */

  /* 453  */ FIX44::RequestForPositions::NoPartyIDs parties;
  /* 448  */ parties.set(FIX::PartyID( m_partyID ));
  /* 452  */ parties.set(FIX::PartyRole( FIX::PartyRole_CLIENT_ID /* 3 */ ));
  message.addGroup( parties );

  /* 1    */ message.set( FIX::Account( m_accountID ));
  /* 581  */ message.set( FIX::AccountType( FIX::AccountType_ACCOUNT_IS_CARRIED_ON_CUSTOMER_SIDE_OF_BOOKS /* 1 */ ));
  /* 60   */ message.set( FIX::TransactTime() );

  /* Clearing Business Date :: format YYYYMMDD-HH:mm:ss */
  if ( clearingBusinessDate != "" )
  {
    LOG_DEBUG   << "clearingBusinessDate: " << clearingBusinessDate;
    /* 715  */ message.set( FIX::ClearingBusinessDate( clearingBusinessDate.substr(0,8) )); // + "-00:00:00"));
  }
  else
  {
    /* 715  */ message.set( FIX::ClearingBusinessDate( YmdHMS("%Y%m%d") ));
  }

  /* Insert T_BALANCE ::  */
  std::string s1 = "INSERT INTO  `T_BALANCE` (`Account`) VALUES ('"+  m_accountID +"')";
  FIX::MySQLQuery q1( s1 );
  if( m_sql->execute( q1 ) != true )
  {
    LOG_WARNING << "INSERT ERROR T_BALANCE :" << q1.reason() << std::endl << s1;
  }
  else
  {
    LOG_VERBOSE << "INSERT T_BALANCE : " << q1.reason() << std::endl << s1;
  }

  /* send message */
  SetMessageHeader( message );
  FIX::Session::sendToTarget( message );

//  std::cout << "<AN> RequestforPositions: " << std::endl << message.toXML() << std::endl;
  LOG_DEBUG   << "<AN> RequestforPositions: " << std::endl << message;
  LOG_VERBOSE << "<AN> RequestforPositions: " << std::endl << message.toXML();
}

引数でいつからの情報を取得するのかという日付を受け取れるようにしときます。
あと、取得タイプ。

日付は19日以内?しか指定できないようです
取得タイプ 0 は現在のポジション情報のみ
1 で口座情報もとれるので、デフォルトは1を指定しときます。

口座情報を受け取った後のDB格納用に基本情報を INSERT しておいてます

Application.h

引数情報を変更したので、ヘッダーも編集
あと、日付を取得する処理と残高をカンマ区切りで表示するための処理も追加したのでそこも変更

Application.h
・・・
  /* AF */ void OrderMassStatusRequest();
  /* AN */ void RequestForPositions(
                    const std::string& clearingBusinessDate = ""
                  , const int reqType = FIX::PosReqType_TRADES  /* 0=Position  1=Trade */
                );

・・・
  std::string YmdHMS( const char* = "%Y%m%d-%H%M%S" );
  std::string numFormat(const long);

Application.cpp

Application.cpp
・・・
std::string Application::YmdHMS( const char* format )
{
  FIX::UtcTimeStamp time;
  struct tm aa = time.getTmUtc();
  char buf[ 100 ];
  std::strftime(buf, sizeof(buf), format, &aa);
  return buf;
}

std::string Application::numFormat( const long num )
{
    std::vector<long>    sepnum;
    long number = abs(num);
    long sgn = num >= 0 ? 1 : -1;

    while ( number / 1000 ) {
        sepnum.push_back(number % 1000);
        number /= 1000;
    }

    std::stringstream  ss;
    ss << number * sgn;
    for ( std::vector<long>::reverse_iterator i = sepnum.rbegin();
                                        i < sepnum.rend(); i++ ) {
        ss << "," << std::setfill('0') << std::setw(3) << *i;
    }
    return std::string(ss.str());
}

日付を取得する処理と残高をカンマ区切りで表示するための処理の実態を定義

DBにテーブル追加

口座情報を保存するためのテーブルも作成

T_BALANCE
DROP TABLE IF EXISTS T_BALANCE;
CREATE TABLE T_BALANCE
(
  `Account`               VARCHAR(32)       NOT NULL                       COMMENT '1    Account ID',
  `TransactTime`          CHAR(17)          NULL                           COMMENT '60   yyyymmdd-hh:mm:ss',
  `Currency`              CHAR(3)           NULL                           COMMENT '15  ',
  `MarginRatio`           REAL              NULL                           COMMENT '898  Account equity ratio',
  `StartCash`             INT UNSIGNED      NULL                           COMMENT '921  current trading day opening balance',
  `EndCash`               INT UNSIGNED      NULL                           COMMENT '922  current balance max 40億',

  `Created_at`            DATETIME          NOT NULL  DEFAULT CURRENT_TIMESTAMP,
  `Updated_at`            DATETIME          NOT NULL  DEFAULT CURRENT_TIMESTAMP   ON UPDATE CURRENT_TIMESTAMP,
  `Deleted_at`            DATETIME          NULL,
  PRIMARY KEY (`Account`)
)
COMMENT 'Balance';

DESC T_BALANCE;

口座情報の履歴も保存

T_BALANCE_HIST
DROP TABLE IF EXISTS T_BALANCE_HIST;
CREATE TABLE T_BALANCE_HIST
(
  `id`                    INT UNSIGNED      NOT NULL  AUTO_INCREMENT       COMMENT 'id',

  `Account`               VARCHAR(32)       NOT NULL                       COMMENT '1    Account ID',
  `Result`                CHAR(1)           NOT NULL                       COMMENT '905  1:Accepted  3:Reject',
  `TransactTime`          CHAR(17)          NOT NULL                       COMMENT '60   yyyymmdd-hh:mm:ss',
  `TransactDay`           CHAR(8)           NOT NULL                       COMMENT '60   yyyymmdd',
  `FinansialStatus`       CHAR(1)           NOT NULL                       COMMENT '291  1=Disabled  3:Active',
  `Currency`              CHAR(3)           NULL                           COMMENT '15  ',
  `MarginRatio`           REAL              NULL                           COMMENT '898  Account equity ratio',
  `StartCash`             INT UNSIGNED      NULL                           COMMENT '921  current trading day opening balance',
  `EndCash`               INT UNSIGNED      NULL                           COMMENT '922  current balance max 40億',

  `Created_at`            DATETIME          NOT NULL  DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  INDEX `I_BALANCE_HIST_Day` (`TransactDay`)
)
COMMENT 'Balance History';

DESC T_BALANCE_HIST;


<前 【QuickFIX】13 マーケット情報受信 < X > MarketDataIncrementalRefresh
次> 【QuickFIX】15 口座情報取得 < AZ > CollateralResponse


一覧

01 サンプルのコンパイル
02 ログイン時にPassword(554)を送信
03 送受信ログをMySQLに保存
04 情報保存用にMySQLコネクションを保持..他
05 各種メッセージの枠を作成
06 独自メッセージ仕様
07 セッション開始 TradingSessionStatus
08 通貨ペア要求 SecurityListRequest
09 通貨ペア取得 SecurityList
10 デモ環境サーバへ接続
11 ログ出力設定
12 マーケット情報要求 < V > MarketDataRequest
13 マーケット情報受信 < X > MarketDataIncrementalRefresh
14 口座情報要求 < AN > RequestForPositions
15 口座情報取得 < AZ > CollateralResponse


2
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?