Class: Fsxtrader::Trader

Inherits:
Object
  • Object
show all
Defined in:
lib/fsxtrader/trader.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Trader

Initializes a new Fsxtrader::Trader class

  • Expects an OpenStruct returned from a CoolOptions.parse!(…) call



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fsxtrader/trader.rb', line 9

def initialize(options)
  @agent = WWW::Mechanize.new
  @agent.user_agent_alias = 'Mac FireFox'
  @agent.redirect_ok = true

  @config_file = options.config_path
  @verbose = options.verbose || false

  yml = File.open(@config_file)
  @config = YAML.load(yml)
  @auth = @config['auth']
  
  return unless everything_ok?
  
  @trades = []
  @config['trades'].each do |key, val|
    @trades << Trade.new(key, val)
  end
  if @trades.empty?
    puts "No trades found."
    return
  else
    puts "Trades Found:"
    @trades.each do |t|
      puts "\t#{t.key}: #{t.trade_str}"
    end
  end
  symbols = @trades.collect { |trade| trade.symbol }
  qp = QuoteParser.new(symbols)
  @quotes = qp.quotes
  check
end

Instance Method Details

#checkObject

Check trades



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fsxtrader/trader.rb', line 43

def check
  # First make sure we have a quote for each trade
  quote_symbols = @quotes.keys
  @trades.each do |trade|
    raise MissingQuoteError, "for symbol: #{trade.symbol}" unless quote_symbols.include?(trade.symbol)
  end
  # Check prices
  @trades.each do |trade|
    puts "Trade: #{trade}" if @verbose
    current_price = @quotes[trade.symbol]
    puts "\tCurrent Price: #{current_price}" if @verbose
    cond = "#{current_price} #{trade.condition} #{trade.limit_price}"
    condition_met = eval(cond)
    puts "\tCondition met?: #{condition_met}" if @verbose
    if condition_met
      make_trade(trade)
    end
  end
end

#comment_out_trade(trade) ⇒ Object

Comments out a trade in the config file - this gets called after every successful trade (so that duplicate trades do not occur).



105
106
107
108
109
110
111
112
113
# File 'lib/fsxtrader/trader.rb', line 105

def comment_out_trade(trade)
  config_txt = File.read(@config_file)
  config_txt.gsub!("#{trade.key}:", "# Trade Executed on #{Time.now.pretty_print}:\n  # #{trade.key}(executed):")
  FileUtils.mv(@config_file, "#{@config_file}.bak")
  # Now write out the new file
  File.open(@config_file, 'w') do |f|
    f.puts config_txt
  end
end

#make_trade(trade) ⇒ Object

Make a trade



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fsxtrader/trader.rb', line 64

def make_trade(trade)
   if !logged_in?

  trade_type = 'old' if ['sell', 'cover'].include?(trade.action)
  trade_type = 'new' if ['buy', 'short'].include?(trade.action)

  trade_url = "http://apps.facebook.com/hedgestop/index.php?p=trade&type=#{trade_type}&ticker="

  page = @agent.get(trade_url)
  form = page.form('place_order')
  b = form.buttons[1]

  if @verbose
    pp page
    puts "\nForm: \n"
    pp form
    pp b
  end

  case trade_type
  when 'old'
    button = (trade.action == 'sell') ? form.buttons[0] : form.buttons[1]
  when 'new'
    button = (trade.action == 'buy') ? form.buttons[0] : form.buttons[1]
  end
  form.ticker = trade.symbol
  form.shares = trade.shares
  confirm_page = @agent.submit(form, button)
  pp confirm_page if @verbose

  confirm_form = confirm_page.form('confirm_order')
  @agent.submit(confirm_form)

  puts "Trade Executed: #{trade}"

  # So that we don't execute this same trade again:
  comment_out_trade(trade)
end