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); }
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); }
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); }