Skip to content

Commit

Permalink
Go on
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicHong committed Dec 2, 2012
1 parent c8c8c99 commit d831354
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 11 deletions.
7 changes: 6 additions & 1 deletion app/models/cash_trade.rb
Expand Up @@ -6,18 +6,23 @@ class CashTrade < Trade
def security
Cash.first
end

def vol
self.amount
end
def price
1
end
def cf
self.amount
- super
end

def is_cash?
true
end
def fee
0
end

private
def default_values
Expand Down
3 changes: 1 addition & 2 deletions app/models/portfolio.rb
Expand Up @@ -22,12 +22,11 @@ def position(*from, till)
position = Hash.new { |hash, key| hash[key] = {:position => 0, :cost => 0} }
return position if ts.nil?
ts.each { |t|
next if t.is_cash?
vol = position[t.security][:position]
cost = position[t.security][:cost]
position[t.security][:position]= (t.buy ? (vol+t.vol) : (vol-t.vol))
if t.buy
position[t.security][:cost]=(vol*cost + (t.vol*t.price+t.fee))/(vol+t.vol)
position[t.security][:cost]=(vol*cost + (t.amount+t.fee))/(vol+t.vol)
end
}
position
Expand Down
3 changes: 2 additions & 1 deletion app/models/trade.rb
Expand Up @@ -7,6 +7,7 @@ class Trade < ActiveRecord::Base
belongs_to :portfolio
belongs_to :security

validates :buy, :inclusion => {:in => [true, false]}
validates :portfolio_id, :existence => true
validates :security_id, :existence => true
validates :trade_date, :presence => true
Expand All @@ -15,7 +16,7 @@ class Trade < ActiveRecord::Base
:unless => Proc.new{|trade| trade.type == "CashTrade"}
validates :price, :numericality => {:greater_than_or_equal_to => 0},
:unless => Proc.new{|trade| trade.type == "CashTrade"}
validates :amount, :numericality => true
validates :amount, :numericality => {:greater_than_or_equal_to => 0}
validate :trade_date_must_be_no_later_than_clear_date

# Return cashflow for this trade
Expand Down
6 changes: 0 additions & 6 deletions lib/tasks/sample_data.rake
Expand Up @@ -47,7 +47,6 @@ namespace :db do
:clear_date => tdate,
:vol => 100.00,
:price => 19.00,
:amount => 100 * 19.00 +1.5,
:fee => 1.5,
:security_id => @gree.id
)
Expand All @@ -60,7 +59,6 @@ namespace :db do
:clear_date => tdate.next_day(n),
:vol => 100.00 * n,
:price => 20.00 * (1 + n.to_f/100),
:amount => 100 * n * 20.00 * (1 + n.to_f/100) +1.5 * n,
:fee => 1.5 * n,
:security_id => @cmb.id
)
Expand All @@ -73,7 +71,6 @@ namespace :db do
:clear_date => tdate.next_day(1),
:vol => 100.00,
:price => 20.00,
:amount => 100.00 * 20.00 - 1.5,
:fee => 1.5,
:security_id => @gree.id
)
Expand All @@ -86,7 +83,6 @@ namespace :db do
:clear_date => tdate.next_day(n).advance(:hours => 3),
:vol => 100.00 * n,
:price => 20.00 * (1 + n.to_f/100),
:amount => 100.00 * n * 20.00 * (1 + n.to_f/100) - 1.5 * n,
:fee => 1.5 * n,
:security_id => @cmb.id
)
Expand All @@ -101,7 +97,6 @@ namespace :db do
:clear_date => tdate.next_day(n),
:vol => 100.00 * n,
:price => 25.00 * (1 + n.to_f/100),
:amount => 100.00 * n * 25.00 * (1 + n.to_f/100) + 1.5 * n,
:fee => 1.5 * n,
:security_id => @cnooc.id
)
Expand All @@ -116,7 +111,6 @@ namespace :db do
:clear_date => tdate.next_day(n).advance(:hours => 3),
:vol => 100.00 * n,
:price => 25.00 * (1 + n.to_f/100),
:amount => 100 * n * 25.00 *(1 + n.to_f/100) - 1.5 * n,
:fee => 1.5 * n,
:security_id => @cnooc.id
)
Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Expand Up @@ -41,6 +41,7 @@
association :security
end
factory :cash_trade do |cash_trade|
buy true
trade_date DateTime.parse("2012-07-19")
amount 100
association :portfolio
Expand Down
4 changes: 3 additions & 1 deletion spec/models/cash_trade_spec.rb
Expand Up @@ -5,12 +5,14 @@
let(:stock) { FactoryGirl.create(:stock) }
let(:cash_trade) { FactoryGirl.create(:cash_trade) }
let(:trade) { FactoryGirl.create(:trade) }

subject { cash_trade }
it "should create a CashTrade even if a security_id hasn't been set" do
portfolio.cash_trades.create!(
:buy => false,
:trade_date => DateTime.parse("2012-07-19"),
:amount => 100)
end

it "should return Cash as Security even if security is set otherwise" do
cash_trade.security = Stock.first
cash_trade.security.should == Cash.first
Expand Down
5 changes: 5 additions & 0 deletions spec/models/trade_spec.rb
Expand Up @@ -131,6 +131,11 @@
@trade.vol = -1
@trade.should_not be_valid
end
it "is not valid if price < 0 " do
@trade.price = -1
@trade.should_not be_valid
end

end
end

Expand Down

0 comments on commit d831354

Please sign in to comment.