偏差率 BIAS トレーディング戦略

作者: リン・ハーン優しさ, 作成日: 2020-06-30 09:58:26, 更新日: 2023-10-28 15:29:08

[TOC]

img

概要

この世界には,長時間結束した後に分離する.また,長時間分裂した後に逆のことをする.そして,この現象は先物市場でも存在する.上昇するだけだが落ちない多様性はない.しかし,いつ上昇するか,いつ落ちるか,それは偏差率に依存する.この記事では,我々は単純な取引戦略を構築するために偏差率を使用します.

簡潔な紹介

img

偏差率BIASは,移動平均から派生した技術指標である.これは主に変動における移動平均から価格偏差の程度を測定するための割合形式である.移動平均がトレーダーの平均コストである場合,偏差率はトレーダーの平均収益率である.

偏差率の原則

偏差率の理論的基礎はトレーダーの心分析である.価格が市場の平均コストよりも高ければ,ロングポジショントレーダーは利益を現金化するという考えを持ち,価格が下がる.価格が市場の平均コストよりも低い場合,ショートセラーが利益を得ていることを意味し,利益を現金化するという考えが価格の上昇を引き起こすことを意味します.

  • 価格が移動平均値から上向きに偏ると,偏差率は大きすぎるので,将来的に価格が下がる可能性が高い.

  • 価格が移動平均値から下向きに偏ると,偏差率は小さすぎるので,将来的に価格が上昇する確率は高い.

移動平均は価格から計算されるが,外的な形態では,価格は間違いなく移動平均に近づくか,または価格は常に移動平均の周りに変動する.価格が移動平均からあまりにも遠く離れている場合,価格が移動平均以上または以下であるかどうかにかかわらず,最終的には移動平均に傾き,偏差率は価格が移動平均から逸脱する百分比値である.

偏差率を計算する式

偏差率 = [ (日の閉値 - N 日間の平均価格) / N 日間の平均価格] * 100%

その中でも,Nは移動平均パラメータであり,Nの期間が異なるため,偏差率の計算結果も異なる.一般的には,Nの値は:6,12,24,36などである.実際の使用では,異なる種類に応じて動的に調整することもできる.しかし,パラメータの選択は非常に重要です.パラメータが小さすぎれば,偏差率は過度に敏感になり,パラメータが大きすぎれば,偏差率は過度に遅くなる.偏差率の計算結果は正と負である.正偏差率が大きくなるほど,牛の利益は大きくなり,価格の訂正の確率も大きい.偏差率が大きくなるほど,利益は小さくなり,価格のリバウンドの確率も大きい.

戦略の論理

偏差率は移動平均の別の形態であるため,両移動平均戦略に基づいた二重偏差率戦略も適応できます.短期偏差率と長期偏差率の位置関係から判断すると,現在の市場状態が判断されます.長期偏差率が短期偏差率よりも大きい場合,それは実際には長期移動平均を横断した短期移動平均を表しています.

  • 長期ポジション開設:現在の保有ポジションがない場合,長期偏差率は短期偏差率より大きい場合
  • ショートポジション開設:現在のホールディングポジションがない場合,長期偏差率は短期偏差率より低い場合
  • 長期ポジションの閉じる:長期ポジションを保持し,長期偏差率は短期偏差率より低い場合
  • 負債の負債の負債と負債の負債の負債の負債の負債の負債

戦略の書き込み

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

# Strategy main function
def onTick():
     pass


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

FMZプラットフォームは,ローテーション訓練モードを採用します.main機能とonTick基本関数は,戦略の入力関数であり,プログラムはメイン関数から開始してコードを行ごとに実行します.メイン関数では,whileループを繰り返して実行しますonTick戦略のコアコードは全てonTick function.

ステップ2:仮想位置を定義する

mp = 0

仮想ポジションの利点は,書き込みが簡単で,繰り返しの操作が速いことである.通常バックテスト環境で使用される.すべてのオーダーが完全に満たされていると仮定するが,実際のポジションは通常実際の取引で使用される.仮想ポジションは開閉後の状態を記録するため,グローバル変数として定義する必要がある.

ステップ3:K線を取得

exchange.SetContractType('rb000') # Subscribe to futures varieties
bars_arr = exchange.GetRecords() # Get K-line array
if len(bars_arr) <long + 1: # If the number of K lines is too small
     return

FMZ関数を使用するSetContractTypeしかし,バックテストと実際の市場状況では,レバーインデックスはデータとして使用され,特定のメインコントラクトは注文をするために使用されます.GetRecordsリバーインデックスのK線データを取得する関数です.偏差率を計算するのに一定の時間がかかるため,プログラムエラーを避けるために,十分なK線がない場合,ifフィルタリングする命令です

