Exemplo n.º 1
0
        public Tuple <List <Bar>, TwsError> GetPreviousCloses(Contract contract, int days)
        {
            Semaphore semaphore = new Semaphore(0, 1);

            Tuple <List <Bar>, TwsError> reTuple;
            List <Bar> barsList = null;
            TwsError   error    = null;

            int reqId = int.MinValue;

            void hHistoricalBarReceived(object sender, HistoricalBarRecv_EventArgs e)
            {
                if (e.RequestId == reqId)
                {
                    if (barsList == null)
                    {
                        barsList = new List <Bar>();
                    }

                    barsList.Add(e.HistoricalBar);
                }
            }

            void hHistoricalDataReceived(object sender, RequestId_EventArgs e)
            {
                if (e.RequestId == reqId)
                {
                    semaphore.Release();
                }
            }

            void hErrorReceived(object sender, ErrorRecv_EventArgs e)
            {
                if (e.Error.id != -1 && e.Error.id == reqId)
                {
                    error = e.Error;
                    semaphore.Release();
                }
            }

            HistoricalBarReceived_Event     += hHistoricalBarReceived;
            HistoricalDataEndReceived_Event += hHistoricalDataReceived;
            ErrorReceived_Event             += hErrorReceived;

            reqId = RequestPreviousCloses(contract, days);
            RequestId_Event?.Invoke(contract, new RequestId_EventArgs(reqId));

            semaphore.WaitOne();

            HistoricalDataEndReceived_Event -= hHistoricalDataReceived;
            HistoricalBarReceived_Event     -= hHistoricalBarReceived;
            ErrorReceived_Event             -= hErrorReceived;

            reTuple = new Tuple <List <Bar>, TwsError>(barsList, error);
            return(reTuple);
        }
Exemplo n.º 2
0
 public ErrorRecv_EventArgs(TwsError err)
 {
     this.Error = err;
 }