mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-06 17:42:13 +00:00
99 lines
2.6 KiB
C#
99 lines
2.6 KiB
C#
namespace SampleHistoryTestingParallel
|
|
{
|
|
using Ecng.Common;
|
|
|
|
using StockSharp.Algo;
|
|
using StockSharp.Algo.Candles;
|
|
using StockSharp.Algo.Indicators;
|
|
using StockSharp.Algo.Strategies;
|
|
using StockSharp.Messages;
|
|
|
|
class SmaStrategy : Strategy
|
|
{
|
|
private readonly CandleSeries _series;
|
|
private bool _isShortLessThenLong;
|
|
|
|
private readonly StrategyParam<int> _longSmaPeriod;
|
|
|
|
public int LongSmaPeriod
|
|
{
|
|
get { return _longSmaPeriod.Value; }
|
|
}
|
|
|
|
private readonly StrategyParam<int> _shortSmaPeriod;
|
|
|
|
public int ShortSmaPeriod
|
|
{
|
|
get { return _shortSmaPeriod.Value; }
|
|
}
|
|
|
|
public SmaStrategy(CandleSeries series, SimpleMovingAverage longSma, SimpleMovingAverage shortSma)
|
|
{
|
|
_series = series;
|
|
|
|
LongSma = longSma;
|
|
ShortSma = shortSma;
|
|
|
|
_longSmaPeriod = this.Param("LongSmaPeriod", longSma.Length);
|
|
_shortSmaPeriod = this.Param("ShortSmaPeriod", shortSma.Length);
|
|
}
|
|
|
|
public SimpleMovingAverage LongSma { get; private set; }
|
|
public SimpleMovingAverage ShortSma { get; private set; }
|
|
|
|
protected override void OnStarted()
|
|
{
|
|
_series
|
|
.WhenCandlesFinished()
|
|
.Do(ProcessCandle)
|
|
.Apply(this);
|
|
|
|
this
|
|
.GetCandleManager()
|
|
.Start(_series);
|
|
|
|
// çàïîìèíàåì òåêóùåå ïîëîæåíèå îòíîñèòåëüíî äðóã äðóãà
|
|
_isShortLessThenLong = ShortSma.GetCurrentValue() < LongSma.GetCurrentValue();
|
|
|
|
base.OnStarted();
|
|
}
|
|
|
|
private void ProcessCandle(Candle candle)
|
|
{
|
|
// åñëè íàøà ñòðàòåãèÿ â ïðîöåññå îñòàíîâêè
|
|
if (ProcessState == ProcessStates.Stopping)
|
|
{
|
|
// îòìåíÿåì àêòèâíûå çàÿâêè
|
|
CancelActiveOrders();
|
|
return;
|
|
}
|
|
|
|
// äîáàâëÿåì íîâóþ ñâå÷ó
|
|
LongSma.Process(candle);
|
|
ShortSma.Process(candle);
|
|
|
|
// âû÷èñëÿåì íîâîå ïîëîæåíèå îòíîñèòåëüíî äðóã äðóãà
|
|
var isShortLessThenLong = ShortSma.GetCurrentValue() < LongSma.GetCurrentValue();
|
|
|
|
// åñëè ïðîèçîøëî ïåðåñå÷åíèå
|
|
if (_isShortLessThenLong != isShortLessThenLong)
|
|
{
|
|
// åñëè êîðîòêàÿ ìåíüøå ÷åì äëèííàÿ, òî ïðîäàæà, èíà÷å, ïîêóïêà.
|
|
var direction = isShortLessThenLong ? Sides.Sell : Sides.Buy;
|
|
|
|
// âû÷èñëÿåì ðàçìåð äëÿ îòêðûòèÿ èëè ïåðåâîðîòà ïîçû
|
|
var volume = Position == 0 ? Volume : Position.Abs() * 2;
|
|
|
|
// ðåãèñòðèðóåì çàÿâêó (îáû÷íûì ñïîñîáîì - ëèìèòèðîâàííîé çàÿâêîé)
|
|
RegisterOrder(this.CreateOrder(direction, (decimal)(Security.GetCurrentPrice(this, direction) ?? 0), volume));
|
|
|
|
// ïåðåâîðà÷èâàåì ïîçèöèþ ÷åðåç êîòèðîâàíèå
|
|
//var strategy = new MarketQuotingStrategy(direction, volume);
|
|
//ChildStrategies.Add(strategy);
|
|
|
|
// çàïîìèíàåì òåêóùåå ïîëîæåíèå îòíîñèòåëüíî äðóã äðóãà
|
|
_isShortLessThenLong = isShortLessThenLong;
|
|
}
|
|
}
|
|
}
|
|
} |