Exemple #1
0
        [InlineData("COTTON", 0, 500, 0.01, CurrencyType.USD, 71.40, 1, 500)]               //High tick value and a high contract size
        //Test to see all pipvalue calculations
        public void PipValueCalcutionsCFD(string symbol, decimal tickvalue, decimal contractsize, decimal ticksize, CurrencyType basecurrency, decimal tick, decimal basevalue, decimal value)
        {
            //Arrange
            SimAccount naccount = new SimAccount("testing", "test account", 1000, 100);
            TickImpl   ntick    = new TickImpl(symbol);

            ntick.Bid     = tick;
            ntick.Ask     = tick;
            ntick.BidSize = int.MaxValue;
            ntick.AskSize = ntick.BidSize;
            TickImpl ntickbase = new TickImpl("USD" + basecurrency.ToString());

            ntickbase.Ask     = basevalue;
            ntickbase.Bid     = basevalue;
            ntickbase.BidSize = int.MaxValue;
            ntickbase.AskSize = int.MaxValue;

            CFDSecurity nsecurity = new CFDSecurity(symbol);

            nsecurity.TickValue    = tickvalue;
            nsecurity.ContractSize = contractsize;
            nsecurity.TickSize     = ticksize;
            nsecurity.Currency     = basecurrency;

            //add security
            naccount.Securities.AddSecurity(nsecurity);

            //Act
            naccount.OnTick(ntickbase);
            naccount.OnTick(ntick);
            naccount.OnTick(ntick);

            //Assert
            naccount.Securities[symbol].PipValue.Should().BeApproximately(value, .00001M);
        }
Exemple #2
0
        public void PositionAccountTest()
        {
            ForexSecurity ts      = new ForexSecurity(s);
            IAccount      account = new SimAccount("ME");

            account.Securities.AddSecurity(ts);

            TradeImpl t = new TradeImpl("TST", 100, 100);

            t.Account  = account;
            t.Security = ts;
            TradeImpl t2 = new TradeImpl("TST", 200, 200);

            Assert.True(t.IsValid);
            Assert.True(t2.IsValid);
            t2.AccountName = "HIM";
            PositionImpl p = new PositionImpl(t);

            p.Adjust(t);
            bool failed = false;

            try
            {
                p.Adjust(t2);
            }
            catch (Exception) { failed = true; }
            Assert.True(failed);
        }
Exemple #3
0
        public void InitAndAdjust()
        {
            string        sym     = "IBM";
            ForexSecurity ts      = new ForexSecurity(sym);
            IAccount      account = new SimAccount("TEST");

            account.Securities.AddSecurity(ts);

            // startup position tracker
            PositionTracker pt  = new PositionTracker(account);
            PositionTracker pt2 = new PositionTracker(account);
            // give pt our initial position
            PositionImpl init = new PositionImpl(ts, 0, 0, 0, account);

            pt.Adjust(init);
            pt2.Adjust(init);
            // fill a trade in both places
            TradeImpl fill = new TradeImpl(ts.Name, 100, 100);

            fill.Account  = account;
            fill.Security = ts;

            pt.Adjust(fill);
            pt2.Adjust(fill);
            // make sure it's only 100 in both places
            Assert.Equal(100, pt[sym].Size);
            Assert.Equal(100, pt2[sym].Size);
        }
Exemple #4
0
        public void Adjust()
        {
            string        s       = "IBM";
            ForexSecurity ts      = new ForexSecurity(s);
            IAccount      account = new SimAccount("TEST", "testing", 1000M, 100, "SIM");

            account.Securities.AddSecurity(ts);

            TradeImpl t1 = new TradeImpl(s, 100, 100);

            t1.Account  = account;
            t1.Security = ts;

            PositionTracker pt = new PositionTracker(account);

            // make we have no position yet
            Assert.True(pt[t1.Symbol].IsFlat);

            // send some adjustments
            decimal cpl = 0;

            cpl += pt.Adjust(t1);
            cpl += pt.Adjust(t1);

            // verify that adjustments took hold
            Assert.Equal(0, cpl);
            Assert.Equal(200, pt[t1.Symbol].Size);
        }
