[Quantproject-developers] QuantProject/b4_Business/a1_Financial/a2_Accounting Account.cs,1.6,1.7
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2004-11-29 15:22:41
|
Update of /cvsroot/quantproject/QuantProject/b4_Business/a1_Financial/a2_Accounting In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4183/b4_Business/a1_Financial/a2_Accounting Modified Files: Account.cs Log Message: - implemented the interface IComparable - added the GetFitnessValue method - added an IEndOfDayTimer - added an IDataStreamer - added an IOrderExecutor - added an ArrayList of active orders - added an AccountReport - added the AddOrder method - added Contains( Instrument instrument ) - added Contains( string ticker ) - added Add( EndOfDayTransaction transaction ) - added GetMarketValue( EndOfDayDateTime endOfDayDateTime ) instead of ExtendedDateTime parameter - added GetMarketValue( string ticker ) - added GetProfitNetLoss( EndOfDayDateTime endOfDayDateTime ) instead of ExtendedDateTime parameter - CreateReport now uses an EndOfDayDateTime parameter instead of an ExtendedDateTime parameter Index: Account.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b4_Business/a1_Financial/a2_Accounting/Account.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Account.cs 15 Aug 2004 00:08:41 -0000 1.6 --- Account.cs 29 Nov 2004 15:22:31 -0000 1.7 *************** *** 31,38 **** using QuantProject.ADT; using QuantProject.ADT.Histories; ! //using QuantProject.Data.MicrosoftExcel; using QuantProject.Business.Financial.Accounting.Reporting; using QuantProject.Business.Financial.Instruments; using QuantProject.Business.Strategies; namespace QuantProject.Business.Financial.Accounting --- 31,42 ---- using QuantProject.ADT; using QuantProject.ADT.Histories; ! using QuantProject.Data.DataProviders; using QuantProject.Business.Financial.Accounting.Reporting; + using QuantProject.Business.Financial.Accounting.Transactions; using QuantProject.Business.Financial.Instruments; + using QuantProject.Business.Financial.Ordering; using QuantProject.Business.Strategies; + using QuantProject.Business.Timing; + namespace QuantProject.Business.Financial.Accounting *************** *** 44,64 **** [Serializable] ! public class Account : Keyed { private double cashAmount; private AccountStrategy accountStrategy; public Portfolio Portfolio = new Portfolio(); //public AccountReport accountReport; ! public double CashAmount ! { ! get ! { ! return cashAmount; ! } ! } ! public AccountStrategy AccountStrategy { get { return accountStrategy; } --- 48,88 ---- [Serializable] ! public class Account : Keyed , IComparable { private double cashAmount; private AccountStrategy accountStrategy; + private IEndOfDayTimer endOfDayTimer; + private IDataStreamer dataStreamer; + private IOrderExecutor orderExecutor; + private ArrayList activeOrders; + private AccountReport accountReport; public Portfolio Portfolio = new Portfolio(); //public AccountReport accountReport; ! public double CashAmount ! { ! get { return cashAmount; } ! } ! public IEndOfDayTimer EndOfDayTimer ! { ! get { return this.endOfDayTimer; } ! set { this.endOfDayTimer = value; } ! } ! ! public IDataStreamer DataStreamer ! { ! get { return this.dataStreamer; } ! set { this.dataStreamer = value; } ! } ! ! public IOrderExecutor OrderExecutor ! { ! get { return this.orderExecutor; } ! set { this.orderExecutor = value; } ! } ! ! public AccountStrategy AccountStrategy { get { return accountStrategy; } *************** *** 66,70 **** } ! public Transactions Transactions = new Transactions(); public Account( string accountName ) : base ( accountName ) --- 90,94 ---- } ! public TransactionHistory Transactions = new TransactionHistory(); public Account( string accountName ) : base ( accountName ) *************** *** 76,79 **** --- 100,104 ---- { cashAmount = 0; + this.activeOrders = new ArrayList(); accountStrategy = new AccountStrategy( this ); } *************** *** 83,87 **** --- 108,145 ---- this.initialize(); } + public Account( string accountName , IEndOfDayTimer endOfDayTimer , + IDataStreamer dataStreamer , IOrderExecutor orderExecutor ) : base( accountName ) + { + this.endOfDayTimer = endOfDayTimer; + this.dataStreamer = dataStreamer; + this.orderExecutor = orderExecutor; + this.orderExecutor.OrderFilled += new OrderFilledEventHandler( + this.orderFilledEventHandler ); + this.initialize(); + } + + private void orderFilledEventHandler( Object sender , OrderFilledEventArgs + eventArgs ) + { + this.Add( eventArgs.EndOfDayTransaction ); + } + public virtual double GetFitnessValue() + { + if ( this.accountReport == null ) + // the account report has not been computed yet + this.accountReport = this.CreateReport( this.Key , + 1 , this.endOfDayTimer.GetCurrentTime() ); + return this.accountReport.Summary.ReturnOnAccount; + } + public int CompareTo( Object account ) + { + int returnValue = 0; + if ( this.GetFitnessValue() < (( Account )account).GetFitnessValue() ) + returnValue = -1; + if ( this.GetFitnessValue() > (( Account )account).GetFitnessValue() ) + returnValue = 1; + return returnValue; + } public void Clear() { *************** *** 107,116 **** } ! public bool Contains( Instrument instrument ) ! { ! return Portfolio.Contains( instrument ); ! } - #region "Add( TimedTransaction transaction )" private void updateCash( Transaction transaction ) --- 165,187 ---- } ! public void AddCash( double moneyAmount ) ! { ! this.AddCash( this.endOfDayTimer.GetCurrentTime().GetNearestExtendedDateTime() , ! moneyAmount ); ! } ! ! public void AddOrder( Order order ) ! { ! this.orderExecutor.Execute( order ); ! } ! public bool Contains( Instrument instrument ) ! { ! return Portfolio.Contains( instrument ); ! } ! public bool Contains( string ticker ) ! { ! return Portfolio.Contains( ticker ); ! } private void updateCash( Transaction transaction ) *************** *** 126,152 **** //this.accountReport.AddRecord( this ); } ! #endregion ! public double GetMarketValue( ExtendedDateTime extendedDateTime ) ! { ! return this.CashAmount + this.Portfolio.GetMarketValue( extendedDateTime ); ! } ! public double GetProfitNetLoss( ExtendedDateTime extendedDateTime ) { ! return GetMarketValue( extendedDateTime ) + this.Transactions.TotalWithdrawn - this.Transactions.TotalAddedCash; } ! public History GetProfitNetLossHistory( ExtendedDateTime finalDateTime ) { History history = new History(); Account account = new Account( "ToGetProfitNetLossHistory" ); foreach ( ArrayList arrayList in this.Transactions.Values ) ! foreach ( TimedTransaction transaction in arrayList ) { account.Add( transaction ); ! history.MultiAdd( transaction.ExtendedDateTime.DateTime , ! account.GetProfitNetLoss( transaction.ExtendedDateTime ) ); } history.MultiAdd( finalDateTime.DateTime , --- 197,235 ---- //this.accountReport.AddRecord( this ); } ! public void Add( EndOfDayTransaction transaction ) ! { ! this.Transactions.Add( transaction ); ! this.updateCash( transaction ); ! this.Portfolio.Update( transaction ); ! //this.accountReport.AddRecord( this ); ! } ! ! public double GetMarketValue( EndOfDayDateTime endOfDayDateTime ) ! { ! return this.CashAmount + this.Portfolio.GetMarketValue( endOfDayDateTime ); ! } ! public double GetMarketValue( string ticker ) ! { ! return HistoricalDataProvider.GetMarketValue( ticker , ! this.endOfDayTimer.GetCurrentTime().GetNearestExtendedDateTime() ); ! } ! public double GetProfitNetLoss( EndOfDayDateTime endOfDayDateTime ) { ! return GetMarketValue( endOfDayDateTime ) + this.Transactions.TotalWithdrawn - this.Transactions.TotalAddedCash; } ! public History GetProfitNetLossHistory( EndOfDayDateTime finalDateTime ) { History history = new History(); Account account = new Account( "ToGetProfitNetLossHistory" ); foreach ( ArrayList arrayList in this.Transactions.Values ) ! foreach ( EndOfDayTransaction transaction in arrayList ) { account.Add( transaction ); ! history.MultiAdd( transaction.EndOfDayDateTime.DateTime , ! account.GetProfitNetLoss( transaction.EndOfDayDateTime ) ); } history.MultiAdd( finalDateTime.DateTime , *************** *** 161,192 **** "\nPortfolioContent : " + this.Portfolio.ToString() + "\nPortfolioMarketValue : " + this.Portfolio.GetMarketValue( ! new ExtendedDateTime( dateTime , BarComponent.Close ) ) + "\nAccountProfitNetLoss : " + this.GetProfitNetLoss( ! new ExtendedDateTime( dateTime , BarComponent.Close ) ); } ! public AccountReport CreateReport( string reportName , ! int numDaysForInterval , ExtendedDateTime endDateTime ) ! { ! AccountReport accountReport = new AccountReport( this ); ! return accountReport.Create( reportName , numDaysForInterval , endDateTime ); ! } ! public AccountReport CreateReport( string reportName , ! int numDaysForInterval , ExtendedDateTime endDateTime , string buyAndHoldTicker ) ! { ! AccountReport accountReport = new AccountReport( this ); ! return accountReport.Create( reportName , numDaysForInterval , ! endDateTime , buyAndHoldTicker ); ! } ! public void Serialize( string filePathAndName ) ! { ! // //Dim FS As New IO.FileStream("c:\Rect.xml", IO.FileMode.Create, IO.FileAccess.Write) ! // Dim XMLFormatter As New SoapFormatter() ! // Dim R As New Rectangle(8, 8, 299, 499) ! // XMLFormatter.Serialize(FS, R) ! FileStream fileStream = new FileStream( filePathAndName , FileMode.Create , FileAccess.Write ); ! SoapFormatter soapFormatter = new SoapFormatter(); ! soapFormatter.Serialize( fileStream , this ); ! } ! } } --- 244,298 ---- "\nPortfolioContent : " + this.Portfolio.ToString() + "\nPortfolioMarketValue : " + this.Portfolio.GetMarketValue( ! new EndOfDayDateTime( dateTime , EndOfDaySpecificTime.MarketClose ) ) + "\nAccountProfitNetLoss : " + this.GetProfitNetLoss( ! new EndOfDayDateTime( dateTime , EndOfDaySpecificTime.MarketClose ) ); } ! public AccountReport CreateReport( string reportName , ! int numDaysForInterval , EndOfDayDateTime endDateTime ) ! { ! AccountReport accountReport = new AccountReport( this ); ! return accountReport.Create( reportName , numDaysForInterval , endDateTime ); ! } ! public AccountReport CreateReport( string reportName , ! int numDaysForInterval , EndOfDayDateTime endDateTime , string buyAndHoldTicker ) ! { ! AccountReport accountReport = new AccountReport( this ); ! return accountReport.Create( reportName , numDaysForInterval , ! endDateTime , buyAndHoldTicker ); ! } ! public void Serialize( string filePathAndName ) ! { ! // //Dim FS As New IO.FileStream("c:\Rect.xml", IO.FileMode.Create, IO.FileAccess.Write) ! // Dim XMLFormatter As New SoapFormatter() ! // Dim R As New Rectangle(8, 8, 299, 499) ! // XMLFormatter.Serialize(FS, R) ! FileStream fileStream = new FileStream( filePathAndName , FileMode.Create , FileAccess.Write ); ! SoapFormatter soapFormatter = new SoapFormatter(); ! soapFormatter.Serialize( fileStream , this ); ! } ! #region ClosePosition_position ! private OrderType closePosition_getOrderType( Position position ) ! { ! OrderType returnValue; ! if ( position.Quantity >= 0 ) ! // long position ! returnValue = OrderType.MarketSell; ! else ! // short position ! returnValue = OrderType.MarketCover; ! return returnValue; ! } ! public void ClosePosition( Position position ) ! { ! OrderType orderType = closePosition_getOrderType( position ); ! Order order = new Order( orderType , position.Instrument , position.Quantity ); ! this.orderExecutor.Execute( order ); ! } ! #endregion ClosePosition_position ! public void ClosePosition( string ticker ) ! { ! this.ClosePosition( (Position)this.Portfolio[ ticker ] ); ! } ! } } |