fb:porticula NoPaste
mtgox.bi
Uploader: | OneCypher |
Datum/Zeit: | 30.06.2011 21:09:26 |
#INCLUDE "vbcompat.bi"
'The fbJSON project:
'Forum: http://www.freebasic.net/forum/viewtopic.php?p=155994
'google-code: http://code.google.com/p/fbjson/
'Download: http://code.google.com/p/fbjson/downloads/detail?name=fbJSON-april222011.zip&can=2&q=
#include "fbJSON\fbJSON.bi"
#include "fbJSON\fbJSON.bas"
#define separator ";"
#define logging
#ifndef true
#define true (1<>0)
#endif
#ifndef false
#define false (1<>1)
#endif
function GetDT() as string
'Get Date and Time as string
return Format(TimeValue(Time)+DATEVALUE(DATE), "dd.mm.yyyy" & separator & "hh:mm:ss")
end function
sub WriteLog(msg as string)
'Write a CSV-like log
#ifdef logging
Dim dt as string = GetDT
print dt & separator & msg
#endif
end sub
function GetData(pipestring as string) as fbJSON ptr
'Get data from a pipe and return this data as JSON
dim pipeout as string
dim pipeout2 as string
dim param as string
open pipe pipestring for input as #1
line input #1, pipeout
do until eof(1)
line input #1, pipeout2
pipeout = pipeout & pipeout2
loop
return fbJSON_ImportString(pipeout)
end function
Type mtgox_account
username as string
password as string
declare function UpdateBalance() as integer
declare function BuyBTC(amount as double, price as double) as integer
declare function SellBTC(amount as double, price as double) as integer
declare function UpdateTicker() as integer
LastSell as double
LastBuy as double
LastVolume as double
LastBTC as double
LastUSD as double
declare Constructor(usrname as string, pwd as string)
UseProxy as integer = true
end type
function mtgox_account.UpdateBalance() as integer
Dim jout as fbJSON ptr
Dim pipestring as string
Dim postdata as string = "name=" & username & "&pass=" & password
dim url as string = "https://mtgox.com/code/getFunds.php"
dim param as string = "-q -O - --no-check-certificate --save-cookies mt1.txt --post-data '" & postdata & "' "
if UseProxy then param = "--proxy=on " & param
pipestring = "wget " & param & url
jout = GetData(pipestring)
if jout = NULL then
WriteLog "Error" & separator & "No data!"
return false
else
if jout->childbyname("Error") = NULL then
LastBTC = jout->childbyname("btcs")->tonumber
LastUSD = jout->childbyname("usds")->tonumber
WriteLog "New Balance" & separator & LastBTC & separator & "BTC" & separator & LastUSD & separator & "USD"
return true
else
WriteLog "Error" & separator & jout->childbyname("Error")->tostring
return false
end if
end if
end function
function mtgox_account.BuyBTC(amount as double, price as double) as integer
dim jout as fbJSON ptr
dim pipestring as string
Dim postdata as string = "name=" & username & "&pass=" & password & "&amount=" & amount & "&price=" & price
dim url as string = "https://mtgox.com/code/buyBTC.php"
dim param as string = "-q -O - --no-check-certificate --save-cookies mt1.txt --post-data '" & postdata & "' "
if UseProxy then param = "--proxy=on " & param
pipestring = "wget " & param & url
jout = GetData(pipestring)
if jout = NULL then
WriteLog "Error" & separator & "No data!"
return false
else
if jout->childbyname("Error") = NULL then
WriteLog "Buying" & separator & amount & separator & "BTC" & separator & price & separator & "USD"
return true
else
WriteLog "Error" & separator & jout->childbyname("Error")->tostring
return false
end if
end if
end function
function mtgox_account.SellBTC(amount as double, price as double) as integer
dim jout as fbJSON ptr
dim pipestring as string
Dim postdata as string = "name=" & username & "&pass=" & password & "&amount=" & amount & "&price=" & price
dim url as string = "https://mtgox.com/code/sellBTC.php"
dim param as string = "-q -O - --no-check-certificate --save-cookies mt1.txt --post-data '" & postdata & "' "
if UseProxy then param = "--proxy=on " & param
pipestring = "wget " & param & url
jout = GetData(pipestring)
if jout = NULL then
WriteLog "Error" & separator & "No data!"
return false
else
if jout->childbyname("Error") = NULL then
WriteLog "Selling" & separator & amount & separator & "BTC" & separator & price & separator & "USD"
return true
else
WriteLog "Error" & separator & jout->childbyname("Error")->tostring
return false
end if
end if
end function
function mtgox_account.UpdateTicker() as integer
Dim jout as fbJSON ptr
Dim pipestring as string
dim url as string = "http://mtgox.com/code/data/ticker.php"
dim param as string = "-q -O - --no-check-certificate --save-cookies mt1.txt "
if UseProxy then param = "--proxy=on " & param
pipestring = "wget " & param & url
jout = GetData(pipestring)
if jout = NULL then
WriteLog "Error" & separator & "No data!"
return false
else
if jout->childbyname("Error") = NULL then
jout = jout->childbyname("ticker")
LastBuy = jout->childbyname("buy")->tonumber
LastSell = jout->childbyname("sell")->tonumber
LastVolume = jout->childbyname("vol")->tonumber
WriteLog "New tickerdata" & separator & LastBuy & separator & "BTC" & separator & LastSell & separator & "BTC" & separator & LastVolume & separator & "BTC"
return true
else
WriteLog "Error" & separator & jout->childbyname("Error")->tostring
return false
end if
end if
end function
constructor mtgox_account(usrname as string, pwd as string)
username = usrname
password = pwd
end constructor