基于日线收盘价比较的量化交易策略


创建日期: 2023-11-21 14:34:11 最后修改: 2023-11-21 14:34:11
复制: 0 点击次数: 647
avatar of ChaoZhang ChaoZhang
1
关注
1366
关注者

基于日线收盘价比较的量化交易策略

概述

本策略称为“日线收盘价比较策略”,是基于日线收盘价进行交易决策的量化策略。该策略通过计算当前日线收盘价和前一日线收盘价的差值,据此产生交易信号。当差值超过设定的阈值时,进行买入或卖出操作。

策略原理

该策略的核心逻辑是比较当前K线的收盘价和前一根K线的收盘价。 specifically:

  1. 计算当前日线收盘价和前一日线收盘价的差值(today - yesterday)
  2. 计算差值与前一日收盘价的比例(difference / yesterday’s close)
  3. 如果比例大于设定的正阈值,则产生买入信号;如果比例小于设定的负阈值,则产生卖出信号
  4. 根据信号进入做多或空仓部位

该策略没有设置止损和止盈条件,依赖阈值条件形成的交易信号进行入场和平仓。

优势分析

  • 思路简单容易理解,适合量化交易的入门学习
  • 仅基于日线收盘价交易,避免过于频繁交易
  • 可通过调整阈值来控制交易频率

风险分析

  • 没有止损设置,无法控制单笔损失
  • 可能会产生连续的交易信号导致过度交易
  • 回撤可能较大,无法很好控制总体亏损

优化方向

  • 增加止损逻辑,控制单笔损失
  • 增加开仓次数限制,避免过度交易
  • 优化参数,寻找最佳交易频率

总结

本策略通过比较日线收盘价形成交易信号,思路简单,适合入门学习。但该策略存在一定风险,需要进一步优化以用于实盘交易。

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

//@version=2
strategy("Daily Close Comparison Strategy (by ChartArt) correct results", shorttitle="CA_-_Daily_Close_Strat", overlay=false)

// ChartArt's Daily Close Comparison Strategy
//
// Version 1.0
// Idea by ChartArt on February 28, 2016.
//
// This strategy is equal to the very
// popular "ANN Strategy" coded by sirolf2009,
// but without the Artificial Neural Network (ANN).
//
// Main difference besides stripping out the ANN
// is that I use close prices instead of OHLC4 prices.
// And the default threshold is set to 0 instead of 0.0014
// with a step of 0.001 instead of 0.0001.
//
// This strategy goes long if the close of the current day
// is larger than the close price of the last day.
// If the inverse logic is true, the strategy
// goes short (last close larger current close).
//
// This simple strategy does not have any
// stop loss or take profit money management logic.
//
// List of my work: 
// https://www.tradingview.com/u/ChartArt/
// 
//  __             __  ___       __  ___ 
// /  ` |__|  /\  |__)  |   /\  |__)  |  
// \__, |  | /~~\ |  \  |  /~~\ |  \  |  
// 
// 

threshold = input(title="Price Difference Threshold correct results", type=float, defval=0, step=0.004)

getDiff() =>
    yesterday=request.security(syminfo.tickerid, 'D', close[1])
    today=close
    delta=today-yesterday
    percentage=delta/yesterday
    
closeDiff = getDiff()
 
buying = closeDiff > threshold ? true : closeDiff < -threshold ? false : buying[1]

hline(0, title="zero line")

bgcolor(buying ? green : red, transp=25)
plot(closeDiff, color=silver, style=area, transp=75)
plot(closeDiff, color=aqua, title="prediction")

longCondition = buying
if (longCondition)
    strategy.entry("Long", strategy.long)

shortCondition = buying != true
if (shortCondition)
    strategy.entry("Short", strategy.short)