Tape
当成交量差超过阈值时进行交易,灵感来自 Tape 指标。
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy reacting to volume delta similar to "Tape" indicator.
/// </summary>
public class TapeStrategy : Strategy
{
private readonly StrategyParam<decimal> _threshold;
private readonly StrategyParam<DataType> _candleType;
private decimal _lastPrice;
private decimal _lastVolume;
public decimal VolumeDeltaThreshold { get => _threshold.Value; set => _threshold.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public TapeStrategy()
{
_threshold = Param(nameof(VolumeDeltaThreshold), 100m)
.SetGreaterThanZero()
.SetDisplay("Volume Delta", "Volume delta threshold", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_lastPrice = 0m;
_lastVolume = 0m;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_lastPrice = 0m;
_lastVolume = 0m;
var sub = SubscribeCandles(CandleType);
sub.Bind(Process).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, sub);
DrawOwnTrades(area);
}
}
private void Process(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
if (_lastPrice == 0)
{
_lastPrice = candle.ClosePrice;
_lastVolume = candle.TotalVolume;
return;
}
var deltaVolume = (candle.TotalVolume - _lastVolume) * Math.Sign(candle.ClosePrice - _lastPrice);
if (deltaVolume > VolumeDeltaThreshold && Position <= 0)
BuyMarket();
else if (deltaVolume < -VolumeDeltaThreshold && Position >= 0)
SellMarket();
_lastPrice = candle.ClosePrice;
_lastVolume = candle.TotalVolume;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class tape_strategy(Strategy):
def __init__(self):
super(tape_strategy, self).__init__()
self._threshold = self.Param("VolumeDeltaThreshold", 100) \
.SetDisplay("Volume Delta", "Volume delta threshold", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._last_price = 0.0
self._last_volume = 0.0
@property
def threshold(self):
return self._threshold.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(tape_strategy, self).OnReseted()
self._last_price = 0.0
self._last_volume = 0.0
def OnStarted2(self, time):
super(tape_strategy, self).OnStarted2(time)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle):
if candle.State != CandleStates.Finished:
return
if self._last_price == 0:
self._last_price = candle.ClosePrice
self._last_volume = candle.TotalVolume
return
delta_volume = (candle.TotalVolume - self._last_volume) * Math.Sign(candle.ClosePrice - self._last_price)
if delta_volume > self.threshold and self.Position <= 0:
self.BuyMarket()
elif delta_volume < -self.threshold and self.Position >= 0:
self.SellMarket()
self._last_price = candle.ClosePrice
self._last_volume = candle.TotalVolume
def CreateClone(self):
return tape_strategy()