基于STOCH交易策略的自动交易


创建日期: 2023-09-28 11:38:44 最后修改: 2023-09-28 11:38:44
复制: 0 点击次数: 498
avatar of ChaoZhang ChaoZhang
1
关注
1259
关注者

本策略基于STOCH指标设计了一个简单的自动交易系统。该策略适合外汇,股票指数,大宗商品等市场,也可扩展至股票和加密货币市场。

策略概述

该策略使用STOCH指标识别超买超卖状态,结合PIVOT点设定止损位置,实现趋势跟踪。当STOCH指标显示超买超卖时,进行做多做空操作;止损点设在当日PIVOT点附近,可有效控制风险;部分止盈点设定在一定盈利后关闭部分头寸。

策略原理

该策略使用STOCH指标的快线%K和慢线%D实现金叉做多和死叉做空。具体逻辑是,当%K线从下向上突破%D线时,进行做多操作;当%K线从上向下突破%D线时,进行做空操作。这样可捕捉超买超卖状态。

为控制风险,长仓做多止损点设在当日最低PIVOT点附近,空仓做空止损点设在当日最高PIVOT点附近,可有效锁定风险。

部分止盈逻辑是,开仓后特定盈利水平时,关闭50%头寸。这样可优化资金使用效率。

综上,该策略整体 Capture超买超卖状态的恰当时点; Control风险控制方面;Optimize资金使用效率。可谓Capture, Control 和Optimize的有机结合。

策略优势

  • 使用STOCH指标可有效捕捉超买超卖现象,辅以PIVOT点可控制风险,从而全面掌控交易风险。

  • 部分止盈机制可优化资金使用效率。采用部分平仓的方法,既确保部分利润,也保留后续运行盈利空间。

  • 策略参数可自定义,交易者可根据市场和风险偏好调整参数,实现策略的灵活运用。

  • 策略逻辑非常简单清晰,容易理解掌握,适合不同交易者使用。代码直观易读,便于修改维护。

策略风险

  • 作为趋势跟踪策略,容易被困于震荡行情中,无法实现盈利。

  • STOCH指标可能产生错误信号,引发不必要的交易行为。应适当过滤信号,避免无谓交易。

  • 止损点靠近当日枢轴点,在突破盘整后可能过于接近,应适当拉大止损距离。

  • 部分策略参数如期间长度等需要根据不同市场调整,否则会影响策略表现。

  • 回测仅基于历史数据,无法保证未来表现。实盘中会受到更多不可控因素影响。

  • 自动交易系统需确保服务器稳定性,避免因连接问题导致无法正常交易。

策略优化方向

  • 可引入趋势过滤,避免趋势不明时盲目交易。例如加入MA指标判断趋势方向。

  • 可加入交易量监测,像放量,空头放量等,过滤假突破。避免被套。

  • 可根据不同品种、周期调整参数,优化策略表现。例如调整STOCH的参数。

  • 可考虑加入机器学习算法,利用大数据训练模型,自动优化参数。

  • 可设定盈亏比来引入风险控制,避免出现大额亏损的单子。

  • 可加入更多条件来过滤入场时机,提高策略胜率。如引入股票基本面模型等。

总结

本策略整体采用了较为简单直观的趋势跟踪方法,以STOCH指标识别超买超卖状态,并加入PIVOT止损来控制风险,同时引入部分止盈来优化资金效率。从Capture,Control和Optimize三个层面进行设计,形成一个较为完整的自动交易系统。该策略逻辑简单易懂,参数可自定义,可为不同交易者所用。但也存在一定的风险与不足,需要在实盘运用中不断测试与优化,方能长期产生稳定收益。

策略源码
/*backtest
start: 2022-09-21 00:00:00
end: 2023-09-27 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Peter_O

//@version=4
// strategy(title="TradingView Alerts to MT4 MT5 - Forex, indices, commodities, stocks, crypto", commission_type=strategy.commission.cash_per_contract, commission_value=0.00003, overlay=false, default_qty_value=20000, initial_capital=1000)
//
// This script was created for educational purposes only.
// It is showing how to use Alerts-Straight-From-Strategies and
// dynamic variables in TradingView alerts.
// And how to auto-execute them in Forex, indices, commodities markets
// 
// (This method will also work with stocks and crypto - anything your 
// broker is offering via their MT4/MT5 platform).
 
TakeProfitLevel=input(400)
TakePartialProfitLevel=input(150)

// **** Entries logic **** {
periodK = input(13, title="K", minval=1)
periodD = input(3, title="D", minval=1)
smoothK = input(4, title="Smooth", minval=1)
k = sma(stoch(close, high, low, periodK), smoothK)
d = sma(k, periodD)
plot(k, title="%K", color=color.blue)
plot(d, title="%D", color=color.orange)
h0 = hline(80)
h1 = hline(20)
fill(h0, h1, color=color.purple, transp=75)

GoLong=crossover(k,d) and k<80 and year>2009
GoShort=crossunder(k,d) and k>20 and year>2009

AlertTest=open>close or open<close or open==close
// } End of entries logic

// **** Pivot-points and stop-loss logic **** {
piv_high = pivothigh(high,1,1)
piv_low = pivotlow(low,1,1)
var float stoploss_long=low
var float stoploss_short=high

pl=valuewhen(piv_low,piv_low,0)
ph=valuewhen(piv_high,piv_high,0)

if GoLong 
    stoploss_long := low<pl ? low : pl
if GoShort 
    stoploss_short := high>ph ? high : ph
// } End of Pivot-points and stop-loss logic

// **** Trade counter and partial closing mechanism **** {
var int trade_id=0
if GoLong or GoShort
    trade_id:=trade_id+1

TakePartialProfitLong = barssince(GoLong)<barssince(GoShort) and crossover(high,(valuewhen(GoLong,close,0)+TakePartialProfitLevel*syminfo.mintick))
TakePartialProfitShort = barssince(GoLong)>barssince(GoShort) and crossunder(low,(valuewhen(GoShort,close,0)-TakePartialProfitLevel*syminfo.mintick))
// } End of Trade counter and partial closing mechanism

strategy.entry("Long", strategy.long, when=GoLong)
strategy.exit("XPartLong", from_entry="Long", qty_percent=50, profit=TakePartialProfitLevel)
strategy.exit("XLong", from_entry="Long", stop=stoploss_long, profit=TakeProfitLevel)
strategy.entry("Short", strategy.short, when=GoShort)
strategy.exit("XPartShort", from_entry="Short", qty_percent=50, profit=TakePartialProfitLevel)
strategy.exit("XShort", from_entry="Short", stop=stoploss_short, profit=TakeProfitLevel)

if GoLong
    alertsyntax_golong='long slprice=' + tostring(stoploss_long) + ' tradeid=' + tostring(trade_id) + ' tp=' + tostring(TakeProfitLevel)
    alert(message=alertsyntax_golong, freq=alert.freq_once_per_bar_close)
if GoShort
    alertsyntax_goshort='short slprice=' + tostring(stoploss_short) + ' tradeid=' + tostring(trade_id) + ' tp=' + tostring(TakeProfitLevel)
    alert(message=alertsyntax_goshort, freq=alert.freq_once_per_bar_close)
if TakePartialProfitLong
    alertsyntax_closepartlong='closepart tradeid=' + tostring(trade_id) + ' part=0.5'
    alert(message=alertsyntax_closepartlong, freq=alert.freq_once_per_bar_close)
if TakePartialProfitShort
    alertsyntax_closepartshort='closepart tradeid=' + tostring(trade_id) + ' part=0.5'
    alert(message=alertsyntax_closepartshort, freq=alert.freq_once_per_bar_close)