Exemple #5
0
        public void UsingTrades()
        {
            // long
            string        s  = "IBM";
            ForexSecurity ts = new ForexSecurity(s);

            ts.LotSize  = 1;
            ts.PipValue = 1;
            ts.PipSize  = 1;

            PortfolioManager portfolio = new TradingPortfolio();
            IAccount         account   = new SimAccount("TEST");

            portfolio.SetAccount(account);
            portfolio.Securities.AddSecurity(ts);

            TradeImpl t1 = new TradeImpl(s, 80, 100, dt);

            t1.Account  = account;
            t1.Security = ts;
            PositionImpl p = new PositionImpl(t1);

            Assert.True(p.IsLong);
            Assert.True(p.Size == 100);

            TradeImpl t2 = new TradeImpl(s, 84, -100, dt);

            t2.Account  = account;
            t2.Security = ts;

            decimal pl = p.Adjust(t2);

            Assert.True(p.IsFlat);
            Assert.Equal((84 - 80) * 100, pl);

            // short
            TradeImpl t3 = new TradeImpl(s, 84, -100, dt);

            t3.Account  = account;
            t3.Security = ts;

            p = new PositionImpl(t3);
            Assert.True(!p.IsLong);
            Assert.True(p.Size == -100);

            TradeImpl t4 = new TradeImpl(s, 80, 100, dt);

            t4.Account  = account;
            t4.Security = ts;

            pl = p.Adjust(new TradeImpl(t4));
            Assert.True(pl == (84 - 80) * 100);
            Assert.True(p.IsFlat);
        }
Exemple #6
0
        public void NewPosition()
        {
            string        s       = "IBM";
            ForexSecurity ts      = new ForexSecurity(s);
            IAccount      account = new SimAccount("TEST");

            account.Securities.AddSecurity(ts);
            PositionImpl p = new PositionImpl(ts, 80, 500, 0, account);

            PositionTracker pt = new PositionTracker(account);

            Assert.True(pt[s].IsFlat);
            pt.NewPosition(p);
            Assert.Equal(500, pt[s].Size);
        }
Exemple #7
0
        public void ClosedPL()
        {
            const string  sym = "RYN";
            ForexSecurity ts  = new ForexSecurity(sym);
            IAccount      acc = new SimAccount("TST");

            acc.Securities.AddSecurity(ts);

            PositionTracker pt = new PositionTracker(acc);
            Position        p  = new PositionImpl(ts, 44.39m, 800, 0, acc);

            pt.Adjust(p);
            Position p2 = new PositionImpl(ts, 44.39m, -800, 0, acc);

            pt.Adjust(p2);
            Assert.Equal(0, pt[sym].GrossPnL);
        }
Exemple #8
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);
        }
Exemple #9
0
        public void MaxDD()
        {
            const string  sym     = "TST";
            SimAccount    account = new SimAccount("TEST", "testing", 1000M, 100);
            ForexSecurity sec     = new ForexSecurity(sym);

            sec.PipValue = 1;
            sec.LotSize  = 1;
            sec.PipSize  = 1;
            account.Securities.AddSecurity(sec);

            IPositionTracker pt = account.Positions;

            System.Collections.Generic.List <Trade> fills = new System.Collections.Generic.List <Trade>();
            TradeImpl t = new TradeImpl(sym, 10, 100);

            t.Security = sec;
            t.Account  = account;
            System.Collections.Generic.List <decimal> ret = new System.Collections.Generic.List <decimal>();
            fills.Add(t);
            pt.Adjust(t);
            t          = new TradeImpl(sym, 11, -100);
            t.Security = sec;
            t.Account  = account;
            fills.Add(t);
            ret.Add(pt.Adjust(t));
            t          = new TradeImpl(sym, 11, -100);
            t.Security = sec;
            t.Account  = account;
            pt.Adjust(t);
            fills.Add(t);
            t          = new TradeImpl(sym, 13, 100);
            t.Account  = account;
            t.Security = sec;
            fills.Add(t);
            ret.Add(pt.Adjust(t));
            decimal maxdd  = Calc.MaxDdVal(ret.ToArray());
            decimal maxddp = Calc.MaxDDPct(fills);

            Assert.Equal(-300, maxdd);
            Assert.Equal(-.18m, Math.Round(maxddp, 2));
        }
