Sha256: e329075fd527f87244544c49e4d05cf3455c148b7f3b28126311342f26d36a79

Contents?: true

Size: 1.72 KB

Versions: 17

Compression:

Stored size: 1.72 KB

Contents

require 'distribution'
N = Distribution::Normal

class Iro::Purse
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia
  store_in collection: 'iro_purses'

  field :slug
  validates :slug, presence: true, uniqueness: true
  index({ slug: -1 }, { unique: true })

  has_many :positions,  class_name: 'Iro::Position', inverse_of: :purse

  has_and_belongs_to_many :strategies, class_name: 'Iro::Strategy', inverse_of: :purses

  belongs_to :stock, class_name: 'Iro::Stock', inverse_of: :strategies

  field :unit,             type: :integer, default: 10
  ## with unit 10,  .001
  ## with unit 100, .0001
  field :summary_unit,    type: :float, default: 0.001

  ## for rolling only:
  field :height,           type: :integer, default: 100

  field :mark_every_n_usd, type: :float, default: 1
  field :n_next_positions, type: :integer, default: 5

  field :available_amount, type: :float
  def available
    available_amount
  end

  def balance
    0.01
  end

  def delta_wt_avg( begin_end, long_short, inner_outer )
    max_loss_total = 0

    out = positions.send( long_short ).map do |pos|
      max_loss_total += pos.max_loss * pos.q
      pos.max_loss * pos.q * pos.send( inner_outer ).send( "#{begin_end}_delta" )
    end
    # puts! out, 'delta_wt_avg 1'
    out = out.reduce( &:+ ) / max_loss_total rescue 0
    # puts! out, 'delta_wt_avg 2'
    return out
  end
  ## delta to plot percentage
  ## convert to normal between 0 and 3 std
  def delta_to_plot_p( *args )
    x = delta_wt_avg( *args ).abs
    if x < 0.5
      y = 1
    else
      y = 2 - 1/( 1.5 - x )
    end
    y_ = "#{ (y*100) .to_i}%"
    return y_
  end

  def to_s
    slug
  end
  def self.list
    [[nil,nil]] + all.map { |p| [p, p.id] }
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
iron_warbler-2.0.7.43 app/models/iro/purse.rb
iron_warbler-2.0.7.42 app/models/iro/purse.rb
iron_warbler-2.0.7.41 app/models/iro/purse.rb
iron_warbler-2.0.7.40 app/models/iro/purse.rb
iron_warbler-2.0.7.39 app/models/iro/purse.rb
iron_warbler-2.0.7.38 app/models/iro/purse.rb
iron_warbler-2.0.7.37 app/models/iro/purse.rb
iron_warbler-2.0.7.36 app/models/iro/purse.rb
iron_warbler-2.0.7.35 app/models/iro/purse.rb
iron_warbler-2.0.7.34 app/models/iro/purse.rb
iron_warbler-2.0.7.33 app/models/iro/purse.rb
iron_warbler-2.0.7.32 app/models/iro/purse.rb
iron_warbler-2.0.7.31 app/models/iro/purse.rb
iron_warbler-2.0.7.30 app/models/iro/purse.rb
iron_warbler-2.0.7.29 app/models/iro/purse.rb
iron_warbler-2.0.7.28 app/models/iro/purse.rb
iron_warbler-2.0.7.27 app/models/iro/purse.rb