И все таки продукты S# не дружат с комплексными индикаторами..
Просто в Designer нет возможности работать со значением индикатора MACDsignal или ни кто не рассказывает как.
Решил схитрить, и написать код(приведен ниже) но если использовать MACDsignal то Designer вообще начинает адски глючить. ну и бонально стратегия не запускается. Помогите а? Конечно да я сейчас решил проблему сделав макд и ема, но у вас есть и др индикаторы комплексные, что с ними делать...
Кодnamespace StockSharp.Designer.Strategies
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Media;
using System.Runtime.InteropServices;
using Ecng.Common;
using Ecng.ComponentModel;
using Ecng.Collections;
using MoreLinq;
using StockSharp.Messages;
using StockSharp.Algo;
using StockSharp.Algo.Candles;
using StockSharp.Algo.Strategies;
using StockSharp.Algo.Indicators;
using StockSharp.Logging;
using StockSharp.BusinessEntities;
using StockSharp.Localization;
using StockSharp.Xaml;
using StockSharp.Xaml.Charting;
using StockSharp.Xaml.Diagram.Elements;
[Guid("eea2da25-de12-4b6c-b43a-6a98e2fdb01c")]
[DisplayName(@"MACDS_RSI")]
public class NewStrategy : Strategy
{
private readonly MovingAverageConvergenceDivergenceSignal _macdSig;
private readonly RelativeStrengthIndex _rsi;
private readonly StrategyParam<int> _longParam;
private readonly StrategyParam<int> _shortParam;
private readonly StrategyParam<int> _sigParam;
private readonly StrategyParam<int> _rsiParam;
private readonly StrategyParam<int> _konMACDSParam;
private readonly StrategyParam<int> _konRsiParam;
private bool _isSell=false;
private bool _isBuy=false;
[DiagramExternal]
public event Action<Order> NewMyOrder;
[DiagramExternal]
public event Action<MyTrade> NewMyTrade2;
// [DiagramExternal]
// public event Action<IIndicatorValue> NewMacdSigIndicator;
[DiagramExternal]
public event Action<IIndicatorValue> NewRsiIndicator;
public NewStrategy()
{
_longParam = new StrategyParam<int>(this, nameof(Long), 40);
_shortParam = new StrategyParam<int>(this, nameof(Short), 20);
_sigParam = new StrategyParam<int>(this, nameof(Sig), 9);
_rsiParam = new StrategyParam<int>(this, nameof(Rsi), 18);
_konMACDSParam=new StrategyParam<int> (this, nameof(KonMACDS), 0);
_konRsiParam=new StrategyParam<int> (this, nameof(KonRsi), 50);
_macdSig = new MovingAverageConvergenceDivergenceSignal(new MovingAverageConvergenceDivergence(new ExponentialMovingAverage { Length = Long },new ExponentialMovingAverage { Length = Short }),new ExponentialMovingAverage { Length = Sig });
_rsi = new RelativeStrengthIndex { Length = Rsi };
}
public int Long
{
get => _longParam.Value;
set
{
_longParam.Value = value;
_macdSig.Macd.LongMa.Length = value;
}
}
public int Short
{
get => _shortParam.Value;
set
{
_shortParam.Value = value;
_macdSig.Macd.ShortMa.Length = value;
}
}
public int Sig
{
get => _sigParam.Value;
set
{
_sigParam.Value = value;
_macdSig.SignalMa.Length = value;
}
}
public int Rsi
{
get => _rsiParam.Value;
set
{
_rsiParam.Value = value;
_rsi.Length = value;
}
}
public int KonMACDS
{
get => _konMACDSParam.Value;
set
{
_konMACDSParam.Value = value;
}
}
public int KonRsi
{
get => _konRsiParam.Value;
set
{
_konRsiParam.Value = value;
}
}
protected override void OnReseted()
{
this.AddInfoLog("OnReseted");
_macdSig.Reset();
_rsi.Reset();
base.OnReseted();
}
protected override void OnStarted()
{
this.AddInfoLog("OnStarted");
_macdSig.Reset();
_rsi.Reset();
this
.WhenNewMyTrade()
.Do(trade => NewMyTrade2?.Invoke(trade))
.Apply(this);
this
.WhenOrderRegistered()
.Or(this.WhenOrderChanged())
.Do(order => NewMyOrder?.Invoke(order))
.Apply(this);
Connector.RegisterSecurity(Security);
base.OnStarted();
}
protected override void OnStopped()
{
this.AddInfoLog("OnStopped");
Connector.UnRegisterSecurity(Security);
base.OnStopped();
}
[DiagramExternal]
public void ProcessPeriod(Unit shortPeriod, Unit longPeriod, Unit sigPeriod, Unit rsiPeriod, Unit KonMACDSPeriod, Unit KonRsiPeriod)
{
if (shortPeriod != null)
Short = (int)shortPeriod.Value;
if (longPeriod != null)
Long = (int)longPeriod.Value;
if (sigPeriod != null)
Sig = (int)sigPeriod.Value;
if (rsiPeriod != null)
Rsi = (int)rsiPeriod.Value;
if (KonMACDSPeriod != null)
KonMACDS = (int)KonMACDSPeriod.Value;
if (KonRsiPeriod != null)
KonRsi = (int)KonRsiPeriod.Value;
}
[DiagramExternal]
public void ProcessCandle(Candle candle)
{
// strategy are stopping
if (ProcessState == ProcessStates.Stopping)
{
CancelActiveOrders();
return;
}
if(this.LogLevel==LogLevels.Debug)
this.AddInfoLog(LocalizedStrings.Str3634Params.Put(candle.OpenTime, candle.OpenPrice, candle.HighPrice, candle.LowPrice, candle.ClosePrice, candle.TotalVolume, candle.Security));
// process new candle
var macdSValue = _macdSig.Process(candle);
var rsiValue = _rsi.Process(candle);
//NewMacdSigIndicator?.Invoke(macdSValue);
NewRsiIndicator?.Invoke(rsiValue);
// calc new values for short and long
var isSell = _rsi.GetCurrentValue() < KonRsi && macdSValue.GetValue<decimal>() < KonMACDS;
var isBuy = _rsi.GetCurrentValue() > KonRsi && macdSValue.GetValue<decimal>() > KonMACDS;
//Удалить
this.AddInfoLog(_rsi.GetCurrentValue() + "||" + rsiValue);
//trade
if (_isSell!=isSell)
{
// if short less than long, the sale, otherwise buy
var direction = Sides.Sell;
// calc size for open position or revert
var volume = Position == 0 ? Volume : Position.Abs().Min(Volume) * 2;
// calc order price as a close price + offset
var price = candle.ClosePrice-Security.PriceStep;
RegisterOrder(this.CreateOrder(direction, price, volume));
// store current values for short and long
_isSell = isSell;
}
if (_isBuy!=isBuy)
{
// if short less than long, the sale, otherwise buy
var direction = Sides.Buy;
// calc size for open position or revert
var volume = Position == 0 ? Volume : Position.Abs().Min(Volume) * 2;
// calc order price as a close price + offset
var price = candle.ClosePrice + Security.PriceStep;
RegisterOrder(this.CreateOrder(direction, price, volume));
// store current values for short and long
_isBuy = isBuy;
}
}
}
}