mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-06 09:32:55 +00:00
78 lines
2.1 KiB
C#
78 lines
2.1 KiB
C#
namespace SampleRandomEmulation
|
|
{
|
|
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;
|
|
|
|
public SmaStrategy(CandleSeries series, SimpleMovingAverage longSma, SimpleMovingAverage shortSma)
|
|
{
|
|
_series = series;
|
|
|
|
LongSma = longSma;
|
|
ShortSma = shortSma;
|
|
}
|
|
|
|
public SimpleMovingAverage LongSma { get; private set; }
|
|
public SimpleMovingAverage ShortSma { get; private set; }
|
|
|
|
protected override void OnStarted()
|
|
{
|
|
_series
|
|
.WhenCandlesFinished()
|
|
.Do(ProcessCandle)
|
|
.Apply(this);
|
|
|
|
// çàïîìèíàåì òåêóùåå ïîëîæåíèå îòíîñèòåëüíî äðóã äðóãà
|
|
_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;
|
|
}
|
|
}
|
|
}
|
|
} |