Exemple #10
0
        public void BlankPositionReq()
        {
            string        sym     = "IBM";
            ForexSecurity ts      = new ForexSecurity(sym);
            IAccount      account = new SimAccount("TEST");

            account.Securities.AddSecurity(ts);

            PositionTracker pt     = new PositionTracker(account);
            bool            except = false;
            int             s      = 100;

            try
            {
                s = pt[sym].Size;
            }
            catch { except = true; }
            Assert.Equal(0, s);
            Assert.False(except);
        }
Exemple #11
0
        public void Basics()
        {
            SimAccount a = new SimAccount();

            Assert.True(a.IsValid);
            Assert.True(a.Id == "empty");
            const string myid = "quantler";

            a = new SimAccount(myid);
            Assert.True(a.IsValid);
            Assert.True(a.Id == myid);
            a = new SimAccount("SIM1", "Menno's Account", initialbalance, leverage);
            Assert.True(a.Balance == initialbalance);
            Assert.True(a.Leverage == leverage);
            Assert.True(a.Margin == 0);
            Assert.True(a.FreeMargin == (a.Equity - a.Margin));
            Assert.True(a.Equity == a.Balance);
            Assert.True(a.Currency == CurrencyType.USD);
            Assert.True(a.FloatingPnL == 0);
        }
Exemple #12
0
        public void AdjustedDateTime()
        {
            // long
            string        s  = "IBM";
            ForexSecurity ts = new ForexSecurity(s);

            ts.LotSize  = 1;
            ts.PipValue = 1;
            ts.PipSize  = 1;

            IAccount account = new SimAccount("TEST");

            account.Securities.AddSecurity(ts);

            TradeImpl t1 = new TradeImpl(s, 80, 100, dt);

            t1.Account  = account;
            t1.Security = ts;
            PositionImpl p = new PositionImpl(t1);

            p.LastModified.Should().BeAfter(DateTime.MinValue);
        }
Exemple #13
0
        private static PortfolioManager Get(string symbol)
        {
            //Initialize
            TradingPortfolio np = new TradingPortfolio();

            //ADD INITIAL SECURITIES
            ForexSecurity security = new ForexSecurity(symbol);

            security.LotSize  = 100000;     //Lots
            security.PipSize  = 0.0001M;    //PipSize in 4 decimal places
            security.TickSize = 0.00001M;   //Size of one tick
            security.PipValue = 1M;         //PipValue, if unable to calculate to base currency
            security.Digits   = 5;

            var account = new SimAccount("SIMULATED", "Sim account for backtesting", 10000, 100, "SIM");

            np.SetAccount(account);
            np.Securities.AddSecurity(security);

            //ADD AGENT AND INJECT TEMPLATES
            np.InjectAgent <ExampleTradingAgent>();

            //Set streams
            TradingAgent agent = (TradingAgent)np.Agents[0];

            agent.AgentId   = 115230;
            agent.TimeFrame = TimeSpan.FromSeconds(TimeFrameInSeconds);
            OHLCBarStream ls = new OHLCBarStream(np.Securities[symbol], np.Agents[0].TimeFrame);

            agent.SetDefaultStream(ls);
            ls.Initialize();
            agent.Initialize();
            agent.Start();

            SimpleBacktester.OnMessage  += SimpleBacktester_OnMessage;
            SimpleBacktester.OnProgress += SimpleBacktester_OnProgress;

            return(np);
        }
