Ejemplo n.º 1
0
        /// <summary>
        /// Background thread.
        /// </summary>
        public void Run()
        {
            EncogLogging.Log(EncogLogging.LevelDebug, "Waiting for packets");

            try
            {
                while (!_done)
                {
                    IndicatorPacket packet = Link.ReadPacket();

                    // really do not care if we timeout, just keep listening
                    if (packet == null)
                    {
                        continue;
                    }
                    if (string.Compare(packet.Command,
                                       IndicatorLink.PacketHello, true) == 0)
                    {
                        RemoteType    = packet.Args[0];
                        IndicatorName = packet.Args[1];
                        _listener     = _server
                                        .ResolveIndicator(IndicatorName);
                        _listener.NotifyConnect(Link);
                    }
                    else if (string.Compare(packet.Command,
                                            IndicatorLink.PacketGoodbye, true) == 0)
                    {
                        _done = true;
                    }
                    else
                    {
                        _listener.NotifyPacket(packet);
                    }
                }
            }
            catch (IndicatorError ex)
            {
                EncogLogging.Log(EncogLogging.LevelDebug,
                                 "Client ended connection:" + ex.Message);

                _done = true;
            }
            catch (Exception t)
            {
                EncogLogging.Log(EncogLogging.LevelCritical, t);
            }
            finally
            {
                _done = true;
                _server.Connections.Remove(this);
                if (_listener != null)
                {
                    _listener.NotifyTermination();
                }
                _server.NotifyListenersConnections(Link, false);
                EncogLogging.Log(EncogLogging.LevelDebug,
                                 "Shutting down client handler");
                Link.Close();
            }
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public override void NotifyPacket(IndicatorPacket packet)
        {
            if (string.Compare(packet.Command, IndicatorLink.PACKET_BAR, true) == 0)
            {
                String security = packet.Args[1];
                long when = long.Parse(packet.Args[0]);
                String key = security.ToLower();
                InstrumentHolder holder = null;

                if (this.data.ContainsKey(key))
                {
                    holder = this.data[key];
                }
                else
                {
                    holder = new InstrumentHolder();
                    this.data[key] = holder;
                }

                if (holder.Record(when, 2, packet.Args))
                {
                    this.rowsDownloaded++;
                }
            }
        }
Ejemplo n.º 3
0
        public override void NotifyPacket(IndicatorPacket packet)
        {
            if (string.Compare(packet.Command, IndicatorLink.PACKET_BAR) == 0)
            {
                String security = packet.Args[1];
                long when = long.Parse(packet.Args[0]);

                double dataClose = CSVFormat.EgFormat.Parse(packet.Args[2]);
                double lastValue = CSVFormat.EgFormat.Parse(packet.Args[4]);
                double period = 14;

                double result;

                if (double.IsNaN(lastValue))
                    result = dataClose;
                else
                    result = dataClose * (2.0 / (1 + period)) + (1 - (2.0 / (1 + period))) * lastValue;

                String[] args = {
                    CSVFormat.EgFormat.Format(result,EncogFramework.DefaultPrecision),
                    "?",
                    "?",
                    "?",
                    "?",
                    "?",
                    "?",
                    "?"};

                this.Link.WritePacket(IndicatorLink.PACKET_IND, args);
            }
        }
Ejemplo n.º 4
0
        public override void NotifyPacket(IndicatorPacket packet)
        {
            if (string.Compare(packet.Command, IndicatorLink.PacketBar, true) == 0)
            {                
                double dataClose = CSVFormat.EgFormat.Parse(packet.Args[2]);
                double lastValue = CSVFormat.EgFormat.Parse(packet.Args[4]);
                const double period = 14;

                double result;

                if (double.IsNaN(lastValue))
                    result = dataClose;
                else
                    result = dataClose*(2.0/(1 + period)) + (1 - (2.0/(1 + period)))*lastValue;

                String[] args = {
                                    CSVFormat.EgFormat.Format(result, EncogFramework.DefaultPrecision),
                                    "?",
                                    "?",
                                    "?",
                                    "?",
                                    "?",
                                    "?",
                                    "?"
                                };

                Link.WritePacket(IndicatorLink.PacketInd, args);
            }
        }
Ejemplo n.º 5
0
 public abstract void NotifyPacket(IndicatorPacket packet);
Ejemplo n.º 6
0
        /// <summary>
        /// Called to notify the indicator that a bar has been received. 
        /// </summary>
        /// <param name="packet">The packet received.</param>
        public override void NotifyPacket(IndicatorPacket packet)
        {
            long when = long.Parse(packet.Args[0]);         

            if (_method == null)
            {
                if (_holder.Record(when, 2, packet.Args))
                {
                    _rowsDownloaded++;
                }
            }
            else
            {
                var input = new BasicMLData(Config.PredictWindow);

                const int fastIndex = 2;
                const int slowIndex = fastIndex + Config.InputWindow;

                for (int i = 0; i < 3; i++)
                {
                    double fast = CSVFormat.EgFormat.Parse(packet.Args[fastIndex + i]);
                    double slow = CSVFormat.EgFormat.Parse(packet.Args[slowIndex + i]);
                    double diff = _fieldDifference.Normalize((fast - slow)/Config.PipSize);
                    input[i] = _fieldDifference.Normalize(diff);
                }

                IMLData result = _method.Compute(input);

                double d = result[0];
                d = _fieldOutcome.DeNormalize(d);

                String[] args = {
                                    "?", // line 1
                                    "?", // line 2
                                    CSVFormat.EgFormat.Format(d, EncogFramework.DefaultPrecision), // bar 1
                                }; // arrow 2

                Link.WritePacket(IndicatorLink.PacketInd, args);
            }
        }