プログラム学習 a


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

## round_1
class Float
  def round_1
    return 0 if self == 0 || self == nil
    num = self.to_f * 10
    return (num.round() /10.0)
  end
end


## kyu
def kyu(arr,ex)
  ex+=1
  oarr = []
  (arr.size).times{|i|
    if i < ex-1
      oarr << ""
      next
    end

    # 最安値
    min_val = arr[(i-ex+1)..i].map{|m| m.split(',')[3]}.min.to_i
    min_tm = ''
    arr[(i-ex+1)..i].each{|l|
      min_tm = l.split(',')[4] if min_val.to_s == l.split(',')[3]
    }

    # 最高値
    max_val = arr[(i-ex+1)..i].map{|m| m.split(',')[3]}.max.to_i
    max_tm = ''
    arr[(i-ex+1)..i].each{|l|
      max_tm = l.split(',')[4] if max_val.to_s == l.split(',')[3]
    }

#    sa = max_val - min_val
    val = arr[i].split(',')[3].to_i
    sa  = val - min_val # 現在値 - ex分間の最安値

#    sa = -sa if max_tm < min_tm
    if val == max_val || max_tm > min_tm
      sa = val - min_val
    else
      sa = val - max_val
    end

    per = (sa*100/min_val.to_f).round_1
    oarr << per.to_s
  }
  return oarr
end



## del ita
ldarr = File.readlines('data.txt').map{|m| m.split(',')[0..8].join(',')}

## 1min
oarr = []
old_time = old_deki = ''
old_line = ''
icnt = acnt = deki = 0


ldarr.each{|line|
  item = line.split(',')
  next if item.size != 9
  next if not item[0] =~ /[0-9]/

  val  = item[3]
  time = item[4]
  deki = item[6]
  next if val == ''

  if old_line == ''
    old_time = time
    old_deki = deki
    old_line = line
    next
  end

  icnt+=1
  acnt+=1 if old_deki != deki

  if old_time != time
    oarr << old_line + "," + icnt.to_s + "," + acnt.to_s + "," + (deki.to_i - old_deki.to_i).to_s
    icnt = acnt = 0
  end

  old_deki = deki
  old_time = time
  old_line = line
}
oarr << old_line + "," + icnt.to_s + "," + acnt.to_s + "," + (deki.to_i - old_deki.to_i).to_s


## 1min
arr_1min = []
('09:00'..'09:59').each{|m| arr_1min << m}
('10:00'..'10:59').each{|m| arr_1min << m}
('11:00'..'11:00').each{|m| arr_1min << m}
('12:30'..'12:59').each{|m| arr_1min << m}
('13:00'..'13:59').each{|m| arr_1min << m}
('14:00'..'14:59').each{|m| arr_1min << m}
('15:00'..'15:15').each{|m| arr_1min << m}


#puts oarr
#exit

## add min
cnt = 0
warr = []
arr_1min.each{|m|
  break if oarr[cnt] == nil
  if m != oarr[cnt].split(',')[4]
     if cnt-1 < 0
       cnt+=1
       next
     end
     warr << oarr[cnt-1].split(',')[0..3].join(',') + "," + m +
            "," + oarr[cnt-1].split(',')[5..8].join(',') + ",0,0,0"
  else
    warr << oarr[cnt]
    cnt+=1
  end
}

## out
#puts warr

arr1 = kyu(warr,1)
arr3 = kyu(warr,3)
arr5 = kyu(warr,5)

puts "レコード 記録日付 記録時刻 現在値 現在値時刻 前日比 出来高 売買代金 VWAP 板数 出数 出来 %"
(warr.size).times{|i|
  puts warr[i].chomp + "," + arr1[i].to_s + "," + arr3[i].to_s + "," + arr5[i].to_s
}