Ejemplo n.º 1
0
        private async Task RequestSubscribeForSymbol(string symbol, params ProtoOATrendbarPeriod[] periods)
        {
            await ValidateConnected();

            if (periods.Length == 0)
            {
                periods = new ProtoOATrendbarPeriod[] {
                    ProtoOATrendbarPeriod.M1 // TEMP TEST - For Spotware troubleshooting missing trendbars
                };
            }

            var subscribed = GetSubscribed(symbol);

            List <ProtoOATrendbarPeriod> list = new List <ProtoOATrendbarPeriod>(periods.Where(p => !subscribed.Contains(p)));

            // TODO: Verify previously subscribed trendbars stay subscribed.  Otherwise, eliminate the where clause above.

            var _msg = outgoingMsgFactory.CreateSubscribeForSpotsRequest(AccountId, AccessToken, symbol, clientMsgId, list);

            if (isDebugIsOn)
            {
                //#if LOG_SENTITIVE_INFO
#if TRACE_SUBSCRIPTIONS
                Debug.WriteLine("[tradeapi] SendSubscribeForSpotsRequest(): {0}", OpenApiMessagesPresentation.ToString(_msg));
#endif
                //#else
                //                Console.WriteLine("SendSubscribeForSpotsRequest(): {0}", OpenApiMessagesPresentation.ToString(_msg));
                //#endif
            }
            writeQueueSync.Enqueue(_msg.ToByteArray());
        }
Ejemplo n.º 2
0
        public static TimeSpan GetMaximumTime(this ProtoOATrendbarPeriod period)
        {
            switch (period)
            {
            case ProtoOATrendbarPeriod.M1:
            case ProtoOATrendbarPeriod.M2:
            case ProtoOATrendbarPeriod.M3:
            case ProtoOATrendbarPeriod.M4:
            case ProtoOATrendbarPeriod.M5:
                return(TimeSpan.FromMilliseconds(302400000));

            case ProtoOATrendbarPeriod.M10:
            case ProtoOATrendbarPeriod.M15:
            case ProtoOATrendbarPeriod.M30:
            case ProtoOATrendbarPeriod.H1:
                return(TimeSpan.FromMilliseconds(21168000000));

            case ProtoOATrendbarPeriod.H4:
            case ProtoOATrendbarPeriod.H12:
            case ProtoOATrendbarPeriod.D1:
                return(TimeSpan.FromMilliseconds(31622400000));

            case ProtoOATrendbarPeriod.W1:
            case ProtoOATrendbarPeriod.Mn1:
                return(TimeSpan.FromMilliseconds(158112000000));

            default:
                throw new ArgumentOutOfRangeException(nameof(period));
            }
        }
Ejemplo n.º 3
0
        public static ProtoMessage Get_Trendbars_Req(long ctidTraderAccountId,
                                                     long fromTimestamp,
                                                     long toTimestamp,
                                                     ProtoOATrendbarPeriod period,
                                                     long symbolId = 0)
        {
            ProtoOAGetTrendbarsReq message = new ProtoOAGetTrendbarsReq
            {
                payloadType         = ProtoOAPayloadType.ProtoOaGetTrendbarsReq,
                ctidTraderAccountId = ctidTraderAccountId,
                fromTimestamp       = fromTimestamp,
                toTimestamp         = toTimestamp,
                Period = period
            };

            if (symbolId > 0)
            {
                message.symbolId = symbolId;
            }

            Log.Info("ProtoOAGetTrendbarsReq:: " +
                     $"ctidTraderAccountId: {ctidTraderAccountId}; " +
                     $"fromTimestamp: {fromTimestamp} ({EpochToString(fromTimestamp)}; " +
                     $"toTimestamp: {toTimestamp} ({EpochToString(toTimestamp)}; " +
                     $"period: {period}");

            InnerMemoryStream.SetLength(0);
            Serializer.Serialize(InnerMemoryStream, message);

            return(Encode((uint)message.payloadType, InnerMemoryStream.ToArray()));
        }
