revMedia ALPHA .. Financial Chart script

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

xeir
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 35
Joined: Thu Jul 03, 2008 5:54 am

revMedia ALPHA .. Financial Chart script

Post by xeir » Mon Jul 27, 2009 5:27 pm

On the main page http://revmedia.runrev.com/revMedia/ the demo shows converting financial data to a graph. Here is the code that was shown.

Code: Select all

put url "http://table.finance.yahoo.com/table.csv?s=AAPL" into field 1
replace comma with tab in field 1
set the itemdelimiter to tab
repeat with i= 2 to 1000
  put item 7 of line i of field 1 into tClose
  put 1000-i, 200-round(tClose) & return before tAdjustedPoints
end repeat
set the points of graphic "Adjusted close" to tAdjustedPoints
show group "Adjusted close"
However, I have not gotten the sample to run as it errors out on the second to last line. I have created a graphic and made it into a group, but I'm guessing there's something missing.

I would really like to see this work. Any thoughts?

edljr
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 56
Joined: Sun Oct 26, 2008 6:47 am
Location: Tullahoma, Tennessee, United States
Contact:

Financial Chart script

Post by edljr » Mon Jul 27, 2009 6:24 pm

I think what you are missing is the rest of the "group." I tried this without the last line of the code (show group...) and it works great. I made the graphic a polygon and did not use a group.

HTH,
Ed

xeir
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 35
Joined: Thu Jul 03, 2008 5:54 am

Post by xeir » Mon Jul 27, 2009 6:45 pm

Ahh, the polygon, THAT's where I went wrong. Knew I didn't have enough coffee this morning, grin.

Thanks for the clarification!

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Mon Jul 27, 2009 7:33 pm

I think the group was there just to hold the polygon graphic for the points, and the background image with the axes and labels.

Ron Zellner
Posts: 106
Joined: Wed May 31, 2006 9:56 pm
Location: College Station, Texas
Contact:

Control display area

Post by Ron Zellner » Tue Jul 28, 2009 3:25 am

When I did this my chart just spread out over the card- so I created a rectangle (named "Frame", 1000 Wide, 500 High) where I want to display the chart, and added these lines

Code: Select all

   set the points of graphic "Adjusted close" to tAdjustedPoints
   set the width of graphic "Adjusted close" to the width of graphic "Frame"
   set the height of graphic "Adjusted close" to the height of graphic "Frame" *.9
   set the bottom of graphic "Adjusted close" to the bottom of graphic "Frame"
   set the left of graphic "Adjusted close" to the left of graphic "Frame"
That puts the chart where I want it when it runs.
Then created a field under "Frame" named "Dates" (Times, 10 pt) and added these lines to put the dates under the chart (adjust the spaces to match the chart width)

Code: Select all

put "" into field "Dates"
  repeat with i= 2 to 1000 Step 100
      put item 1 of line i of field 1 & "               " after field "Dates"
  end repeat

Ron Zellner
Posts: 106
Joined: Wed May 31, 2006 9:56 pm
Location: College Station, Texas
Contact:

Final version

Post by Ron Zellner » Tue Jul 28, 2009 6:07 pm

This is what I ended up with:

Code: Select all

