Class: Ish::CoveredCallWatcher

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

Instance Method Summary collapse

Constructor Details

#initializeCoveredCallWatcher

Returns a new instance of CoveredCallWatcher.



4
5
6
7
# File 'lib/ish/covered_call_watcher.rb', line 4

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

#new_orderObject



9
10
11
12
13
14
15
16
17
# File 'lib/ish/covered_call_watcher.rb', line 9

def new_order
  ccall = ::Ish::CoveredCall.all.first
  xml = ccall.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

#watch_onceObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
# File 'lib/ish/covered_call_watcher.rb', line 19

def watch_once
  ccalls = ::Ish::CoveredCall.all
  ccalls.each do |ccall|
    puts! ccall.ticker, 'Watching this ccall'

    path = "/v1/market/ext/quotes.json?symbols=#{ccall.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! [ ccall.upper_panic_threshold, ccall.lower_panic_threshold ], 'upper/lower panic'

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

      IshManager::ApplicationMailer.ccall_followup_alert( ccall, { 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 < ccall.lower_panic_threshold
      xml = ccall.rolldown_xml access_token=@access_token, natural=natural
      print! xml, 'xml'

      IshManager::ApplicationMailer.ccall_followup_alert( ccall, { action: :rolldown } ).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