app/models/iro/position.rb in iron_warbler-2.0.7.19 vs app/models/iro/position.rb in iron_warbler-2.0.7.20
- old
+ new
@@ -2,47 +2,63 @@
class Iro::Position
include Mongoid::Document
include Mongoid::Timestamps
store_in collection: 'iro_positions'
+ attr_accessor :gain_loss_amount
+
STATUS_ACTIVE = 'active'
STATUS_PROPOSED = 'proposed'
STATUSES = [ nil, 'active', 'inactive', 'proposed' ]
field :status
validates :status, presence: true
scope :active, ->{ where( status: 'active' ) }
belongs_to :purse, class_name: 'Iro::Purse', inverse_of: :positions
+ index({ purse_id: 1, ticker: 1 })
+
+ belongs_to :stock, class_name: 'Iro::Stock', inverse_of: :positions
+ def ticker
+ stock&.ticker || '-'
+ end
+
belongs_to :strategy, class_name: 'Iro::Strategy', inverse_of: :positions
- field :ticker
- validates :ticker, presence: true
- index({ purse_id: 1, ticker: 1 })
+ # field :ticker
+ # validates :ticker, presence: true
- KINDS = [ nil, 'covered_call', 'credit_put_spread', 'credit_call_spread' ]
- field :kind
+ field :outer_strike, type: :float
+ # validates :outer_strike, presence: true
- field :strike, type: :float
- validates :strike, presence: true
+ field :inner_strike, type: :float
+ validates :inner_strike, presence: true
field :expires_on
validates :expires_on, presence: true
field :quantity, type: :integer
validates :quantity, presence: true
+ def q; quantity; end
field :begin_on
- field :begin_price, type: :float
- field :begin_delta, type: :float
+ field :begin_outer_price, type: :float
+ field :begin_outer_delta, type: :float
+ field :begin_inner_price, type: :float
+ field :begin_inner_delta, type: :float
+
field :end_on
- field :end_price, type: :float
- field :end_delta, type: :float
+ field :end_outer_price, type: :float
+ field :end_outer_delta, type: :float
- field :net_amount
- field :net_percent
+ field :end_inner_price, type: :float
+ field :end_inner_delta, type: :float
+ def breakeven
+ inner_strike - begin_outer_price + begin_inner_price
+ end
+
def current_underlying_strike
Iro::Stock.find_by( ticker: ticker ).last
end
def refresh
@@ -57,18 +73,35 @@
end_price: out[:last],
})
print '_'
end
+ def net_percent
+ net_amount / max_gain
+ end
+ def net_amount # each
+ strategy.send("net_amount_#{strategy.kind}", self)
+ end
+ def max_gain # each
+ strategy.send("max_gain_#{strategy.kind}", self)
+ end
+ def max_loss # each
+ strategy.send("max_loss_#{strategy.kind}", self)
+ end
+
field :next_delta, type: :float
field :next_outcome, type: :float
field :next_symbol
field :next_mark
field :next_reasons, type: :array, default: []
field :should_rollp, type: :float
+ ##
+ ## decisions
+ ##
+
def should_roll?
puts! 'shold_roll?'
update({
next_reasons: [],
@@ -81,11 +114,11 @@
elsif can_roll?
if end_delta < strategy.threshold_delta
next_reasons.push "delta is lower than threshold"
out = 0.91
- elsif 1 - end_price/begin_price > strategy.threshold_netp
+ elsif 1 - end_outer_price/begin_outer_price > strategy.threshold_netp
next_reasons.push "made enough percent profit (dubious)"
out = 0.61
else
next_reasons.push "neutral"
out = 0.33
@@ -217,6 +250,14 @@
out = out - 1.day
end
return out
end
+ def to_s
+ out = "#{stock} (#{q}) #{expires_on.to_datetime.strftime('%b %d')} #{strategy.kind_short} ["
+ if outer_strike
+ out = out + "$#{outer_strike}->"
+ end
+ out = out + "$#{inner_strike}] "
+ return out
+ end
end