Exemple #1
0
 void tl_gotTick(Tick t)
 {
     try
     {
         _tlt.newTick(t);
         _kt.newTick(t);
         if (spillTick != null)
         {
             spillTick(t);
         }
         RefreshRow(t);
         BarList bl = null;
         if (bardict.TryGetValue(t.symbol, out bl))
         {
             if (SecurityImpl.Parse(t.symbol).Type == SecurityType.CASH)
             {
                 Tick    k = _kt[t.symbol];
                 decimal p = usebidonfx ? k.bid : k.ask;
                 int     s = usebidonfx ? k.bs : k.os;
                 bardict[t.symbol].newPoint(t.symbol, p, k.time, k.date, s);
             }
             else
             {
                 bardict[t.symbol].newTick(t);
             }
         }
     }
     catch (System.Threading.ThreadInterruptedException) { }
 }
        public override void GotTick(Tick tick)           //数据进来,先进gottick,如果需要对成交价等进行更改,在gottick里进行
        {
            // keep track of time from tick

            _time = tick.time;
            if (_date > tick.date)
            {
                // this is important for consistency between runs for ATSTestBench and ATSTestBenchBatch
                Reset();
            }
            _date = tick.date;
            // ensure response is active
            if (!isValid)
            {
                return;
            }
            // ensure we are tracking active status for this symbol
            int idx = _active.addindex(tick.symbol, true);

            // if we're not active, quit
            if (!_active[idx])
            {
                return;
            }
            // check for shutdown time
            // apply bar tracking to all ticks that enter
            _kt.newTick(tick);
            _blt.newTick(tick);

            // ignore anything that is not a trade
            if (!tick.isTrade)
            {
                return;                      //如果没有trade,这个数据是用不到的
            }
        }
