Home   Archive   Permalink



Simple Problem

How do I update a Field, Ive tired all sorts of variations.
    
R E B O L [title: "MtGox Price"]
    
view layout [
across
f1: field "jjhjhhj"
f2: field "ffffffff"
ck1: radio text "USD"
ck2: radio text "GBP"
ck3: radio text "EUR"
    
;button "Put DateInF1" [f1/text now]
;btn "WriteToF1" [f1/text form f2]    
;btn "WriteToF1" [f1/text form f2/text]
btn "WriteToF1" [write f1/text value f2/text]
;btn "WriteToF1" [f1/text "something"]
;btn "WriteToF1" [alert "anything"]     ;This works, so why nothing else.
]


posted by:   Ade       21-Aug-2013/19:55:29-7:00



The set-face commands works.
    
btn "Click" [set-face f1 ft/text]


posted by:   Ade       21-Aug-2013/22:53:31-7:00



This works too:
    
button "Put DateInF1" [f1/text: now show f1]
    
set-face is actually the preferred method in R3, so it's probably best to get used to using it.

posted by:   Nick       22-Aug-2013/0:07:46-7:00



btn "Get Time" [forever [wait 00:00:01 f2/text: now/Time show f2]]
    
    
But if I try to Predefine the time
    
f3: to-string now/Time
and insert that, it doesn't work, it crashes out.
    
btn "Get Time" [forever [wait 00:00:01 f2/text: f3 show f2]]
    
Seems inconsistent, what am I failing to understand, I've tried form, f3/text, to-string, value, etc and other combinations.

posted by:   Ade       22-Aug-2013/7:41:54-7:00



Here's what I'm trying to do, I'm trying to get the current price of Bitcoin from MtGox.
In order to do that I need to query their API using a UTC epoch time in milliseconds since Jan-01-1970.
I can calculate milliseconds to today, but need to know todays millisecond value too, to the nearest hour.
    
I've tried adding (time/hour * 3600) to my already computed milliseconds for the day, but when I add in the extra complexity of trying to add in today's milliseconds it does'nt seem to want to work, Is there a limit to how complex you can make an expression?
So I'm trying to calculate the no of miliseconds today separately.
hoping I can add them together then have the number of Miliseconds since Jan-01-1970 to now so I can query MtGOx only for the latest price, otherwise it sends me the whole days trading as I have to specify prices from UTC in miliseconds.
I don't want hours of data, I only want the price as it was a few minutes ago, so I need the UTC time (in miliseconds) of a few minutes ago.
Hence I'm trying to work out no of miliseconds since jan 01 1970
    
R E B O L [title: "MtGox Price"]
    