Exemple #14
0
        public void FlipSideInOneTrade()
        {
            // this is illegal on the exchanges, but supported by certain
            // retail brokers so we're going to allow Quantler to support it
            // BE CAREFUL WITH THIS FEATURE.  make sure you won't be fined for doing this, before you do it.
            string        s  = "IBM";
            ForexSecurity ts = new ForexSecurity(s);

            ts.LotSize  = 1;
            ts.PipValue = 1;
            ts.PipSize  = 1;

            PortfolioManager portfolio = new TradingPortfolio();
            IAccount         account   = new SimAccount("TEST");

            portfolio.SetAccount(account);
            portfolio.Securities.AddSecurity(ts);

            // long position
            var t = new TradeImpl(s, 100m, 200);

            t.Account  = account;
            t.Security = ts;
            decimal cpl = portfolio.Positions.Adjust(t);

            Assert.Equal(cpl, 0);
            // sell more than we've got to change sides
            TradeImpl flip = new TradeImpl(s, 99, -400);

            flip.Account  = account;
            flip.Security = ts;
            cpl           = portfolio.Positions.Adjust(flip);
            // make sure we captured close of trade
            Assert.Equal(-200, cpl);
            // make sure we captured new side and price
            Assert.Equal(-200, portfolio.Positions[s].Size);
            Assert.Equal(99, portfolio.Positions[s].AvgPrice);
        }
Exemple #15
0
        public void KeyRatios()
        {
            EquitySecurity ts = new EquitySecurity(sym);

            ts.LotSize  = 1;
            ts.PipValue = 1;
            ts.PipSize  = 1;
            IAccount account = new SimAccount("TEST");

            account.Securities.AddSecurity(ts);
            rt = new Results(.01m, account);

            // get some trades
            List <Trade> fills = new List <Trade>(new Trade[] {
                // go long
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = account, Commission = 1
                },                                                                                      // 100 @ $100
                // increase bet
                new TradeImpl(sym, p + inc, s * 2)
                {
                    Security = ts, Account = account, Commission = 2
                },                                                                                      // 300 @ $100.066666
                // take some profits
                new TradeImpl(sym, p + inc * 2, s * -1)
                {
                    Security = ts, Account = account, Commission = 1
                },                                                                                      // 200 @ 100.0666 (profit = 100 * (100.20 - 100.0666) = 13.34) / maxMIU(= 300*100.06666) = .04% ret
                // go flat (round turn)
                new TradeImpl(sym, p + inc * 2, s * -2)
                {
                    Security = ts, Account = account, Commission = 2
                },                                                                                      // 0 @ 0
                // go short
                new TradeImpl(sym, p, s * -2)
                {
                    Security = ts, Account = account, Commission = 2
                },                                                                                      // -200 @ 100
                // decrease bet
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = account, Commission = 1
                },                                                                                      // -100 @100
                // exit (round turn)
                new TradeImpl(sym, p + inc, s)
                {
                    Security = ts, Account = account, Commission = 1
                },                                                                                      // 0 @ 0 (gross profit = -0.10*100 = -$10)
                // do another entry
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = account, Commission = 1
                }                                                                                       // 100 @ 100
            });

            //fill all trades
            foreach (var t in fills)
            {
                account.Positions.Adjust(t);
            }

            // check ratios
#if DEBUG
            Console.WriteLine(rt.ToString());
#endif

            Assert.Equal(0.0238M, Math.Round(rt.PortfolioPctReturns.Average() * 100, 4));
            Assert.Equal(0.1028M, Math.Round(Calc.StdDev(rt.PortfolioPctReturns) * 100, 4));
            Assert.Equal(-9.496M, rt.SharpeRatio);
            Assert.Equal(-27.164M, rt.SortinoRatio);
            Assert.Equal(2.060M, rt.ProfitFactor);
            Assert.Equal(1100, rt.SharesTraded);
            Assert.Equal(11, Math.Round(rt.Commissions));
            Assert.Equal(19, Math.Round(rt.NetPL));
            Assert.Equal(10000, rt.InitialCapital);
            Assert.Equal(10019, Math.Round(rt.Balance));
            Assert.Equal(0.0019M, Math.Round(rt.ROI, 4));
            Assert.Equal(-0.0015M, Math.Round(rt.MaxDDPortfolio, 4));
        }
