Module: Tushare::Util

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.holiday?(date) ⇒ Boolean

判断是否为交易日,返回 true or false

Returns:

  • (Boolean)


404
405
406
407
408
409
410
# File 'lib/tushare/util.rb', line 404

def holiday?(date)
  trade_cals = trade_cal
  holidays = trade_cals.select { |object| object['is_open'] == 0 }
                       .map { |object| object['date'] }
  [6, 7].include?(date.cwday) ||
    holidays.include?(date.strftime('%F'))
end

.trade_calObject

交易日历 isOpen=1是交易日,isOpen=0为休市



394
395
396
397
398
399
400
401
# File 'lib/tushare/util.rb', line 394

def trade_cal
  resp = HTTParty.get(ALL_CAL_FILE)
  result = []
  CSV.new(resp.body.encode('utf-8', 'gbk')).drop(1).each do |arr|
    result << { 'date' => arr[0], 'is_open' => arr[1] }
  end
  result
end

Instance Method Details

#_code_to_symbol(code) ⇒ Object

生成symbol代码标志



356
357
358
359
360
# File 'lib/tushare/util.rb', line 356

def _code_to_symbol(code)
  return INDEX_LIST[code] if INDEX_LABELS.include?(code)
  return ''               if code.nil? or code.size != 6
  ['5','6','9'].include?(code[0]) ? "sh#{code}" : "sz#{code}"
end

#_write_consoleObject



362
363
364
365
# File 'lib/tushare/util.rb', line 362

def _write_console()
  $stdout.write(DATA_GETTING_FLAG)
  $stdout.flush()
end

#_write_headObject



366
367
368
369
# File 'lib/tushare/util.rb', line 366

def _write_head()
  $stdout.write(DATA_GETTING_TIPS)
  $stdout.flush()
end

#check_quarter(quarter) ⇒ Object



375
376
377
# File 'lib/tushare/util.rb', line 375

def check_quarter(quarter)
  fail "quarter param: #{quarter} is wrong" unless (1..4).cover? quarter
end

#check_year(year) ⇒ Object



371
372
373
# File 'lib/tushare/util.rb', line 371

def check_year(year)
  fail "year param: #{year} is wrong format" if year.to_i < 1989
end

#fetch_ftp_file(url, &block) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/tushare/util.rb', line 379

def fetch_ftp_file(url, &block)
  uri = URI(url)
  local_file_path = "/tmp/#{File.basename(uri.path)}"
  ftp = Net::FTP.new(uri.host)
  ftp.
  ftp.getbinaryfile(uri.path, local_file_path)
  return local_file_path unless block_given?
  file = File.new(local_file_path)
  result = yield file if block_given?
  file.close
  File.delete(local_file_path)
  result
end