Class: Ish::IronCondorWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ish/iron_condor_watcher.rb

Overview

result = @access_token.get('/v1/accounts.json', => 'application/json') json = JSON.parse result.body

Instance Method Summary collapse

Constructor Details

#initializeIronCondorWatcher

Returns a new instance of IronCondorWatcher.


7
8
9
10
# File 'lib/ish/iron_condor_watcher.rb', line 7

def initialize
  @consumer = OAuth::Consumer.new ALLY_CREDS[:consumer_key], ALLY_CREDS[:consumer_secret], { :site => 'https://api.tradeking.com' }
  @access_token = OAuth::AccessToken.new(@consumer, ALLY_CREDS[:access_token], ALLY_CREDS[:access_token_secret])
end

Instance Method Details

#ally_status_updateObject


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ish/iron_condor_watcher.rb', line 12

def ally_status_update
  path = "/v1/accounts/#{ALLY_CREDS[:account_id]}/orders.xml"
  response = @access_token.get( path, {'Accept' => 'application/json'})
  print! response.body, 'body'

  # have model AllyOrder ?
  # Then, if the order is filled, adjust the condor (suppose rolled down):
  # update field :put_sell_strike
  # update field :put_buy_strike
  # update field :status => :filled 
end

#watch_onceObject

def new_order

  condor = ::Ish::IronCondor.all.first
  xml = condor.new_multileg_order_example
  print! xml, 'xml'
  path = "/v1/accounts/#{ALLY_CREDS[:account_id]}/orders.xml"
  # path = "/v1/accounts/#{ALLY_CREDS[:account_id]}/orders/preview.xml"
  response = @access_token.post(path, xml)
  print! response.body, 'response'
end

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
# File 'lib/ish/iron_condor_watcher.rb', line 36

def watch_once
  condors = ::Ish::IronCondor.all_filled
  condors.each do |condor|
    puts! condor.ticker, 'Watching this condor'

    path = "/v1/market/ext/quotes.json?symbols=#{condor.ticker}"
    response = @access_token.get(path, {'Accept' => 'application/json'})
    json = JSON.parse( response.body ).deep_symbolize_keys
    bid = json[:response][:quotes][:quote][:bid].to_f
    ask = json[:response][:quotes][:quote][:ask].to_f
    natural = ( bid + ask ) / 2

    puts! [ bid, ask ], 'bid, ask'
    puts! [ condor.upper_panic_threshold, condor.lower_panic_threshold ], 'upper/lower panic'

    ## upper panic
    if bid > condor.upper_panic_threshold
      xml = condor.rollup_xml access_token=@access_token, natural=natural
      print! xml, 'xml'

      IshManager::ApplicationMailer.condor_followup_alert( condor, { action: :rollup } ).deliver_later
      
      ## place order
      path_preview = "/v1/accounts/#{ALLY_CREDS[:account_id]}/orders/preview.xml"
      response = @access_token.post( path_preview, xml )
      print! response.body
      # path_order   = "/v1/accounts/#{ALLY_CREDS[:account_id]}/orders.xml"
      # response = @access_token.post( path_order, xml )
      # print! response.body
    end

    ## lower panic
    if ask < condor.lower_panic_threshold
      xml = condor.rolldown_xml access_token=@access_token, natural=natural
      print! xml, 'xml'

      IshManager::ApplicationMailer.condor_followup_alert( { action: 'rolldown', condor_id: condor.id } ).deliver_later
      
      ## place order
      path_preview = "/v1/accounts/#{ALLY_CREDS[:account_id]}/orders/preview.xml"
      response = @access_token.post( path_preview, xml )
      print! response.body
      # path_order   = "/v1/accounts/#{ALLY_CREDS[:account_id]}/orders.xml"
      # response = @access_token.post( path_order, xml )
      # print! response.body
    end

  end
end