Beispiel #1
0
        /// <summary>
        /// Gets the web data for a ticker
        /// </summary>
        /// <param name="ticker"></param>
        /// <returns></returns>
        private static HttpWebResponse GetWebData(TickerData ticker)
        {
            try
            {
                string yRange;
                // if quotes needs to be updated
                if (ticker.QuoteDataStatus >= QuoteDataStatus.Failed & ticker.Quotes != null)
                {
                    switch (ticker.Quotes.Periodicity)
                    {
                    case Periodicity.OneMinute:
                        yRange = "1d";
                        break;

                    case Periodicity.FiveMinutes:
                        yRange = "5d";
                        break;

                    case Periodicity.EndOfDay:
                        yRange = "3y";
                        break;

                    default:
                        throw new ArgumentException();
                    }
                }
                // if only RT window update is needed
                else
                {
                    yRange = "1d";
                }

                HttpWebResponse response = null;
                string          url      = "http://chartapi.finance.yahoo.com/instrument/1.0/" + ticker.Ticker + "/chartdata;type=quote;range=" + yRange + "/csv/";

                WebRequest request = WebRequest.Create(url);
                request.Credentials = CredentialCache.DefaultCredentials;

                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(response);
                }

                return(null);
            }
            catch (Exception ex)
            {
                LogAndMessage.LogAndAdd(MessageType.Error, "Web request failed: " + ex);
                return(null);
            }
        }
Beispiel #2
0
        public void Disconnect()
        {
            // stop timer
            timer.Change(Timeout.Infinite, Timeout.Infinite);

            // indicate disconnection
            connected = false;

            // clean up tickers from refreshQueue
            refreshQueue.Clear();

            LogAndMessage.LogAndAdd(MessageType.Info, "Stopped updating data.");
        }
Beispiel #3
0
        public void Connect()
        {
            LogAndMessage.LogAndAdd(MessageType.Info, "Start updating data at every " + config.RefreshPeriod.ToString() + " seconds.");

            // start timer
            timer.Change(1000, 300);

            // make it start an immediate refresh
            lastRun = -1;

            // indicate "successfull" connection
            connected = true;
        }
Beispiel #4
0
        /// <summary>
        /// Ticker event handler
        /// 1. It checks if the delay is over. Then it puts all tickers in the refreshQueue
        /// 2. It checks if there is anything in the refreshQueue and less then 3 requests are currently executing, then it starts up a threadpool thread to execute the refresh.
        /// </summary>
        /// <param name="sender"></param>
        private void timer_tick(object sender)
        {
            if (inTimer || !connected)
            {
                return;
            }

            inTimer = true;

            try
            {
                // if refresh is needed...
                long currentRun = (int)(DateTime.Now.TimeOfDay.TotalSeconds / config.RefreshPeriod);
                if (currentRun != lastRun)
                {
                    lastRun = currentRun;

                    // enqueue all tickers
                    string[] symbols = tickers.GetAllTickers();

                    foreach (string symbol in symbols)
                    {
                        EnqueueTickerForRefresh(symbol);
                    }
                }

                // it there are tickers enqueued and there are less then 3 downloads already running
                if (refreshQueue.Count > 0 && Interlocked.Read(ref concurrentDownloads) < 3)
                {
                    // increment no of downloads
                    Interlocked.Increment(ref concurrentDownloads);

                    // dequeue the ticker and get the tickerdata for it
                    string     ticker     = refreshQueue.Dequeue();
                    TickerData tickerData = tickers.GetTickerData(ticker);

                    // make an idle threadpool thread execute the download in the background
                    ThreadPool.QueueUserWorkItem(RefreshTicker, tickerData);
                }
            }
            catch (Exception ex)
            {
                LogAndMessage.LogAndAdd(MessageType.Error, "Failed to process all tickers: " + ex);
            }
            finally
            {
                DataSourceBase.NotifyQuotesUpdate();
                inTimer = false;
            }
        }
