4.5 C++ 言語 早いスタート

作者: リン・ハーン優しさ作成日:2019年4月30日 08:50:54 更新日:

概要

C++ は非常に難しいプログラミング言語です.難しい部分は主に深く学ぶことです.しかし,C++ で戦略論理を書くだけで,非常に複雑な戦略でない限り,多くの深い知識を必要としません.いくつかの基礎を学ぶだけで十分です.

img

なぜ C++ を量的な取引に使うのか?

C++を使用する人は,C++を使用する人が多い.C++を使用する人は,C++を使用する人が多い.C++を使用する人は,C++を使用する人が多い.C++を使用する人は,C++を使用する人が少ない.C++を使用する人は,C++を使用する人が少ない.C++を使用する人は,C++を使用する人が少ない.C++を使用する人は,C++を使用する人が少ない.C++を使用する人は,C++を使用する人は,C++を使用する人が少ない.C++を使用する人は,C++を使用する人が少ない.C++を使用する人は,C++を使用する人が少ない.C++を使用する人は,C++を使用する人が少ない.C++を使用する人は,C++を使用する人が少ない.C++を使用する人は,C++を使用する人が少ない.

しかし,定量投資機関では,基礎となる定量取引システムソフトウェアのほとんどは,独自の言語特性が他の言語よりも,特に数値計算において,いくつかの面で効率的で速くするため,C++で書かれています.これはまた,C++が金融派生品と高周波取引により適していることを意味します.

完全な戦略

このセクションの内容をより早く理解できるように,C++言語を導入する前に,C++が書いた戦略を見てみましょう.このセクションの名詞概念を予備的に理解できるように.最も簡単なMACD戦略を例に挙げましょう:

img

MACDには2つの曲線があり,それは速いラインとスローラインです.この2つのラインに基づいて取引論理を設計しましょう.速いラインがスローラインを横切ると,ロングポジションを開きます.速いラインがスローラインを横切ると,ショートポジションを開きます.完全な戦略論理は:

  • ロングポジションが開いている場合:現在ポジションがない場合,高速ラインがスローラインよりも大きい場合.

  • ショートポジションが開いている場合: 現時点ではポジションがない場合,高速ラインはスローラインより小さい場合.

  • ロングポジションを閉じる:現在ロングポジションを保持し,高速線がスロー線より小さい場合.

  • ショートポジションを閉じる:現在のショートポジションを保持し,高速ラインがスローラインよりも大きい場合.

C++言語を使って上記の戦略ロジックを書くと:

double position = 0;  //Position status parameter, the default position is 0
uint64_t lastSignalTime = 0; // Last signal trigger time

bool onTick(string symbol) { // onTick function, inside the function is the strategy logic
    auto ct = exchange.SetContractType(symbol); // set the trading variety
    if (ct == false) { // if setting the trading variety is not successful 
        return false; // return false
    }
    auto r = exchange.GetRecords(); // get the k line array
    if (!r.Valid || r.sizeO < 20) { // if get the k line array is not successful or the number of k line is less than 20 
        return false; // return false
    }
    auto signalTime = r[r.size() - 2].Time; // get the previous k line time
    if (signalTime <= lastSignalTime) { // if the previous k line time is less than or equal to the last trigger signal time
        return false; // return false
    }
    auto macd = TA.MACD(r); // calculate the MACD indicator
    auto slow = macd[0][macd[0].size() - 2]; // get the previous k line MACD value 
    auto fast = macd[l][macd[l].size() - 2]; // get the previous k line MACD average value
    string action; // define a string variable action
    if (fast >= slow && position <= 0) { // if the previous k line macd value is greater than or equal to the previous k line macd average value, and there are no long position holding
        action = "buy"; // assign buy to the variable action
    } else if (fast <= slow && position >= 0) { // if the previous k line macd value is less than or equal to the previous k line macd average value, and there are no short position holding
        action = "sell"; // assign sell to the variable action
    }
    if (actton.size() > 0) { // If there are orders for placing order
        If (position != 0) { // If there are holding position
            ext::Trade("cover", symbol); // call the C++ trading class library and close all position
        }
        position = ext::Trade(action, symbol, 1); // call the C++ trading class library, placing orders according the direction of variable "action", and renew the position status
        lastSignalTime = signalTime; // reset the time of last trigger signal
    }
    return true; // return true
}

