algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
example-live-program.cpp File Reference

The live program to copy: KIS, Kiwoom or Toss, journalled, recoverable, stoppable. More...

#include "kis_credentials.hpp"
#include "kiwoom_credentials.hpp"
#include "live_program.hpp"
#include <algotradelib/kis>
#include <algotradelib/kiwoom>
#include <algotradelib/tossinvest>
#include <algotradelib/vanilla>
#include <boost/asio/ssl/context.hpp>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <filesystem>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
Include dependency graph for example-live-program.cpp:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 Chooses a broker from the command line and observes or trades through it.

Detailed Description

The live program to copy: KIS, Kiwoom or Toss, journalled, recoverable, stoppable.

example-live-program <kis|kiwoom|toss> <symbol> [--live] [--journal PATH] [--control PATH]
[--toss-account SEQUENCE] [--credentials DIR] [--market-data kis|toss]
[--kiwoom-account LABEL]

Without --live it observes: real market data, holdings and orders run through the real pipeline, guard and gates, and the send step goes to an executor that holds no session, so nothing can leave. With --live it trades, through a journal, after recovering whatever a previous run left owed.

Demo or live is the environment's, not this program's. KIS and Kiwoom are demo unless KIS_DEMO=0 / KIWOOM_DEMO=0 says live, beside the credentials they belong to. Toss has no paper-trading environment: toss --live trades real money.

KIS credentials and market data (kis_credentials.hpp). With a credentials directory (--credentials DIR, else ATL_CREDENTIALS_DIR) KIS reads DIR/kis/demo.json or real.json. A KIS demo host refuses reference data and the calendar (EGW02006), so on demo the market reads go to a second session that cannot trade:

  • --market-data kis, the default on demo when DIR/kis/real.json exists: the real account's keys with an empty KisAccount;
  • --market-data toss: Toss from DIR/tossinvest/real.json, with no account selected. The price band and reference data are then Toss's, which can differ from KIS's (after the close Toss publishes the next session's band, KIS keeps the day's): KIS may refuse an order clamped to Toss's band.

Without a directory, or without DIR/kis/real.json, the demo session reads its own market data, and stops at reference data. With kis/real.json the run reads reference data and the calendar through the real keys, so the market phase is known and, with --live, the strategy trades. Every KIS client here is paced at one request a second (kis_credentials::paced_options): KIS refuses faster reads (EGW00201), and the calendar's pages take about 20 s at startup. The Kiwoom client is paced the same way (kiwoom_credentials::paced_options): Kiwoom refuses a second request to the same API at once (1700), and a quote and a price-limit read are one API.

Kiwoom credentials (kiwoom_credentials.hpp). Kiwoom issues one key per account, so with a directory Kiwoom reads DIR/kiwoom/demo-<label>.json (or real-<label>.json with KIWOOM_DEMO=0), the label from --kiwoom-account (default kr). With a directory, the KIWOOM_* variables are not read; without one they are, and --kiwoom-account is refused.

Toss reads its environment variables, so --credentials given to it is refused rather than ignored.

The broker-free part – the order of journal, recovery, loop and report – is live_program.hpp, which the test suite drives against an in-memory session. This file only chooses a broker and a strategy. The strategy is deliberately trivial: BuyAndHold of one share. Replace it with yours, and its limits in settings_for; on KIS and Toss nothing else has to change. On Kiwoom two statements are yours to make before it can place (see run_kiwoom).

Definition in file example-live-program.cpp.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Chooses a broker from the command line and observes or trades through it.

Parameters
argcThe argument count.
argvThe arguments.
Returns
A live_program::Exit value, 64 for a usage error, or 70 if an exception escaped (what was in flight may then be unknown).

Definition at line 462 of file example-live-program.cpp.