Beispiel #5
0
        public void GetQuotesEx(string ticker, ref QuotationArray quotes)
        {
            // save database periodicity at the very first call
            if (!firstGetQuotesExCall)
            {
                periodicity          = quotes.Periodicity;
                firstGetQuotesExCall = true;
            }

            try
            {
                TickerData tickerData = tickers.GetTickerData(ticker);
                // if ticker is not yet known (first time to use in charts and no RT window use)
                if (tickerData == null)
                {
                    tickerData = tickers.RegisterTicker(ticker);
                }

                if (tickerData.QuoteDataStatus == QuoteDataStatus.Offline)
                {
                    // mark it for periodic Quotation data update
                    tickerData.MarkTickerForGetQuotes(periodicity);

                    // we do not want to run data refresh on AB's thread...
                    // we also want async processing of historical data download...
                    if (connected)
                    {
                        EnqueueTickerForRefresh(ticker);
                    }
                }

                lock (tickerData)
                {
                    // merge downloaded data to AB's quotation array
                    quotes.Merge(tickerData.Quotes);

                    // save last download time
                    tickerData.LastDownloaded = tickerData.LastProcessed;
                }
            }
            catch (Exception ex)
            {
                LogAndMessage.LogAndAdd(MessageType.Error, "Failed to subscribe to quote update: " + ex);
            }
        }
Beispiel #6
0
        public void UpdateRecentInfo(string ticker)
        {
            try
            {
                TickerData tickerData = tickers.GetTickerData(ticker);
                // if ticker is not yet known (first time to use in RT window and no use in charts)
                if (tickerData == null)
                {
                    tickerData = tickers.RegisterTicker(ticker);
                }

                tickerData.MarkTickerForRecentInfo();
            }
            catch (Exception ex)
            {
                LogAndMessage.LogAndAdd(MessageType.Error, "Failed to subscribe to real time window update: " + ex);
            }
        }
Beispiel #7
0
        public override PluginStatus GetStatus()
        {
            PluginStatus status = new PluginStatus();

            if (database.IsConnected)
            {
                status.Status       = StatusCode.OK;
                status.Color        = System.Drawing.Color.ForestGreen;
                status.ShortMessage = "Ready";
            }
            else
            {
                status.Status       = StatusCode.Warning;
                status.Color        = System.Drawing.Color.Red;
                status.ShortMessage = "Error";
            }

            status.LongMessage = LogAndMessage.GetMessages();

            // if there is no message, we show short message
            if (string.IsNullOrEmpty(status.LongMessage))
            {
                status.LongMessage = status.ShortMessage;
                // save as the last shown message to avoid status popup
                lastLongMessage = status.ShortMessage;
            }

            // if new message we use a new lastLongMessageTime value to cause status popup
            if (lastLongMessage != status.LongMessage)
            {
                lastLongMessage     = status.LongMessage;
                lastLongMessageTime = (int)DateTime.Now.TimeOfDay.TotalMilliseconds;
            }

            // set status and "timestamp"
            status.Status = (StatusCode)((int)status.Status + lastLongMessageTime);

            SetContextMenuState();

            return(status);
        }