Exemple #3
0
 public override void GotTick(Tick tick)
 {
     // track order ids
     _orderid.addindex(tick.symbol, 0);
     // track ticks
     _kt.newTick(tick);
     // see if we need to send an order
     if (_orderid[tick.symbol] == 0)
     {
         // get complete last tick for this symbol
         Tick k = _kt[tick.symbol];
         // see if we have proper info to place order
         if ((Side && k.hasBid) || (!Side && k.hasAsk))
         {
             _orderid[tick.symbol] = _idt.NextId;
             D(tick.symbol + " sending limit order: " + _orderid[tick.symbol]);
             sendorder(new LimitOrder(tick.symbol, Side, Ordersize, Side ? k.bid - Distance : k.ask + Distance, _idt.AssignId));
         }
     }
     else // otherwise increment counter
     {
         _tickcounter[tick.symbol]++;
     }
     // see if we need to cancel
     if (_tickcounter[tick.symbol] > Frequency)
     {
         D(tick.symbol + " hit frequency, canceling: " + _orderid[tick.symbol]);
         sendcancel(_orderid[tick.symbol]);
     }
 }
        /// <summary>
        /// Called whenever we got a tick signal
        /// </summary>
        /// <param name="k"></param>
        public override void GotTick(Tick k)
        {
            // Sync symbols
            if (_symbols.getindex(k.symbol) < 0)
            {
                _symbols.addindex(k.symbol, k.symbol);
            }

            SyncDateTime(k);

            // ensure response is active
            if (!isValid)
            {
                return;
            }

            // ensure we are tracking active status for this symbol
            int idx = _active.addindex(k.symbol, true);

            // if we're not active, quit
            if (!_active[idx])
            {
                return;
            }

            //// check for shutdown time
            //if (k.time > Shutdown)
            //{
            //    // if so, shutdown the system
            //    if (!_isShutDown)
            //    {
            //        shutdown();
            //    }
            //    // and quit
            //    return;
            //}
            //else
            //{
            //    // make sure the flag is on
            //    _isShutDown = false;
            //}
            // apply bar tracking to all ticks that enter
            _kt.newTick(k);
            _blt.newTick(k);

            // ignore anything that is not a trade
            if (!k.isTrade)
            {
                return;
            }
        }
        // GotTick is called everytime a new quote or trade occurs
        public override void GotTick(TradeLink.API.Tick tick)
        {
            // tmp workaround for "how to show debug messages from response constructor"
            if (debug_message_from_constructor.Length > 0)
            {
                D(debug_message_from_constructor);
                debug_message_from_constructor = "";
            }

            // keep track of time from tick
            time = tick.time;

            // safe tick to files
            //_ta.newTick(tick);

            // ignore quotes
            if (!tick.isTrade)
            {
                return;
            }

            // ignore ticks with timestamp prior to 9:30:00am
            if (tick.time < 93000)
            {
                return;
            }

            // ensure we track this symbol, all the other trackers will be indexed inside track_symbols_NewTxt()
            track_symbols.addindex(tick.symbol, false);

            // track tick
            track_ticks.newTick(tick);
            track_barlists.newTick(tick); // dimon: give any ticks (trades) to this symbol and tracker will create barlists automatically

            // if we don't have enough bars, wait for more ticks
            if (!track_barlists[tick.symbol].Has(_slow_ma_bar))
            {
                return;
            }
        }
        public void Basics()
        {
            kt = new TickTracker();

            // startup tests
            Assert.IsFalse(kt[sym].isValid,"valid tick at startup");
            Assert.IsFalse(kt[sym].isTrade,"trade at startup");
            Assert.IsFalse(kt[sym].isQuote,"quote at startup");
            Assert.IsFalse(kt[sym].hasAsk,"ask at startup");
            Assert.IsFalse(kt[sym].hasBid,"bid at startup");

            // trade occurs
            kt.newTick(TickImpl.NewTrade(sym, p, s));

            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.IsTrue(kt[sym].isTrade, "no trade at startup");
            Assert.AreEqual(p, kt.Last(sym),"wrong trade price");
            Assert.AreEqual(s, kt[sym].size, "wrong trade size");
            Assert.IsFalse(kt[sym].isQuote, "quote at startup");
            Assert.IsFalse(kt[sym].hasAsk, "ask at startup");
            Assert.IsFalse(kt[sym].hasBid, "bid at startup");

            // bid
            kt.newTick(TickImpl.NewBid(sym, p, s));
            // verify bid
            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.AreEqual(p, kt.Bid(sym), "wrong bid price");
            Assert.AreEqual(ss, kt[sym].bs, "wrong bid size");
            Assert.IsFalse(kt[sym].hasAsk, "ask at startup");
            Assert.IsTrue(kt[sym].hasBid, "bid at startup");

            // verify trade still shows
            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.IsTrue(kt[sym].isTrade, "no trade at startup");
            Assert.AreEqual(p, kt.Last(sym), "wrong trade price");
            Assert.AreEqual(s, kt[sym].size, "wrong trade size");
            Assert.IsFalse(kt[sym].isQuote, "quote at startup");
            Assert.IsFalse(kt[sym].hasAsk, "ask at startup");
            Assert.IsTrue(kt[sym].hasBid, "bid at startup");

            // ask
            kt.newTick(TickImpl.NewAsk(sym, p, s));
            // verify ask
            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.IsTrue(kt[sym].isFullQuote, "no quote at startup");
            Assert.AreEqual(p, kt.Ask(sym), "wrong ask price");
            Assert.AreEqual(ss, kt[sym].os, "wrong ask size");
            Assert.IsTrue(kt[sym].hasAsk, "ask at startup");
            Assert.IsTrue(kt[sym].hasBid, "bid at startup");


            // verify trade still shows
            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.IsTrue(kt[sym].isTrade, "no trade at startup");
            Assert.AreEqual(p, kt.Last(sym), "wrong trade price");
            Assert.AreEqual(s, kt[sym].size, "wrong trade size");
            Assert.IsFalse(kt[sym].isQuote, "quote at startup");
            Assert.IsTrue(kt[sym].hasAsk, "ask at startup");
            Assert.IsTrue(kt[sym].hasBid, "bid at startup");

            // verify bid still shows
            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.IsTrue(kt[sym].isFullQuote, "no quote at startup");
            Assert.AreEqual(p, kt.Bid(sym), "wrong bid price");
            Assert.AreEqual(ss, kt[sym].bs, "wrong bid size");
            Assert.IsTrue(kt[sym].hasAsk, "ask at startup");
            Assert.IsTrue(kt[sym].hasBid, "bid at startup");


            

            
        }
        // GotTick is called everytime a new quote or trade occurs
        public override void GotTick(TradeLink.API.Tick tick)
        {
            // store tick
            tick_archiver.newTick(tick);
            return; // for now only track/store ticks here...

            // ignore quotes
            if (!tick.isTrade)
            {
                return;
            }

            // ensure we track this symbol, all the other trackers will be indexed inside track_symbols_NewTxt()
            track_symbols.addindex(tick.symbol, false);

            // track tick
            track_ticks.newTick(tick);

            // another "track tick" :)
            //
            // For now we track tick in 2 different trackers:
            // TickTracker
            // and
            // BarListTracker (connected with MessageTracker)
            //
            // todo: Read more on topic of TickTracker vs BarListTracker. And eventually get rid of one of them.
            //      TickTracker - seems have more functionality, but
            //      BarListTracker - can build bars with any interval (we can mix diff. bar size in one strategy easily using this!)
            //
            track_barlists.newTick(tick); // dimon: give any ticks (trades) to this symbol and tracker will create barlists automatically

            // if we don't have enough bars, wait for more ticks
            //if (!track_barlists[tick.symbol].Has(BarsBack)) return;

            // get current position
            Position pos = track_positions[tick.symbol];

            // if we're flat and haven't seen this symbol yet, then...
            if (pos.isFlat && !track_symbols[tick.symbol])
            {
                // strart tracking it (other trackers will be updated accordingly, see track_symbols_NewTxt()
                track_symbols[tick.symbol] = true;

                D(tick.symbol + ": entering long");
                O(new MarketOrder(tick.symbol, EntrySize));
            }
            else if (!pos.isFlat && !track_exitsignals[tick.symbol])
            {
                // get most recent tick data
                Tick k = track_ticks[tick.symbol];
                // estimate our exit price
                decimal exitprice = UseQuotes
                    ? (k.hasAsk && pos.isLong ? k.ask
                    : (k.hasBid && pos.isShort ? k.bid : 0))
                    : (k.isTrade ? k.trade : 0);
                // assuming we could estimate an exit, see if our exit would hit our target
                if ((exitprice != 0) && (Calc.OpenPT(exitprice, pos) > ProfitTarget))
                {
                    track_exitsignals[tick.symbol] = true;
                    D("hit profit target");
                    O(new MarketOrderFlat(pos));
                }
            }
            // --------------------------------------------
            //
            // this is a grey box that manages exits, so wait until we have a position
            //if (!pt[tick.symbol].isFlat) return;
            //
            //// calculate the MA from closing bars
            //decimal MA = Calc.Avg(Calc.Closes(track_barlists[tick.symbol], BarsBack));

            //// if we're short, a cross is when market moves above MA
            //// if we're long, cross is when market goes below MA
            //bool cross = pt[tick.symbol].isShort ? (tick.trade > MA) : (tick.trade < MA);

            //// if we have a cross, then flat us for the requested size
            //if (cross)
            //    sendorder(new MarketOrderFlat(pt[tick.symbol], exitpercent));

            //// notify gauntlet and kadina about our moving average and cross
            //sendindicators(new string[] { MA.ToString(), cross.ToString() });
            // --------------------------------------------
        }
        // GotTick is called everytime a new quote or trade occurs
        public override void GotTick(TradeLink.API.Tick tick)
        {
            base.GotTick(tick);

            // keep track of time from tick
            time = tick.time;

            // ignore quotes
            if (!tick.isTrade)
            {
                return;
            }

            // ignore ticks with timestamp prior to 9:30:00am
            if (tick.time < 93000)
            {
                return;
            }

            // --------------------------------------------------- rabbitmq begin -----------
            log_file.WriteLine(JsonConvert.SerializeObject(tick, Formatting.Indented));    // write all ticks into external file (to get a feeling on size)
            if (tick.time == 93205)
            {
                ;
            }
            if (false)
            {
                if (tick.time > 93000 && tick.time < 93500)
                {
                    string rabbit_serverAddress = "amqp://localhost/";
                    string rabbit_exchange      = "exch";
                    string rabbit_exchangeType  = "fanout";
                    string rabbit_routingKey    = "rout";
                    string rabbit_message       = JsonConvert.SerializeObject(tick, Formatting.Indented);

                    ConnectionFactory rabbit_cf = new ConnectionFactory();
                    rabbit_cf.Uri = rabbit_serverAddress;
                    IConnection rabbit_conn = rabbit_cf.CreateConnection();
                    IModel      rabbit_ch   = rabbit_conn.CreateModel();
                    rabbit_ch.ExchangeDeclare(rabbit_exchange, rabbit_exchangeType);
                    IBasicProperties msg_props = rabbit_ch.CreateBasicProperties();
                    msg_props.ContentType = "text/plain";
                    rabbit_ch.BasicPublish(rabbit_exchange,
                                           rabbit_routingKey,
                                           msg_props,
                                           Encoding.UTF8.GetBytes(rabbit_message));     // or Encoding.UTF8.GetBytes();  - we convert message into a byte array
                }
            }
            // --------------------------------------------------- rabbitmq end -----------

            // ensure we track this symbol, all the other trackers will be indexed inside track_symbols_NewTxt()
            track_symbols.addindex(tick.symbol, false);

            // track tick
            track_ticks.newTick(tick);

            // track (custom) bars
            track_barlists.newTick(tick); // dimon: give any ticks (trades) to this symbol and tracker will create barlists automatically

            // check if need to exit position:
            log_file.WriteLine("check if need to exit position: track_positions[tick.symbol].isLong=" + track_positions[tick.symbol].isLong.ToString());

            if (!track_positions[tick.symbol].isLong) // isFlat)        - we sell only if we have long positions
            {
                // should exit long position due to hit target?
                if (track_positions[tick.symbol].isLong)
                {
                    // time to exit long position?
                    bool should_exit = track_positions[tick.symbol].AvgPrice * (decimal)_target_price_k <= tick.trade;
                    if (should_exit)
                    {
                        string comment = "exit long position due to hit target";
                        sendorder(new SellMarket(tick.symbol, EntrySize, comment));
                        log_file.WriteLine("close position: " + comment);
                        return;
                    }
                }

                // should exit short position due to hit target?
                if (track_positions[tick.symbol].isShort)
                {
                    bool should_exit = track_positions[tick.symbol].AvgPrice * (decimal)_target_price_k >= tick.trade;
                    if (should_exit)
                    {
                        string comment = "exit short position due to hit target";
                        sendorder(new BuyMarket(tick.symbol, EntrySize, comment));
                        log_file.WriteLine("close position: " + comment);
                        return;
                    }
                }

                // should exit long position due to hit stop?
                if (track_positions[tick.symbol].isLong)
                {
                    bool should_exit = track_positions[tick.symbol].AvgPrice * (decimal)_stop_k >= tick.trade;
                    if (should_exit)
                    {
                        string comment = "exit long position due to hit stop";
                        sendorder(new SellMarket(tick.symbol, EntrySize, comment));
                        log_file.WriteLine("close position: " + comment);
                        return;
                    }
                }

                // should exit short position due to hit stop?
                if (track_positions[tick.symbol].isShort)
                {
                    bool should_exit = track_positions[tick.symbol].AvgPrice * (decimal)_target_price_k >= tick.trade;
                    if (should_exit)
                    {
                        string comment = "exit short position due to hit stop";
                        sendorder(new BuyMarket(tick.symbol, EntrySize, comment));
                        log_file.WriteLine("close position: " + comment);
                        return;
                    }
                }
            }
        }
        public void Basics()
        {
            kt = new TickTracker();

            // startup tests
            Assert.IsFalse(kt[sym].isValid, "valid tick at startup");
            Assert.IsFalse(kt[sym].isTrade, "trade at startup");
            Assert.IsFalse(kt[sym].isQuote, "quote at startup");
            Assert.IsFalse(kt[sym].hasAsk, "ask at startup");
            Assert.IsFalse(kt[sym].hasBid, "bid at startup");

            // trade occurs
            kt.newTick(TickImpl.NewTrade(sym, p, s));

            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.IsTrue(kt[sym].isTrade, "no trade at startup");
            Assert.AreEqual(p, kt.Last(sym), "wrong trade price");
            Assert.AreEqual(s, kt[sym].size, "wrong trade size");
            Assert.IsFalse(kt[sym].isQuote, "quote at startup");
            Assert.IsFalse(kt[sym].hasAsk, "ask at startup");
            Assert.IsFalse(kt[sym].hasBid, "bid at startup");

            // bid
            kt.newTick(TickImpl.NewBid(sym, p, s));
            // verify bid
            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.AreEqual(p, kt.Bid(sym), "wrong bid price");
            Assert.AreEqual(ss, kt[sym].bs, "wrong bid size");
            Assert.IsFalse(kt[sym].hasAsk, "ask at startup");
            Assert.IsTrue(kt[sym].hasBid, "bid at startup");

            // verify trade still shows
            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.IsTrue(kt[sym].isTrade, "no trade at startup");
            Assert.AreEqual(p, kt.Last(sym), "wrong trade price");
            Assert.AreEqual(s, kt[sym].size, "wrong trade size");
            Assert.IsFalse(kt[sym].isQuote, "quote at startup");
            Assert.IsFalse(kt[sym].hasAsk, "ask at startup");
            Assert.IsTrue(kt[sym].hasBid, "bid at startup");

            // ask
            kt.newTick(TickImpl.NewAsk(sym, p, s));
            // verify ask
            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.IsTrue(kt[sym].isFullQuote, "no quote at startup");
            Assert.AreEqual(p, kt.Ask(sym), "wrong ask price");
            Assert.AreEqual(ss, kt[sym].os, "wrong ask size");
            Assert.IsTrue(kt[sym].hasAsk, "ask at startup");
            Assert.IsTrue(kt[sym].hasBid, "bid at startup");


            // verify trade still shows
            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.IsTrue(kt[sym].isTrade, "no trade at startup");
            Assert.AreEqual(p, kt.Last(sym), "wrong trade price");
            Assert.AreEqual(s, kt[sym].size, "wrong trade size");
            Assert.IsFalse(kt[sym].isQuote, "quote at startup");
            Assert.IsTrue(kt[sym].hasAsk, "ask at startup");
            Assert.IsTrue(kt[sym].hasBid, "bid at startup");

            // verify bid still shows
            Assert.IsTrue(kt[sym].isValid, "no valid tick at startup");
            Assert.IsTrue(kt[sym].isFullQuote, "no quote at startup");
            Assert.AreEqual(p, kt.Bid(sym), "wrong bid price");
            Assert.AreEqual(ss, kt[sym].bs, "wrong bid size");
            Assert.IsTrue(kt[sym].hasAsk, "ask at startup");
            Assert.IsTrue(kt[sym].hasBid, "bid at startup");
        }