void main() { // program starts from here
    while (true) { // enter the loop
        if (exchange.IO("status") == 0) { // if the connection with the exchange is not successful
            Sleep(1000); // pause for 1 second
            continue; // skip this loop, continue to the next loop
        }
        if (!onTtck("this_week")) { // if the connection is ok, enter the if loop and start to execute the onTick function
            Sleep(1000); // pause for 1 second
        }
    }
}

上記のコードは C++ で書かれた完全な定量的な取引戦略です. リアルマーケットで適用され,自動的に注文をします. コードサイズに関しては,他の言語よりも複雑です. C++ 言語は主にFMZ Quant プラットフォーム上の高周波戦略開発のために使用されます.

コードリングは以前よりも少し多くなりましたが,すでに多くの不必要なトレーディングクラスライブラリが削減され,基本システムレベルの処理の大半は FMZ Quant プラットフォームによってパッケージ化されています.

初心者にとって,戦略全体の設計プロセスは変わらず,市場多様性を設定し,K線データを取得し,ポジション情報を取得し,取引ロジックを計算し,オーダーを出す.

識別子

識別子は名前でもある. C++ の変数と関数名には小文字敏感性がある.つまり変数名テストと変数名テストは2つの異なる変数である.識別子の最初の文字は文字で,下記_で,次の文字は数字でもあり得る.

mohd    zara    abc    move_name    a_123
myname50    _temp    j    a23b9    retVal

コメント

コメントには,単行コメントとブロックレベルのコメントが含まれます.単行コメントは,以下のとおり,斜線と星号 (/* ) で始まり,星号と斜線 (*/ ) で終了する,二つの斜線で始まります:

// this is a single-line comment

/*
 * this is a multiple-line comment
 * block-level comments
 *
 */

半角点と宣言ブロック

C++では,セミコントンはステートメントのターミネーターである.つまり,各ステートメントはセミコントで終わらなければならない.これは論理的エンティティの終わりを示します.例えば,以下の3つの異なるステートメントがあります:

x = y;
y = y + 1;
add(x, y);

変数

変数とは,操作記憶領域である.C++で変数を定義するには,まず変数の種類を定義する必要があります.定量的な取引戦略の開発では,一般的に使用するタイプは:整数 (int ),浮遊物 (double ),文字列 (string) と自動派生型 (auto ).

整数は整数として理解できる.浮点点型は小数点を持つ数値として理解できる.文字列は文字文字であり,英語または他の言語文字である.

この API が何種類のデータを返すのかわからないとき,自動派生型 (auto) を使えば,自動的にデータ型を決定するのに役立ちます. 下のように:

int numbers = 10; // use int to define a integer variable and assign 10 to this variable
double PI = 3.14159; // use double to define a float variable and assign 10 to this variable
string name = "FMZ Quant"; // use string to define a string variable and assign "FMZ Quant" to this variable
auto bar = exchange.GetRecords(); // use auto to define a variable (automatic derivation type) and assign k line array to this variable

配列

配列は,データを保存するための容器である. C++配列は,一定のサイズを持つ同じタイプの要素の固定順序のコレクションを格納することができる. したがって,C++ で,配列を宣言するには,要素の種類と要素の数を指定する必要があります. すべての配列は最初の要素としてインデックスが0です.配列の最初のデータが"[0]",第2のデータは" [1]"で,次のように示します:

// define a array, array name is balance. there are 5 floating(double) type data inside it
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

double salary = balance[0];  // get the first data in the array, the result is : 1000.0
double salary = balance[1];  // get the second data in the array, the result is : 2.0

機能

函数とは,タスクを一緒に実行する命令の集合である.関数の宣言には,関数の名前,返却の種類,パラメータが含まれます.返却型は,この関数を呼び出すときに関数が実行されたときに返されるデータタイプです.パラメータはオプションで,関数はパラメータを含まないこともできます.関数が呼び出されるとき,関数にもパラメータを渡すことができます.以下の例を見てください:

// create a function called "max"
// when this function is called, it returns the int type data
// this function has 2 parameters, and they both are int type
// this function is for passing 2 int type numbers, and return the bigger one

int max(int num1, int num2) {
    int result; // define a int variable result
    if (num1 > num2) // if the num1 > num2
        result = num1; // assign the value of num1 to result
    else // otherwise
        result = num2; // assign the value of num2 to result
    return result; // return the value of result
}

オペレーター

C++ を用いて定量的な取引戦略を書き出すには,一般的に使用される演算子には,算術演算子,関係演算子,論理演算子,割り当て演算子が3つあります.算術演算子は,足し,引く,掛け,割る数学的演算子です.関係演算子は,2つの値が小さいか大きいかを比較することができます.論理演算子には主に:論理AND,論理OR,論理nonが含まれます.割り当て演算子とは,私たちが前に話した変数割り当てです.下記のように:

int main() {
    // arithmetic operator
    int a = 10;
    int b = 5;
    a + b; // the result is 15
    a - b; // the result is 5
    a * b; // the result is 50
    a / b; // the result is 2
    a % b; // the result is 0
    a++; // the result is 11
    a--; // the result is 9

    // relational operators
    a == b; // the result is false
    a != b; // the result is true
    a >= b; // the result is true
    a <= b; // the result is false

    logical operators
    true && true // the result is true
    true && false // the result is false
    false || false // the result is false
    true || false // the result is true
    !(true) // the result is false
    !(false) // the result is true
    return 0;
}

優先順位

100*(10-1)/(10+5) の表現がある場合,プログラムが最初に計算するステップはどれか? 中学校の数学は,同じレベルの操作である場合,通常は左から右に計算されます. 足し算,減算,掛け算,割り算がある場合は,まず掛け算,割り算を計算し,それから足し算,引算を行います. 括弧がある場合は,まず括弧の内側を計算します. 動作法則が満たされている場合は,計算法を使用して計算できます. C++ の上記の原理は,以下のように:

auto num = 100*(10-1)/(10+5); // the value of num is 60
1 > 2 && (2 > 3 || 3 < 5); // the result is : false
1 > 2 && 2 > 3 || 3 < 5; // the result is : true

条件付き声明

C++では,次の条件文を使用できます. C++では,次の条件文を使用します. C++では,次の条件文を使用します.

  • if ステートメント - このステートメントを使用して,指定条件が true であればのみコードを実行します.

  • 条件が false ならば実行されます. 条件が false ならば実行されます.

  • 実行する複数のコードブロックの1つを選択するためにこのコマンドを使用します.

  • スイッチ文 - この文を使用して実行する複数のコードブロックの 1 つを選択します

If 声明

この命令は,指定条件が真である場合にのみコードを実行します.小文字を if で使ってください.大文字 (IF) を使えば,C++ エラーが生じる! 下の図のように:

// grammar
if (condition) {
    //execute code only if the condition is true
}
 
//example
if (time<20) { // if current time is less than 20:00
    x = "Good day"; // when the time is less that 20:00, assign the "good day" to x
}

もし... 違うなら

指定された条件が真である場合実行コード,条件が偽である場合実行される他のコードは,以下のとおりです.

//grammar
if (condition) {
    // execute code if the condition is true
} else {
    // the other code executed when the condition is false
}
 
//example
if (time<20) { // if current time is less than 20:00
    x = "Good day"; // when the time is less that 20:00, assign the "good day" to x
} else { // otherwise
    x = "Good evening"; // assign the "Good evening" to x
}

スイッチ 宣言

このコマンドを使用して実行する複数のコードブロックの 1 つを選択します

switch (condition)
{
    case 1: // code to be executed if condition = 1;
        break;
    case 2: // code to be executed if condition = 2;
        break;
    default: // code to be executed if condition doesn't match any cases
}

ループについて

Forループはコードブロックの N 回を繰り返し実行することができ,実行フローは以下のように (以下のように示されています):