Exemple #16
0
        public void RoundTurnStat()
        {
            ForexSecurity ts = new ForexSecurity(sym);

            ts.LotSize = 1;
            ts.PipSize = 1;
            IAccount account = new SimAccount("TEST");

            account.Securities.AddSecurity(ts);
            rt = new Results(.01m, account);

            // get some trades
            List <Trade> fills = new List <Trade>(new Trade[] {
                // go long
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = account
                },
                // increase bet
                new TradeImpl(sym, p + inc, s * 2)
                {
                    Security = ts, Account = account
                },
                // take some profits
                new TradeImpl(sym, p + inc * 2, s * -1)
                {
                    Security = ts, Account = account
                },
                // go flat (round turn)
                new TradeImpl(sym, p + inc * 2, s * -2)
                {
                    Security = ts, Account = account
                },
                // go short
                new TradeImpl(sym, p, s * -2)
                {
                    Security = ts, Account = account
                },
                // decrease bet
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = account
                },
                // exit (round turn)
                new TradeImpl(sym, p + inc, s)
                {
                    Security = ts, Account = account
                },
                // do another entry
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = account
                }
            });

            //fill all trades
            foreach (var t in fills)
            {
                account.Positions.Adjust(t);
            }

            // check trade count
            Assert.Equal(fills.Count, rt.Trades);

            // check round turn count
            Assert.Equal(2, rt.RoundTurns);

            // verify trade winners
            Assert.Equal(2, rt.Winners);

            // verify round turn winners
            Assert.Equal(1, rt.RoundWinners);

            // verify round turn losers
            Assert.Equal(1, rt.RoundLosers);
        }
Exemple #17
0
        public void MultipleAccount()
        {
            // setup defaults for 1st and 2nd accounts and positions
            string        s   = "TST";
            ForexSecurity sym = new ForexSecurity(s);
            IAccount      a1  = new SimAccount("account1");

            a1.Securities.AddSecurity(sym);

            IAccount a2 = new SimAccount("account2");

            a1.Securities.AddSecurity(sym);

            int     s1 = 300;
            int     s2 = 500;
            decimal p  = 100m;

            // create position tracker
            PositionTracker pt = new PositionTracker(a1);

            // set initial position in 1st account
            pt.Adjust(new PositionImpl(sym, p, s1, 0, a1));

            // set initial position in 2nd account
            pt.Adjust(new PositionImpl(sym, p, s2, 0, a2));

            // verify I can query default account and it's correct
            Assert.Equal(s1, pt[sym].Size);
            // change default to 2nd account
            pt.DefaultAccount = a2;
            // verify I can query default and it's correct
            Assert.Equal(s2, pt[sym].Size);
            // verify I can query 1st account and correct
            Assert.Equal(s1, pt[sym, a1].Size);
            // verify I can query 2nd account and correct
            Assert.Equal(s2, pt[sym, a2].Size);
            // get fill in sym for 1st account
            TradeImpl f = new TradeImpl(sym.Name, p, s1);

            f.Account  = a1;
            f.Security = sym;
            pt.Adjust(f);
            // get fill in sym for 2nd account
            TradeImpl f2 = new TradeImpl(sym.Name, p, s2);

            f2.Account  = a2;
            f2.Security = sym;
            pt.Adjust(f2);
            // verify that I can querry 1st account and correct
            Assert.Equal(s1 * 2, pt[sym, a1].Size);
            // verify I can query 2nd account and correct
            Assert.Equal(s2 * 2, pt[sym, a2].Size);
            // reset
            pt.Clear();
            // ensure I can query first and second account and get flat symbols
            Assert.Equal(0, pt[sym].Size);
            Assert.Equal(0, pt[sym, a1].Size);
            Assert.Equal(0, pt[sym, a2].Size);
            Assert.True(pt[sym, a1].IsFlat);
            Assert.True(pt[sym, a2].IsFlat);
            Assert.True(pt[sym].IsFlat);
            Assert.Equal(null, pt.DefaultAccount);
        }
