クロコディールライン取引システム Python版

作者: リン・ハーン優しさ, 作成日:2020-05-07 14:33:19, 更新日:2023-11-06 19:40:42

img

概要

金融取引をした人はおそらく経験があるでしょう. 時々価格変動は規則的ですが,より頻繁にランダムなウォークの不安定な状態を示します. 市場のリスクと機会が隠されているのはこの不安定性です. 不安定性は予測不可能なことも意味します. そのため,予測不可能な市場環境で収益をより安定させる方法もすべてのトレーダーにとって問題です. この記事では,すべての人にインスピレーションを与えることを望んで,ワニ取引ルール戦略を紹介します.

クロコディールラインとは

img

クロコディール線は,実は3つの特別な移動平均線で,青い線の下巴,赤い線の歯,緑色の線の上唇に対応しています.下巴は13期移動平均線で,未来に8バー移動します.歯は8期移動平均線で,将来に5バー移動します.上唇は5期移動平均線で,将来に3バー移動します.

クロコディールラインの原理

クロコディールラインは,幾何学と非線形動力学に基づいて概括された技術分析方法の集合である. クロコディルの,歯,上唇が閉ざされたり巻き込まれたりすると,クロコディールは眠っていることを意味します.この時点で,私たちは通常,断片が現れるまで市場の外にとどまり,明らかなトレンド市場にのみ参加します.

ワニが眠る時間が長くなるほど,目が覚めたら腹が減るので,目が覚めたら口を大きく開く.上唇が歯の上にあり,歯がの上にあり,市場が牛市場に入ったことを示し,ワニは牛肉を食べる.上唇が歯の下にあり,歯がの下にあるならば,市場が熊市場に入ったことを示し,ワニは熊肉を食べる.満腹するまで,再び口を閉じる (保持して利益を得る).

クロコディール線計算式