Beispiel #8
0
        internal AmiVar GetExtraData(string ticker, string name, Periodicity periodicity, int arraySize)
        {
            if (!connected)
            {
                return(new AmiVar(ATFloat.Null));            // to prevent AFL engine to report AFL method call failure
            }
            try
            {
                TickerData tickerData = tickers.GetTickerData(ticker);

                // if it is a new ticker or  data is not available yet
                if (string.IsNullOrEmpty(name) | tickerData == null)
                {
                    return(new AmiVar(ATFloat.Null));        // to prevent AFL engine to report AFL method call failure
                }

                lock (tickerData)
                {
                    string[] parts = name.Split('.');
                    object   obj   = tickerData;
                    Type     type;

                    // walk the object hierarchy using reflection
                    for (int i = 0; i < parts.GetLength(0); i++)
                    {
                        type = obj.GetType();
                        obj  = type.InvokeMember(parts[i], BindingFlags.Default | BindingFlags.GetField | BindingFlags.GetProperty, null, obj, null);
                        if (obj == null && i < parts.GetLength(0) - 1)
                        {
                            LogAndMessage.LogAndAdd(MessageType.Warning, "Extra data field does not exist: " + name, tickerData);
                            return(new AmiVar(ATFloat.Null));            // to prevent AFL engine to report AFL method call failure
                        }
                    }

                    // convert object value to AmiVar and return it to AB

                    if (obj == null)       // it was a string or an object
                    {
                        return(new AmiVar(""));
                    }

                    Type valType = obj.GetType();

                    if (valType == typeof(bool))
                    {
                        return(new AmiVar((bool)obj ? 1.0f : 0.0f));
                    }

                    if (valType.BaseType == typeof(System.Enum))
                    {
                        return(new AmiVar((int)obj));
                    }

                    if (valType == typeof(short))
                    {
                        return(new AmiVar((short)obj));
                    }

                    if (valType == typeof(int))
                    {
                        return(new AmiVar((int)obj));
                    }

                    if (valType == typeof(long))
                    {
                        return(new AmiVar((long)obj));
                    }

                    if (valType == typeof(float))
                    {
                        return(new AmiVar((float)obj));
                    }

                    if (valType == typeof(double))
                    {
                        return(new AmiVar((float)(double)obj));
                    }

                    if (valType == typeof(string))
                    {
                        return(new AmiVar((string)obj));
                    }

                    if (valType == typeof(DateTime))
                    {
                        return(new AmiVar(ATFloat.DateTimeToABDateNum((DateTime)obj)));
                    }

                    if (valType == typeof(TimeSpan))
                    {
                        return(new AmiVar(ATFloat.TimeSpanToABTimeNum((TimeSpan)obj)));
                    }

                    return(new AmiVar(ATFloat.Null));            // to prevent AFL engine to report AFL method call failure
                }
            }
            catch (MissingMethodException)
            {
                LogAndMessage.LogAndAdd(MessageType.Warning, "Extra data field does not exist: " + name);
                return(new AmiVar(ATFloat.Null));                // to prevent AFL engine to report AFL method call failure
            }
            catch (Exception ex)
            {
                LogAndMessage.LogAndAdd(MessageType.Error, "Failed to get extra data: " + ex);
                return(new AmiVar(ATFloat.Null));                // to prevent AFL engine to report AFL method call failure
            }
        }
Beispiel #9
0
        private void mDisconnect_Click(object sender, EventArgs e)
        {
            LogAndMessage.Log(MessageType.Info, "Manually disconnected.");

            database.Disconnect();
        }
Beispiel #10
0
        public override bool Notify(ref PluginNotification notifyData)
        {
            bool result = true;

            switch (notifyData.Reason)
            {
            case Reason.DatabaseLoaded:

                // if database is loaded
                if (database != null)
                {
                    // disconnect from data provider and reset all data
                    database.Disconnect();
                }

                // start logging the opening of the database
                LogAndMessage.Log(MessageType.Info, "Database: " + notifyData.DatabasePath);

                allowMixedEodIntra = notifyData.Workspace.AllowMixedEODIntra != 0;
                LogAndMessage.Log(MessageType.Info, "Mixed EOD/Intra: " + allowMixedEodIntra);

                numBars = notifyData.Workspace.NumBars;
                LogAndMessage.Log(MessageType.Info, "Number of bars: " + numBars);

                LogAndMessage.Log(MessageType.Info, "Database config: " + Settings);

                // create the config object
                config = YConfiguration.GetConfigObject(Settings);

                // create new database object
                database = new YDatabase(config, notifyData.Workspace);

                // connect database to data provider
                database.Connect();

                break;

            // user changed the db
            case Reason.DatabaseUnloaded:

                // disconnect from data provider
                if (database != null)
                {
                    database.Disconnect();
                }

                // clean up
                database = null;

                break;

            // seams to be obsolete
            case Reason.SettingsChanged:

                break;

            // user right clicks data plugin area in AB
            case Reason.RightMouseClick:

                if (database != null)
                {
                    currentTicker = notifyData.CurrentSI.ShortName;

                    SetContextMenuState();

                    ShowContextMenu(mContextMenu);
                }

                break;

            default: result = false;

                break;
            }
            return(result);
        }