Ejemplo n.º 1
0
        public void Fill_RegularLiquidity()
        {
            SimBroker broker = new SimBroker();

            broker.BrokerModel = _trans;
            ForexSecurity tsec = new ForexSecurity(S);

            tsec.LotSize       = 1;
            tsec.OrderStepSize = 1;

            OrderImpl limitBuy  = new OrderImpl(tsec, Direction.Long, 1, 133m);
            OrderImpl limitSell = new OrderImpl(tsec, Direction.Short, 1, 133.5m);
            OrderImpl stopBuy   = new OrderImpl(tsec, Direction.Long, 3, 0, 135.70m);
            OrderImpl stopSell  = new OrderImpl(tsec, Direction.Short, 4, 0, 135.75m);

            PendingOrderImpl plimitBuy  = new PendingOrderImpl(limitBuy);
            PendingOrderImpl plimitSell = new PendingOrderImpl(limitSell);
            PendingOrderImpl pstopBuy   = new PendingOrderImpl(stopBuy);
            PendingOrderImpl pstopSell  = new PendingOrderImpl(stopSell);

            broker.SendOrderStatus(plimitBuy);
            broker.SendOrderStatus(plimitSell);
            broker.SendOrderStatus(pstopBuy);
            broker.SendOrderStatus(pstopSell);

            // OHLC for 6/21/2012 on SPY
            TickImpl openingTick  = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(9, 30, 00, 000), 135.67m, 10670270, "NYS");
            TickImpl endMornTick  = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(12, 00, 00, 000), 135.78m, 10670270, "NYS");
            TickImpl endLunchTick = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(14, 15, 00, 000), 132.33m, 10670270, "NYS");
            TickImpl closingTick  = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(16, 00, 00, 000), 132.44m, 10670270, "NYS");

            broker.Execute(openingTick);
            broker.Execute(endMornTick);
            broker.Execute(endLunchTick);
            broker.Execute(closingTick);

            List <Trade> trades = broker.GetTradeList();

            Assert.True(trades.Count == 4);

            foreach (Trade trade in trades)
            {
                if (trade.Xsize == 1)
                {
                    Assert.Equal(132.33m, trade.Xprice);
                }
                else if (trade.Xsize == 2)
                {
                    Assert.Equal(132.33m, trade.Xprice);
                }
                else if (trade.Xsize == 3)
                {
                    Assert.Equal(135.78m, trade.Xprice);
                }
                else if (trade.Xsize == 4)
                {
                    Assert.Equal(135.78m, trade.Xprice);
                }
            }
        }
Ejemplo n.º 2
0
        public void MOCs()
        {
            SimBroker broker = new SimBroker();

            broker.BrokerModel = _trans;
            ForexSecurity tsec = new ForexSecurity(S);

            tsec.LotSize       = 1;
            tsec.OrderStepSize = 1;

            OrderImpl moc = new OrderImpl(tsec, Direction.Long, 200);

            moc.ValidInstruct = OrderInstructionType.MOC;
            PendingOrderImpl pmoc = new PendingOrderImpl(moc);

            Assert.True(moc.ValidInstruct == OrderInstructionType.MOC, "unexpected order instruction: " + moc.ValidInstruct);
            Assert.Equal(0, broker.SendOrderStatus(pmoc));

            TickImpl openingTick  = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(9, 30, 00, 000), 9, 10000, "NYS");
            TickImpl endMornTick  = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(12, 00, 00, 000), 9, 10000, "NYS");
            TickImpl endLunchTick = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(14, 15, 00, 000), 9, 10000, "NYS");
            TickImpl closingTick  = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(16, 00, 00, 000), 9, 10000, "NYS");

            int c = 0;

            c = broker.Execute(openingTick); Assert.Equal(0, c);
            c = broker.Execute(endMornTick); Assert.Equal(0, c);
            c = broker.Execute(endLunchTick); Assert.Equal(0, c);
            c = broker.Execute(closingTick); Assert.Equal(1, c); // should execute on the first tick at/after 16:00:00
        }
Ejemplo n.º 3
0
        public void DayFill()
        {
            SimBroker broker = new SimBroker();

            broker.BrokerModel = _trans;
            ForexSecurity tsec = new ForexSecurity(S);

            tsec.LotSize       = 1;
            tsec.OrderStepSize = 1;

            OrderImpl        day  = new OrderImpl(tsec, Direction.Long, 200);
            PendingOrderImpl pday = new PendingOrderImpl(day);

            broker.SendOrderStatus(pday);

            TickImpl openingTick  = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(9, 30, 00, 000), 9, 10000, "NYS");
            TickImpl endMornTick  = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(12, 00, 00, 000), 9, 10000, "NYS");
            TickImpl endLunchTick = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(14, 15, 00, 000), 9, 10000, "NYS");
            TickImpl closingTick  = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(16, 00, 00, 000), 9, 10000, "NYS");

            int c;

            c = broker.Execute(openingTick); Assert.Equal(1, c); // should execute on first received tick
            c = broker.Execute(endMornTick); Assert.Equal(0, c);
            c = broker.Execute(endLunchTick); Assert.Equal(0, c);
            c = broker.Execute(closingTick); Assert.Equal(0, c);
        }
