BTC 难度调整策略
BTC 难度调整策略基于比特币挖矿难度的变化进行交易。启用阈值模式时,仅当百分比变化超过设定阈值才开仓。难度上调时开多,难度下调时开空。
详情
- 入场条件:
- 阈值模式:
abs(change) >= Threshold且change < 0→ 开多。 - 阈值模式:
abs(change) >= Threshold且change > 0→ 开空。 - 无阈值模式:
difficulty > 上一值→ 开多。 - 无阈值模式:
difficulty < 上一值→ 开空。
- 阈值模式:
- 多空方向: 双向。
- 出场条件:
- 相反信号关闭并反转仓位。
- 止损: 无。
- 默认参数:
CandleType= 1 天ThresholdMode= falseThreshold= 10
- 过滤器:
- 分类: 基本面
- 方向: 多 & 空
- 指标: 无
- 止损: 无
- 复杂度: 低
- 时间框架: 日线
- 季节性: 无
- 神经网络: 无
- 背离: 无
- 风险级别: 中等
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// BTC Difficulty Adjustments Strategy - uses rate of change indicator
/// to detect momentum shifts and trade accordingly.
/// </summary>
public class BtcDifficultyAdjustmentsStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _rocPeriod;
private readonly StrategyParam<int> _smaPeriod;
private decimal _prevRoc;
private decimal _prevSma;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int RocPeriod { get => _rocPeriod.Value; set => _rocPeriod.Value = value; }
public int SmaPeriod { get => _smaPeriod.Value; set => _smaPeriod.Value = value; }
public BtcDifficultyAdjustmentsStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
_rocPeriod = Param(nameof(RocPeriod), 50)
.SetGreaterThanZero()
.SetDisplay("ROC Period", "Rate of change period", "Indicators");
_smaPeriod = Param(nameof(SmaPeriod), 100)
.SetGreaterThanZero()
.SetDisplay("SMA Period", "Trend filter SMA period", "Indicators");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevRoc = 0m;
_prevSma = 0m;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var roc = new RateOfChange { Length = RocPeriod };
var sma = new SimpleMovingAverage { Length = SmaPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(roc, sma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, sma);
DrawOwnTrades(area);
}
var rocArea = CreateChartArea();
if (rocArea != null)
{
DrawIndicator(rocArea, roc);
}
}
private void ProcessCandle(ICandleMessage candle, decimal rocValue, decimal smaValue)
{
if (candle.State != CandleStates.Finished)
return;
if (_prevRoc == 0m || _prevSma == 0m)
{
_prevRoc = rocValue;
_prevSma = smaValue;
return;
}
// Buy when ROC crosses above zero and price above SMA
if (_prevRoc <= 0m && rocValue > 0m && candle.ClosePrice > smaValue && Position <= 0)
{
BuyMarket();
}
// Sell when ROC crosses below zero and price below SMA
else if (_prevRoc >= 0m && rocValue < 0m && candle.ClosePrice < smaValue && Position >= 0)
{
SellMarket();
}
_prevRoc = rocValue;
_prevSma = smaValue;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import RateOfChange, SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class btc_difficulty_adjustments_strategy(Strategy):
def __init__(self):
super(btc_difficulty_adjustments_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._roc_period = self.Param("RocPeriod", 50) \
.SetDisplay("ROC Period", "Rate of change period", "Indicators")
self._sma_period = self.Param("SmaPeriod", 100) \
.SetDisplay("SMA Period", "Trend filter SMA period", "Indicators")
self._prev_roc = 0.0
self._prev_sma = 0.0
@property
def candle_type(self):
return self._candle_type.Value
@property
def roc_period(self):
return self._roc_period.Value
@property
def sma_period(self):
return self._sma_period.Value
def OnReseted(self):
super(btc_difficulty_adjustments_strategy, self).OnReseted()
self._prev_roc = 0.0
self._prev_sma = 0.0
def OnStarted2(self, time):
super(btc_difficulty_adjustments_strategy, self).OnStarted2(time)
roc = RateOfChange()
roc.Length = self.roc_period
sma = SimpleMovingAverage()
sma.Length = self.sma_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(roc, sma, self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, sma)
self.DrawOwnTrades(area)
roc_area = self.CreateChartArea()
if roc_area is not None:
self.DrawIndicator(roc_area, roc)
def OnProcess(self, candle, roc_value, sma_value):
if candle.State != CandleStates.Finished:
return
roc_val = float(roc_value)
sma_val = float(sma_value)
if self._prev_roc == 0 or self._prev_sma == 0:
self._prev_roc = roc_val
self._prev_sma = sma_val
return
if self._prev_roc <= 0 and roc_val > 0 and float(candle.ClosePrice) > sma_val and self.Position <= 0:
self.BuyMarket()
elif self._prev_roc >= 0 and roc_val < 0 and float(candle.ClosePrice) < sma_val and self.Position >= 0:
self.SellMarket()
self._prev_roc = roc_val
self._prev_sma = sma_val
def CreateClone(self):
return btc_difficulty_adjustments_strategy()