StockSharp

Форк
0
142 строки · 3.2 Кб
1
namespace SampleHistoryTesting
2
{
3
	using System;
4
	using System.Collections.Generic;
5

6
	using Ecng.Common;
7

8
	using StockSharp.Algo;
9
	using StockSharp.Messages;
10

11
	class OwnMessageAdapter : MessageAdapter
12
	{
13
		public OwnMessageAdapter(IdGenerator transactionIdGenerator)
14
			: base(transactionIdGenerator)
15
		{
16
			this.AddMarketDataSupport();
17
			this.AddSupportedMarketDataType(DataType.CandleTimeFrame);
18

19
			this.AddSupportedResultMessage(MessageTypes.SecurityLookup);
20
		}
21

22
		/// <inheritdoc />
23
		public override bool IsAllDownloadingSupported(DataType dataType)
24
			=> dataType == DataType.Securities || base.IsAllDownloadingSupported(dataType);
25

26
		private readonly TimeSpan[] _timeFrames = new[]
27
		{
28
			TimeSpan.FromMinutes(1),
29
			TimeSpan.FromMinutes(5),
30
		};
31

32
		/// <inheritdoc />
33
		protected override IEnumerable<TimeSpan> TimeFrames => _timeFrames;
34

35
		protected override bool OnSendInMessage(Message message)
36
		{
37
			switch (message.Type)
38
			{
39
				case MessageTypes.Reset:
40
					SendOutMessage(new ResetMessage());
41
					break;
42

43
				case MessageTypes.Connect:
44
					SendOutMessage(new ConnectMessage());
45
					break;
46

47
				case MessageTypes.Disconnect:
48
					SendOutMessage(new DisconnectMessage());
49
					break;
50

51
				case MessageTypes.SecurityLookup:
52
				{
53
					var lookupMsg = (SecurityLookupMessage)message;
54

55
					foreach (var id in new[] { "SBER@TQBR" })
56
					{
57
						SendOutMessage(new SecurityMessage
58
						{
59
							SecurityId = id.ToSecurityId(),
60
							OriginalTransactionId = lookupMsg.TransactionId,
61
						});	
62
					}
63

64
					SendSubscriptionResult(lookupMsg);
65
					break;
66
				}
67

68
				case MessageTypes.MarketData:
69
				{
70
					var mdMsg = (MarketDataMessage)message;
71

72
					switch (mdMsg.DataType)
73
					{
74
						case MarketDataTypes.CandleTimeFrame:
75
						{
76
							if (mdMsg.IsSubscribe)
77
							{
78
								var start = mdMsg.From.Value;
79
								var stop = mdMsg.To.Value;
80

81
								var tf = mdMsg.GetTimeFrame();
82

83
								// sends subscribed successfully
84
								SendSubscriptionReply(mdMsg.TransactionId);
85

86
								const decimal step = 0.01m;
87

88
								for (var curr = start; curr < stop; curr += tf)
89
								{
90
									var price = RandomGen.GetInt(100, 110);
91

92
									var open = price + RandomGen.GetInt(10) * step;
93
									var high = open + RandomGen.GetInt(10) * step;
94
									var low = high - RandomGen.GetInt(10) * step;
95
									var close = low + RandomGen.GetInt(10) * step;
96

97
									if (high < low)
98
									{
99
										var d = high;
100
										high = low;
101
										low = d;
102
									}
103

104
									SendOutMessage(new TimeFrameCandleMessage
105
									{
106
										OriginalTransactionId = mdMsg.TransactionId,
107
										OpenPrice = open,
108
										HighPrice = high,
109
										LowPrice = low,
110
										ClosePrice = close,
111
										OpenTime = curr,
112
										State = CandleStates.Finished,
113
									});
114
								}
115
							
116
								SendSubscriptionResult(mdMsg);
117
							}
118
							else
119
							{
120
								// sends unsubscribed successfully
121
								SendSubscriptionReply(mdMsg.TransactionId);
122
							}
123

124
							break;
125
						}
126
						default:
127
							// not supported other data types
128
							SendSubscriptionNotSupported(mdMsg.TransactionId);
129
							break;
130
					}
131

132
					break;
133
				}
134
			
135
				default:
136
					return false;
137
			}
138

139
			return true;
140
		}
141
	}
142
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.