Ejemplo n.º 4
0
        public void OpGs()
        {
            SimBroker broker = new SimBroker();

            broker.BrokerModel = _trans;
            const string  s    = "NYS";
            ForexSecurity tsec = new ForexSecurity(s);

            tsec.LotSize       = 1;
            tsec.OrderStepSize = 1;

            // build and send an OPG order
            OrderImpl        opg  = new OrderImpl(tsec, Direction.Long, 200, 10);
            PendingOrderImpl popg = new PendingOrderImpl(opg);

            Assert.Equal(0, broker.SendOrderStatus(popg));

            // build a tick on another exchange
            TickImpl it = TickImpl.NewTrade(s, 9, 100);

            it.Exchange = "ISLD";

            // fill order (should fail)
            int c = broker.Execute(it);

            Assert.Equal(0, c);

            // build opening price for desired exchange
            TickImpl nt = TickImpl.NewTrade(s, 9, 10000);

            nt.Exchange = "NYS";
            // fill order (should work)

            c = broker.Execute(nt);

            Assert.Equal(1, c);

            // add another OPG, make sure it's not filled with another tick

            TickImpl next = TickImpl.NewTrade(s, 9, 2000);

            next.Exchange = "NYS";

            OrderImpl        late  = new OrderImpl(tsec, Direction.Long, 200, 10);
            PendingOrderImpl plate = new PendingOrderImpl(late);

            broker.SendOrderStatus(plate);
            c = broker.Execute(next);
            Assert.Equal(0, c);
        }
Ejemplo n.º 5
0
 void HistSource_gotTick(Tick t)
 {
     // process next tick if present
     if (hasnext)
     {
         // execute any pending orders
         SimBroker.Execute(next);
         // send existing tick
         if (hasevent)
         {
             GotTick(next);
         }
         // update last time
         lasttime = next.datetime;
         orderok &= lasttick || (t.datetime >= next.datetime) || (cidx != next.symidx);
     }
     if (lasttick)
     {
         hasnext = false;
         return;
     }
     // update next tick
     next          = t;
     next.symidx   = cidx;
     hasnext       = true;
     _nextticktime = t.datetime;
     _tickcount++;
 }
Ejemplo n.º 6
0
        void FlushCache(long endsim)
        {
            bool simrunning = true;

            while (simrunning)
            {
                // get next times of ticks in cache
                long[] times = nexttimes();
                // copy our master index list into a temporary for sorting
                Buffer.BlockCopy(idx, 0, cidx, 0, idx.Length * 4);
                // sort loaded instruments by time
                Array.Sort(times, cidx);
                int nextidx = 0;
                // get next time from all instruments we have loaded
                while ((nextidx < times.Length) && (times[nextidx] == -1))
                {
                    nextidx++;
                }
                // test to see if ticks left in simulation
                simrunning = (nextidx < times.Length) && (times[nextidx] <= endsim);
                // if no ticks left or we exceeded simulation time, quit
                if (!simrunning)
                {
                    break;
                }
                // get next tick
                Tick k = Workers[cidx[nextidx]].NextTick();
                // process pending orders
                _executions += SimBroker.Execute(k);
                // notify tick
                GotTick(k);
                // count tick
                _tickcount++;
            }
        }