on mouseUp
   
   global tClose2
   put url "http://table.finance.yahoo.com/table.csv?s=AAPL" into field "MainData"
   replace comma with tab in field "MainData"
   set the itemdelimiter to tab
   put empty into Temp
   
   repeat with i= 2 to 1000
      put item 7 of line i of field "MainData" into tClose
      put 1000-i, 200-round(tClose) & return before tAdjustedPoints
      put round(item 7 of line i of field "MainData",1)  &"," after  Temp
   end repeat
   put empty into field "Scale"
   put max (Temp) -  min (Temp) into theRange
   put theRange/10 into theInterval
   Put 0 into Inc
   repeat with L = 45 down to 1 step -5
      add 1 to Inc
      put round ((theInterval*Inc),0) + round(min (Temp),0) into line L of field "Scale"
   end repeat
   
   put round(max (Temp),0) into line 1 of field "Scale"
   put round(min (Temp),0) into line 50 of field "Scale"
   
   set the points of graphic "Adjusted close" to tAdjustedPoints
   set the width of graphic "Adjusted close" to the width of graphic "Frame"
   set the height of graphic "Adjusted close" to the height of graphic "Frame" *.99
   set the bottom of graphic "Adjusted close" to the bottom of graphic "Frame"
   set the left of graphic "Adjusted close" to the left of graphic "Frame"
   
   put "" into field "Dates"
      repeat with i= 1002 down to 2 Step -100
      put item 1 of line i of field "MainData" & "                  " after field "Dates"
   end repeat
end mouseUp
Where:
Main the data field is now named "MainData"
Create a rectangle (named "Frame", W-1000 , H-500, Border-2)
Field "Scale" (text height 10, 12 pt, bold, H-516, W-60) is placed next to graphic "Frame" and field "Dates" (10 pt, bold, H-26, W-1068) is placed below graphic "Frame" to create labels.
Then place a transparent field (Text height 50, horizontal & vertical grids checked, Tab stops = 100) over graphic "Frame" to create a grid.

The data acquired actually goes back to 1984- but this routine only utilizes the last 1000 lines of data which includes 2005 to present. Variations could easily be made to include earlier years.

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Post by Simon Knight » Thu Nov 05, 2009 4:00 pm

Hi,
I'm not sure if I should post inside a dormant thread but here goes! I have just started with Revolution and I want to create a graph of some data stored in a CSV file. I have copied the code displayed in this thread and much to my surprise managed to get it to work. I now have some questions that I hope that someone will answer:

1. I am not sure why the replace comma with tab command is used unless it is just used to make the data display look better inside the field. Am I correct in thinking that the extraction would work just as well on the original commas?

2. The lines:
repeat with i= 2 to 1000
      put item 7 of line i of field "MainData" into tClose
      put 1000-i, 200-round(tClose) & return before tAdjustedPoints
      put round(item 7 of line i of field "MainData",1)  &"," after  Temp
   end repeat
do the extraction but can some one explain exactly what is happening especially with the middle put line.

Thanks in anticipation, and sorry if these are very dumb questions.
best wishes
Skids

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Post by bn » Thu Nov 05, 2009 6:11 pm

Hi Simon,
my try at an explanation
1. I am not sure why the replace comma with tab command is used unless it is just used to make the data display look better inside the field. Am I correct in thinking that the extraction would work just as well on the original commas?
Yes, than you would not have to set the itemdelimiter to tab since the default itemdelimiter is comma. But in the field the data would be hard to read without the tab.

Code: Select all

   -- take the first thousand lines of the field, i.e. the most recent quotes,
   -- line 1 is the header, hence repeat with i = 2 to 1000
   -- you will get the most recent 999 quotes
   repeat with i= 2 to 1000 
      -- item 7 is the "adj close"
      put item 7 of line i of field "MainData" into tClose 
      
      -- here it gets interesting, the first (1000-1) part is the x (horizontal) position of the point
      -- 200 - round(tClose) is the y position of the point
      -- 200 - tClose because y on the computer starts at 0 = top, you want it the other way around as 
      -- in the real world 0 at the bottom, here it is assumed that the max tClose is not greater then 200
      -- the syntax is a little unusual but ok, normaly you would write
      -- put 1000-1 & "," & 200-round(tClose) & return before tAdjustedPoints
      -- mind you putting 'before' is used here, you would preferably use 'after' because putting after is a lot 
      -- faster in Rev then putting before because of memory shuffling in the case of before, for the graphic it is 
      -- the same whether it draws from left to right or right to left
      -- you could write with the same result

      -- put 1000-i, 200-round(tClose) & return after tAdjustedPoints 

      put 1000-i, 200-round(tClose) & return before tAdjustedPoints 
      
      -- Temp is a comma separated list of values to find the min and max value of the quotes
      put round(item 7 of line i of field "MainData",1)  & "," after  Temp 
   end repeat
   
   -- here you would want to add the following because of the trailing return, in this case it 
   -- does not hurt but trailing returns can cause trouble in fields and so on.
   delete char - 1 of tAdjustedPoints
