title: How should I think about implement the closing of an open equity position in my portfolio app?
body: <p>I have a model <code>PortStock</code> that has a schema that looks like this:</p>
  
  <pre><code># == Schema Information
  #
  # Table name: port_stocks
  #
  #  id                :bigint(8)        not null, primary key
  #  portfolio_id      :integer
  #  stock_id          :integer
  #  volume            :integer
  #  transaction_price :float
  #  current_price     :float
  #  percent_change    :float
  #  created_at        :datetime         not null
  #  updated_at        :datetime         not null
  #  current_value     :float
  #  dollar_change     :float
  #  total_spend       :float
  #  transaction_date  :datetime
  #  action            :integer
  #  position          :integer          default("open")
  #  ticker            :string
  #  slug              :string
  #
  </code></pre>
  
  <p>When someone buys a stock, the app creates a <code>port_stock</code> that has <code>position: "open", action: :buy</code> in their <code>portfolio</code>.</p>
  
  <p>When they <code>sell</code> that position, essentially what happens is they create another <code>port_stock</code> but it just has <code>position: "closed", action: :sell</code>.</p>
  
  <p>I have a few questions:</p>
  
  <ol>
  <li>Is that really the most optimal structure? One immediate issue I see with this is that I have to <code>close</code> the <code>buy</code> position. So in essence, the full <code>completed position</code> would be two <code>closed</code>  <code>buy</code> and <code>sell</code> <code>port_stock</code> records. If there is only one, then the position is considered open. But does that make sense?</li>
  <li>Should I create a method in my model to both close and open positions? I originally did this just in my controllers, but now that I am writing unit tests I don't have a clean way to close an existing open position.</li>
  </ol>
  
  <p>Should I be approaching the buying &amp; selling of <code>port_stock</code> differently?</p>
  
  <p>Thoughts?</p>