ステップ4:偏差率を計算する

close = bars_arr[-2]['Close'] # Get the closing price of the previous K line
ma1 = TA.MA(bars_arr, short)[-2] # Calculate the short-term moving average value of the previous K line
bias1 = (close-ma1) / ma1 * 100 # Calculate the short-term deviation rate value
ma2 = TA.MA(bars_arr, long)[-2] # Calculate the long-term average of the previous K line
bias2 = (close-ma2) / ma2 * 100 # Calculate the long-term deviation rate value

偏差率を計算する式によると,まず閉値を得ます.この戦略では,前回のK線閉値を使用します.つまり,現在のK線信号が確立され,次のK線は注文の配置です.その後,FMZ内蔵を使用します.talib移動平均を計算するライブラリです 例えば移動平均はTA.MAこの関数は2つのパラメータを受け取ります. K線配列と移動平均周期です.

ステップ 5: 注文の提出

global mp # global variables
current_price = bars_arr[-1]['Close'] # latest price
if mp> 0: # If you are holding long positions
    if bias2 <= bias1: # If the long-term deviation rate is less than or equal to the short-term deviation rate
        exchange.SetDirection("closebuy") # Set the trading direction and type
        exchange.Sell(current_price-1, 1) # Closing long positions
        mp = 0 # reset virtual holding positions
if mp <0: # If you are holding short positions
    if bias2 >= bias1: # If the long-term deviation rate is greater than or equal to the short-term deviation rate
        exchange.SetDirection("closesell") # Set the trading direction and type
        exchange.Buy(current_price + 1, 1) # closing short positions
        mp = 0 # reset virtual holding positions
if mp == 0: # If there is no holding position
    if bias2> bias1: # Long-term deviation rate is greater than short-term deviation rate
        exchange.SetDirection("buy") # Set the trading direction and type
        exchange.Buy(current_price + 1, 1) # open long positions
        mp = 1 # reset virtual holding position
    if bias2 <bias1: # The long-term deviation rate is less than the short-term deviation rate
        exchange.SetDirection("sell") # Set the trading direction and type
        exchange.Sell(current_price-1, 1) # open short positions
        mp = -1 # reset virtual holding position

完全な戦略

# Backtest configuration
'''backtest
start: 2018-01-01 00:00:00
end: 2020-01-01 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
'''

# External parameters
short = 10
long = 50


# Global variables
mp = 0


# 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) <long + 1: # If the number of K lines is too small
        return

    # Calculate BIAS
    close = bars_arr[-2]['Close'] # Get the closing price of the previous K line
    ma1 = TA.MA(bars_arr, short)[-2] # Calculate the short-term moving average of the previous K line
    bias1 = (close-ma1) / ma1 * 100 # Calculate the short-term deviation rate value
    ma2 = TA.MA(bars_arr, long)[-2] # Calculate the long-term average of the previous K line
    bias2 = (close-ma2) / ma2 * 100 # Calculate the long-term deviation rate value

    # Placing Orders
    global mp # global variables
    current_price = bars_arr[-1]['Close'] # latest price
    if mp> 0: # If you are holding long positions
        if bias2 <= bias1: # If the long-term deviation rate is less than or equal to the short-term deviation rate
            exchange.SetDirection("closebuy") # Set the trading direction and type
            exchange.Sell(current_price-1, 1) # closing long positions
            mp = 0 # reset virtual holding position
    if mp <0: # If you are holding short positions
        if bias2 >= bias1: # If the long-term deviation rate is greater than or equal to the short-term deviation rate
            exchange.SetDirection("closesell") # Set the trading direction and type
            exchange.Buy(current_price + 1, 1) # closing short positions
            mp = 0 # reset virtual holding position
    if mp == 0: # If there is no holding position
        if bias2> bias1: # Long-term deviation rate is greater than short-term deviation rate
            exchange.SetDirection("buy") # Set the trading direction and type
            exchange.Buy(current_price + 1, 1) # opening long positions
            mp = 1 # reset virtual holding position
        if bias2 <bias1: # The long-term deviation rate is less than the short-term deviation rate
            exchange.SetDirection("sell") # Set the trading direction and type
            exchange.Sell(current_price-1, 1) # open short positions
            mp = -1 # reset virtual holding position
        

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

戦略の全文はFMZのウェブサイトで公開されています.https://www.fmz.com/strategy/215129

戦略のバックテスト

バックテスト設定

img

業績報告

img img

資金曲線

img

概要

デバイエーションレートは,トレーダーにとって効果的な参照を提供できるシンプルで効果的な取引ツールです.実際の使用では,その価値を真に反映するためにMACDおよびボリンジャー帯指標と柔軟に適用できます.


関連性

もっと