上唇 = REF ((SMA ((VAR1,5,1),3) 歯 = REF ((SMA ((VAR1,8,1),5) シーン = REF ((SMA ((VAR1,13,1)

クロコジルの戦略組成

ステップ1: 戦略の枠組みを作成する

# Strategy main function
def onTick():
     pass

# Program entry
def main ():
     while True: # Enter infinite loop mode
         onTick() # execute strategy main function
         Sleep(1000) # sleep for 1 second

FMZ 投票モードを使用すると,一つは onTick関数で,もう一つはメイン関数で,この onTick関数はメイン関数で無限ループで実行されます.

ステップ2: Python ライブラリをインポートする

import talib
import numpy as np

SMA関数は戦略で使われます. SMAは算術平均値です. タリブライブラリには既に準備済みの SMA関数がありますので,タリブ Python ライブラリを直接インポートして直接呼び出してください. この関数を呼び出すとき,numpy フォーマットパラメータをパスする必要があります. したがって,戦略の初めにこれらの2つの Python ライブラリをインポートするためにインポートを使用する必要があります.

ステップ3:K線配列データを変換する

# Convert the K-line array into an array of highest price, lowest price, and closing price, for conversion to numpy.array
def get_data(bars):
    arr = []
    for i in bars:
        arr.append(i['Close'])
    return arr

ここでは get_data 関数を作成しました.この関数の目的は,通常の K 線配列を numpy 形式のデータに処理することです.入力パラメータは K 線配列で,出力結果は numpy 形式の処理データです.

ステップ4:位置データを取得

# Get the number of positions
def get_position ():
     # Get position
     position = 0 # The number of assigned positions is 0
     position_arr = _C (exchange.GetPosition) # Get array of positions
     if len (position_arr)> 0: # If the position array length is greater than 0
         for i in position_arr:
             if i ['ContractType'] == 'rb000': # If the position symbol is equal to the subscription symbol
                 if i ['Type']% 2 == 0: # If it is long position
                     position = i ['Amount'] # Assigning a positive number of positions
                 else:
                     position = -i ['Amount'] # Assigning a negative number of positions
     return position

ポジションステータスには戦略論理が含まれます.最初の10レッスンでは常に仮想ポジションを使用していますが,実際の取引環境では,ポジション方向,ポジション利益と損失,ポジション数などを含む実際のポジション情報を得るためにGetPosition関数を使用することが最善です.

ステップ5:データを取得

exchange.SetContractType('rb000') # Subscribe the futures varieties
     bars_arr = exchange.GetRecords() # Get K line array
     if len(bars_arr) < 22: # If the number of K lines is less than 22
         return

データを取得する前に,まず SetContractType 関数を使用して関連する先物種にサブスクリプトする必要があります. FMZ はすべての中国の商品先物種をサポートします.先物符号にサブスクリプトした後,GetRecords 関数を使用してK線データを取得できます.この関数は配列を返します.

ステップ 6: データ を 計算 する

np_arr = np.array (get_data (bars_arr)) # Convert closing price array
sma13 = talib.SMA (np_arr, 130) [-9] # chin
sma8 = talib.SMA (np_arr, 80) [-6] # teeth
sma5 = talib.SMA (np_arr, 50) [-4] # upper lip
current_price = bars_arr [-1] ['Close'] # latest price

タリブライブラリを使用してSMAを計算する前に,通常のK線配列をnumpyデータに処理するためにnumpyライブラリを使用する必要があります. その後,ワニラインの下巴,歯,上唇を別々に取得します. さらに,注文をするときに価格パラメータを入力する必要があります.

ステップ7 注文する

position = get_position ()
if position == 0: # If there is no position
     if current_price> sma5: # If the current price is greater than the upper lip
         exchange.SetDirection ("buy") # Set the trading direction and type
         exchange.Buy (current_price + 1, 1) # open long position order
     if current_price <sma13: # If the current price is less than the chin
         exchange.SetDirection ("sell") # Set the trading direction and type
         exchange.Sell (current_price-1, 1) # open short position order
    
if position> 0: # If you have long positions
     if current_price <sma8: # If the current price is less than teeth
         exchange.SetDirection ("closebuy") # Set the trading direction and type
         exchange.Sell (current_price-1, 1) # close long position

if position <0: # If you have short position
     if current_price> sma8: # If the current price is greater than the tooth
         exchange.SetDirection ("closesell") # Set the trading direction and type
         exchange.Buy (current_price + 1, 1) # close short position

オーダーを出す前に,実際のポジションを取得する必要があります.先ほど定義した get_position 関数は実際のポジションの数を返します.現在のポジションが長ければ,正の数を返します.現在のポジションが短ければ,負の数を返します.ポジションがない場合は,0を返します.最後に,上記の取引論理に従って注文を入れるには,買い売り関数を使用しますが,それ以前に,取引方向とタイプも設定する必要があります.

完全な戦略

'' 'backtest
start: 2019-01-01 00:00:00
end: 2020-01-01 00:00:00
period: 1h
exchanges: [{"eid": "Futures_CTP", "currency": "FUTURES"}]
'' '

import talib
import numpy as np


# Convert the K-line array into an array of highest price, lowest price, and closing price, used to convert to numpy.array type data
def get_data (bars):
    arr = []
    for i in bars:
        arr.append (i ['Close'])
    return arr


# Get the number of positions
def get_position ():
    # Get position
    position = 0 # The number of assigned positions is 0
    position_arr = _C (exchange.GetPosition) # Get array of positions
    if len (position_arr)> 0: # If the position array length is greater than 0
        for i in position_arr:
            if i ['ContractType'] == 'rb000': # If the position symbol is equal to the subscription symbol
                if i ['Type']% 2 == 0: # If it is long
                    position = i ['Amount'] # Assign a positive number of positions
                else:
                    position = -i ['Amount'] # Assign a negative number of positions
    return position
    
    

# Strategy main function
def onTick ():
    # retrieve data
    exchange.SetContractType ('rb000') # Subscribe to futures varieties
    bars_arr = exchange.GetRecords () # Get K line array
    if len (bars_arr) <22: # If the number of K lines is less than 22
        return
    
    # Calculation
    np_arr = np.array (get_data (bars_arr)) # Convert closing price array
    sma13 = talib.SMA (np_arr, 130) [-9] # chin
    sma8 = talib.SMA (np_arr, 80) [-6] # teeth
    sma5 = talib.SMA (np_arr, 50) [-4] # upper lip
    current_price = bars_arr [-1] ['Close'] # latest price

    position = get_position ()
    if position == 0: # If there is no position
        if current_price> sma5: # If the current price is greater than the upper lip
            exchange.SetDirection ("buy") # Set the trading direction and type
            exchange.Buy (current_price + 1, 1) # open long position order
        if current_price <sma13: # If the current price is less than the chin
            exchange.SetDirection ("sell") # Set the trading direction and type
            exchange.Sell (current_price-1, 1) # open short position order
        
    if position> 0: # If you have long positions
        if current_price <sma8: # If the current price is less than teeth
            exchange.SetDirection ("closebuy") # Set the trading direction and type
            exchange.Sell (current_price-1, 1) # close long position

    if position <0: # If you have short positions
        if current_price> sma8: # If the current price is greater than the tooth
            exchange.SetDirection ("closesell") # Set the trading direction and type
            exchange.Buy (current_price + 1, 1) # close short position

            
# Program main function
def main ():
    while True: # loop
        onTick () # execution strategy main function
        Sleep (1000) # sleep for 1 second

設定なしで完全な戦略をコピーするには,下記のリンクを直接クリックしてください:https://www.fmz.com/strategy/199025

終わり

クロコディール・トレードルールの最大の役割は,現在の市場価格の変化に関係なく,取引時に市場と同じ方向を維持し,統合市場が現れるまで利益を得続けることを助けることです. クロコディールラインは,他のMACDおよびKDJ指標とうまく使用できます.


関連性

もっと