Permalink
Browse files

Done Position.position()

  • Loading branch information...
1 parent 2758d0a commit 7b5f0ba7b796d318abd7fbe8ce1da3915aa1edb0 @DominicHong committed Jul 9, 2012
View
@@ -11,7 +11,23 @@ def cash(at = DateTime.now)
0
end
def position(*from, till)
+ rails ArgumentError, "must be no more than ONE from_date" if from.length > 2
+ from = from.first
+ if from.nil?
+ ts = self.trades.where("trade_date <= ?", till).order("trade_date")
+ else
+ ts = self.trades.where("trade_date >= ? AND trade_date <= ?", from, till).order("trade_date")
+ end
position = Hash.new { |hash, key| hash[key] = {:position => 0, :cost => 0} }
+ return position if ts.nil?
+ ts.each { |t|
+ 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)
+ end
+ }
position
end
end
@@ -1,8 +1,8 @@
-<footer>
- <nav class="footer">
- <ul>
- <li><%= link_to "About", about_path %></li>
- <li><%= link_to "Contact", contact_path %></li>
- </ul>
- </nav>
+<footer class="footer">
+ <nav>
+ <ul>
+ <li><%= link_to "About", about_path %></li>
+ <li><%= link_to "Contact", contact_path %></li>
+ </ul>
+ </nav>
</footer>
@@ -1,10 +1,14 @@
-<header>
- <nav class="header">
- <ul>
- <li><%= link_to "Home", root_path %></li>
- <li><%= link_to "Trades", trades_path %></li>
- <li><%= link_to "Portfolios", portfolios_path %></li>
- <li><%= link_to "Quotes", "#" %></li>
- </ul>
- </nav>
+<header class="navbar navbar-fixed-top">
+ <div class="navbar-inner">
+ <div class="container">
+ <nav>
+ <ul class="nav pull-right">
+ <li><%= link_to "Home", root_path %></li>
+ <li><%= link_to "Trades", trades_path %></li>
+ <li><%= link_to "Portfolios", portfolios_path %></li>
+ <li><%= link_to "Quotes", "#" %></li>
+ </ul>
+ </nav>
+ </div>
+ </div>
</header>
View
@@ -7,6 +7,8 @@
#
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
# Mayor.create(:name => 'Daley', :city => cities.first)
+
+
Stock.create(:market => "sz", :sid=>"000898", :name=>"鞍钢股份").save!
Stock.create(:market => "sh", :sid=>"600019", :name=>"宝钢股份").save!
Stock.create(:market => "sh", :sid=>"600036", :name=>"招商银行").save!
@@ -9,6 +9,7 @@ namespace :db do
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"
View
@@ -1,241 +0,0 @@
-<!DOCTYPE html>
-<html>
- <head>
- <title>Ruby on Rails: Welcome aboard</title>
- <style type="text/css" media="screen">
- body {
- margin: 0;
- margin-bottom: 25px;
- padding: 0;
- background-color: #f0f0f0;
- font-family: "Lucida Grande", "Bitstream Vera Sans", "Verdana";
- font-size: 13px;
- color: #333;
- }
-
- h1 {
- font-size: 28px;
- color: #000;
- }
-
- a {color: #03c}
- a:hover {
- background-color: #03c;
- color: white;
- text-decoration: none;
- }
-
-
- #page {
- background-color: #f0f0f0;
- width: 750px;
- margin: 0;
- margin-left: auto;
- margin-right: auto;
- }
-
- #content {
- float: left;
- background-color: white;
- border: 3px solid #aaa;
- border-top: none;
- padding: 25px;
- width: 500px;
- }
-
- #sidebar {
- float: right;
- width: 175px;
- }
-
- #footer {
- clear: both;
- }
-
- #header, #about, #getting-started {
- padding-left: 75px;
- padding-right: 30px;
- }
-
-
- #header {
- background-image: url("assets/rails.png");
- background-repeat: no-repeat;
- background-position: top left;
- height: 64px;
- }
- #header h1, #header h2 {margin: 0}
- #header h2 {
- color: #888;
- font-weight: normal;
- font-size: 16px;
- }
-
-
- #about h3 {
- margin: 0;
- margin-bottom: 10px;
- font-size: 14px;
- }
-
- #about-content {
- background-color: #ffd;
- border: 1px solid #fc0;
- margin-left: -55px;
- margin-right: -10px;
- }
- #about-content table {
- margin-top: 10px;
- margin-bottom: 10px;
- font-size: 11px;
- border-collapse: collapse;
- }
- #about-content td {
- padding: 10px;
- padding-top: 3px;
- padding-bottom: 3px;
- }
- #about-content td.name {color: #555}
- #about-content td.value {color: #000}
-
- #about-content ul {
- padding: 0;
- list-style-type: none;
- }
-
- #about-content.failure {
- background-color: #fcc;
- border: 1px solid #f00;
- }
- #about-content.failure p {
- margin: 0;
- padding: 10px;
- }
-
-
- #getting-started {
- border-top: 1px solid #ccc;
- margin-top: 25px;
- padding-top: 15px;
- }
- #getting-started h1 {
- margin: 0;
- font-size: 20px;
- }
- #getting-started h2 {
- margin: 0;
- font-size: 14px;
- font-weight: normal;
- color: #333;
- margin-bottom: 25px;
- }
- #getting-started ol {
- margin-left: 0;
- padding-left: 0;
- }
- #getting-started li {
- font-size: 18px;
- color: #888;
- margin-bottom: 25px;
- }
- #getting-started li h2 {
- margin: 0;
- font-weight: normal;
- font-size: 18px;
- color: #333;
- }
- #getting-started li p {
- color: #555;
- font-size: 13px;
- }
-
-
- #sidebar ul {
- margin-left: 0;
- padding-left: 0;
- }
- #sidebar ul h3 {
- margin-top: 25px;
- font-size: 16px;
- padding-bottom: 10px;
- border-bottom: 1px solid #ccc;
- }
- #sidebar li {
- list-style-type: none;
- }
- #sidebar ul.links li {
- margin-bottom: 5px;
- }
-
- .filename {
- font-style: italic;
- }
- </style>
- <script type="text/javascript">
- function about() {
- info = document.getElementById('about-content');
- if (window.XMLHttpRequest)
- { xhr = new XMLHttpRequest(); }
- else
- { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
- xhr.open("GET","rails/info/properties",false);
- xhr.send("");
- info.innerHTML = xhr.responseText;
- info.style.display = 'block'
- }
- </script>
- </head>
- <body>
- <div id="page">
- <div id="sidebar">
- <ul id="sidebar-items">
- <li>
- <h3>Browse the documentation</h3>
- <ul class="links">
- <li><a href="http://guides.rubyonrails.org/">Rails Guides</a></li>
- <li><a href="http://api.rubyonrails.org/">Rails API</a></li>
- <li><a href="http://www.ruby-doc.org/core/">Ruby core</a></li>
- <li><a href="http://www.ruby-doc.org/stdlib/">Ruby standard library</a></li>
- </ul>
- </li>
- </ul>
- </div>
-
- <div id="content">
- <div id="header">
- <h1>Welcome aboard</h1>
- <h2>You&rsquo;re riding Ruby on Rails!</h2>
- </div>
-
- <div id="about">
- <h3><a href="rails/info/properties" onclick="about(); return false">About your application&rsquo;s environment</a></h3>
- <div id="about-content" style="display: none"></div>
- </div>
-
- <div id="getting-started">
- <h1>Getting started</h1>
- <h2>Here&rsquo;s how to get rolling:</h2>
-
- <ol>
- <li>
- <h2>Use <code>rails generate</code> to create your models and controllers</h2>
- <p>To see all available options, run it without parameters.</p>
- </li>
-
- <li>
- <h2>Set up a default route and remove <span class="filename">public/index.html</span></h2>
- <p>Routes are set up in <span class="filename">config/routes.rb</span>.</p>
- </li>
-
- <li>
- <h2>Create your database</h2>
- <p>Run <code>rake db:create</code> to create your database. If you're not using SQLite (the default), edit <span class="filename">config/database.yml</span> with your username and password.</p>
- </li>
- </ol>
- </div>
- </div>
-
- <div id="footer">&nbsp;</div>
- </div>
- </body>
-</html>
Oops, something went wrong.

0 comments on commit 7b5f0ba

Please sign in to comment.