Sha256: 9fbb71b3fca90188f7aed0f0e1a6a58a768ab72d31082f57ad0ff1312a6b20b3

Contents?: true

Size: 1.15 KB

Versions: 47

Compression:

Stored size: 1.15 KB

Contents

class AlphavantageStockwatcher

  def initialize
  end

  # every minute, for alphavantage.co
  def watch
    while true

      if Time.now.hour > 14 && Time.now.hours < 21

        stocks = Ish::StockWatch.where( :notification_type => :EMAIL )
        # puts! stocks.map(&:ticker), "Watching these stocks:"
        stocks.each do |stock|
          # puts! stock.ticker, 'stock'
          r = HTTParty.get "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=#{stock.ticker}&interval=1min&apikey=X1C5GGH5MZSXMF3O", timeout: 10
          r2 = JSON.parse( r.body )['Time Series (1min)']
          r3 = r2[r2.keys.first]['4. close'].to_f
          if stock.direction == :ABOVE && r3 >= stock.price ||
             stock.direction == :BELOW && r3 <= stock.price
            IshManager::ApplicationMailer.stock_alert( stock ).deliver

            ## actions
            ## exit the position
            # stock.stock_actions.where( :is_active => true ).each do |action|
            #   # @TODO: actions
            # end
            
          end
        end
        print '.'
      else
        print '-'
      end
      
      sleep 60

    end
  end

end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
ish_models-0.0.33.153 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.152 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.151 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.150 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.149 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.148 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.147 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.146 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.145 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.144 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.143 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.142 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.141 lib/warbler/alphavantage_stockwatcher.rb
ish_models-0.0.33.140 lib/ish/alphavantage_stockwatcher.rb
ish_models-0.0.33.139 lib/ish/alphavantage_stockwatcher.rb
ish_models-0.0.33.138 lib/ish/alphavantage_stockwatcher.rb
ish_models-0.0.33.137 lib/ish/alphavantage_stockwatcher.rb
ish_models-0.0.33.136 lib/ish/alphavantage_stockwatcher.rb
ish_models-0.0.33.135 lib/ish/alphavantage_stockwatcher.rb
ish_models-0.0.33.134 lib/ish/alphavantage_stockwatcher.rb