the scaling of the graphic "Adjusted close" is done automatically by Rev when you set the graphic to the boundaries of the graphic "Frame".
The points of the graphic will change accordingly to their location on screen after scaling.
I hope I explained a bit of the code, if you have questions go ahead.
regards
Bernd

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Post by Simon Knight » Thu Nov 05, 2009 10:09 pm

Bernd,
Thank you for a very thorough explanation, especially the explanation that 'after' is faster than 'before'.

I think that my next task is to attempt to rewrite the code so that it is not so closely coupled to the data.

Is it normal in Revolution to write data to fields or does using a variable work just as well?

I am amazed that it so easy to get so much done in so few lines of code.
best wishes
Skids

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Post by bn » Thu Nov 05, 2009 10:19 pm

Simon,
I played around with the data a bit and wanted to make a graphic with the opening value, min max value and closing value, I dont know how you call them.
Assuming you have already retrieved the data and it is in the field (I dont check for that) then put this into a button

Code: Select all

on mouseUp
   put line 2 to 100 of field "MainData" into tData -- you can adjust the number of days you want, 500 or 1000 is feasible but low resolution
   set the itemdelimiter to tab
   put "" into tCollect
   repeat for each line aLine in tData
      delete item 7 of aLine
      delete item 6 of aLine
      delete item 1 of aLine
      put aLine & return after tCollect
   end repeat
   delete char -1 of tCollect --trailing return
   put tCollect into tData
   
   -- let us find out the max value of tData
   put tData into tDataTemp -- on a copy of the data
   replace tab with comma in tDataTemp -- we want a comma separated list of all values
   replace return with comma in tDataTemp -- turn lines into comma
   put round(max (tDataTemp),0) into tMax -- get the max
   put round(min (tDataTemp),0) into tMin -- get the min
   
   put the number of lines of tData * 5 into tColum -- construct the x axis
   
   repeat for each line aLine in tData
      put tColum & "," & round (tMax - item 2 of aLine) & return after tTemp
      put tColum & "," & round (tMax - item 3 of aLine) & return & return after tTemp
      
      put tColum - 2 & "," & round (tMax - item 1 of aLine) & return after tTemp
      put tColum & "," & round (tMax - item 1 of aLine) & return & return after tTemp
      
      put tColum + 2  & "," & round(tMax - item 4 of aLine) & return after tTemp
      put tColum  & "," & round (tMax - item 4 of aLine) & return & return after tTemp
      
      put tColum - 5 into  tColum
   end repeat
   delete char -1 of tTemp
   
   lock screen
   
   set the points of graphic "Adjusted close" to tTemp 
   
   set the width of graphic "Adjusted close" to the width of graphic "Frame" 
   set the height of graphic "Adjusted close" to the height of graphic "Frame" *.99 
   set the bottom of graphic "Adjusted close" to the bottom of graphic "Frame" 
   set the left of graphic "Adjusted close" to the left of graphic "Frame" 
   
   
   put empty into field "Scale"
   put tMax into line 1 of field "Scale" 
   put tMin into line 50 of field "Scale" 
   
   put tMax - tMin into theRange
   put theRange/10 into theInterval 
   Put 0 into Inc 
   repeat with L = 45 down to 1 step -5 
      add 1 to Inc 
      put round ((theInterval*Inc),0) + tMin into line L of field "Scale" 
   end repeat
   
   put tMax into line 1 of field "Scale" 
   put tMin into line 50 of field "Scale"
   
   put empty into field "dates"
   
