Esempio n. 1
0
        public override void GotTick(TradeLink.API.Tick tick)
        {
            // ensure we track this symbol
            _entrysignal.addindex(tick.symbol, false);
            // track tick
            _kt.newTick(tick);
            // get current position
            Position p = _pt[tick.symbol];

            // if we're flat and no signal, enter
            if (p.isFlat && !_entrysignal[tick.symbol])
            {
                _entrysignal[tick.symbol] = true;
                D(tick.symbol + ": entering long");
                O(new MarketOrder(tick.symbol, EntrySize));
            }
            else if (!p.isFlat && !_exitsignal[tick.symbol])
            {
                // get most recent tick data
                Tick k = _kt[tick.symbol];
                // estimate our exit price
                decimal exitprice = UseQuotes
                    ? (k.hasAsk && p.isLong ? k.ask
                    : (k.hasBid && p.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, p) > ProfitTarget))
                {
                    _exitsignal[tick.symbol] = true;
                    D("hit profit target");
                    O(new MarketOrderFlat(p));
                }
            }
        }
Esempio n. 2
0
        void raw_GotTick(TradeLink.API.Tick t)
        {
            if (!syms.Contains(t.symbol))
            {
                syms.Add(t.symbol);
            }
            tickcount++;
            bool viol = t.datetime < lasttime;

            GOODTIME &= !viol;
            lasttime  = t.datetime;
        }
Esempio n. 3
0
 void execute_GotTick(TradeLink.API.Tick t)
 {
     tickcount++;
     // generate fills periodically
     if (fillcount >= desiredfills)
     {
         return;
     }
     if (tickcount % 50 == 0)
     {
         bool side = fillcount % 2 == 0;
         h.SimBroker.SendOrderStatus(new MarketOrder(t.symbol, side, 100));
     }
 }
Esempio n. 4
0
        public void GotTick(TradeLink.API.Tick tick)
        {
            // if we don't have 10 trades yet, add it
            if (_dt.Rows.Count < ROWS)
            {
                _dt.Rows.Add(tick.time, tick.symbol, tick.trade, tick.size, tick.ex);
                refresh();
                return;
            }

            // otherwise, go through list and check to see if it's bigger
            for (int i = 0; i < _dt.Rows.Count; i++)
            {
                if ((int)_dt.Rows[i]["TradeSize"] < tick.size)
                {
                    _dt.Rows[i].ItemArray = new object[] { tick.time, tick.symbol, tick.trade, tick.size, tick.ex };
                    refresh();
                    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 override void GotTick(TradeLink.API.Tick tick)
        {
            // ignore quotes
            if (!tick.isTrade)
            {
                return;
            }
            // get current position
            Position p = pt[tick.symbol];

            // if we're flat, enter
            if (p.isFlat)
            {
                D("entering long");
                O(new BuyMarket(tick.symbol, 1));
            }
            // otherwise if we're up 10/th of a point, flat us
            else if (Calc.OpenPT(tick.trade, p) > .1m)
            {
                D("hit profit target");
                O(new MarketOrderFlat(p));
            }
        }
Esempio n. 7
0
 static void HistSource_gotTick(TradeLink.API.Tick t)
 {
     _tw.newTick((TickImpl)t);
 }
Esempio n. 8
0
 void h_GotTick(TradeLink.API.Tick t)
 {
     tickcount++;
     bt.newTick(t);
 }
        // 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;
                    }
                }
            }
        }