Exemple #1
0
		public static DateTimeOffset StrToTime(this string str)
		{
			var parsedDate = new DateTime
			(
				DateTime.Now.Year,
				DateTime.Now.Month,
				DateTime.Now.Day,
				int.Parse(str.Substring(0, 2)),
				int.Parse(str.Substring(2, 2)),
				int.Parse(str.Substring(4, 2))
			);

			return parsedDate.ApplyTimeZone(TimeHelper.Est);
		}
Exemple #2
0
		/// <summary>
		/// Проверить, является ли дата торгуемой.
		/// </summary>
		/// <param name="security">Инструмент.</param>
		/// <param name="date">Передаваемая дата, которую необходимо проверить.</param>
		/// <returns><see langword="true"/>, если торгуемая дата, иначе, неторгуемая.</returns>
		public static bool IsTradeDate(this HydraTaskSecurity security, DateTime date)
		{
			if (security == null)
				throw new ArgumentNullException(nameof(security));

			return security.Security.Board.IsTradeDate(date.ApplyTimeZone(security.Security.Board.TimeZone), true);
		}
Exemple #3
0
		public static DateTimeOffset StrToDateTime(this string str)
		{
			var parsedDate = new DateTime
			(
				int.Parse(str.Substring(0, 4)),
				int.Parse(str.Substring(4, 2)),
				int.Parse(str.Substring(6, 2)),
				int.Parse(str.Substring(8, 2)),
				int.Parse(str.Substring(10, 2)),
				int.Parse(str.Substring(12, 2))
			);

			return parsedDate.ApplyTimeZone(TimeHelper.Est);
		}
		private void OnNewBar(int row, int rowCount, string smartId, SmartComTimeFrames timeFrame, DateTime time, decimal open, decimal high, decimal low, decimal close, decimal volume, decimal openInt)
		{
			this.AddDebugLog("OnNewHistoryTrade row = {0} rowCount = {1} securityId = {2} timeFrame = {3} time = {4} open = {5} high = {6} low = {7} close = {8} volume = {9} openInt = {10}",
				row, rowCount, smartId, timeFrame, time, open, high, low, close, volume, openInt);

			var infos = _candleTransactions.TryGetValue(smartId);
			var timeFrameKey = (TimeSpan)timeFrame;

			Tuple<long, List<CandleMessage>> transactionInfo;
			if (infos == null || !infos.TryGetValue(timeFrameKey, out transactionInfo))
			{
				this.AddErrorLog(LocalizedStrings.Str1855Params, smartId, timeFrame);
				return;
			}

			transactionInfo.Item2.Add(new TimeFrameCandleMessage
			{
				SecurityId = new SecurityId { Native = smartId },
				OpenPrice = open,
				HighPrice = high,
				LowPrice = low,
				ClosePrice = close,
				TotalVolume = volume,
				OpenTime = time.ApplyTimeZone(TimeHelper.Moscow) - (TimeSpan)timeFrame,
				CloseTime = time.ApplyTimeZone(TimeHelper.Moscow),
				OpenInterest = openInt,
				OriginalTransactionId = transactionInfo.Item1,
				IsFinished = row == (rowCount - 1),
			});

			if ((row + 1) < rowCount)
				return;

			transactionInfo.Item2.OrderBy(c => c.OpenTime).ForEach(SendOutMessage);

			infos.Remove(timeFrameKey);

			if (infos.IsEmpty())
				_candleTransactions.Remove(smartId);
		}
		private static ExecutionMessage CreateTrade(string smartId, DateTime time, decimal price, decimal volume, long tradeId, SmartOrderAction action)
		{
			return new ExecutionMessage
			{
				SecurityId = new SecurityId { Native = smartId },
				TradeId = tradeId,
				TradePrice = price,
				Volume = volume,
				OriginSide = action.ToSide(),
				ServerTime = time.ApplyTimeZone(TimeHelper.Moscow),
				ExecutionType = ExecutionTypes.Tick,
			};
		}
		private void OnOrderChanged(string portfolioName, string secSmartId, SmartOrderState state, SmartOrderAction action, SmartOrderType smartType, bool isOneDay,
			decimal? price, int volume, decimal? stop, int balance, DateTime time, string smartOrderId, long orderId, int status, int transactionId)
		{
			this.AddInfoLog(() => "SmartTrader.UpdateOrder: id {0} smartId {1} type {2} direction {3} price {4} volume {5} balance {6} time {7} security {8} state {9}"
				.Put(orderId, smartOrderId, smartType, action, price, volume, balance, time, secSmartId, state));

			var side = action.ToSide();

			if (side == null)
				throw new InvalidOperationException(LocalizedStrings.Str1872Params.Put(action, orderId, transactionId));

			// http://stocksharp.com/forum/yaf_postsm28324_Oshibka-pri-vystavlienii-ili-sniatii-zaiavki.aspx#post28324
			if (transactionId == 0)
				return;

			if (state.IsReject())
			{
				// заявка была ранее зарегистрирована через SmartTrader
				//if (_smartIdOrders.ContainsKey(smartOrderId))
				//{
				// замечены SystemCancel приходящие в процессе Move после которых приходит Active
				if (state != SmartOrderState.SystemCancel)
				{
					//var trId = GetTransactionBySmartId(smartOrderId);

					SendOutMessage(new ExecutionMessage
					{
						ExecutionType = ExecutionTypes.Order,
						OriginalTransactionId = transactionId,
						OrderStringId = smartOrderId,
						ServerTime = time.ApplyTimeZone(TimeHelper.Moscow),
						OrderState = OrderStates.Failed,
						Error = new InvalidOperationException(LocalizedStrings.Str1873Params.Put(transactionId)),
					});
				}
				//}

				return;
			}

			var orderType = smartType.ToOrderType();

			var orderState = OrderStates.Active;
			var orderStatus = OrderStatus.Accepted;

			switch (state)
			{
				case SmartOrderState.ContragentReject:
					orderStatus = OrderStatus.NotAcceptedByManager;
					orderState = OrderStates.Failed;
					break;
				// Принята ТС
				case SmartOrderState.Submited:
					orderStatus = OrderStatus.SentToServer;
					break;
				// Зарегистрирована в ТС
				case SmartOrderState.Pending:
					orderStatus = OrderStatus.ReceiveByServer;

					if (orderType == OrderTypes.Conditional)
						orderState = OrderStates.Active;

					break;
				// Выведена на рынок
				case SmartOrderState.Open:
					orderStatus = OrderStatus.Accepted;

					if (orderType == OrderTypes.Conditional && orderId != 0)
						orderState = OrderStates.Done;
					else
						orderState = OrderStates.Active;
					break;
				// Снята по окончанию торгового дня
				case SmartOrderState.Expired:
					orderState = OrderStates.Done;
					break;
				// Отменёна
				case SmartOrderState.Cancel:
					orderState = OrderStates.Done;
					break;
				// Исполнена
				case SmartOrderState.Filled:
					orderState = OrderStates.Done;
					break;
				// Частично исполнена
				case SmartOrderState.Partial:
					if (orderState == OrderStates.None)
						orderState = OrderStates.Active;
					break;
				// Отклонена биржей
				case SmartOrderState.ContragentCancel:
					orderStatus = OrderStatus.CanceledByManager;
					orderState = OrderStates.Failed;
					break;
				// Отклонена биржей
				case SmartOrderState.SystemReject:
					orderStatus = OrderStatus.NotDone;
					orderState = OrderStates.Failed;
					break;
				// Отклонена биржей
				case SmartOrderState.SystemCancel:
					orderStatus = OrderStatus.GateError;
					orderState = OrderStates.Failed;
					break;
				default:
					throw new ArgumentOutOfRangeException();
			}

			SendOutMessage(new ExecutionMessage
			{
				SecurityId = new SecurityId { Native = secSmartId },
				PortfolioName = portfolioName,
				Side = (Sides)side,
				OrderPrice = price ?? 0,
				Volume = volume,
				ServerTime = time.ApplyTimeZone(TimeHelper.Moscow),
				Balance = balance,
				OrderId = orderId == 0 ? (long?)null : orderId,
				OrderType = orderType,
				OrderState = orderState,
				OrderStatus = orderStatus,
				OriginalTransactionId = transactionId,
				OrderStringId = smartOrderId,
				ExpiryDate = isOneDay ? DateTimeOffset.Now.Date.ApplyTimeZone(TimeHelper.Moscow) : (DateTimeOffset?)null,
				Condition = orderType == OrderTypes.Conditional ? new SmartComOrderCondition { StopPrice = stop } : null,
				ExecutionType = ExecutionTypes.Order,
			});
		}
		private void OnNewMyTrade(string portfolio, string smartId, long orderId, decimal? price, decimal? volume, DateTime time, long tradeId)
		{
			this.AddInfoLog("SmartTrader.AddTrade: tradeId {0} orderId {1} price {2} volume {3} time {4} security {5}",
				tradeId, orderId, price, volume, time, smartId);

			SendOutMessage(new ExecutionMessage
			{
				ExecutionType = ExecutionTypes.Trade,
				SecurityId = new SecurityId { Native = smartId },
				OrderId = orderId == 0 ? (long?)null : orderId,
				TradeId = tradeId,
				ServerTime = time.ApplyTimeZone(TimeHelper.Moscow),
				Volume = volume,
				TradePrice = price,
			});
		}