Exemple #18
0
        public void AccountBalanceUpdateForex()
        {
            //Arrange
            SimAccount    testaccount = new SimAccount("SIM1", "Menno's Account", initialbalance, leverage);
            ForexSecurity ts          = new ForexSecurity(sym);

            ts.LotSize      = 1000;
            ts.ContractSize = ts.LotSize;
            ts.PipSize      = 0.0001M;
            ts.TickSize     = ts.PipSize / 10;
            testaccount.Securities.AddSecurity(ts);
            TickImpl tick       = TickImpl.NewQuote(sym, p, p, int.MaxValue, int.MaxValue, ts.DestEx, ts.DestEx);
            TickImpl secondtick = TickImpl.NewQuote(sym, p + inc, p + inc, int.MaxValue, int.MaxValue, ts.DestEx, ts.DestEx);

            List <Trade> fills = new List <Trade>(new Trade[] {
                // go long
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                          // 1000 @ $1
                // increase bet
                new TradeImpl(sym, p + inc, s * 2)
                {
                    Security = ts, Account = testaccount, Commission = 2
                },                                                                                          // 2000 @ $1.1
                // take some profits
                new TradeImpl(sym, p + inc * 2, s * -1)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                          // -1000 @ $1.2 (profit = 1000 * (1.2 - 1.0) = 200)
                // go flat (round turn)
                new TradeImpl(sym, p + inc * 2, s * -2)
                {
                    Security = ts, Account = testaccount, Commission = 2
                },                                                                                          // -2000 @ $1.2 (profit = 2000 * (1.2 - 1.1) = 200)
                // go short
                new TradeImpl(sym, p, s * -2)
                {
                    Security = ts, Account = testaccount, Commission = 2
                },                                                                                          // -2000 @ $1
                // decrease bet
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                          // 1000 @ $1
                // exit (round turn)
                new TradeImpl(sym, p + inc, s)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                          // 1000 @ $1 (loss = 1000 * (1.2 - 1.0) = 100)
                // do another entry
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = testaccount, Commission = 1
                }                                                                                           // 100 @ 100
            });

            //Act, fill all trades
            testaccount.OnTick(tick);
            testaccount.OnTick(secondtick);
            foreach (var t in fills)
            {
                testaccount.OnFill(t);
            }

            //Assert
            Assert.True(testaccount.Balance == 10300);
            Assert.True(testaccount.Margin == 11);
            Assert.True(testaccount.MarginLevel == (testaccount.Equity / testaccount.Margin) * 100);
        }