end mouseUp
and it will produce this type of graphic.
It is more to show that with the points of a polygon graphic you can do more things then just draw a line. If you have points of a graphic (which you can look at in the property inspector btw), which are, well points, x,y return. If you have and empty line between point lists then they will be shown independently, i.e. not connecting line. You could display two circles within one graphic this way. This is the trick I use to display all these values.

regards
Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Post by bn » Thu Nov 05, 2009 10:40 pm

Simon,
Is it normal in Revolution to write data to fields or does using a variable work just as well?
actually you I to avoid working on data within fields. It is slower then working on variables. I think of fields more of temporary repositories or a way to display data. Whenever you do something to the data do it in a variable and then put it back into the field.
regards
Bernd

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Post by Simon Knight » Thu Nov 05, 2009 11:15 pm

Bernd, That is very clever. If I understand correctly the code that is adding a double return is in effect breaking the line or lifting the pen. You have just shown me how to create a scatter plot graph - many thanks
best wishes
Skids

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Post by bn » Thu Nov 05, 2009 11:22 pm

Simon,
"lifting the pen" that is it.
scatergraph or even standard deviations etc.
By all means, Maltes Chart Engine does wonders, and if you are into a lot of charts it is well worth it. But to get to know the polygon graphics beast nothing beats experimenting with it.
regards
Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Post by bn » Fri Nov 06, 2009 12:22 pm

Simon,
to make a scattergraph it is easier to just do this

Code: Select all

on mouseUp
   --set the markerpoints of grc "adjusted close" to makeCross() -- deblock for cross
   set the markerpoints of grc "adjusted close" to makeSquareBox()
   set the markerfillcolor of grc "adjusted close" to  red -- sets the fill of the marker
   set the hiliteColor of grc "adjusted close" to blue -- sets the 'line color of the marker
   set the markerLIneSize of grc "adjusted close" to 1 -- adjust the linesize of the marker
   set the linesize of grc "adjusted close" to 0 -- make the lines invisible
   set the markerfilled of grc "adjusted close" to true -- makes the fill of markers visible
end mouseUp

function makeSquareBox
   -- you can change the form of the markerpoints here
   return \
          "-2,-2" & cr &\
   "2,-2" & cr &\
   "2,2" & cr &\
   "-2,2" & cr &\
   "-2,-2" 
end makeSquareBox

function makeCross
   return \
          "0,-3" & cr &\
   "0,3" & cr & cr &\
   "3,0" & cr &\
   "-3,0" 
end makeCross
put this into a button on the original graphic with the linechart, it will turn it into a scattergram, because you set the linesize to 0 and only the markers will be shown. Alternatively you could show markers and line to mark the data points.
You get the idea
regards
Bernd

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Post by Simon Knight » Sun Nov 08, 2009 9:49 am

Bernd, Yet again many thanks. I have used your code and plotted the kind of scatter graph that I require and I am sure that I would not have found the "markerpoints" property of a polygon without your guidance. I have been experimenting with loading data in from a text file using this code snip:
answer file "Select the text file" with type "Text|csv"
if the result is "Cancel" then exit mouseUp
put "file:" & it into tURL -- merges the filepath with the text file:
put URL tURL into tFileData -- store the contents into a variable tData

I was surprised at having to use the URL command which I found by searching the forum; initially I had been trying to use some form of "file" command. Is it me or is the dictionary hard to use? For example while searching for a command to open and read a file's contents one of the commands I found was "answer file" and I would expect a description of what is returned by the command to be displayed just after the description of the command syntax but I have to scroll down to the notes section to discover that the command returns a file path and does not do anything with the file. The design of the dictionary requires me to do a lot of scrolling (I'm using a laptop) and reading to discover what the commands and functions do.(Still it could just be me!). I'm just very glad that this forum is populated by helpful people such as yourself.
best wishes
Skids

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”