Files
StockSharp/Samples/Testing/SampleHistoryTesting/OwnMessageAdapter.cs
T

134 lines
3.4 KiB
C#

namespace SampleHistoryTesting
{
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo;
using StockSharp.Messages;
class OwnMessageAdapter : MessageAdapter
{
public OwnMessageAdapter(IdGenerator transactionIdGenerator)
: base(transactionIdGenerator)
{
this.AddMarketDataSupport();
this.AddSupportedMarketDataType(MarketDataTypes.CandleTimeFrame);
this.AddSupportedOutMessage(MessageTypes.SecurityLookupResult);
}
/// <inheritdoc />
public override bool IsAllDownloadingSupported(DataType dataType)
=> dataType == DataType.Securities || base.IsAllDownloadingSupported(dataType);
private readonly HashSet<TimeSpan> _timeFrames = new HashSet<TimeSpan>(new[] { TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(5) });
/// <inheritdoc />
protected override IEnumerable<TimeSpan> GetTimeFrames(SecurityId securityId, DateTimeOffset? from, DateTimeOffset? to)
=> _timeFrames;
protected override void OnSendInMessage(Message message)
{
switch (message.Type)
{
case MessageTypes.Reset:
SendOutMessage(new ResetMessage());
break;
case MessageTypes.Connect:
SendOutMessage(new ConnectMessage());
break;
case MessageTypes.Disconnect:
SendOutMessage(new DisconnectMessage());
break;
case MessageTypes.SecurityLookup:
{
var lookupMsg = (SecurityLookupMessage)message;
foreach (var id in new[] { "RIZ2@FORTS", "@ES#@CMEMINI" })
{
SendOutMessage(new SecurityMessage
{
SecurityId = id.ToSecurityId(),
OriginalTransactionId = lookupMsg.TransactionId,
});
}
SendOutMessage(new SecurityLookupResultMessage { OriginalTransactionId = lookupMsg.TransactionId });
break;
}
case MessageTypes.MarketData:
{
var mdMsg = (MarketDataMessage)message;
switch (mdMsg.DataType)
{
case MarketDataTypes.CandleTimeFrame:
{
if (mdMsg.IsSubscribe)
{
var start = mdMsg.From.Value;
var stop = mdMsg.To.Value;
var tf = mdMsg.GetTimeFrame();
// sends subscribed successfully
SendOutMarketDataReply(mdMsg.TransactionId);
const decimal step = 0.01m;
for (var curr = start; curr < stop; curr += tf)
{
var price = RandomGen.GetInt(100, 110);
var open = price + RandomGen.GetInt(10) * step;
var high = open + RandomGen.GetInt(10) * step;
var low = high - RandomGen.GetInt(10) * step;
var close = low + RandomGen.GetInt(10) * step;
if (high < low)
{
var d = high;
high = low;
low = d;
}
SendOutMessage(new TimeFrameCandleMessage
{
OriginalTransactionId = mdMsg.TransactionId,
OpenPrice = open,
HighPrice = high,
LowPrice = low,
ClosePrice = close,
OpenTime = curr,
State = CandleStates.Finished,
});
}
SendOutMessage(new SubscriptionFinishedMessage { OriginalTransactionId = mdMsg.TransactionId });
}
else
{
// sends unsubscribed successfully
SendOutMarketDataReply(mdMsg.TransactionId);
}
break;
}
default:
// not supported other data types
SendOutMarketDataNotSupported(mdMsg.TransactionId);
break;
}
break;
}
}
}
}
}