HiI’m a newbie to q but have used APL a lot and fine it eerily familiar!I have tried to build a script comprising one multi-line niladicfunction which I hoped to be able load and run.The script loads and creates the function but when I run it I get anerror halfway through.Cutting and pasting the script executes all the expressions correctly.I have obviously misunderstood how q works and would appreciate anyhelp to explain where I have gone wrong. Script and session shownbelow (apologies for the formatting).Thanks---------------------------------------------------------------------------demo1.qexample:{ /Read csv file of spot ticks, produce High/Low/Open/Close and movingaverage summary by 30 min intervals /and write back out to another csv file spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; /Load table fromfile (columns: source, timestamp, bid, offer) NonZero:select from spot where (bid\<\>0)&offer\<\>0; /View to removezero prices MidPrice:(NonZero.bid+NonZero.offer)%2; /Needed for the query: do itjust the once HalfHour:{[time] floor (
int$time)%1000*60*30}; /Function topartition time series /get the High/Low/Open/Close hloc:value select(first timestamp),(high:max MidPrice),(low:minMidPrice),(open:first MidPrice),(close:last MidPrice) byperiod:timestamp.date,HalfHour[timestamp.time] from NonZero; hloc:update moveavg20:20 mavg 0.5*high+low from hloc; /append movingaverage column save :c:/q/data/hloc.csv} /Write the output---------------------------------------------------------------------------sessionC:\q\w32\>q c:/q/scripts/demo1.q -p 5001KDB+ 2.7 2011.03.07 Copyright (C) 1993-2011 Kx Systemsw32/ 4()core 3958MB jstembridge c0234 192.168.169.234 PLAY 2011.06.05q)\f /check to see if main function created,
exampleq)example{spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; HalfHour:{[x] floor (
int$x)%1000*60*30}; NonZero:select from spot where (bid<>0)&offer<>0; MidPrice:(NonZero.bid+NonZero.offer)%2; hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),(open:fi.. save :c:/q/data/hloc.csv}q)example[] /run it{spot:("SZFF"; enlist ",") 0:
:c:/q/data/spot.csv; NonZero:select from spot where (not null bid)¬ null offer; MidPrice:(NonZero.bid+NonZero.offer)%2; HalfHour:{[time] floor (int$time)%1000*60*30}; hloc:value select(first timestamp),(high:max MidPrice),(low:minMidPrice),(open:first MidPrice),(close:last MidPrice) byperiod:timestamp.date,HalfHour[timestamp.time] from NonZero; hloc:update moveavg20:20 mavg 0.5*high+low from hloc; save
:c:/q/data/hloc.csv}'NonZero.offerq))q))spot / examine state+source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/MAR10CME/Br..q))HalfHour{[x] floor (
int$x)%1000*60*30}q))NonZero+source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/MAR10CME/Br..q))MidPrice()q))hloc()q))\q)spot'spotq)delete example from
.q)/copy and paste contents of script fileq)example:{ spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; /Load tablefrom file (columns: source, timestamp, bid, offer)'{ HalfHour:{[x] floor (
int$x)%1000*60*30}; /Function to partition time seriesq NonZero:select from spot where (bid<>0)&offer<>0; /View toremove zero prices) MidPrice:(NonZero.bid+NonZero.offer)%2; /Needed for theselect: do it just the once/get the High/Low/Open/Close hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),(open:first MidPrice),(close:last MidPrice) by period:HalfHour[timestamp.time]from NonZero; save :c:/q/data/hloc.csv;/Write the output }q)'}q)\f,
HalfHourq)\vMidPrice
NonZerohloc
spotq)/seems to have run the contents of example but does not create thefunction itselfq)hlocperiod| timestamp high low open close------| -------------------------------------------------------0 | 2009.08.21T00:00:00.000 1.68455 1.58095 1.65025 1.632451 | 2009.08.21T00:30:00.000 1.68335 1.58015 1.64955 1.631452 | 2009.08.21T01:00:00.000 1.68335 1.57825 1.64865 1.631353 | 2009.08.21T01:30:00.000 1.68195 1.57725 1.64685 1.629754 | 2009.08.21T02:00:00.000 1.68145 1.57635 1.64665 1.630955 | 2009.08.21T02:30:00.000 1.68075 1.57605 1.64695 1.630256 | 2009.08.21T03:00:00.000 1.68085 1.57685 1.64865 1.631757 | 2009.08.21T03:30:00.000 1.68125 1.57685 1.64615 1.631458 | 2009.08.21T04:00:00.000 1.68135 1.57695 1.64425 1.63359 | 2009.08.21T04:30:00.000 1.68275 1.57725 1.64425 1.632910 | 2009.08.21T05:00:00.000 1.68345 1.57745 1.64325 1.632911 | 2009.08.21T05:30:00.000 1.68305 1.57785 1.64415 1.631712 | 2009.08.21T06:00:00.000 1.68325 1.57465 1.64485 1.631413 | 2009.08.20T06:40:00.000 1.68255 1.57375 1.65745 1.6325514 | 2009.08.20T07:00:00.000 1.68575 1.57305 1.65365 1.631715 | 2009.08.20T07:30:00.000 1.68635 1.57365 1.65645 1.63216 | 2009.08.20T08:00:00.000 1.68625 1.57425 1.65585 1.6277517 | 2009.08.20T08:30:00.000 1.68455 1.57125 1.65745 1.6291518 | 2009.08.20T09:00:00.000 1.68615 1.5715 1.65485 1.6289519 | 2009.08.20T09:30:00.000 1.68715 1.57495 1.65185 1.6278..q)\l c:/q/scripts/demo1.q /reload the script, createsq)\fs#
HalfHour`exampleq)\t example / run the main function with timer354q)/works this time
I copied/pasted your message to a text editor, and it seems that youdont precede a q line of code with a whitespace character inside afunction.when you define a function, each line must start with a whitespacecharacter:myfunction: { ; ; };On Mar 11, 11:25?am, john stembridge <john.stembri…>wrote:> Hi> I’m a newbie to q but have used APL a lot and fine it eerily familiar!> I have tried to build a script comprising one multi-line niladic> function which I hoped to be able load and run.>> The script loads and creates the function but when I run it I get an> error halfway through.> Cutting and pasting the script executes all the expressions correctly.>> I have obviously misunderstood how q works and would appreciate any> help to explain where I have gone wrong. Script and session shown> below (apologies for the formatting).>> Thanks> ---------------------------------------------------------------------------> demo1.q> example:{> ?/Read csv file of spot ticks, produce High/Low/Open/Close and moving> average summary by 30 min intervals> ?/and write back out to another csv file> ?spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; /Load table from> file (columns: source, timestamp, bid, offer)> ?NonZero:select from spot where (bid<>0)&offer<>0; /View to remove> zero prices> ?MidPrice:(NonZero.bid+NonZero.offer)%2; /Needed for the query: do it> just the once> ?HalfHour:{[time] floor (
int$time)%10006030}; /Function to> partition time series> ?/get the High/Low/Open/Close> ?hloc:value select(first timestamp),(high:max MidPrice),(low:min> MidPrice),(open:first MidPrice),(close:last MidPrice) by> period:timestamp.date,HalfHour[timestamp.time] from NonZero;> ?hloc:update moveavg20:20 mavg 0.5high+low from hloc; /append moving> average column> ?save :c:/q/data/hloc.csv} /Write the output> --------------------------------------------------------------------------->> session> C:\q\w32>q c:/q/scripts/demo1.q -p 5001> KDB+ 2.7 2011.03.07 Copyright (C) 1993-2011 Kx Systems> w32/ 4()core 3958MB jstembridge c0234 192.168.169.234 PLAY 2011.06.05>> q)\f /check to see if main function created> ,
example> q)example> {spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv;> ?HalfHour:{[x] floor (
int$x)%10006030};> ?NonZero:select from spot where (bid<>0)&offer<>0;> ?MidPrice:(NonZero.bid+NonZero.offer)%2;> ?hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> (open:fi..> ?save :c:/q/data/hloc.csv}>> q)example[] /run it> {spot:("SZFF"; enlist ",") 0:
:c:/q/data/spot.csv;> ?NonZero:select from spot where (not null bid)¬ null offer;> ?MidPrice:(NonZero.bid+NonZero.offer)%2;> ?HalfHour:{[time] floor (int$time)%1000*60*30};> ?hloc:value select(first timestamp),(high:max MidPrice),(low:min> MidPrice),(open> :first MidPrice),(close:last MidPrice) by> period:timestamp.date,HalfHour[timesta> mp.time] from NonZero;> ?hloc:update moveavg20:20 mavg 0.5*high+low from hloc;> ?save
:c:/q/data/hloc.csv}> 'NonZero.offer> q))>> q))spot / examine state> +source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/> MAR10CME/Br..> q))HalfHour> {[x] floor (
int$x)%10006030}> q))NonZero> +source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/> MAR10CME/Br..> q))MidPrice> ()> q))hloc> ()> q))\> q)spot> 'spot>> q)delete example from
.>> q)/copy and paste contents of script file> q)example:{> ?spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; ? ? /Load table> from file (c> olumns: source, timestamp, bid, offer)> '{ HalfHour:{[x] floor (
int$x)%100060*30}; ? ? ? ? ? ? ? ? ? ?/> Function to par> tition time series>> q NonZero:select from spot where (bid<>0)&offer<>0; ? ? /View to> remove zero pri> ces> ) MidPrice:(NonZero.bid+NonZero.offer)%2; ? ? ? ? ? ? ? ? ? ? ? /> Needed for the> select: do it just the once> /get the High/Low/Open/Close> ?hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> (open:first> ?MidPrice),(close:last MidPrice) by period:HalfHour[timestamp.time]> from NonZero> ;> ?save :c:/q/data/hloc.csv;> /Write the output> ?}> q)'}>> q)\f> ,
HalfHour> q)\v> MidPrice
NonZerohloc
spot> q)/seems to have run the contents of example but does not create the> function itself>> q)hloc> period| timestamp ? ? ? ? ? ? ? high ? ?low ? ? open ? ?close> ------| -------------------------------------------------------> 0 ? ? | 2009.08.21T00:00:00.000 1.68455 1.58095 1.65025 1.63245> 1 ? ? | 2009.08.21T00:30:00.000 1.68335 1.58015 1.64955 1.63145> 2 ? ? | 2009.08.21T01:00:00.000 1.68335 1.57825 1.64865 1.63135> 3 ? ? | 2009.08.21T01:30:00.000 1.68195 1.57725 1.64685 1.62975> 4 ? ? | 2009.08.21T02:00:00.000 1.68145 1.57635 1.64665 1.63095> 5 ? ? | 2009.08.21T02:30:00.000 1.68075 1.57605 1.64695 1.63025> 6 ? ? | 2009.08.21T03:00:00.000 1.68085 1.57685 1.64865 1.63175> 7 ? ? | 2009.08.21T03:30:00.000 1.68125 1.57685 1.64615 1.63145> 8 ? ? | 2009.08.21T04:00:00.000 1.68135 1.57695 1.64425 1.6335> 9 ? ? | 2009.08.21T04:30:00.000 1.68275 1.57725 1.64425 1.6329> 10 ? ?| 2009.08.21T05:00:00.000 1.68345 1.57745 1.64325 1.6329> 11 ? ?| 2009.08.21T05:30:00.000 1.68305 1.57785 1.64415 1.6317> 12 ? ?| 2009.08.21T06:00:00.000 1.68325 1.57465 1.64485 1.6314> 13 ? ?| 2009.08.20T06:40:00.000 1.68255 1.57375 1.65745 1.63255> 14 ? ?| 2009.08.20T07:00:00.000 1.68575 1.57305 1.65365 1.6317> 15 ? ?| 2009.08.20T07:30:00.000 1.68635 1.57365 1.65645 1.632> 16 ? ?| 2009.08.20T08:00:00.000 1.68625 1.57425 1.65585 1.62775> 17 ? ?| 2009.08.20T08:30:00.000 1.68455 1.57125 1.65745 1.62915> 18 ? ?| 2009.08.20T09:00:00.000 1.68615 1.5715 ?1.65485 1.62895> 19 ? ?| 2009.08.20T09:30:00.000 1.68715 1.57495 1.65185 1.6278> ..>> q)\l c:/q/scripts/demo1.q /reload the script, creates> q)\f> s#
HalfHour`example>> q)\t example / run the main function with timer> 354> q)/works this time</john.stembri…>
disregard my previous answer… the copy paste was funky… stilllooking into itOn Mar 11, 12:16?pm, hlivni <hagai.li…> wrote:> I copied/pasted your message to a text editor, and it seems that you> dont precede a q line of code with a whitespace character inside a> function.>> when you define a function, each line must start with a whitespace> character:>> myfunction: {> ?;> ?;> ?};>> On Mar 11, 11:25?am, john stembridge <john.stembri…>> wrote:>> > Hi> > I’m a newbie to q but have used APL a lot and fine it eerily familiar!> > I have tried to build a script comprising one multi-line niladic> > function which I hoped to be able load and run.>> > The script loads and creates the function but when I run it I get an> > error halfway through.> > Cutting and pasting the script executes all the expressions correctly.>> > I have obviously misunderstood how q works and would appreciate any> > help to explain where I have gone wrong. Script and session shown> > below (apologies for the formatting).>> > Thanks> > ---------------------------------------------------------------------------> > demo1.q> > example:{> > ?/Read csv file of spot ticks, produce High/Low/Open/Close and moving> > average summary by 30 min intervals> > ?/and write back out to another csv file> > ?spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; /Load table from> > file (columns: source, timestamp, bid, offer)> > ?NonZero:select from spot where (bid<>0)&offer<>0; /View to remove> > zero prices> > ?MidPrice:(NonZero.bid+NonZero.offer)%2; /Needed for the query: do it> > just the once> > ?HalfHour:{[time] floor (
int$time)%10006030}; /Function to> > partition time series> > ?/get the High/Low/Open/Close> > ?hloc:value select(first timestamp),(high:max MidPrice),(low:min> > MidPrice),(open:first MidPrice),(close:last MidPrice) by> > period:timestamp.date,HalfHour[timestamp.time] from NonZero;> > ?hloc:update moveavg20:20 mavg 0.5high+low from hloc; /append moving> > average column> > ?save :c:/q/data/hloc.csv} /Write the output> > --------------------------------------------------------------------------->> > session> > C:\q\w32>q c:/q/scripts/demo1.q -p 5001> > KDB+ 2.7 2011.03.07 Copyright (C) 1993-2011 Kx Systems> > w32/ 4()core 3958MB jstembridge c0234 192.168.169.234 PLAY 2011.06.05>> > q)\f /check to see if main function created> > ,
example> > q)example> > {spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv;> > ?HalfHour:{[x] floor (
int$x)%10006030};> > ?NonZero:select from spot where (bid<>0)&offer<>0;> > ?MidPrice:(NonZero.bid+NonZero.offer)%2;> > ?hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> > (open:fi..> > ?save :c:/q/data/hloc.csv}>> > q)example[] /run it> > {spot:("SZFF"; enlist ",") 0:
:c:/q/data/spot.csv;> > ?NonZero:select from spot where (not null bid)¬ null offer;> > ?MidPrice:(NonZero.bid+NonZero.offer)%2;> > ?HalfHour:{[time] floor (int$time)%1000*60*30};> > ?hloc:value select(first timestamp),(high:max MidPrice),(low:min> > MidPrice),(open> > :first MidPrice),(close:last MidPrice) by> > period:timestamp.date,HalfHour[timesta> > mp.time] from NonZero;> > ?hloc:update moveavg20:20 mavg 0.5*high+low from hloc;> > ?save
:c:/q/data/hloc.csv}> > 'NonZero.offer> > q))>> > q))spot / examine state> > +source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/> > MAR10CME/Br..> > q))HalfHour> > {[x] floor (
int$x)%10006030}> > q))NonZero> > +source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/> > MAR10CME/Br..> > q))MidPrice> > ()> > q))hloc> > ()> > q))\> > q)spot> > 'spot>> > q)delete example from
.>> > q)/copy and paste contents of script file> > q)example:{> > ?spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; ? ? /Load table> > from file (c> > olumns: source, timestamp, bid, offer)> > '{ HalfHour:{[x] floor (
int$x)%100060*30}; ? ? ? ? ? ? ? ? ? ?/> > Function to par> > tition time series>> > q NonZero:select from spot where (bid<>0)&offer<>0; ? ? /View to> > remove zero pri> > ces> > ) MidPrice:(NonZero.bid+NonZero.offer)%2; ? ? ? ? ? ? ? ? ? ? ? /> > Needed for the> > select: do it just the once> > /get the High/Low/Open/Close> > ?hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> > (open:first> > ?MidPrice),(close:last MidPrice) by period:HalfHour[timestamp.time]> > from NonZero> > ;> > ?save :c:/q/data/hloc.csv;> > /Write the output> > ?}> > q)'}>> > q)\f> > ,
HalfHour> > q)\v> > MidPrice
NonZerohloc
spot> > q)/seems to have run the contents of example but does not create the> > function itself>> > q)hloc> > period| timestamp ? ? ? ? ? ? ? high ? ?low ? ? open ? ?close> > ------| -------------------------------------------------------> > 0 ? ? | 2009.08.21T00:00:00.000 1.68455 1.58095 1.65025 1.63245> > 1 ? ? | 2009.08.21T00:30:00.000 1.68335 1.58015 1.64955 1.63145> > 2 ? ? | 2009.08.21T01:00:00.000 1.68335 1.57825 1.64865 1.63135> > 3 ? ? | 2009.08.21T01:30:00.000 1.68195 1.57725 1.64685 1.62975> > 4 ? ? | 2009.08.21T02:00:00.000 1.68145 1.57635 1.64665 1.63095> > 5 ? ? | 2009.08.21T02:30:00.000 1.68075 1.57605 1.64695 1.63025> > 6 ? ? | 2009.08.21T03:00:00.000 1.68085 1.57685 1.64865 1.63175> > 7 ? ? | 2009.08.21T03:30:00.000 1.68125 1.57685 1.64615 1.63145> > 8 ? ? | 2009.08.21T04:00:00.000 1.68135 1.57695 1.64425 1.6335> > 9 ? ? | 2009.08.21T04:30:00.000 1.68275 1.57725 1.64425 1.6329> > 10 ? ?| 2009.08.21T05:00:00.000 1.68345 1.57745 1.64325 1.6329> > 11 ? ?| 2009.08.21T05:30:00.000 1.68305 1.57785 1.64415 1.6317> > 12 ? ?| 2009.08.21T06:00:00.000 1.68325 1.57465 1.64485 1.6314> > 13 ? ?| 2009.08.20T06:40:00.000 1.68255 1.57375 1.65745 1.63255> > 14 ? ?| 2009.08.20T07:00:00.000 1.68575 1.57305 1.65365 1.6317> > 15 ? ?| 2009.08.20T07:30:00.000 1.68635 1.57365 1.65645 1.632> > 16 ? ?| 2009.08.20T08:00:00.000 1.68625 1.57425 1.65585 1.62775> > 17 ? ?| 2009.08.20T08:30:00.000 1.68455 1.57125 1.65745 1.62915> > 18 ? ?| 2009.08.20T09:00:00.000 1.68615 1.5715 ?1.65485 1.62895> > 19 ? ?| 2009.08.20T09:30:00.000 1.68715 1.57495 1.65185 1.6278> > ..>> > q)\l c:/q/scripts/demo1.q /reload the script, creates> > q)\f> > s#
HalfHour`example>> > q)\t example / run the main function with timer> > 354> > q)/works this time</john.stembri…></hagai.li…>
The problem is with MidPrice. You cannot use ‘.’ notation withinfunctions unless the table is globalInstead ofMidPrice:(NonZero.bid+NonZero.offer)%2;doMidPrice:(NonZero[bid]+NonZero[
offer])%2;On Mar 11, 1:17 pm, hlivni <hagai.li…> wrote:> disregard my previous answer… the copy paste was funky… still> looking into it>> On Mar 11, 12:16 pm, hlivni <hagai.li…> wrote:>> > I copied/pasted your message to a text editor, and it seems that you> > dont precede a q line of code with a whitespace character inside a> > function.>> > when you define a function, each line must start with a whitespace> > character:>> > myfunction: {> > ;> > ;> > };>> > On Mar 11, 11:25 am, john stembridge <john.stembri…>> > wrote:>> > > Hi> > > I’m a newbie to q but have used APL a lot and fine it eerily familiar!> > > I have tried to build a script comprising one multi-line niladic> > > function which I hoped to be able load and run.>> > > The script loads and creates the function but when I run it I get an> > > error halfway through.> > > Cutting and pasting the script executes all the expressions correctly.>> > > I have obviously misunderstood how q works and would appreciate any> > > help to explain where I have gone wrong. Script and session shown> > > below (apologies for the formatting).>> > > Thanks> > > ---------------------------------------------------------------------------> > > demo1.q> > > example:{> > > /Read csv file of spot ticks, produce High/Low/Open/Close and moving> > > average summary by 30 min intervals> > > /and write back out to another csv file> > > spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; /Load table from> > > file (columns: source, timestamp, bid, offer)> > > NonZero:select from spot where (bid<>0)&offer<>0; /View to remove> > > zero prices> > > MidPrice:(NonZero.bid+NonZero.offer)%2; /Needed for the query: do it> > > just the once> > > HalfHour:{[time] floor (
int$time)%10006030}; /Function to> > > partition time series> > > /get the High/Low/Open/Close> > > hloc:value select(first timestamp),(high:max MidPrice),(low:min> > > MidPrice),(open:first MidPrice),(close:last MidPrice) by> > > period:timestamp.date,HalfHour[timestamp.time] from NonZero;> > > hloc:update moveavg20:20 mavg 0.5high+low from hloc; /append moving> > > average column> > > save :c:/q/data/hloc.csv} /Write the output> > > --------------------------------------------------------------------------->> > > session> > > C:\q\w32>q c:/q/scripts/demo1.q -p 5001> > > KDB+ 2.7 2011.03.07 Copyright (C) 1993-2011 Kx Systems> > > w32/ 4()core 3958MB jstembridge c0234 192.168.169.234 PLAY 2011.06.05>> > > q)\f /check to see if main function created> > > ,
example> > > q)example> > > {spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv;> > > HalfHour:{[x] floor (
int$x)%10006030};> > > NonZero:select from spot where (bid<>0)&offer<>0;> > > MidPrice:(NonZero.bid+NonZero.offer)%2;> > > hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> > > (open:fi..> > > save :c:/q/data/hloc.csv}>> > > q)example[] /run it> > > {spot:("SZFF"; enlist ",") 0:
:c:/q/data/spot.csv;> > > NonZero:select from spot where (not null bid)¬ null offer;> > > MidPrice:(NonZero.bid+NonZero.offer)%2;> > > HalfHour:{[time] floor (int$time)%1000*60*30};> > > hloc:value select(first timestamp),(high:max MidPrice),(low:min> > > MidPrice),(open> > > :first MidPrice),(close:last MidPrice) by> > > period:timestamp.date,HalfHour[timesta> > > mp.time] from NonZero;> > > hloc:update moveavg20:20 mavg 0.5*high+low from hloc;> > > save
:c:/q/data/hloc.csv}> > > 'NonZero.offer> > > q))>> > > q))spot / examine state> > > +source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/> > > MAR10CME/Br..> > > q))HalfHour> > > {[x] floor (
int$x)%10006030}> > > q))NonZero> > > +source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/> > > MAR10CME/Br..> > > q))MidPrice> > > ()> > > q))hloc> > > ()> > > q))\> > > q)spot> > > 'spot>> > > q)delete example from
.>> > > q)/copy and paste contents of script file> > > q)example:{> > > spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; /Load table> > > from file (c> > > olumns: source, timestamp, bid, offer)> > > '{ HalfHour:{[x] floor (
int$x)%100060*30}; /> > > Function to par> > > tition time series>> > > q NonZero:select from spot where (bid<>0)&offer<>0; /View to> > > remove zero pri> > > ces> > > ) MidPrice:(NonZero.bid+NonZero.offer)%2; /> > > Needed for the> > > select: do it just the once> > > /get the High/Low/Open/Close> > > hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> > > (open:first> > > MidPrice),(close:last MidPrice) by period:HalfHour[timestamp.time]> > > from NonZero> > > ;> > > save :c:/q/data/hloc.csv;> > > /Write the output> > > }> > > q)'}>> > > q)\f> > > ,
HalfHour> > > q)\v> > > MidPrice
NonZerohloc
spot> > > q)/seems to have run the contents of example but does not create the> > > function itself>> > > q)hloc> > > period| timestamp high low open close> > > ------| -------------------------------------------------------> > > 0 | 2009.08.21T00:00:00.000 1.68455 1.58095 1.65025 1.63245> > > 1 | 2009.08.21T00:30:00.000 1.68335 1.58015 1.64955 1.63145> > > 2 | 2009.08.21T01:00:00.000 1.68335 1.57825 1.64865 1.63135> > > 3 | 2009.08.21T01:30:00.000 1.68195 1.57725 1.64685 1.62975> > > 4 | 2009.08.21T02:00:00.000 1.68145 1.57635 1.64665 1.63095> > > 5 | 2009.08.21T02:30:00.000 1.68075 1.57605 1.64695 1.63025> > > 6 | 2009.08.21T03:00:00.000 1.68085 1.57685 1.64865 1.63175> > > 7 | 2009.08.21T03:30:00.000 1.68125 1.57685 1.64615 1.63145> > > 8 | 2009.08.21T04:00:00.000 1.68135 1.57695 1.64425 1.6335> > > 9 | 2009.08.21T04:30:00.000 1.68275 1.57725 1.64425 1.6329> > > 10 | 2009.08.21T05:00:00.000 1.68345 1.57745 1.64325 1.6329> > > 11 | 2009.08.21T05:30:00.000 1.68305 1.57785 1.64415 1.6317> > > 12 | 2009.08.21T06:00:00.000 1.68325 1.57465 1.64485 1.6314> > > 13 | 2009.08.20T06:40:00.000 1.68255 1.57375 1.65745 1.63255> > > 14 | 2009.08.20T07:00:00.000 1.68575 1.57305 1.65365 1.6317> > > 15 | 2009.08.20T07:30:00.000 1.68635 1.57365 1.65645 1.632> > > 16 | 2009.08.20T08:00:00.000 1.68625 1.57425 1.65585 1.62775> > > 17 | 2009.08.20T08:30:00.000 1.68455 1.57125 1.65745 1.62915> > > 18 | 2009.08.20T09:00:00.000 1.68615 1.5715 1.65485 1.62895> > > 19 | 2009.08.20T09:30:00.000 1.68715 1.57495 1.65185 1.6278> > > ..>> > > q)\l c:/q/scripts/demo1.q /reload the script, creates> > > q)\f> > > s#
HalfHour`example>> > > q)\t example / run the main function with timer> > > 354> > > q)/works this time</john.stembri…></hagai.li…></hagai.li…>
Thanks Magnus, that was the problem.I now have one more issue :-)the last line of my script attempts to write the table hloc as a csvfile.save :c:/q/data/hloc.csvAgain, it works fine when run in the session but gives the followingerror when run within the script:q)example[]k){$[1=#p:
:*|\:x:-1!x;set[x;. *p]; x 0:.h.tx[p 1]@.*p]}'hloc.:
hlocq.q))my guess is that it can’t see table hloc but it was created in thesame context so I don’t understand why it can’t be found.(i’ve also tried save :c:/q/data/hloc.csv set hloc)Again, I'd be very grateful for any assistance...JohnOn Mar 11, 6:23?pm, aka\_magnus <niall....> wrote:> The problem is with MidPrice. You cannot use '.' notation within> functions unless the table is global>> Instead of> MidPrice:(NonZero.bid+NonZero.offer)%2;> do> MidPrice:(NonZero[
bid]+NonZero[offer])%2;>> On Mar 11, 1:17 pm, hlivni <hagai.li...> wrote:>>>> > disregard my previous answer... the copy paste was funky... still> > looking into it>> > On Mar 11, 12:16 pm, hlivni <hagai.li...> wrote:>> > > I copied/pasted your message to a text editor, and it seems that you> > > dont precede a q line of code with a whitespace character inside a> > > function.>> > > when you define a function, each line must start with a whitespace> > > character:>> > > myfunction: {> > > ?<q code>;> > > ?<q code>;> > > ?};>> > > On Mar 11, 11:25 am, john stembridge <john.stembri...>> > > wrote:>> > > > Hi> > > > I'm a newbie to q but have used APL a lot and fine it eerily familiar!> > > > I have tried to build a script comprising one multi-line niladic> > > > function which I hoped to be able load and run.>> > > > The script loads and creates the function but when I run it I get an> > > > error halfway through.> > > > Cutting and pasting the script executes all the expressions correctly.>> > > > I have obviously misunderstood how q works and would appreciate any> > > > help to explain where I have gone wrong. Script and session shown> > > > below (apologies for the formatting).>> > > > Thanks> > > > ---------------------------------------------------------------------------> > > > demo1.q> > > > example:{> > > > ?/Read csv file of spot ticks, produce High/Low/Open/Close and moving> > > > average summary by 30 min intervals> > > > ?/and write back out to another csv file> > > > ?spot:("SZFF"; enlist ",") 0:
:c:/q/data/spot.csv; /Load table from> > > > file (columns: source, timestamp, bid, offer)> > > > ?NonZero:select from spot where (bid<>0)&offer<>0; /View to remove> > > > zero prices> > > > ?MidPrice:(NonZero.bid+NonZero.offer)%2; /Needed for the query: do it> > > > just the once> > > > ?HalfHour:{[time] floor (int$time)%1000*60*30}; /Function to> > > > partition time series> > > > ?/get the High/Low/Open/Close> > > > ?hloc:value select(first timestamp),(high:max MidPrice),(low:min> > > > MidPrice),(open:first MidPrice),(close:last MidPrice) by> > > > period:timestamp.date,HalfHour[timestamp.time] from NonZero;> > > > ?hloc:update moveavg20:20 mavg 0.5*high+low from hloc; /append moving> > > > average column> > > > ?save
:c:/q/data/hloc.csv} /Write the output> > > > --------------------------------------------------------------------------->> > > > session> > > > C:\q\w32>q c:/q/scripts/demo1.q -p 5001> > > > KDB+ 2.7 2011.03.07 Copyright (C) 1993-2011 Kx Systems> > > > w32/ 4()core 3958MB jstembridge c0234 192.168.169.234 PLAY 2011.06.05>> > > > q)\f /check to see if main function created> > > > ,example> > > > q)example> > > > {spot:("SZFF"; enlist ",") 0:
:c:/q/data/spot.csv;> > > > ?HalfHour:{ floor (int$x)%1000*60*30};> > > > ?NonZero:select from spot where (bid<>0)&offer<>0;> > > > ?MidPrice:(NonZero.bid+NonZero.offer)%2;> > > > ?hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> > > > (open:fi..> > > > ?save
:c:/q/data/hloc.csv}>> > > > q)example /run it> > > > {spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv;> > > > ?NonZero:select from spot where (not null bid)&not null offer;> > > > ?MidPrice:(NonZero.bid+NonZero.offer)%2;> > > > ?HalfHour:{[time] floor (
int$time)%10006030};> > > > ?hloc:value select(first timestamp),(high:max MidPrice),(low:min> > > > MidPrice),(open> > > > :first MidPrice),(close:last MidPrice) by> > > > period:timestamp.date,HalfHour[timesta> > > > mp.time] from NonZero;> > > > ?hloc:update moveavg20:20 mavg 0.5*high+low from hloc;> > > > ?save :c:/q/data/hloc.csv}> > > > 'NonZero.offer> > > > q))>> > > > q))spot / examine state> > > > +
sourcetimestamp
bidoffer!(
CME/Br. Pound/MAR10CME/Br. Pound/> > > > MAR10
CME/Br..> > > > q))HalfHour> > > > { floor (int$x)%1000*60*30}> > > > q))NonZero> > > > +
sourcetimestamp
bidoffer!(
CME/Br. Pound/MAR10CME/Br. Pound/> > > > MAR10
CME/Br..> > > > q))MidPrice> > > > ()> > > > q))hloc> > > > ()> > > > q))> > > > q)spot> > > > 'spot>> > > > q)delete example from .>> > > > q)/copy and paste contents of script file> > > > q)example:{> > > > ?spot:("SZFF"; enlist ",") 0:
:c:/q/data/spot.csv; ? ? /Load table> > > > from file (c> > > > olumns: source, timestamp, bid, offer)> > > > ‘{ HalfHour:{ floor (int$x)%1000*60*30}; ? ? ? ? ? ? ? ? ? ?/> > > > Function to par> > > > tition time series>> > > > q NonZero:select from spot where (bid<>0)&offer<>0; ? ? /View to> > > > remove zero pri> > > > ces> > > > ) MidPrice:(NonZero.bid+NonZero.offer)%2; ? ? ? ? ? ? ? ? ? ? ? /> > > > Needed for the> > > > select: do it just the once> > > > /get the High/Low/Open/Close> > > > ?hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> > > > (open:first> > > > ?MidPrice),(close:last MidPrice) by period:HalfHour[timestamp.time]> > > > from NonZero> > > > ;> > > > ?save
:c:/q/data/hloc.csv;> > > > /Write the output> > > > ?}> > > > q)’}>> > > > q)\f> > > > ,HalfHour> > > > q)\v> > > >
MidPriceNonZero
hlocspot> > > > q)/seems to have run the contents of example but does not create the> > > > function itself>> > > > q)hloc> > > > period| timestamp ? ? ? ? ? ? ? high ? ?low ? ? open ? ?close> > > > ------| -------------------------------------------------------> > > > 0 ? ? | 2009.08.21T00:00:00.000 1.68455 1.58095 1.65025 1.63245> > > > 1 ? ? | 2009.08.21T00:30:00.000 1.68335 1.58015 1.64955 1.63145> > > > 2 ? ? | 2009.08.21T01:00:00.000 1.68335 1.57825 1.64865 1.63135> > > > 3 ? ? | 2009.08.21T01:30:00.000 1.68195 1.57725 1.64685 1.62975> > > > 4 ? ? | 2009.08.21T02:00:00.000 1.68145 1.57635 1.64665 1.63095> > > > 5 ? ? | 2009.08.21T02:30:00.000 1.68075 1.57605 1.64695 1.63025> > > > 6 ? ? | 2009.08.21T03:00:00.000 1.68085 1.57685 1.64865 1.63175> > > > 7 ? ? | 2009.08.21T03:30:00.000 1.68125 1.57685 1.64615 1.63145> > > > 8 ? ? | 2009.08.21T04:00:00.000 1.68135 1.57695 1.64425 1.6335> > > > 9 ? ? | 2009.08.21T04:30:00.000 1.68275 1.57725 1.64425 1.6329> > > > 10 ? ?| 2009.08.21T05:00:00.000 1.68345 1.57745 1.64325 1.6329> > > > 11 ? ?| 2009.08.21T05:30:00.000 1.68305 1.57785 1.64415 1.6317> > > > 12 ? ?| 2009.08.21T06:00:00.000 1.68325 1.57465 1.64485 1.6314> > > > 13 ? ?| 2009.08.20T06:40:00.000 1.68255 1.57375 1.65745 1.63255> > > > 14 ? ?| 2009.08.20T07:00:00.000 1.68575 1.57305 1.65365 1.6317> > > > 15 ? ?| 2009.08.20T07:30:00.000 1.68635 1.57365 1.65645 1.632> > > > 16 ? ?| 2009.08.20T08:00:00.000 1.68625 1.57425 1.65585 1.62775> > > > 17 ? ?| 2009.08.20T08:30:00.000 1.68455 1.57125 1.65745 1.62915> > > > 18 ? ?| 2009.08.20T09:00:00.000 1.68615 1.5715 ?1.65485 1.62895> > > > 19 ? ?| 2009.08.20T09:30:00.000 1.68715 1.57495 1.65185 1.6278> > > > ..>> > > > q)\l c:/q/scripts/demo1.q /reload the script, creates> > > > q)\f> > > >
s#HalfHour
example>> > > > q)\t example / run the main function with timer> > > > 354> > > > q)/works this time- Hide quoted text ->> - Show quoted text -</john.stembri…></hagai.li…></hagai.li…></niall…>
The table hloc is defined inside your fuction “example”, and thus is alocal table to your function. The save function looks for the globalhloc table, which does not exist.You can either assign hloc as global using :: (double colon assignmentwithin a function defines the variable/table as global), or useanother method to write the local table to csv from within yourexample function, such as this (instead of using save)::c:/q/data/hloc.csv 0: csv 0: hloc;On Mar 14, 5:29?am, john stembridge <john.stembri...> wrote:> Thanks Magnus, that was the problem.>> I now have one more issue :-)>> the last line of my script attempts to write the table hloc as a csv> file.> save
:c:/q/data/hloc.csv>> Again, it works fine when run in the session but gives the following> error when run within the script:> q)example> k){$[1=#p:\:*|
-1!x;set[x;. p]; ? x 0:.h.tx[p 1]@.p]}> 'hloc> .:>
hloc> q.q))>> my guess is that it can't see table hloc but it was created in the> same context so I don't understand why it can't be found.> (i've also tried save
:c:/q/data/hloc.csv set hloc)>> Again, I’d be very grateful for any assistance…>> John>> On Mar 11, 6:23?pm, aka_magnus <niall…> wrote:>>>>>>>> > The problem is with MidPrice. You cannot use ‘.’ notation within> > functions unless the table is global>> > Instead of> > MidPrice:(NonZero.bid+NonZero.offer)%2;> > do> > MidPrice:(NonZero[bid]+NonZero[
offer])%2;>> > On Mar 11, 1:17 pm, hlivni <hagai.li…> wrote:>> > > disregard my previous answer… the copy paste was funky… still> > > looking into it>> > > On Mar 11, 12:16 pm, hlivni <hagai.li…> wrote:>> > > > I copied/pasted your message to a text editor, and it seems that you> > > > dont precede a q line of code with a whitespace character inside a> > > > function.>> > > > when you define a function, each line must start with a whitespace> > > > character:>> > > > myfunction: {> > > > ?;> > > > ?;> > > > ?};>> > > > On Mar 11, 11:25 am, john stembridge <john.stembri…>> > > > wrote:>> > > > > Hi> > > > > I’m a newbie to q but have used APL a lot and fine it eerily familiar!> > > > > I have tried to build a script comprising one multi-line niladic> > > > > function which I hoped to be able load and run.>> > > > > The script loads and creates the function but when I run it I get an> > > > > error halfway through.> > > > > Cutting and pasting the script executes all the expressions correctly.>> > > > > I have obviously misunderstood how q works and would appreciate any> > > > > help to explain where I have gone wrong. Script and session shown> > > > > below (apologies for the formatting).>> > > > > Thanks> > > > > ---------------------------------------------------------------------------> > > > > demo1.q> > > > > example:{> > > > > ?/Read csv file of spot ticks, produce High/Low/Open/Close and moving> > > > > average summary by 30 min intervals> > > > > ?/and write back out to another csv file> > > > > ?spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; /Load table from> > > > > file (columns: source, timestamp, bid, offer)> > > > > ?NonZero:select from spot where (bid<>0)&offer<>0; /View to remove> > > > > zero prices> > > > > ?MidPrice:(NonZero.bid+NonZero.offer)%2; /Needed for the query: do it> > > > > just the once> > > > > ?HalfHour:{[time] floor (
int$time)%10006030}; /Function to> > > > > partition time series> > > > > ?/get the High/Low/Open/Close> > > > > ?hloc:value select(first timestamp),(high:max MidPrice),(low:min> > > > > MidPrice),(open:first MidPrice),(close:last MidPrice) by> > > > > period:timestamp.date,HalfHour[timestamp.time] from NonZero;> > > > > ?hloc:update moveavg20:20 mavg 0.5high+low from hloc; /append moving> > > > > average column> > > > > ?save :c:/q/data/hloc.csv} /Write the output> > > > > --------------------------------------------------------------------------->> > > > > session> > > > > C:\q\w32>q c:/q/scripts/demo1.q -p 5001> > > > > KDB+ 2.7 2011.03.07 Copyright (C) 1993-2011 Kx Systems> > > > > w32/ 4()core 3958MB jstembridge c0234 192.168.169.234 PLAY 2011.06.05>> > > > > q)\f /check to see if main function created> > > > > ,
example> > > > > q)example> > > > > {spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv;> > > > > ?HalfHour:{[x] floor (
int$x)%10006030};> > > > > ?NonZero:select from spot where (bid<>0)&offer<>0;> > > > > ?MidPrice:(NonZero.bid+NonZero.offer)%2;> > > > > ?hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> > > > > (open:fi..> > > > > ?save :c:/q/data/hloc.csv}>> > > > > q)example[] /run it> > > > > {spot:("SZFF"; enlist ",") 0:
:c:/q/data/spot.csv;> > > > > ?NonZero:select from spot where (not null bid)¬ null offer;> > > > > ?MidPrice:(NonZero.bid+NonZero.offer)%2;> > > > > ?HalfHour:{[time] floor (int$time)%1000*60*30};> > > > > ?hloc:value select(first timestamp),(high:max MidPrice),(low:min> > > > > MidPrice),(open> > > > > :first MidPrice),(close:last MidPrice) by> > > > > period:timestamp.date,HalfHour[timesta> > > > > mp.time] from NonZero;> > > > > ?hloc:update moveavg20:20 mavg 0.5*high+low from hloc;> > > > > ?save
:c:/q/data/hloc.csv}> > > > > 'NonZero.offer> > > > > q))>> > > > > q))spot / examine state> > > > > +source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/> > > > > MAR10CME/Br..> > > > > q))HalfHour> > > > > {[x] floor (
int$x)%10006030}> > > > > q))NonZero> > > > > +source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/> > > > > MAR10CME/Br..> > > > > q))MidPrice> > > > > ()> > > > > q))hloc> > > > > ()> > > > > q))\> > > > > q)spot> > > > > 'spot>> > > > > q)delete example from
.>> > > > > q)/copy and paste contents of script file> > > > > q)example:{> > > > > ?spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; ? ? /Load table> > > > > from file (c> > > > > olumns: source, timestamp, bid, offer)> > > > > '{ HalfHour:{[x] floor (
int$x)%100060*30}; ? ? ? ? ? ? ? ? ? ?/> > > > > Function to par> > > > > tition time series>> > > > > q NonZero:select from spot where (bid<>0)&offer<>0; ? ? /View to> > > > > remove zero pri> > > > > ces> > > > > ) MidPrice:(NonZero.bid+NonZero.offer)%2; ? ? ? ? ? ? ? ? ? ? ? /> > > > > Needed for the> > > > > select: do it just the once> > > > > /get the High/Low/Open/Close> > > > > ?hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> > > > > (open:first> > > > > ?MidPrice),(close:last MidPrice) by period:HalfHour[timestamp.time]> > > > > from NonZero> > > > > ;> > > > > ?save :c:/q/data/hloc.csv;> > > > > /Write the output> > > > > ?}> > > > > q)'}>> > > > > q)\f> > > > > ,
HalfHour> > > > > q)\v> > > > > MidPrice
NonZerohloc
spot> > > > > q)/seems to have run the contents of example but does not create the> > > > > function itself>> > > > > q)hloc> > > > > period| timestamp ? ? ? ? ? ? ? high ? ?low ? ? open ? ?close> > > > > ------| -------------------------------------------------------> > > > > 0 ? ? | 2009.08.21T00:00:00.000 1.68455 1.58095 1.65025 1.63245> > > > > 1 ? ? | 2009.08.21T00:30:00.000 1.68335 1.58015 1.64955 1.63145> > > > > 2 ? ? | 2009.08.21T01:00:00.000 1.68335 1.57825 1.64865 1.63135> > > > > 3 ? ? | 2009.08.21T01:30:00.000 1.68195 1.57725 1.64685 1.62975> > > > > 4 ? ? | 2009.08.21T02:00:00.000 1.68145 1.57635 1.64665 1.63095> > > > > 5 ? ? | 2009.08.21T02:30:00.000 1.68075 1.57605 1.64695 1.63025> > > > > 6 ? ? | 2009.08.21T03:00:00.000 1.68085 1.57685 1.64865 1.63175> > > > > 7 ? ? | 2009.08.21T03:30:00.000 1.68125 1.57685 1.64615 1.63145> > > > > 8 ? ? | 2009.08.21T04:00:00.000 1.68135 1.57695 1.64425 1.6335> > > > > 9 ? ? | 2009.08.21T04:30:00.000 1.68275 1.57725 1.64425 1.6329> > > > > 10 ? ?| 2009.08.21T05:00:00.000 1.68345 1.57745 1.64325 1.6329> > > > > 11 ? ?| 2009.08.21T05:30:00.000 1.68305 1.57785 1.64415 1.6317> > > > > 12 ? ?| 2009.08.21T06:00:00.000 1.68325 1.57465 1.64485 1.6314> > > > > 13 ? ?| 2009.08.20T06:40:00.000 1.68255 1.57375 1.65745 1.63255> > > > > 14 ? ?| 2009.08.20T07:00:00.000 1.68575 1.57305 1.65365 1.6317> > > > > 15 ? ?| 2009.08.20T07:30:00.000 1.68635 1.57365 1.65645 1.632> > > > > 16 ? ?| 2009.08.20T08:00:00.000 1.68625 1.57425 1.65585 1.62775> > > > > 17 ? ?| 2009.08.20T08:30:00.000 1.68455 1.57125 1.65745 1.62915> > > > > 18 ? ?| 2009.08.20T09:00:00.000 1.68615 1.5715 ?1.65485 1.62895> > > > > 19 ? ?| 2009.08.20T09:30:00.000 1.68715 1.57495 1.65185 1.6278> > > > > ..>> > > > > q)\l c:/q/scripts/demo1.q /reload the script, creates> > > > > q)\f> > > > > s#
HalfHour`example>> > > > > q)\t example / run the main function with timer> > > > > 354> > > > > q)/works this time- Hide quoted text ->> > - Show quoted text -</john.stembri…></hagai.li…></hagai.li…></niall…></john.stembri…>
try
`:c:/q/data/hloc.csv 0: .h.cd hloc
Many thanks to Gabber and Magnus, I understand a little bit more now.On Mar 15, 3:18?pm, aka_magnus <niall…> wrote:> try> :c:/q/data/hloc.csv 0: .h.cd hloc>> On Mar 14, 8:29 am, john stembridge <john.stembri...> wrote:>>>> > Thanks Magnus, that was the problem.>> > I now have one more issue :-)>> > the last line of my script attempts to write the table hloc as a csv> > file.> > save
:c:/q/data/hloc.csv>> > Again, it works fine when run in the session but gives the following> > error when run within the script:> > q)example> > k){$[1=#p:\:*|
-1!x;set[x;. p]; ? x 0:.h.tx[p 1]@.p]}> > 'hloc> > .:> >
hloc> > q.q))>> > my guess is that it can't see table hloc but it was created in the> > same context so I don't understand why it can't be found.> > (i've also tried save
:c:/q/data/hloc.csv set hloc)>> > Again, I’d be very grateful for any assistance…>> > John>> > On Mar 11, 6:23 pm, aka_magnus <niall…> wrote:>> > > The problem is with MidPrice. You cannot use ‘.’ notation within> > > functions unless the table is global>> > > Instead of> > > MidPrice:(NonZero.bid+NonZero.offer)%2;> > > do> > > MidPrice:(NonZero[bid]+NonZero[
offer])%2;>> > > On Mar 11, 1:17 pm, hlivni <hagai.li…> wrote:>> > > > disregard my previous answer… the copy paste was funky… still> > > > looking into it>> > > > On Mar 11, 12:16 pm, hlivni <hagai.li…> wrote:>> > > > > I copied/pasted your message to a text editor, and it seems that you> > > > > dont precede a q line of code with a whitespace character inside a> > > > > function.>> > > > > when you define a function, each line must start with a whitespace> > > > > character:>> > > > > myfunction: {> > > > > ?;> > > > > ?;> > > > > ?};>> > > > > On Mar 11, 11:25 am, john stembridge <john.stembri…>> > > > > wrote:>> > > > > > Hi> > > > > > I’m a newbie to q but have used APL a lot and fine it eerily familiar!> > > > > > I have tried to build a script comprising one multi-line niladic> > > > > > function which I hoped to be able load and run.>> > > > > > The script loads and creates the function but when I run it I get an> > > > > > error halfway through.> > > > > > Cutting and pasting the script executes all the expressions correctly.>> > > > > > I have obviously misunderstood how q works and would appreciate any> > > > > > help to explain where I have gone wrong. Script and session shown> > > > > > below (apologies for the formatting).>> > > > > > Thanks> > > > > > ---------------------------------------------------------------------------> > > > > > demo1.q> > > > > > example:{> > > > > > ?/Read csv file of spot ticks, produce High/Low/Open/Close and moving> > > > > > average summary by 30 min intervals> > > > > > ?/and write back out to another csv file> > > > > > ?spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; /Load table from> > > > > > file (columns: source, timestamp, bid, offer)> > > > > > ?NonZero:select from spot where (bid<>0)&offer<>0; /View to remove> > > > > > zero prices> > > > > > ?MidPrice:(NonZero.bid+NonZero.offer)%2; /Needed for the query: do it> > > > > > just the once> > > > > > ?HalfHour:{[time] floor (
int$time)%10006030}; /Function to> > > > > > partition time series> > > > > > ?/get the High/Low/Open/Close> > > > > > ?hloc:value select(first timestamp),(high:max MidPrice),(low:min> > > > > > MidPrice),(open:first MidPrice),(close:last MidPrice) by> > > > > > period:timestamp.date,HalfHour[timestamp.time] from NonZero;> > > > > > ?hloc:update moveavg20:20 mavg 0.5high+low from hloc; /append moving> > > > > > average column> > > > > > ?save :c:/q/data/hloc.csv} /Write the output> > > > > > --------------------------------------------------------------------------->> > > > > > session> > > > > > C:\q\w32>q c:/q/scripts/demo1.q -p 5001> > > > > > KDB+ 2.7 2011.03.07 Copyright (C) 1993-2011 Kx Systems> > > > > > w32/ 4()core 3958MB jstembridge c0234 192.168.169.234 PLAY 2011.06.05>> > > > > > q)\f /check to see if main function created> > > > > > ,
example> > > > > > q)example> > > > > > {spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv;> > > > > > ?HalfHour:{[x] floor (
int$x)%10006030};> > > > > > ?NonZero:select from spot where (bid<>0)&offer<>0;> > > > > > ?MidPrice:(NonZero.bid+NonZero.offer)%2;> > > > > > ?hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> > > > > > (open:fi..> > > > > > ?save :c:/q/data/hloc.csv}>> > > > > > q)example[] /run it> > > > > > {spot:("SZFF"; enlist ",") 0:
:c:/q/data/spot.csv;> > > > > > ?NonZero:select from spot where (not null bid)¬ null offer;> > > > > > ?MidPrice:(NonZero.bid+NonZero.offer)%2;> > > > > > ?HalfHour:{[time] floor (int$time)%1000*60*30};> > > > > > ?hloc:value select(first timestamp),(high:max MidPrice),(low:min> > > > > > MidPrice),(open> > > > > > :first MidPrice),(close:last MidPrice) by> > > > > > period:timestamp.date,HalfHour[timesta> > > > > > mp.time] from NonZero;> > > > > > ?hloc:update moveavg20:20 mavg 0.5*high+low from hloc;> > > > > > ?save
:c:/q/data/hloc.csv}> > > > > > 'NonZero.offer> > > > > > q))>> > > > > > q))spot / examine state> > > > > > +source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/> > > > > > MAR10CME/Br..> > > > > > q))HalfHour> > > > > > {[x] floor (
int$x)%10006030}> > > > > > q))NonZero> > > > > > +source
timestampbid
offer!(CME/Br. Pound/MAR10
CME/Br. Pound/> > > > > > MAR10CME/Br..> > > > > > q))MidPrice> > > > > > ()> > > > > > q))hloc> > > > > > ()> > > > > > q))\> > > > > > q)spot> > > > > > 'spot>> > > > > > q)delete example from
.>> > > > > > q)/copy and paste contents of script file> > > > > > q)example:{> > > > > > ?spot:(“SZFF”; enlist “,”) 0: :c:/q/data/spot.csv; ? ? /Load table> > > > > > from file (c> > > > > > olumns: source, timestamp, bid, offer)> > > > > > '{ HalfHour:{[x] floor (
int$x)%100060*30}; ? ? ? ? ? ? ? ? ? ?/> > > > > > Function to par> > > > > > tition time series>> > > > > > q NonZero:select from spot where (bid<>0)&offer<>0; ? ? /View to> > > > > > remove zero pri> > > > > > ces> > > > > > ) MidPrice:(NonZero.bid+NonZero.offer)%2; ? ? ? ? ? ? ? ? ? ? ? /> > > > > > Needed for the> > > > > > select: do it just the once> > > > > > /get the High/Low/Open/Close> > > > > > ?hloc:select(first timestamp),(high:max MidPrice),(low:min MidPrice),> > > > > > (open:first> > > > > > ?MidPrice),(close:last MidPrice) by period:HalfHour[timestamp.time]> > > > > > from NonZero> > > > > > ;> > > > > > ?save :c:/q/data/hloc.csv;> > > > > > /Write the output> > > > > > ?}> > > > > > q)'}>> > > > > > q)\f> > > > > > ,
HalfHour> > > > > > q)\v> > > > > > MidPrice
NonZerohloc
spot> > > > > > q)/seems to have run the contents of example but does not create the> > > > > > function itself>> > > > > > q)hloc> > > > > > period| timestamp ? ? ? ? ? ? ? high ? ?low ? ? open ? ?close> > > > > > ------| -------------------------------------------------------> > > > > > 0 ? ? | 2009.08.21T00:00:00.000 1.68455 1.58095 1.65025 1.63245> > > > > > 1 ? ? | 2009.08.21T00:30:00.000 1.68335 1.58015 1.64955 1.63145> > > > > > 2 ? ? | 2009.08.21T01:00:00.000 1.68335 1.57825 1.64865 1.63135> > > > > > 3 ? ? | 2009.08.21T01:30:00.000 1.68195 1.57725 1.64685 1.62975> > > > > > 4 ? ? | 2009.08.21T02:00:00.000 1.68145 1.57635 1.64665 1.63095> > > > > > 5 ? ? | 2009.08.21T02:30:00.000 1.68075 1.57605 1.64695 1.63025> > > > > > 6 ? ? | 2009.08.21T03:00:00.000 1.68085 1.57685 1.64865 1.63175> > > > > > 7 ? ? | 2009.08.21T03:30:00.000 1.68125 1.57685 1.64615 1.63145> > > > > > 8 ? ? | 2009.08.21T04:00:00.000 1.68135 1.57695 1.64425 1.6335> > > > > > 9 ? ? | 2009.08.21T04:30:00.000 1.68275 1.57725 1.64425 1.6329> > > > > > 10 ? ?| 2009.08.21T05:00:00.000 1.68345 1.57745 1.64325 1.6329> > > > > > 11 ? ?| 2009.08.21T05:30:00.000 1.68305 1.57785 1.64415 1.6317> > > > > > 12 ? ?| 2009.08.21T06:00:00.000 1.68325 1.57465 1.64485 1.6314> > > > > > 13 ? ?| 2009.08.20T06:40:00.000 1.68255 1.57375 1.65745 1.63255> > > > > > 14 ? ?| 2009.08.20T07:00:00.000 1.68575 1.57305 1.65365 1.6317> > > > > > 15 ? ?| 2009.08.20T07:30:00.000 1.68635 1.57365 1.65645 1.632> > > > > > 16 ? ?| 2009.08.20T08:00:00.000 1.68625 1.57425 1.65585 1.62775> > > > > > 17 ? ?| 2009.08.20T08:30:00.000 1.68455 1.57125 1.65745 1.62915> > > > > > 18 ? ?| 2009.08.20T09:00:00.000 1.68615 1.5715 ?1.65485 1.62895> > > > > > 19 ? ?| 2009.08.20T09:30:00.000 1.68715 1.57495 1.65185 1.6278> > > > > > ..>> > > > > > q)\l c:/q/scripts/demo1.q /reload the script, creates> > > > > > q)\f> > > > > > s#
HalfHour`example>> > > > > > q)\t example / run the main function with timer> > > > > > 354> > > > > > q)/works this time- Hide quoted text ->> > > - Show quoted text – Hide quoted text ->> - Show quoted text -</john.stembri…></hagai.li…></hagai.li…></niall…></john.stembri…></niall…>
in general there’s a large collection of things that work only onglobals, not on locals or function parametersamong these are dereference (getname), dot addressing (i.e. table.color datetime.date), and in-place operations (update from
table…)On Tue, Mar 15, 2011 at 12:52 PM, john stembridge<john.stembridge> wrote:> Many thanks to Gabber and Magnus, I understand a little bit more now.>> On Mar 15, 3:18?pm, aka_magnus <niall…> wrote:>> try>> `:c:/q/data/hloc.csv 0: .h.cd hloc</niall…></john.stembridge>