/
sample_data.rake
119 lines (117 loc) · 4.88 KB
/
sample_data.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
make_users
make_stocks
make_portfolios
make_trades
end
def make_users
@user = User.create!(:name => "Example User", :email => "example@railstutorial.org")
# 99.times do |n|
# name = Faker::Name.name
# email = "example-#{n+1}@railstutorial.org"
# User.create!(:name => name, :email => email)
# end
end
def make_stocks
#Rake::Task['utils:fill_csi300'].invoke
end
def make_portfolios
@mainport = @user.portfolios.create!(:name => "Mainland Shares", :classification => "TRADING")
@hkport = @user.portfolios.create!(:name => "Hongkong Shares", :classification => "TRADING")
@usport = @user.portfolios.create!(:name => "US Shares", :classification => "TRADING")
5.times do |n|
@user.portfolios.create!(:name => Faker::Company.bs, :classification => "TRADING")
end
5.times do |n|
@user.portfolios.create!(:name => Faker::Company.bs, :classification => "AFS")
end
5.times do |n|
@user.portfolios.create!(:name => Faker::Company.catch_phrase, :classification => "HTM")
end
end
def make_trades
@cmb = Stock.where(:sid => "600036", :market => "sh").first
@gree = Stock.where(:sid => "000651", :market => "sz").first
@cnooc = Stock.where(:sid => "00883", :market => "hk").first
@anta = Stock.where(:sid => "02020", :market => "hk").first
tdate = DateTime.parse("2012-3-4")
#Buy GREE in the mainland portfolio
@mainport.trades.create!(:state => 1,
:buy => true,
:trade_date => tdate,
:clear_date => tdate,
:vol => 100.00,
:price => 19.00,
:fee => 1.5,
:security_id => @gree.id
)
#Buy CMB in the mainland portfolio 5 times in consecutive days
5.times do |n|
n = n + 1
@mainport.trades.create!(:state => 1,
:buy => true,
:trade_date => tdate.next_day(n),
:clear_date => tdate.next_day(n),
:vol => 100.00 * n,
:price => 20.00 * (1 + n.to_f/100),
:fee => 1.5 * n,
:security_id => @cmb.id
)
end
tdate = DateTime.parse("2012-3-5")
#Sell Gree in the mainland portfolio
@mainport.trades.create!(:state => 1,
:buy => false,
:trade_date => tdate.next_day(1),
:clear_date => tdate.next_day(1),
:vol => 100.00,
:price => 20.00,
:fee => 1.5,
:security_id => @gree.id
)
#Sell CMB in the mainland portfolio 4 times in consecutive days
4.times do |n|
n = n + 1
@mainport.trades.create!(:state => 1,
:buy => false,
:trade_date => tdate.next_day(n).advance(:hours => 3),
:clear_date => tdate.next_day(n).advance(:hours => 3),
:vol => 100.00 * n,
:price => 20.00 * (1 + n.to_f/100),
:fee => 1.5 * n,
:security_id => @cmb.id
)
end
#Buy CNOOC in the hongkong portfolio 5 times in consecutive days
tdate = DateTime.parse("2012-3-4")
5.times do |n|
n = n + 1
@hkport.trades.create!( :state => 1,
:buy => true,
:trade_date => tdate.next_day(n),
:clear_date => tdate.next_day(n),
:vol => 100.00 * n,
:price => 25.00 * (1 + n.to_f/100),
:fee => 1.5 * n,
:security_id => @cnooc.id
)
end
#Sell CNOOC in the hongkong portfolio 4 times in consecutive days
tdate = DateTime.parse("2012-3-5")
4.times do |n|
n = n + 1
@hkport.trades.create!( :state => 1,
:buy => false,
:trade_date => tdate.next_day(n).advance(:hours => 3),
:clear_date => tdate.next_day(n).advance(:hours => 3),
:vol => 100.00 * n,
:price => 25.00 * (1 + n.to_f/100),
:fee => 1.5 * n,
:security_id => @cnooc.id
)
end
end
end