
これは,移動平均線に基づく突破取引戦略である.それは,一定の周期の平均価格を平均線として計算し,価格が平均線を破るときに取引シグナルを生成する.
この戦略は主に移動平均線指標に基づいています.それはsma関数を使用して,一定の周期内の平均閉店価格を計算し,移動平均線を得ます.最新の閉店価格が移動平均線を下から上へと突破すると,買入シグナルを生成し,最新の閉店価格が移動平均線を上から下へと突破すると,売り出します.
具体的には,移動平均の計算源 ((近期閉店価格) と周期長を策略で定義し,移動平均線データのシーケンスを得ている.それから,この策略は2つの条件を設定する:価格が平均線を上越したときに買取注文を作成し,価格が平均線を下越したときに売れ注文を作成する.注文が作成された後,また,ストップロスを設定する. 注文が利益に達したときにポジションの一部を平定し,注文がセットストップまたはストップ損失に達したときにポジションを平定する.
これはシンプルで実用的なトレンド追跡策で,以下の利点があります.
この戦略には多くの利点がありますが,いくつかのリスクもあります.
これらのリスクを制御するために,他の指標と組み合わせたフィルタリングを最適化したり,大幅の短期トレンド判断を導入したり,機械学習の方法を使用して最適なパラメータの組み合わせを探したりすることができます.
この戦略は,以下のような点で最適化できます.
他の技術指標判断を追加し,取引システムを構成し,戦略勝利率を向上させる.例えば,MACD,KDなどの補助判断指標を追加する.
ストップ・メカニズムの加入. 追跡ストップまたはタイムストップを使用して,利益をロックし,損失を拡大しないようにする.
参数最適化を行う.移動平均線の周期参数を変えて,最適な参数組み合わせを見つける.また,異なるタイプの移動平均線をテストすることもできる.
機械学習判断の向上. ランダムフォレスト,LSTMなどのアルゴリズムを使用して,複数の要因を組み合わせてトレンドの方向を判断する.
エグゼット・イン・エクゼット・ロジックを最適化する.トレンドフィルター条件を設定し,トレンドの終了時に逆操作を避ける.分量平仓ロジックの使用を検討する.
この移動均線突破戦略は,全体として,量化取引の入門戦略として非常に適しています. それは,考え方がシンプルで,理解しやすく,操作し,実戦的な効果があります. また,後続のテストと最適化のための大きな余地があります. 我々は,この基礎で,より多くの技術指標とモデルを導入し,より効果的な量化戦略を開発することができます.
/*backtest
start: 2023-11-20 00:00:00
end: 2023-11-22 08:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
// |-- Initialize Strategy Parameters:
strategy(
// |-- Strategy Title.
title='[Tutorial][RS]Working with orders',
// |-- if shorttitle is specified, it will overwrite the name on the chart window.
shorttitle='WwO',
// |-- if true it overlays current chart window, otherwise it creates a drawer to display plotting outputs.
overlay=true,
// |-- Strategy unit type for default quantity, possible arguments: (strategy.cash, strategy.fixed, strategy.percent_of_equity)
default_qty_type=strategy.cash,
// |-- Value to use for default trade size
default_qty_value=1000,
// |-- Default Account size
initial_capital=100000,
// |-- Account Currency parameter
currency=currency.USD
)
// |-- Strategy Profit/loss parameters:
profit = input(defval=5000, title='Take Profit')
loss = input(defval=5000, title='Stop Loss')
ratio = input(defval=2.0, title='Ratio at wich to take out a percentage off the table (take profit / ratio).')
percent = input(defval=50.0, title='Percentage of position to take profit.')
// |-- Signal Parameters:
// |
// |-- Moving Average input source and length parameters.
src = input(defval=close)
length = input(defval=100)
// |-- Moving Average Data series.
ma = sma(src, length)
// |-- Condition for triggering a buy(long) order(trade).
if crossover(src, ma)
// |-- Create the order.
strategy.order(id='Buy', long=true)
// |-- Issue a exit order to close a percentage of the trade when a specified ratio(take profit / ratio) is reached.
strategy.exit(id='Buy Half Exit', from_entry='Buy', qty_percent=percent, profit=profit/ratio)
// |-- Issue a exit order to close the full position, when take profit or stop loss's are reached.
strategy.exit(id='Buy Full Exit', from_entry='Buy', qty_percent=100, profit=profit, loss=loss)
if crossunder(src, ma)
// |-- Create the order.
strategy.order(id='Sell', long=false)
// |-- Issue a exit order to close a percentage of the trade when a specified ratio(take profit / ratio) is reached.
strategy.exit(id='Sell Half Exit', from_entry='Sell', qty_percent=percent, profit=profit/ratio)
// |-- Issue a exit order to close the full position, when take profit or stop loss's are reached.
strategy.exit(id='Sell Full Exit', from_entry='Sell Half Exit', qty_percent=100, profit=profit, loss=loss)
// |-- Output Functions.
plot(series=ma, title='MA', color=black)