Files
StockSharp/Samples/SmartCom/SampleSmartSMA/SmaStrategy.cs
T

96 lines
2.9 KiB
C#

#region S# License
/******************************************************************************************
NOTICE!!! This program and source code is owned and licensed by
StockSharp, LLC, www.stocksharp.com
Viewing or use of this code requires your acceptance of the license
agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
Removal of this comment is a violation of the license agreement.
Project: SampleSmartSMA.SampleSmartSMAPublic
File: SmaStrategy.cs
Created: 2015, 11, 11, 2:32 PM
Copyright 2010 by StockSharp, LLC
*******************************************************************************************/
#endregion S# License
namespace SampleSmartSMA
{
using Ecng.Common;
using StockSharp.Algo;
using StockSharp.Algo.Candles;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Algo.Strategies.Quoting;
using StockSharp.Messages;
class SmaStrategy : Strategy
{
private readonly ICandleManager _candleManager;
private readonly CandleSeries _series;
private bool _isShortLessThenLong;
public SmaStrategy(ICandleManager candleManager, CandleSeries series, SimpleMovingAverage longSma, SimpleMovingAverage shortSma)
{
_candleManager = candleManager;
_series = series;
LongSma = longSma;
ShortSma = shortSma;
}
public SimpleMovingAverage LongSma { get; }
public SimpleMovingAverage ShortSma { get; }
protected override void OnStarted()
{
_candleManager
.WhenCandlesFinished(_series)
.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(direction), volume));
// ïåðåâîðà÷èâàåì ïîçèöèþ ÷åðåç êîòèðîâàíèå
var strategy = new MarketQuotingStrategy(direction, volume);
ChildStrategies.Add(strategy);
// çàïîìèíàåì òåêóùåå ïîëîæåíèå îòíîñèòåëüíî äðóã äðóãà
_isShortLessThenLong = isShortLessThenLong;
}
}
}
}