public void PositionAccountTest() { ForexSecurity ts = new ForexSecurity(s); PortfolioManager portfolio = new TradingPortfolio(); IAccount account = new SimAccount("TEST"); portfolio.SetAccount(account); portfolio.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); }
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); }
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); }
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); }
public void AdjustedDateTime() { // 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); p.LastModified.Should().BeAfter(DateTime.MinValue); }
public void KeyRatios() { EquitySecurity ts = new EquitySecurity(sym); ts.LotSize = 1; ts.PipValue = 1; ts.PipSize = 1; PortfolioManager portfolio = new TradingPortfolio(); IAccount account = new SimAccount("TEST"); portfolio.SetAccount(account); account.Securities.AddSecurity(ts); rt = new Results(.01m, portfolio); // 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) { portfolio.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)); }
public void RoundTurnStat() { ForexSecurity ts = new ForexSecurity(sym); ts.LotSize = 1; ts.PipSize = 1; PortfolioManager portfolio = new TradingPortfolio(); IAccount account = new SimAccount("TEST"); portfolio.SetAccount(account); portfolio.Securities.AddSecurity(ts); rt = new Results(.01m, portfolio); // 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) { portfolio.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); }