Ejemplo n.º 4
0
        partial void SubscribeToDefaultSymbols()
        {
            //RequestSubscribeForSymbol("EURUSD");
            //RequestSubscribeForSymbol("EURUSD", ProtoOATrendbarPeriod.M1);
            //RequestSubscribeForSymbol("EURUSD", ProtoOATrendbarPeriod.M1, ProtoOATrendbarPeriod.H1);

            {
                var defaultPeriods = new ProtoOATrendbarPeriod[] { ProtoOATrendbarPeriod.M1,
                                                                   //ProtoOATrendbarPeriod.H1
                                                                   //, ProtoOATrendbarPeriod.M2, ProtoOATrendbarPeriod.M3, ProtoOATrendbarPeriod.M5,
                                                                   //ProtoOATrendbarPeriod.M10
                };
                var defaultSymbols = new string[] {
                    //"AUDSGD",
                    //"EURUSD",
                    //"USDCHF",
                };


                foreach (var s in defaultSymbols)
                {
                    RequestSubscribeForSymbol(s, defaultPeriods).FireAndForget();
                }
            }

            //{
            //    var defaultPeriods = new ProtoOATrendbarPeriod[] { ProtoOATrendbarPeriod.M1
            //    //, ProtoOATrendbarPeriod.M2, ProtoOATrendbarPeriod.M3, ProtoOATrendbarPeriod.M5
            //};
            //    var defaultSymbols = new string[] {
            //    "CHFSGD",
            //    "AUS200",
            //};

            //    //RequestSubscribeForSymbol("EURUSD");
            //    //RequestSubscribeForSymbol("EURUSD", ProtoOATrendbarPeriod.M1);
            //    //RequestSubscribeForSymbol("EURUSD", ProtoOATrendbarPeriod.M1, ProtoOATrendbarPeriod.H1);
            //    foreach (var s in defaultSymbols)
            //    {
            //        RequestSubscribeForSymbol(s, defaultPeriods);
            //    }
            //}

            //RequestSubscribeForSymbol("USDJPY", ProtoOATrendbarPeriod.M1);
        }
        public ProtoMessage CreateTrendbarsRequest(long accountId, int symbolId, long from, long to, ProtoOATrendbarPeriod period, string clientMsgId = null)
        {
            var _msg = ProtoOAGetTrendbarsReq.CreateBuilder();

            _msg.SetCtidTraderAccountId(accountId);
            _msg.SetSymbolId(symbolId);
            _msg.SetFromTimestamp(from);
            _msg.SetToTimestamp(to);
            _msg.SetPeriod(period);
            return(CreateMessage((uint)_msg.PayloadType, _msg.Build().ToByteString(), clientMsgId));
        }
Ejemplo n.º 6
0
 private TimeSpan GetMaximumTrendBarTime(ProtoOATrendbarPeriod period) => period switch
 {
Ejemplo n.º 7
0
        public async Task <ProtoOATrendbar[]> GetTrendbars(long accountId, bool isLive, DateTimeOffset from, DateTimeOffset to, ProtoOATrendbarPeriod period, long symbolId)
        {
            VerifyConnection();

            var periodMaximum = GetMaximumTrendBarTime(period);

            if (from == default)
            {
                from = to.Add(-periodMaximum);
            }

            if (to - from > periodMaximum)
            {
                throw new ArgumentOutOfRangeException(nameof(to), "The time range is not valid");
            }

            var client = GetClient(isLive);

            List <ProtoOATrendbar> result = new();

            CancellationTokenSource cancelationTokenSource = new();

            using var disposable = client.OfType <ProtoOAGetTrendbarsRes>().Where(response => response.CtidTraderAccountId == accountId).Subscribe(response =>
            {
                result.AddRange(response.Trendbar);

                cancelationTokenSource.Cancel();
            });

            var requestMessage = new ProtoOAGetTrendbarsReq
            {
                FromTimestamp       = from.ToUnixTimeMilliseconds(),
                ToTimestamp         = to.ToUnixTimeMilliseconds(),
                CtidTraderAccountId = accountId,
                Period   = period,
                SymbolId = symbolId
            };

            await SendMessage(requestMessage, ProtoOAPayloadType.ProtoOaGetTrendbarsReq, client, cancelationTokenSource, () => cancelationTokenSource.IsCancellationRequested);

            return(result.ToArray());
        }
Ejemplo n.º 8
0
        public async Task UnsubscribeFromLiveTrendbar(long accountId, bool isLive, long symbolId, ProtoOATrendbarPeriod period)
        {
            VerifyConnection();

            var client = GetClient(isLive);

            using var cancelationTokenSource = new CancellationTokenSource();

            ProtoOAUnsubscribeLiveTrendbarRes receivedResponse = null;

            using var disposable = client.OfType <ProtoOAUnsubscribeLiveTrendbarRes>().Where(response => response.CtidTraderAccountId == accountId)
                                   .Subscribe(response =>
            {
                receivedResponse = response;

                cancelationTokenSource.Cancel();
            });

            var requestMessage = new ProtoOAUnsubscribeLiveTrendbarReq
            {
                CtidTraderAccountId = accountId,
                Period   = period,
                SymbolId = symbolId
            };

            await SendMessage(requestMessage, ProtoOAPayloadType.ProtoOaUnsubscribeLiveTrendbarReq, client, cancelationTokenSource, () => receivedResponse is not null);
        }
Ejemplo n.º 9
0
        public static ProtoMessage Subscribe_Live_Trendbar_Req(long ctidTraderAccountId, ProtoOATrendbarPeriod period, long symbolId)
        {
            ProtoOASubscribeLiveTrendbarReq message = new ProtoOASubscribeLiveTrendbarReq
            {
                payloadType         = ProtoOAPayloadType.ProtoOaSubscribeLiveTrendbarReq,
                ctidTraderAccountId = ctidTraderAccountId,
                Period   = period,
                symbolId = symbolId
            };

            Log.Info("ProtoOASubscribeLiveTrendbarReq:: " +
                     $"ctidTraderAccountId: {ctidTraderAccountId}; " +
                     $"Period: {period}; " +
                     $"symbolId: {symbolId}");

            InnerMemoryStream.SetLength(0);
            Serializer.Serialize(InnerMemoryStream, message);

            return(Encode((uint)message.payloadType, InnerMemoryStream.ToArray()));
        }