Advertisement
Guest User

Untitled

a guest
Dec 5th, 2023
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 4.57 KB | None | 0 0
  1. function choose(file_list_names::String, is_n::Int, oos1_n::Int, n_reps::Int)
  2.  
  3.     # Initialize arrays with an estimated initial size
  4.     initial_size = 1000  # Adjust based on your initial estimate
  5.     initial_files = 10  # Initial estimate of the number of files
  6.     market_dates = Array{Int,2}(undef, initial_files, initial_size)
  7.     market_data = Array{Float64,2}(undef, initial_files, initial_size)  # Assuming 4 data columns per row
  8.     current_sizes::Array{Int,1} = fill(1, initial_files)
  9.     current_files::Int = 0
  10.     n_markets::Int = 0
  11.  
  12.     open("report.log", "w") do fpReport
  13.         println(fpReport, "CHOOSER log with IS_n=$(is_n)  OOS1_n=$(oos1_n)  Reps=$(n_reps)")
  14.  
  15.         open(file_list_names, "r") do file
  16.             for file_path in eachline(file)
  17.                 current_files += 1
  18.  
  19.                 if current_files > size(market_dates, 1)
  20.                     market_dates, market_data, current_sizes = resize_for_new_file(market_dates, market_data, current_sizes, current_files)
  21.                 end
  22.  
  23.                 prior_date = 0
  24.                 for (line_number, row) in enumerate(CSV.Rows(file_path, header=false))
  25.                     data_size = current_sizes[current_files]
  26.                     if data_size > size(market_dates, 2)
  27.                         market_dates, market_data = resize_for_new_line(market_dates, market_data, data_size * 2)  # Double the size
  28.                     end
  29.                     full_date = itemp = parse(Int, row[1])
  30.                     year = itemp ÷ 10000
  31.                     itemp -= year * 10000
  32.                     month = itemp ÷ 100
  33.                     itemp -= month * 100
  34.                     day = itemp
  35.  
  36.                     if month < 1 || month > 12 || day < 1 || day > 31 || year < 1800 || year > 2030
  37.                         println("\nERROR... Invalid date $full_date in market file $file_path line $(line_number)")
  38.                         return
  39.                     end
  40.  
  41.                     if full_date <= prior_date
  42.                         println("\nERROR... Date failed to increase in market file $file_path line $(line_number)")
  43.                         return
  44.                     end
  45.  
  46.  
  47.                     open = parse(Float64, row[2])
  48.                     high = parse(Float64, row[3])
  49.                     low = parse(Float64, row[4])
  50.                     close = parse(Float64, row[5])
  51.  
  52.                     if high < open || high < close || low > open || low > close
  53.                         println("\nERROR... Open or close outside high/low bounds in market file $file_path line $(line_number)")
  54.                         return
  55.                     end
  56.  
  57.                     prior_date = full_date
  58.                     current_sizes[current_files] += 1
  59.                     market_dates[current_files, line_number] = full_date
  60.                     market_data[current_files, line_number] = close
  61.                 end
  62.                 println(fpReport, "\nMarket file $file_path had $(current_sizes[current_files]) records from date $(market_date[current_files][1]) to $(market_date[current_files][end])")
  63.  
  64.                 n_markets += 1
  65.  
  66.             end
  67.         end
  68.  
  69.     end
  70.  
  71. end
  72.  
  73. function resize_for_new_file(market_dates::Array{Int,2}, market_data::Array{Float64,2}, current_sizes::Array{Int,1}, new_size::Int)
  74.     # Resizing the arrays for additional files
  75.     new_market_dates = Array{Int,2}(undef, new_size, size(market_dates, 2))
  76.     new_market_data = Array{Float64,2}(undef, new_size, size(market_data, 2))
  77.  
  78.     # Copy existing data
  79.     new_market_dates[1:size(market_dates, 1), :] .= market_dates
  80.     new_market_data[1:size(market_data, 1), :] .= market_data
  81.  
  82.     return new_market_dates, new_market_data, vcat(current_sizes, zeros(Int, new_size - length(current_sizes)))
  83. end
  84.  
  85. function resize_for_new_line(market_dates::Array{Int,2}, market_data::Array{Float64,2}, new_size::Int)
  86.     # Resize the arrays only if the new size is greater than the current size
  87.     if new_size > size(market_dates, 2)
  88.         # Create new arrays with the increased size
  89.         new_market_dates = Array{Int}(undef, size(market_dates, 1), new_size)
  90.         new_market_data = Array{Float64}(undef, size(market_data, 1), new_size)
  91.  
  92.         # Copy the existing data into the new arrays
  93.         new_market_dates[:, 1:size(market_dates, 2)] .= market_dates
  94.         new_market_data[:, 1:size(market_data, 2)] .= market_data
  95.  
  96.         # Update the original arrays to point to the new larger arrays
  97.         market_dates = new_market_dates
  98.         market_data = new_market_data
  99.     end
  100.  
  101.     return market_dates, market_data
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement