Exp Multic 策略
该策略在没有任何技术指标的情况下交易一组固定的主要外汇货币对。 对于每个货币对,算法保存方向和仓位量。每次盈利后增加交易量,亏损时反向操作。整体利润或亏损超过设定阈值时停止交易并平掉所有仓位。
细节
- 入场条件:
- 当没有持仓且账户余额高于
Margin时,按预设方向以MinVolume开仓。
- 当没有持仓且账户余额高于
- 多空方向:根据每个货币对的内部方向可做多也可做空。
- 离场条件:
- 当利润大于
KClose * MinVolume时平仓。 - 当亏损大于
KChange * 当前仓位量时平仓并反向。
- 当利润大于
- 止损:无显式止损;风险由收益/亏损阈值控制。
- 默认参数:
Loss= 1900Profit= 4000Margin= 5000MinVolume= 0.01KChange= 2100KClose= 4600
- 筛选:
- 类别:资金管理
- 方向:双向
- 指标:无
- 止损:无
- 复杂度:中等
- 时间框架:逐笔
- 季节性:无
- 神经网络:无
- 背离:无
- 风险级别:高
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>
/// Momentum flip strategy: trades in current momentum direction,
/// flips direction when loss threshold hit, adds on profit.
/// Adapted from multi-currency EA to single-security candle-based approach.
/// </summary>
public class ExpMulticStrategy : Strategy
{
private readonly StrategyParam<int> _period;
private readonly StrategyParam<DataType> _candleType;
private decimal? _prevMomentum;
public int Period { get => _period.Value; set => _period.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public ExpMulticStrategy()
{
_period = Param(nameof(Period), 14)
.SetGreaterThanZero()
.SetDisplay("Period", "Momentum lookback period", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevMomentum = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var momentum = new Momentum { Length = Period };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(momentum, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
var area2 = CreateChartArea();
if (area2 != null)
DrawIndicator(area2, momentum);
}
}
private void ProcessCandle(ICandleMessage candle, decimal momentumValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
{
_prevMomentum = momentumValue;
return;
}
if (_prevMomentum is decimal prev)
{
// Momentum crosses above zero - buy
if (prev <= 0 && momentumValue > 0 && Position <= 0)
BuyMarket();
// Momentum crosses below zero - sell
else if (prev >= 0 && momentumValue < 0 && Position >= 0)
SellMarket();
}
_prevMomentum = momentumValue;
}
}
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 Momentum
from StockSharp.Algo.Strategies import Strategy
class exp_multic_strategy(Strategy):
def __init__(self):
super(exp_multic_strategy, self).__init__()
self._period = self.Param("Period", 14).SetDisplay("Period", "Momentum lookback period", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))).SetDisplay("Candle Type", "Timeframe", "General")
self._prev_momentum = None
@property
def period(self): return self._period.Value
@property
def candle_type(self): return self._candle_type.Value
def OnReseted(self):
super(exp_multic_strategy, self).OnReseted()
self._prev_momentum = None
def OnStarted2(self, time):
super(exp_multic_strategy, self).OnStarted2(time)
momentum = Momentum()
momentum.Length = self.period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(momentum, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
area2 = self.CreateChartArea()
if area2 is not None:
self.DrawIndicator(area2, momentum)
def process_candle(self, candle, momentum_value):
if candle.State != CandleStates.Finished: return
mv = float(momentum_value)
if not self.IsFormedAndOnlineAndAllowTrading():
self._prev_momentum = mv
return
if self._prev_momentum is not None:
if self._prev_momentum <= 0.0 and mv > 0.0 and self.Position <= 0:
self.BuyMarket()
elif self._prev_momentum >= 0.0 and mv < 0.0 and self.Position >= 0:
self.SellMarket()
self._prev_momentum = mv
def CreateClone(self): return exp_multic_strategy()