view layout [
across
f1: field "jjhjhhj"
f2: field "kjkhjhj"
    
f3: value now/Time
    
ck1: radio text "USD"
ck2: radio text "GBP"
ck3: radio text "EUR"
return
btn "Get Time" [forever [wait 00:00:01 f2/text: now/Time show f2]]
return
btn "WriteUTC" [forever [wait 00:00:01 set-face f1 ((now/Date - 01-Jan-1970) * 86400)]]
]
    
    
I tried
btn "WriteUTC" [forever [wait 00:00:01 set-face f1 (((now/Date - 01-Jan-1970) * 86400) + ((time/hour * 3600)]]
    
but no Joy so I figured maybe it's too complex an expression to do in one go, hence I'm trying to do it in pieces.

posted by:   Ade       22-Aug-2013/8:07:58-7:00



OK, so it doesn't like the forever loops, which stop once you click another button anyway, But I can live with that, they only temporary just so i could see action.
    
How come I can alert "hello World"
    
but if I define     str: "hello World" and try to alert str
    
or alert to-string str
    
the Layout just crashes.

posted by:   Ade       23-Aug-2013/17:17:41-7:00



This works as it should:
    
view layout [
     btn [str: "hello World" alert str]
]
    
Make sure that you're not defining str: "hello World" within the GUI layout block. This will create an error:
    
view layout [
     str: "hello World"
     btn [alert str]
]
    
Also, be sure not to define 'str as a function somewhere else in your code.
    
In your main example, you have the problem above. 'f3 should be assigned to some sort of widget. Also, 'time is not a function, and there was a missing parenthesis. This works:
    
R E B O L [title: "MtGox Price"]
view layout [
     across
     f1: field "jjhjhhj"
     f2: field "kjkhjhj"
     f3: field now/Time
     ck1: radio text "USD"
     ck2: radio text "GBP"
     ck3: radio text "EUR"
     return
     btn "Get Time" [f2/text: now/time show f2]    
     btn "WriteUTC" [set-face f1 ((now/Date - 01-Jan-1970) * 86400)]
     btn "WriteUTC2" [
         th: now/time
         set-face f1 (((now/Date - 01-Jan-1970) * 86400) + (th/hour * 3600))
     ]    
]

posted by:   Nick       25-Aug-2013/10:00:22-7:00



PS, the reason these examples didn't work for you is because you're trying to set the value of f1's text property. To do that you need to use a colon. The colon is the symbol used to set values in REBOL. Without the colon symbol you're just referring to ("get"ing) the value:
    
btn "WriteToF1" [f1/text: form f2/text show f1]
btn "WriteToF1" [f1/text: "something" show f1]
    
And you really need to use a copy of f2's text property in the first line, so that the 2 properties aren't permanently tied together:
    
btn "WriteToF1" [f1/text: copy f2/text show f1]
    
Try this:
    
R E B O L []
view layout [
     across
     f1: field "jjhjhhj"
     f2: field "kjkhjhj"
     btn "copy f2 text" [f1/text: copy f2/text show f1]
     btn "something" [f1/text: "something" show f1]
]

posted by:   Nick       25-Aug-2013/15:38:11-7:00



Thanks Nick, managed to get it working, it reads Bitcoin price (currently only in USD)
Hoping to try turn it into a trading robot buying and selling Bitcoins.
    
For now it will simply poll Bitcoin charts every 30 seconds and alert if the price goes above or below certain values.
    
The sequence is Get Data >>> Set U/L Limit >>> Set Limits >>> Monitor.
    
Here's the Code, is there a Rebol Community where I could post this code for further development.
    
    
R E B O L [title: "MtGox Bitcoin Price Alerter"]
    
view     layout
    
[
;*************************************************
;*** Requires current UTC time in milliseconds
;*************************************************
across
h1 "Last used UTC Time (ms)"
return
    
;*************************************************
;*** Advise user to Initialise
;*************************************************
Lab "UTC" f1: field 160 "*** Unitialised ***"
f2: field 160 "http://api.bitcoincharts.com/v1/trades.csv?symbol=mtgoxUSD&start="
str1:    "http://api.bitcoincharts.com/v1/trades.csv?symbol=mtgoxUSD&start="
;*************************************************
;*** Future ability to select currency
;*************************************************
return    
Lab "Curr" ck1: radio text "USD"
ck2: radio text "GBP"
ck3: radio text "EUR"
return
    
;****************************************************************************************
;*** If no prior UTC time, creates one, else gets an updated one from Bitcoincharts
;*** always tries to use last used or saved UTC time to save on download data
;****************************************************************************************
Lab "Init" btn "Get Data" [set-face f1 "Getting Data" wait 00:00:05
    
if not exists? %utc.gox [write %utc.gox ((now/Date - 01-Jan-1970) * 86400) set-face f1 "No File" set-face f2 now]
if exists? %utc.gox [set-face f1 read %utc.gox]
    
url1: ajoin[get-face f2 get-face f1]
set-face a1 read to-url url1
set-face f1 copy/part a1/text 10
write %utc.gox get-face f1
set-face f3 copy/part skip a1/text 11 9
    
wait 00:00:30
]
Lab "spare" btn "spare1"    
Lab "Price" f3:    field 80 "Price"
;*************************************************
;***
;*************************************************
return
Lab "Data" a1: area
return
across
    
Lab "U Lim" f4: field 120 "1000"
Lab "L Lim" f5: field 120 "0.001"
Lab "U/L % limits" f6: field 50 "0.006"
;************************************************************************************
; % limits will set upper and lower limits as a percentage of the price
; as an example MtGox charges 0.6% so limits should default to plus and minus this value at least
; as obviously you want to buy and sell at a price which covers these costs.
; but you can set the % upper and lower alarm limits to whatever you want of course..    
;*************************************************************************************
return
Lab "Alarm" btn "Set alarm Values"[
num1: to-decimal get-face f3
;num1: (num1 * get-face f6)
set-face f4 num1 * (1 + (to-decimal get-face f6))
set-face f5 num1 / (1 + (to-decimal get-face f6))
    
]
Lab "Auto" btn "Monitor" [ forever [
set-face f1 "Getting Data" wait 00:00:05
    
if not exists? %utc.gox [write %utc.gox ((now/Date - 01-Jan-1970) * 86400) set-face f1 "No File" set-face f2 now]
if exists? %utc.gox [set-face f1 read %utc.gox]
    
url1: ajoin[get-face f2 get-face f1]
set-face a1 read to-url url1
set-face f1 copy/part a1/text 10
write %utc.gox get-face f1
set-face f3 copy/part skip a1/text 11 9
num1: to-decimal get-face f3
num2: to-decimal get-face f4
num3: to-decimal get-face f5
    
if num1 > num2 [alert "Upper limit exceeded"]
if num1 < num3 [alert "Lower Limit Exceeded"]
    
;if num1 > to-decimal get-face f4 [alert "Upper limit breached"]
;if num1 < to-deciaml get-face f5 [alert "Low Limit Breached"]
    
wait 00:00:30
]
]
    
]
    


posted by:   Ade       31-Aug-2013/14:59:09-7:00



Ade,
    
Great to see you got it working. http://rebol.org is a popular repository for the community. If you want people to help work on it, posting to github seems to be the way everyone is going currently.

posted by:   Nick       31-Aug-2013/15:30:30-7:00