
この記事では,移動平均の交差原理に基づいた定量取引戦略について説明する.この戦略は,価格と移動平均の関係を比較し,多空方向を判断し,停止止損ポイントを設定しながらリスクを制御する.この戦略のコードは,Pine Scriptを使用して記述され,Dhan取引プラットフォームのAPIを統合し,戦略信号の自動取引を実現する.
この戦略の核心は,移動平均であり,一定の周期内の閉店価格の単純な移動平均を計算してトレンド判断の根拠として使用する.価格が平均線を上越すときに多信号が生成され,下越しは空き信号が生成される.同時に,exrem関数を使用してフィルタリングし,連続した繰り返し信号を出し,信号の質を向上させる.戦略は,現在のポジションの方向と価格と平均線の位置との関係に基づいて,対応するストップ・ロスの価格を設定し,各取引のリスクと利益を制御する.
移動平均線交差は,市場の中長期のトレンドを効果的に捉える簡単な使いやすいトレンド追跡方法である.合理的なパラメータ設定により,この戦略はトレンドの状況で安定した収益を得ることができる.ストップ・ストップの設定は,リターンを制御し,リスク収益率を向上させるのに役立ちます.戦略のコードロジックは明確で,機能のモジュール化,読みやすさ,拡張性があります.さらに,戦略はDhanAPIプラットフォームを統合し,信号の自動化下令取引を実現し,実行効率を改善します.
移動平均線は本質的に遅れの指標であり,市場の転換時に,信号が遅延し,最適な取引時間を誤り,または偽の信号を生成する可能性があります.パラメータの設定が不適切である場合,戦略のパフォーマンスに影響を与え,異なる市場特性と周期に応じて最適化する必要があります.固定パーセントのストップロスは,市場の変動率の変化に適応できない可能性があり,パラメータの設定が不適切である場合,損失をもたらすリスクもあります.
移動平均線交差戦略は,トレンド追跡とストップ・ストップ・損失制御によってトレンド状況で利益を得ることができるシンプルで実用的量化取引戦略である.しかし,戦略自体は一定の限界があり,市場の特徴とリスク好みに応じて最適化および改善が必要である.実用的なアプリケーションでは,厳格な規律の執行とリスクの管理にも注意が必要である.戦略のプログラミングは,Pine Scriptなどの専門言語を使用して,取引プラットフォームAPIを統合し,戦略の自動化実行を実現することができる.
/*backtest
start: 2024-05-01 00:00:00
end: 2024-05-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © syam-mohan-vs @ T7 - wwww.t7wealth.com www.t7trade.com
//This is an educational code done to describe the fundemantals of pine scritpting language and integration with Indian discount broker Dhan. This strategy is not tested or recommended for live trading.
//@version=5
strategy("Pine & Dhan - Moving Average Crossover Strategy", overlay=true)
//Remove excess signals
exrem(condition1, condition2) =>
temp = false
temp := na(temp[1]) ? false : not temp[1] and condition1 ? true : temp[1] and condition2 ? false : temp[1]
ta.change(temp) == true ? true : false
// Define MA period
ma_period = input(20, title = "MA Length")
// Define target and stop loss levels
target_percentage = input.float(title="Target Profit (%)", defval=2.0)
stop_loss_percentage = input.float(title="Stop Loss (%)", defval=1.0)
// Calculate the MA
ma = ta.sma(close, ma_period)
// Entry conditions
long_entry = close >= ma
short_entry = close < ma
// Calculate target and stop loss prices
target_price = long_entry ? strategy.position_avg_price + (close * (target_percentage / 100)) : strategy.position_avg_price - (close * (target_percentage / 100))
stop_loss_price = short_entry ? strategy.position_avg_price + (close * (stop_loss_percentage/ 100)) : strategy.position_avg_price - (close * (stop_loss_percentage / 100))
long_entry := exrem(long_entry,short_entry)
short_entry := exrem(short_entry,long_entry)
// Plot the MA
plot(ma, color=color.blue, linewidth=2, title="MA")
// Plot the entry and exit signals
plotshape(long_entry, style=shape.arrowup, color=color.green, size=size.small,location = location.belowbar)
plotshape(short_entry, style=shape.arrowdown, color=color.red, size=size.small,location = location.abovebar)
//Find absolute value of positon size to exit position properly
size = math.abs(strategy.position_size)
//Replace these four JSON strings with those generated from user Dhan account
long_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"B","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"I","sort_order":"1","price":"0"}]}'
long_exit_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"S","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"M","sort_order":"1","price":"0"}]}'
short_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"S","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"M","sort_order":"1","price":"0"}]}'
short_exit_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"B","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"M","sort_order":"1","price":"0"}]}'
// Submit orders based on signals
if(strategy.position_size == 0)
if long_entry
strategy.order("Long", strategy.long,alert_message=long_msg)
if short_entry
strategy.order("Short", strategy.short,alert_message=short_msg)
if(strategy.position_size > 0)
if(short_entry)
strategy.order("Short", strategy.short, qty = size, alert_message=short_msg)
else
strategy.exit("Long Exit", from_entry="Long", qty = size, stop=stop_loss_price, limit= target_price, alert_message=long_exit_msg)
if(strategy.position_size < 0)
if(long_entry)
strategy.order("Long", strategy.long, qty = size, alert_message=long_msg)
else
strategy.exit("Short Exit", from_entry="Short", qty = size, stop=stop_loss_price, limit= target_price, alert_message=short_exit_msg)