我有每天股票价格和买入信号的文本数据集(1是买入,-1是卖出而0是无所作为)。如何将数据导入NetLogo,因此tick 1将是第1天,所有信息都在第一行,对于每个tick,我可以根据数据得到不同的价格和信号?
我的数据采用以下格式:
tick price signal1 signal2 signal3
1 5.795 0 0 0
2 5.805 0 0 0
3 5.815 0 0 0
4 5.8 0 0 0
5 5.8 0 0 0
6 5.81 0 0 0
7 5.845 0 0 0
8 5.855 0 0 0
9 5.85 0 0 0
10 5.87 0 0 0
11 5.885 0 0 0
12 5.865 0 0 0
13 5.855 0 0 0
14 5.835 0 0 0
15 5.84 -1 0 1
globals [
global-counter
global-price
global-signal1
global-signal2
global-signal3
]
turtles-own [
cash
stock
total-asset
w1
w2
w3
w
]
to setup
clear-all
reset-ticks
create-turtle
stock-import
end
to create-turtle
create-turtles 100
ask turtles [
set shape "person"
setxy random-xcor random-ycor
set cash 1000
set stock 0
set w1 random-float 1
set w2 random-float ( 1 - w1 )
set w3 1 - w1 - w2
]
end
to stock-import
file-open "data.txt"
while [ not file-at-end? ]
[
set global-counter [ ]
set global-price [ ]
set global-signal1 [ ]
set global-signal2 [ ]
set global-signal3 [ ]
let counter file-read
let price file-read
let signal1 file-read
let signal2 file-read
let signal3 file-read
set global-counter lput counter global-counter
set global-price lput price global-price
set global-signal1 lput signal1 global-signal1
set global-signal2 lput signal2 global-signal2
set global-signal3 lput signal3 global-signal3
]
file-close
end
to make-one-trade
ask turtles [ set w ( w1 * global-signal1 + w2 * global-signal2 + w3 * global-signal3 )
set cash ( cash - w * global-price )
set stock ( stock + w1 + w2 + w3 )
set total-asset ( cash + stock * global-price )
]
end
to go
make-one-trade
tick
end
答案 0 :(得分:1)
如果您的数据文件确实使用单个空格来分隔数据,那么您可以使用csv
模块并指定分隔符:
extensions [csv]
to setup
ca
end
to go
file-open "c:/temp/temp.csv"
if file-at-end? [ stop ] ;; protect against end of file
;;read a single line (specifying delimiter)
let _line (csv:from-row file-read-line " ")
print _line ;;or do what you want
end
OT:为什么您的数据空间是分隔的? csv格式(即逗号分隔值)是已建立的国际数据交换标准: http://www.digitalpreservation.gov/formats/fdd/fdd000323.shtml