Tuesday, September 07, 2010

Equally Balanced Cash [EBC]

Descripton
Summary:
This money management script is used to balance the accounts free cash so that all accounts have the same amount of cash.


Variables
TypeIdentifierDescription
Integer_dayUse for the day (1 to 31) of each month in which money management is done.
Integer_monthUse for the month (1 to 12) in which money management is done. (-1 for every month) [Default -1]

OnInitialize
Function Parameters
TypeIdentifierDescription
IntegerdayUse for the day (1 to 31) of month in which money management is done. [Default 1]
IntegermonthUse for the month (1 to 12) in which money management is done. (-1 for every month) [Default -1]
Implementation
	 ' Assign the script parameters
	_day = day
	_month = month

OnMoneyManagement
Function Parameters
TypeIdentifierDescription
Implementation
	 ' The script can only be executed at the specified date.
	If (DateTimeDay(DateTimeCurrent()) <> _day Or (DateTimeMonth(DateTimeCurrent()) <> _month And _month <> -1)) Then
		Return
	End If 
	
	 ' Use for the average account balance denominated in first account currency code.
	Define averageAccountBalance As Number
	 ' Use for holding the accounts free cash indexed by account number.
	Define accountsCash(AccountCount() - 1) As Number
	 ' Calculate the free cash of each account denominated in the first account currency code.
	For i As Integer = 0 To AccountCount() - 1
		averageAccountBalance  += AccountCash(i)
		accountsCash(i) = AccountCash(i)
	Next
	 ' Calculate the average account balance.
	averageAccountBalance  = averageAccountBalance  / AccountCount()
	Define fromCredit,toDebit,debitAmount  As Number 
	 ' Iterate over all of the accounts while moving cash from one account to the other
	 ' in order to even them out.
	For i As Integer = 0 To AccountCount() - 1
		 ' If the current account has more cash than the average account then
		 ' its extra cash can be redistributed.
		If (accountsCash(i)  > averageAccountBalance) Then
			 ' Calculate the extra cash that can be redistributed.
			fromCredit = accountsCash(i) - averageAccountBalance
			 ' Iterate over all the accounts while searching for accounts that 
			 ' have less cash than the average account.
			For j As Integer = 0 To AccountCount() - 1
				 ' If the other account has less cash than the average account then
				 ' it can receive cash from the current account.
				If (fromCredit > 0 And accountsCash(j) < averageAccountBalance) Then
					 ' Calculate the cash required by the other account.
					toDebit =  averageAccountBalance - accountsCash(j)
					 ' Calculate the cash that will be transfered.
					debitAmount = MathMin(fromCredit, toDebit)
					 ' Transfer the cash between the accounts.
					AccountTransferCash(i,j,debitAmount,"Account balance")
					 ' Reduce the amount left to distribute.
					fromCredit = fromCredit - debitAmount
					 ' Reduce the account cash from the source account.
					accountsCash(i) -= debitAmount
					 ' Increase the account cash of the destination account.
					accountsCash(j) += debitAmount
				End If
			Next
		End If
	Next

Copyright © 2010 IQBroker, LLC. All rights reserved.