这是一个基于移动均线的突破交易策略。它通过计算一定周期的平均价格作为均线,当价格突破均线时产生交易信号。
该策略主要基于移动均线指标。它使用sma函数计算一定周期内的平均收盘价,得到移动均线。当最新收盘价从下向上突破移动均线时,产生买入信号;当最新收闭从上向下突破移动均线时,产生卖出信号。
具体来说,它在策略中定义了移动平均线的计算源(近期收盘价)和周期长度,得到移动均线数据序列。然后它设置了两个条件:价格上穿均线时创建买入订单;价格下穿均线时创建卖出订单。订单创建后,它还设置了止盈止损:当订单获利达到设定比例时平仓一部分头寸,当订单达到设定止盈或止损价格时平掉全部头寸。
这是一个简单实用的趋势跟踪策略。它有以下优势:
尽管该策略有很多优点,但也存在一些风险:
为了控制这些风险,我们可以结合其他指标进行过滤优化,引入大盘短期趋势判断,或者使用机器学习方法寻找最佳参数组合。
该策略主要可以从以下几个方面进行优化:
增加其他技术指标判断,组成交易系统,提高策略胜率。比如加入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)