forked from StockSharp/StockSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.cs
186 lines (136 loc) · 5.48 KB
/
Helper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
namespace StockSharp.Algo
{
using System;
using System.Collections.Generic;
using Ecng.Collections;
using Ecng.Common;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
using StockSharp.Localization;
static class Helper
{
public static void ChechOrderState(this Order order, bool checkVolume = true)
{
if (order == null)
throw new ArgumentNullException("order");
if (order.Type == OrderTypes.Conditional && order.Condition == null)
throw new ArgumentException(LocalizedStrings.Str889, "order");
if (order.Security == null)
throw new ArgumentException(LocalizedStrings.Str890, "order");
if (order.Portfolio == null)
throw new ArgumentException(LocalizedStrings.Str891, "order");
if (order.Price < 0)
throw new ArgumentOutOfRangeException("order", order.Price, LocalizedStrings.Str892);
if (order.Price == 0 && (order.Type == OrderTypes.Limit || order.Type == OrderTypes.ExtRepo || order.Type == OrderTypes.Repo || order.Type == OrderTypes.Rps))
throw new ArgumentException(LocalizedStrings.Str893, "order");
if (checkVolume && order.Volume == 0)
throw new ArgumentException(LocalizedStrings.Str894, "order");
if (checkVolume && order.Volume < 0)
throw new ArgumentOutOfRangeException("order", order.Volume, LocalizedStrings.Str895);
}
public static void CheckOnNew(this Order order, bool checkVolume = true, bool checkTransactionId = true)
{
order.ChechOrderState(checkVolume);
if (order.Id != 0 || !order.StringId.IsEmpty())
throw new ArgumentException(LocalizedStrings.Str896Params.Put(order.Id == 0 ? order.StringId : order.Id.To<string>()), "order");
if (!checkTransactionId)
return;
if (order.TransactionId != 0)
throw new ArgumentException(LocalizedStrings.Str897Params.Put(order.TransactionId), "order");
if (order.State != OrderStates.None)
throw new ArgumentException(LocalizedStrings.Str898Params.Put(order.State), "order");
}
public static void InitOrder(this Order order, IConnector connector, IdGenerator transactionIdGenerator)
{
if (order == null)
throw new ArgumentNullException("order");
if (connector == null)
throw new ArgumentNullException("connector");
if (transactionIdGenerator == null)
throw new ArgumentNullException("transactionIdGenerator");
order.Balance = order.Volume;
if (order.ExtensionInfo == null)
order.ExtensionInfo = new Dictionary<object, object>();
//order.InitializationTime = trader.MarketTime;
if (order.TransactionId == 0)
order.TransactionId = transactionIdGenerator.GetNextId();
order.Connector = connector;
if (order.Security is ContinuousSecurity)
order.Security = ((ContinuousSecurity)order.Security).GetSecurity(order.Security.ToExchangeTime(connector.CurrentTime));
order.LocalTime = connector.CurrentTime.LocalDateTime;
order.State = OrderStates.Pending;
}
public static void CheckOnOld(this Order order)
{
order.ChechOrderState(false);
if (order.TransactionId == 0 && order.Id == 0 && order.StringId.IsEmpty())
throw new ArgumentException(LocalizedStrings.Str899, "order");
}
public static void CheckOption(this Security option)
{
if (option == null)
throw new ArgumentNullException("option");
if (option.Type != SecurityTypes.Option)
throw new ArgumentException(LocalizedStrings.Str900Params.Put(option.Type), "option");
if (option.OptionType == null)
throw new ArgumentException(LocalizedStrings.Str703Params.Put(option), "option");
if (option.ExpiryDate == null)
throw new ArgumentException(LocalizedStrings.Str901Params.Put(option), "option");
if (option.UnderlyingSecurityId == null)
throw new ArgumentException(LocalizedStrings.Str902Params.Put(option), "option");
}
public static ExchangeBoard CheckExchangeBoard(this Security security)
{
if (security == null)
throw new ArgumentNullException("security");
if (security.Board == null)
throw new ArgumentException(LocalizedStrings.Str903Params.Put(security), "security");
return security.Board;
}
public static IConnector CheckTrader(this Order order)
{
if (order == null)
throw new ArgumentNullException("order");
if (order.Connector == null)
throw new ArgumentException(LocalizedStrings.Str904Params.Put(order.TransactionId), "order");
return order.Connector;
}
public static bool ChangeContinuousSecurity(this IConnector connector, Order order)
{
if (connector == null)
throw new ArgumentNullException("connector");
if (order == null)
throw new ArgumentNullException("order");
var cs = order.Security as ContinuousSecurity;
while (cs != null)
{
order.Security = cs.GetSecurity(order.Security.ToExchangeTime(connector.CurrentTime));
cs = order.Security as ContinuousSecurity;
}
return true;
}
public static Security CheckPriceStep(this Security security)
{
if (security == null)
throw new ArgumentNullException("security");
if (security.PriceStep == 0)
throw new ArgumentException(LocalizedStrings.Str905Params.Put(security.Id));
return security;
}
public static int ChangeSubscribers<T>(this CachedSynchronizedDictionary<T, int> subscribers, T subscriber, int delta)
{
if (subscribers == null)
throw new ArgumentNullException("subscribers");
lock (subscribers.SyncRoot)
{
var value = subscribers.TryGetValue2(subscriber) ?? 0;
value += delta;
if (value > 0)
subscribers[subscriber] = value;
else
subscribers.Remove(subscriber);
return value;
}
}
}
}