Exemple #19
0
        public void AccountBalanceUpdateCFD()
        {
            //Arrange
            SimAccount  testaccount = new SimAccount("SIM1", "Menno's Account", initialbalance, leverage);
            CFDSecurity ts          = new CFDSecurity(sym);

            ts.LotSize      = 100;
            ts.ContractSize = 50;
            ts.PipSize      = 0.1M;
            ts.TickSize     = ts.PipSize;
            ts.TickValue    = 5;
            ts.Currency     = CurrencyType.USD;
            int size = 100;

            testaccount.Securities.AddSecurity(ts);
            TickImpl tick       = TickImpl.NewQuote(sym, p, p, int.MaxValue, int.MaxValue, ts.DestEx, ts.DestEx);
            TickImpl secondtick = TickImpl.NewQuote(sym, p + inc, p + inc, int.MaxValue, int.MaxValue, ts.DestEx, ts.DestEx);

            List <Trade> fills = new List <Trade>(new Trade[] {
                // go long
                new TradeImpl(sym, p, size)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                             // 1 @ $1
                // increase bet
                new TradeImpl(sym, p + inc, size * 2)
                {
                    Security = ts, Account = testaccount, Commission = 2
                },                                                                                             // 2 @ $1.1
                // take some profits
                new TradeImpl(sym, p + inc * 2, size * -1)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                             // -1 @ $1.2 (profit = 1 * (1.2 - 1.0) * 50 = 100)
                // go flat (round turn)
                new TradeImpl(sym, p + inc * 2, size * -2)
                {
                    Security = ts, Account = testaccount, Commission = 2
                },                                                                                             // -2 @ $1.2 (profit = 2 * (1.2 - 1.1) * 50 = 200)
                // go short
                new TradeImpl(sym, p, size * -2)
                {
                    Security = ts, Account = testaccount, Commission = 2
                },                                                                                             // -2 @ $1
                // decrease bet
                new TradeImpl(sym, p, size)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                             // 1 @ $1
                // exit (round turn)
                new TradeImpl(sym, p + inc, size)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                             // 1 @ $1 (loss = 1000 * (1.2 - 1.0) * 50 = 100)
                // do another entry
                new TradeImpl(sym, p, size)
                {
                    Security = ts, Account = testaccount, Commission = 1
                }                                                                                              // 1 @ $1
            });

            //Act, fill all trades
            testaccount.OnTick(tick);
            testaccount.OnTick(secondtick);
            foreach (var t in fills)
            {
                testaccount.OnFill(t);
            }

            //Assert
            Assert.True(testaccount.Balance == 10015);
            Assert.True(testaccount.Margin == 0);
            Assert.True(testaccount.MarginLevel == 1001500);
        }
Exemple #20
0
        public void LowMarginLevel()
        {
            //Arrange
            SimAccount    testaccount = new SimAccount("SIM1", "Menno's Account", initialbalance, leverage);
            ForexSecurity ts          = new ForexSecurity(sym);

            ts.LotSize  = 1000;
            ts.PipSize  = 0.0001M;
            ts.TickSize = ts.PipSize / 10;
            testaccount.Securities.AddSecurity(ts);
            TickImpl tick       = TickImpl.NewQuote(sym, p, p, int.MaxValue, int.MaxValue, ts.DestEx, ts.DestEx);
            TickImpl secondtick = TickImpl.NewQuote(sym, p + inc, p + inc, int.MaxValue, int.MaxValue, ts.DestEx, ts.DestEx);

            List <Trade> fills = new List <Trade>(new Trade[] {
                // go long
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                          // 100 @ $100
                // increase bet
                new TradeImpl(sym, p + inc, s * 2)
                {
                    Security = ts, Account = testaccount, Commission = 2
                },                                                                                          // 300 @ $100.066666
                // take some profits
                new TradeImpl(sym, p + inc * 2, s * -1)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                          // 200 @ 100.0666 (profit = 100 * (100.20 - 100.0666) = 13.34) / maxMIU(= 300*100.06666) = .04% ret
                // go flat (round turn)
                new TradeImpl(sym, p + inc * 2, s * -2)
                {
                    Security = ts, Account = testaccount, Commission = 2
                },                                                                                          // 0 @ 0
                // go short
                new TradeImpl(sym, p, s * -2)
                {
                    Security = ts, Account = testaccount, Commission = 2
                },                                                                                          // -200 @ 100
                // decrease bet
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                          // -100 @100
                // exit (round turn)
                new TradeImpl(sym, p + inc, s * 5000)
                {
                    Security = ts, Account = testaccount, Commission = 1
                },                                                                                               // 0 @ 0 (gross profit = -0.10*100 = -$10)
                // do another entry
                new TradeImpl(sym, p, s)
                {
                    Security = ts, Account = testaccount, Commission = 1
                }                                                                                           // 100 @ 100
            });

            //Act, fill all trades
            testaccount.OnTick(tick);
            testaccount.OnTick(secondtick);
            foreach (var t in fills)
            {
                testaccount.OnFill(t);
            }

            //Assert
            Assert.True(testaccount.Balance == 10300);
            Assert.True(testaccount.Margin == 55000);
            //Lower than 20% (Margin Call)
            Assert.True(testaccount.MarginLevel < 20M);
        }