for (int a = 10; a < 20; a++){
    // code block
}
  • ステップ 1: int a = 0 を実行し,一度だけ実行する.その目的は,整数変数を宣言し, for ループを制御するために 0 に初期化することです.

  • ステップ2: a<20を実行します.もし true ならば,行2のコードブロックを実行します.

  • ステップ3:a++を実行します. a++を実行した後,aは11になります.

  • ステップ4: a<20を再び実行し,第2ステップ,第3ステップ,第4ステップは繰り返し実行されます. a<20が false になるまで,もし false ならば,行2のコードブロックは実行されず, for ループ全体が終了します.

ループの間

市場が常に変化していることは誰もが知っています. 最新のK線配列を取得したい場合は,常に同じコードを繰り返し実行する必要があります. 次に,ベストな選択は,whileループを使用します. 指定された条件が正しい限り,ループは最新のk線配列データを取得し続けます.

void main() {
    auto ct = exchange.SetContractType(symbol); //set the trading variety
    while(true) {
        auto r = exchange.GetRecords(); // constantly getting k-line arrays
    }
} 

断断の声明

ループには前提条件があります.この前提条件が"true"であるときのみ,ループは繰り返し何かを始め,前提条件が"false"になるまで,ループは終了します.しかし,break文を使用すると,ループの実行中にすぐにループから飛び出すことができます.

# including <iostream>
using namespace std;
int main() {
    for(int a = 0; a < 5; a++) {
        if(a == 2) break;
        cout << a << endl;
    }
    return 0;
}

// print out : 0, 1

声明を継続する

continue 命令もループから飛び出しますが,ループ全体から飛び出しません.代わりにループを中断して次のループに続きます.下記のとおり,a=2 のとき,ループは中断され,ループの前提条件が" false "になるまで次のループは続きます.

# including <iostream>
using namespace std;
int main() {
    for(int a = 0; a < 5; a++) {
        if(a == 2) continue;
        cout << a << endl;
    }
    return 0;
}

// print out : 0, 1, 3, 4

返品申告

return ステートメントは関数の実行を終了し,関数の値を返します. return ステートメントは関数のボディにのみ表示され,コードの他の場所では文法エラーが発生します!

# including <iostream>
using namespace std;

int add(int num1, int num2) {
    return num1 + num2;  // The add function returns the sum of two parameters
}
 
int main()
{
    cout << add(5, 10); // call the add function, and print out the result:50
    return 0;
}

戦略建築

FMZ Quant プラットフォームでは,C++ で戦略を書くことは非常に便利です. FMZ Quant には,多くの公式に組み込まれた標準戦略フレームワークとトレーディングクラスライブラリがあります.

bool onTick() { //onTick function
    // strategy logic
}

void main() { // program starts from here
    while (true) { // enter the loop
        if (exchange.IO("status") == 0) { // if the exchange connection is not stable
            sleep(1000); // pause for 1 second
            continue; // skip this loop, enter the next loop
        }
        if (!onTick()) { // if the exchange connection is stable, enter this if statement, start to execute the onTick function
            sleep(1000);// pause for 1 second
        }
    }
}

上記のように,これは標準戦略フレームワークであり,これらのフォーマットが固定されています. 戦略を書くフレームワークを使用します. 戦略ロジックを2行目から書くだけです. その他の市場取得とオーダー処理はフレームワークとトレーディングクラスのライブラリによって処理されます. 戦略開発に集中する必要があります.

結論から言うと

上記は,C++言語のクイックスタートの内容です.より複雑な戦略を書く必要がある場合は,FMZ QuantプラットフォームのC++言語APIドキュメントを参照してください.

次のセクションの通知

定量的な取引の鍵は,取引ツール (プログラミング言語) ではなく,取引戦略です. 次のセクションでは,実行可能なC++の取引戦略を書こう.

放課後 の 運動

  • C++言語を使って FMZ Quantプラットフォームで 歴史的なK線データを入手してみてください
  • このセクションの初めに戦略コードを書き,その後に特定のコメントを書いてください.

もっと