Ejemplo n.º 7
0
        public void Basics()
        {
            ForexSecurity tsec = new ForexSecurity(S);

            tsec.LotSize       = 1;
            tsec.PipValue      = 1;
            tsec.PipSize       = 1;
            tsec.OrderStepSize = 1;

            SimAccount account = new SimAccount("TEST", "testing", 1000M, 100);

            account.Securities.AddSecurity(tsec);

            SimBroker broker = new SimBroker(account, _trans);

            broker.BrokerModel = _trans;
            broker.GotFill    += broker_GotFill;
            broker.GotOrder   += broker_GotOrder;
            OrderImpl        o  = new OrderImpl();
            PendingOrderImpl po = new PendingOrderImpl(o);
            int error           = broker.SendOrderStatus(po);

            Assert.NotEqual((int)StatusType.OK, error);
            Assert.True(_orders == 0);
            Assert.True(_fills == 0);

            o  = new OrderImpl(tsec, Direction.Long, 100);
            po = new PendingOrderImpl(o);

            broker.SendOrderStatus(po);
            Assert.True(_orders == 1);
            Assert.True(_fills == 0);
            Assert.True(broker.Execute(TickImpl.NewTrade(S, 10, 200)) == 1);
            Assert.True(_fills == 1);

            // test that a limit order is not filled outside the market
            o  = new OrderImpl(tsec, Direction.Long, 100, 9);
            po = new PendingOrderImpl(o);

            broker.SendOrderStatus(po);
            Assert.Equal(0, broker.Execute(TickImpl.NewTrade(S, 10, 100)));
            Assert.True(_fills == 1); // redundant but for counting

            // test that limit order is filled inside the market
            Assert.Equal(1, broker.Execute(TickImpl.NewTrade(S, 8, 100)));
            Assert.True(_fills == 2);
        }
Ejemplo n.º 8
0
        private void histsim_GotTick(Tick t)
        {
            TickImpl tick = (TickImpl)t;

            // execute any pending orders
            SimBroker.Execute(tick);

            // send any new orders from our application
            while (orders.Count > 0)
            {
                SimBroker.SendOrder(orders.Dequeue());
            }

            //Set pending orders
            SetPendingOrders();

            //Count ticks
            TicksProcessed++;

            try
            {
                //Check if this symbol is subscribed
                if (portfolio.Streams.ContainsKey(t.Symbol))
                {
                    //Set got tick for portfolio
                    portfolio.GotTick(tick);
                }
            }
            catch (Exception ex)
            {
                if (OnMessage != null)
                {
                    OnMessage(this, "Portfolio threw exception: - " + ex.Message + Environment.NewLine + ex.StackTrace);
                }
            }

            double percent = Math.Round((histsim.FilesProcessed / (double)histsim.FilesPresent) * 100);

            if (percent > lastp)
            {
                //Update percentage
                if (OnProgress != null)
                {
                    OnProgress(this, string.Format("Progress: {0}%, ROI: {1}%, DD: {2}", percent, portfolio.Results.ROI, portfolio.Results.MaxDDPortfolio), 0);
                }
                lastp = percent;
            }
        }
Ejemplo n.º 9
0
        public void MultiAccount()
        {
            _broker.BrokerModel = _trans;
            const string  sym  = "TST";
            ForexSecurity tsec = new ForexSecurity(sym);

            tsec.LotSize       = 1;
            tsec.OrderStepSize = 1;

            const string me    = "tester";
            const string other = "anotherguy";
            SimAccount   a     = new SimAccount(me);
            SimAccount   b     = new SimAccount(other);
            SimAccount   c     = new SimAccount("sleeper");
            OrderImpl    oa    = new OrderImpl(tsec, Direction.Long, 100);
            OrderImpl    ob    = new OrderImpl(tsec, Direction.Long, 100);

            oa.AccountName = me;
            ob.AccountName = other;
            // send order to accounts
            PendingOrderImpl poa = new PendingOrderImpl(oa, a);
            PendingOrderImpl pob = new PendingOrderImpl(ob, b);

            _broker.SendOrderStatus(poa);
            _broker.SendOrderStatus(pob);

            TickImpl t = new TickImpl(sym)
            {
                Trade = 100m,
                Size  = 200
            };

            Assert.Equal(2, _broker.Execute(t));
            Position apos = _broker.GetOpenPosition(tsec, a);
            Position bpos = _broker.GetOpenPosition(tsec, b);
            Position cpos = _broker.GetOpenPosition(tsec, c);

            Assert.True(apos.IsLong);
            Assert.Equal(100, apos.Size);
            Assert.True(bpos.IsLong);
            Assert.Equal(100, bpos.Size);
            Assert.True(cpos.IsFlat);
            // make sure that default account doesn't register
            // any trades
            Assert.True(_broker.GetOpenPosition(tsec).IsFlat);
        }
Ejemplo n.º 10
0
        private void execute_GotTick(Tick t)
        {
            TickImpl tick = (TickImpl)t;

            SimBroker.Execute(tick);
            tickcount++;
            // generate fills periodically
            if (fillcount >= desiredfills)
            {
                return;
            }
            if (tickcount % 50 == 0)
            {
                bool side = fillcount % 2 == 0;

                OrderImpl    o  = new OrderImpl(new ForexSecurity(t.Symbol), side ? Direction.Long : Direction.Short, 100);
                PendingOrder po = new PendingOrderImpl(o);

                SimBroker.SendOrderStatus(po);
            }
        }