Posted By

rbillig on 12/22/13


Tagged

basic java python bot visual vbs trading cex


Versions (?)

Trading at www.cex.io ( VB Script ) - Create your own Bitcoin Trading BOT


 / Published in: Visual Basic
 

' ' ' 1, Create your cex.io account from https://cex.io/r/0/rbillig/0/ ( mandatory ) ' 2, Tranfer Bitcoins to you account ' 3, Under your CEX account, create a public and secret key ' 4, Modify the 3 vars below ( GUSERNAME, GAPIKEY, GAPIKEYSECRET ) ' 5, If the decimal separator in your country is not ".", modify "GSeparateurDecimalDuSyteme" ( eg "," for France ) ' 6, Example below show how to display your balance and place an order ' 7, Send 0.01 BITCOINs to "1EfjFxXX31z7PQBKFHe7UQ7LduR64Vczf5" and use this basic code exemple to do more and more ' ' Usage ( after acount creation and key and secret key settings ) : ' ' 1, Save this content to a file named c:\test.vbs ' 2, open a Windows command prompt ( start / run / type "cmd" ) ' 3, type "cscript c:\test.vbs" ' ' ' If you win something with this code, help me and donate some BITCOINs to the following adress : ' ' 1EfjFxXX31z7PQBKFHe7UQ7LduR64Vczf5 ' ' ' Creator Remy BILLIG - billig[email protected] - December 2013 - ( France ) ' Special thanks to Demon ( http://demon.tw ) ' ' ' Donation : "1EfjFxXX31z7PQBKFHe7UQ7LduR64Vczf5" ' Thank you ' More info : https://cex.io/api '

  1. option explicit
  2. ' #
  3. ' #
  4. ' # HELP SECTION
  5. ' #
  6. ' #
  7. '
  8. '
  9. ' 1, Create your cex.io account from https://cex.io/r/0/rbillig/0/ ( mandatory )
  10. ' 2, Tranfer Bitcoins to you account
  11. ' 3, Under your CEX account, create a public and secret key
  12. ' 4, Modify the 3 vars below ( G_USERNAME, G_APIKEY, G_APIKEY_SECRET )
  13. ' 5, If the decimal separator in your country is not ".", modify "G_SeparateurDecimalDuSyteme" ( eg "," for France )
  14. ' 6, Example below show how to display your balance and place an order
  15. ' 7, Send 0.01 BITCOINs to "1EfjFxXX31z7PQBKFHe7UQ7LduR64Vczf5" and use this basic code exemple to do more and more
  16. '
  17. ' Usage ( after acount creation and key and secret key settings ) :
  18. '
  19. ' 1, Save this content to a file named c:\test.vbs
  20. ' 2, open a Windows command prompt ( start / run / type "cmd" )
  21. ' 3, type "cscript c:\test.vbs"
  22. '
  23. '
  24. ' If you win something with this code, help me and donate some BITCOINs to the following adress :
  25. '
  26. ' 1EfjFxXX31z7PQBKFHe7UQ7LduR64Vczf5
  27. '
  28. '
  29. ' Creator Remy BILLIG - [email protected] - December 2013 - ( France )
  30. ' Special thanks to Demon ( http://demon.tw )
  31. '
  32. '
  33. ' Donation : "1EfjFxXX31z7PQBKFHe7UQ7LduR64Vczf5"
  34. ' Thank you
  35. ' More info : https://cex.io/api
  36. '
  37. ' #
  38. ' #
  39. ' # END OF HELP SECTION
  40. ' #
  41. ' #
  42.  
  43. dim G_USERNAME, G_APIKEY, G_APIKEY_SECRET
  44. G_USERNAME = "rbillig"
  45. G_APIKEY = "Your Key from https://cex.io/r/0/rbillig/0/"
  46. G_APIKEY_SECRET = "Your Secret Key from https://cex.io/r/0/rbillig/0/"
  47.  
  48.  
  49.  
  50. dim G_SeparateurDecimalDuSyteme
  51. G_SeparateurDecimalDuSyteme = "."
  52.  
  53. dim G_xmlhttp
  54. set G_xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
  55. G_xmlhttp.open "GET", "https://cex.io/api//ticker/GHS/BTC"
  56. G_xmlhttp.setRequestHeader "Content-type","application/x-www-form-urlencoded"
  57.  
  58.  
  59.  
  60. main()
  61. Function main()
  62.  
  63. if G_USERNAME = "rbillig" then
  64.  
  65. wscript.echo vbnewline
  66. wscript.echo " ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !"
  67. wscript.echo vbnewline
  68. wscript.echo "1, Create your cex.io account from https://cex.io/r/0/rbillig/0/ ( mandatory )"
  69. wscript.echo "2, Under your CEX account, create a public and secret key (API dedicated)"
  70. wscript.echo "3, Modify the 3 vars G_USERNAME, G_APIKEY, G_APIKEY_SECRET"
  71. wscript.echo " (in this c:\test.vbs file)"
  72. wscript.echo "4, Send 0.01 BITCOINs to 1EfjFxXX31z7PQBKFHe7UQ7LduR64Vczf5 ( mandatory )"
  73. wscript.echo " and use this basic exemple to do more and more"
  74. wscript.echo vbnewline
  75. wscript.echo " Please read help section from this c:\test.vbs file !!!"
  76. wscript.echo vbnewline
  77. wscript.echo " ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !"
  78. wscript.echo vbnewline
  79.  
  80. else
  81.  
  82.  
  83. '
  84. '
  85. ' Get Balance
  86. '
  87. '
  88.  
  89. dim ObjetMyBalance
  90. Set ObjetMyBalance = GetBalance()
  91. 'DisplayJasonObject(ObjetMyBalance)
  92.  
  93. wscript.echo vbnewline
  94. wscript.echo vbnewline
  95.  
  96. wscript.echo "#"
  97. wscript.echo "# Balance before Order"
  98. wscript.echo "#"
  99.  
  100. wscript.echo ObjetMyBalance.item("BTC")("available") & " BTC"
  101. wscript.echo ObjetMyBalance.item("GHS")("available") & " GHS"
  102.  
  103. ' Carrefull regarding the number of queries per minute
  104. ' => wait 5 second between queries
  105. wscript.sleep 5000
  106.  
  107. '
  108. ' Place an order to buy 1 GHS at the current Rate ( if you remove the comment ... )
  109. '
  110. ' NB, if decimal separator of th system is a "," and not a "." change G_SeparateurDecimalDuSyteme at the beginning
  111. '
  112. wscript.echo "Place Order if you remove the comment"
  113. 'Set Order = PlaceOrder("GHS", "BTC", "buy", "1" , left(ObjetMyBalance.item("asks")(0)(0) ,8) )
  114.  
  115.  
  116. ' Carrefull regarding the number of queries per minute
  117. ' => wait 5 second between queries again
  118. wscript.sleep 5000
  119.  
  120. Set ObjetMyBalance = GetBalance()
  121. 'DisplayJasonObject(ObjetMyBalance)
  122. wscript.echo "#"
  123. wscript.echo "# Balance after Order"
  124. wscript.echo "#"
  125. wscript.echo ObjetMyBalance.item("BTC")("available") & " BTC"
  126. wscript.echo ObjetMyBalance.item("GHS")("available") & " GHS"
  127.  
  128. end if
  129.  
  130. End Function
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. '''
  138. ''' API DEDICATED FUNCTIONS
  139. '''
  140. '''
  141. '''
  142.  
  143. function GetOrderBook(CUR_FROM, CUR_TO )
  144. dim nonce, StringToEncode, signature, JsonObject
  145. G_xmlhttp.open "GET", "https://cex.io/api/order_book/" & CUR_FROM &"/"& CUR_TO, false
  146. G_xmlhttp.send ""
  147. Set JsonObject= New VbsJson
  148. 'Wscript.echo G_xmlhttp.responseText
  149. Set GetOrderBook = JsonObject.Decode(G_xmlhttp.responseText)
  150. end function
  151.  
  152.  
  153. function GetBalance()
  154. dim nonce, signature, JsonObject
  155. nonce = Get_Nonce()
  156. signature = hash_sha256( nonce & G_USERNAME & G_APIKEY, G_APIKEY_SECRET )
  157. G_xmlhttp.open "POST", "https://cex.io/api/balance/", false
  158. G_xmlhttp.setRequestHeader "Content-type","application/x-www-form-urlencoded"
  159. G_xmlhttp.send "key=" & G_APIKEY & "&signature=" & signature & "&nonce=" & nonce
  160. Set JsonObject= New VbsJson
  161. 'Wscript.echo G_xmlhttp.responseText
  162. Set GetBalance = JsonObject.Decode(G_xmlhttp.responseText)
  163. end function
  164.  
  165.  
  166. function PlaceOrder(CUR_FROM, CUR_TO, buy_or_sell, amount, price )
  167. dim nonce, StringToEncode, signature, JsonObject
  168.  
  169. amount=replace(amount ,G_SeparateurDecimalDuSyteme, "." )
  170. price =replace(price ,G_SeparateurDecimalDuSyteme, "." )
  171.  
  172. nonce = Get_Nonce()
  173. signature = hash_sha256( nonce & G_USERNAME & G_APIKEY, G_APIKEY_SECRET )
  174. G_xmlhttp.open "POST", "https://cex.io/api/place_order/" & CUR_FROM &"/"& CUR_TO, false
  175. G_xmlhttp.setRequestHeader "Content-type","application/x-www-form-urlencoded"
  176. G_xmlhttp.send "key=" & G_APIKEY & "&signature=" & signature & "&nonce=" & nonce & "&type=" & buy_or_sell & "&amount=" & replace(amount,G_SeparateurDecimalDuSyteme,".") & "&price=" & replace(price,G_SeparateurDecimalDuSyteme,".")
  177. Set JsonObject= New VbsJson
  178. Wscript.echo G_xmlhttp.responseText
  179. Set PlaceOrder = JsonObject.Decode(G_xmlhttp.responseText)
  180. end function
  181.  
  182. function DisplayJasonObject(o)
  183. dim i, j, k
  184. For Each i In o.keys
  185. Wscript.echo i
  186. if i="timestamp" then
  187. Wscript.echo vbtab & i & "---" & o.item(i)
  188. end if
  189. if i="bids" then
  190. for j=lbound(o.item(i)) to 5 'ubound(o.item(i))
  191. wscript.echo vbtab & o.item(i)(j)(0) & "---" & o.item(i)(j)(1)
  192. next
  193. end if
  194. if i="asks" then
  195. for j=lbound(o.item(i)) to 5 'ubound(o.item(i))
  196. wscript.echo vbtab & o.item(i)(j)(0) & "---" & o.item(i)(j)(1)
  197. next
  198. end if
  199. if i="BTC" or i="NMC" or i="GHS" then
  200. wscript.echo vbtab & "available" & " --- " & o.item(i)("available")
  201. wscript.echo vbtab & "orders " & " --- " & o.item(i)("orders")
  202. end if
  203. Next
  204. end function
  205.  
  206.  
  207. Function hash_sha256( StringToHash, KeyForHash )
  208. 'On déclare la variable servant à crypter
  209. Dim sha256,ObjUTF8, hmac
  210. Set ObjUTF8 = createobject("System.Text.UTF8Encoding")
  211. set sha256 = createobject("system.security.cryptography.HMACSHA256")
  212. ' Hachage
  213. sha256.Key = ObjUTF8.GetBytes_4( KeyForHash )
  214. hmac = sha256.ComputeHash_2( ObjUTF8.GetBytes_4( StringToHash ) )
  215. 'Libération des ressources
  216. sha256.Clear()
  217. ' Renvoi sous format string de type hexadecimale
  218. dim i, a, strText
  219. For i = 1 To LenB(hmac )
  220. a = a & Right("0" & Hex(AscB(MidB(hmac , i, 1))), 2)
  221. Next
  222. hash_sha256 = LCase(a)
  223. End Function
  224.  
  225.  
  226. Function Get_Nonce()
  227. Get_Nonce = int(10*24*3600*(date() + time()))
  228. end function
  229.  
  230.  
  231.  
  232. '''
  233. ''' JSon dedicated FUNCTIONS - Special thanks to Demon ( http://demon.tw )
  234. '''
  235. '''
  236. '''
  237. Class VbsJson
  238. 'Author: Demon
  239. 'Date: 2012/5/3
  240. 'Website: http://demon.tw
  241. Private Whitespace, NumberRegex, StringChunk
  242. Private b, f, r, n, t
  243. Private Sub Class_Initialize
  244. Whitespace = " " & vbTab & vbCr & vbLf
  245. b = ChrW(8)
  246. f = vbFormFeed
  247. r = vbCr
  248. n = vbLf
  249. t = vbTab
  250. Set NumberRegex = New RegExp
  251. NumberRegex.Pattern = "(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?"
  252. NumberRegex.Global = False
  253. NumberRegex.MultiLine = True
  254. NumberRegex.IgnoreCase = True
  255. Set StringChunk = New RegExp
  256. StringChunk.Pattern = "([\s\S]*?)([""\\\x00-\x1f])"
  257. StringChunk.Global = False
  258. StringChunk.MultiLine = True
  259. StringChunk.IgnoreCase = True
  260. End Sub
  261.  
  262. 'Return a JSON string representation of a VBScript data structure
  263. 'Supports the following objects and types
  264. '+-------------------+---------------+
  265. '| VBScript | JSON |
  266. '+===================+===============+
  267. '| Dictionary | object |
  268. '+-------------------+---------------+
  269. '| Array | array |
  270. '+-------------------+---------------+
  271. '| String | string |
  272. '+-------------------+---------------+
  273. '| Number | number |
  274. '+-------------------+---------------+
  275. '| True | true |
  276. '+-------------------+---------------+
  277. '| False | false |
  278. '+-------------------+---------------+
  279. '| Null | null |
  280. '+-------------------+---------------+
  281. Public Function Encode(ByRef obj)
  282. Dim buf, i, c, g
  283. Set buf = CreateObject("Scripting.Dictionary")
  284. Select Case VarType(obj)
  285. Case vbNull
  286. buf.Add buf.Count, "null"
  287. Case vbBoolean
  288. If obj Then
  289. buf.Add buf.Count, "true"
  290. Else
  291. buf.Add buf.Count, "false"
  292. End If
  293. Case vbInteger, vbLong, vbSingle, vbDouble
  294. buf.Add buf.Count, obj
  295. Case vbString
  296. buf.Add buf.Count, """"
  297. For i = 1 To Len(obj)
  298. c = Mid(obj, i, 1)
  299. Select Case c
  300. Case """" buf.Add buf.Count, "\"""
  301. Case "\" buf.Add buf.Count, "\\"
  302. Case "/" buf.Add buf.Count, "/"
  303. Case b buf.Add buf.Count, "\b"
  304. Case f buf.Add buf.Count, "\f"
  305. Case r buf.Add buf.Count, "\r"
  306. Case n buf.Add buf.Count, "\n"
  307. Case t buf.Add buf.Count, "\t"
  308. Case Else
  309. If AscW(c)>= 0 And AscW(c) <= 31 Then
  310. c = Right("0" & Hex(AscW(c)), 2)
  311. buf.Add buf.Count, "\u00" & c
  312. Else
  313. buf.Add buf.Count, c
  314. End If
  315. End Select
  316. Next
  317. buf.Add buf.Count, """"
  318. Case vbArray + vbVariant
  319. g = True
  320. buf.Add buf.Count, "["
  321. For Each i In obj
  322. If g Then g = False Else buf.Add buf.Count, ","
  323. buf.Add buf.Count, Encode(i)
  324. Next
  325. buf.Add buf.Count, "]"
  326. Case vbObject
  327. If TypeName(obj) = "Dictionary" Then
  328. g = True
  329. buf.Add buf.Count, "{"
  330. For Each i In obj
  331. If g Then g = False Else buf.Add buf.Count, ","
  332. buf.Add buf.Count, """" & i & """" & ":" & Encode(obj(i))
  333. Next
  334. buf.Add buf.Count, "}"
  335. Else
  336. Err.Raise 8732,,"None dictionary object"
  337. End If
  338. Case Else
  339. buf.Add buf.Count, """" & CStr(obj) & """"
  340. End Select
  341. Encode = Join(buf.Items, "")
  342. End Function
  343. 'Return the VBScript representation of ``str(``
  344. 'Performs the following translations in decoding
  345. '+---------------+-------------------+
  346. '| JSON | VBScript |
  347. '+===============+===================+
  348. '| object | Dictionary |
  349. '+---------------+-------------------+
  350. '| array | Array |
  351. '+---------------+-------------------+
  352. '| string | String |
  353. '+---------------+-------------------+
  354. '| number | Double |
  355. '+---------------+-------------------+
  356. '| true | True |
  357. '+---------------+-------------------+
  358. '| false | False |
  359. '+---------------+-------------------+
  360. '| null | Null |
  361. '+---------------+-------------------+
  362. Public Function Decode(ByRef str)
  363. Dim idx
  364. idx = SkipWhitespace(str, 1)
  365. If Mid(str, idx, 1) = "{" Then
  366. Set Decode = ScanOnce(str, 1)
  367. Else
  368. Decode = ScanOnce(str, 1)
  369. End If
  370. End Function
  371.  
  372. Private Function ScanOnce(ByRef str, ByRef idx)
  373. Dim c, ms
  374. idx = SkipWhitespace(str, idx)
  375. c = Mid(str, idx, 1)
  376. If c = "{" Then
  377. idx = idx + 1
  378. Set ScanOnce = ParseObject(str, idx)
  379. Exit Function
  380. ElseIf c = "[" Then
  381. idx = idx + 1
  382. ScanOnce = ParseArray(str, idx)
  383. Exit Function
  384. ElseIf c = """" Then
  385. idx = idx + 1
  386. ScanOnce = ParseString(str, idx)
  387. Exit Function
  388. ElseIf c = "n" And StrComp("null", Mid(str, idx, 4)) = 0 Then
  389. idx = idx + 4
  390. ScanOnce = Null
  391. Exit Function
  392. ElseIf c = "t" And StrComp("true", Mid(str, idx, 4)) = 0 Then
  393. idx = idx + 4
  394. ScanOnce = True
  395. Exit Function
  396. ElseIf c = "f" And StrComp("false", Mid(str, idx, 5)) = 0 Then
  397. idx = idx + 5
  398. ScanOnce = False
  399. Exit Function
  400. End If
  401.  
  402. Set ms = NumberRegex.Execute(Mid(str, idx))
  403. If ms.Count = 1 Then
  404. idx = idx + ms(0).Length
  405. ScanOnce = CDbl(replace( ms(0), ".",G_SeparateurDecimalDuSyteme ))
  406. Exit Function
  407. End If
  408.  
  409. Err.Raise 8732,,"No JSON object could be ScanOnced"
  410. End Function
  411. Private Function ParseObject(ByRef str, ByRef idx)
  412. Dim c, key, value
  413. Set ParseObject = CreateObject("Scripting.Dictionary")
  414. idx = SkipWhitespace(str, idx)
  415. c = Mid(str, idx, 1)
  416.  
  417. If c = "}" Then
  418. Exit Function
  419. ElseIf c <> """" Then
  420. Err.Raise 8732,,"Expecting property name"
  421. End If
  422. idx = idx + 1
  423.  
  424. Do
  425. key = ParseString(str, idx)
  426. idx = SkipWhitespace(str, idx)
  427. If Mid(str, idx, 1) <> ":" Then
  428. Err.Raise 8732,,"Expecting : delimiter"
  429. End If
  430. idx = SkipWhitespace(str, idx + 1)
  431. If Mid(str, idx, 1) = "{" Then
  432. Set value = ScanOnce(str, idx)
  433. Else
  434. value = ScanOnce(str, idx)
  435. End If
  436. ParseObject.Add key, value
  437. idx = SkipWhitespace(str, idx)
  438. c = Mid(str, idx, 1)
  439. If c = "}" Then
  440. Exit Do
  441. ElseIf c <> "," Then
  442. Err.Raise 8732,,"Expecting , delimiter"
  443. End If
  444. idx = SkipWhitespace(str, idx + 1)
  445. c = Mid(str, idx, 1)
  446. If c <> """" Then
  447. Err.Raise 8732,,"Expecting property name"
  448. End If
  449. idx = idx + 1
  450. Loop
  451. idx = idx + 1
  452. End Function
  453.  
  454. Private Function ParseArray(ByRef str, ByRef idx)
  455. Dim c, values, value
  456. Set values = CreateObject("Scripting.Dictionary")
  457. idx = SkipWhitespace(str, idx)
  458. c = Mid(str, idx, 1)
  459. If c = "]" Then
  460. ParseArray = values.Items
  461. Exit Function
  462. End If
  463. Do
  464. idx = SkipWhitespace(str, idx)
  465. If Mid(str, idx, 1) = "{" Then
  466. Set value = ScanOnce(str, idx)
  467. Else
  468. value = ScanOnce(str, idx)
  469. End If
  470. values.Add values.Count, value
  471. idx = SkipWhitespace(str, idx)
  472. c = Mid(str, idx, 1)
  473. If c = "]" Then
  474. Exit Do
  475. ElseIf c <> "," Then
  476. Err.Raise 8732,,"Expecting , delimiter"
  477. End If
  478. idx = idx + 1
  479. Loop
  480. idx = idx + 1
  481. ParseArray = values.Items
  482. End Function
  483.  
  484. Private Function ParseString(ByRef str, ByRef idx)
  485. Dim chunks, content, terminator, ms, esc, char
  486. Set chunks = CreateObject("Scripting.Dictionary")
  487. Do
  488. Set ms = StringChunk.Execute(Mid(str, idx))
  489. If ms.Count = 0 Then
  490. Err.Raise 8732,,"Unterminated string starting"
  491. End If
  492.  
  493. content = ms(0).Submatches(0)
  494. terminator = ms(0).Submatches(1)
  495. If Len(content)> 0 Then
  496. chunks.Add chunks.Count, content
  497. End If
  498.  
  499. idx = idx + ms(0).Length
  500.  
  501. If terminator = """" Then
  502. Exit Do
  503. ElseIf terminator <> "\" Then
  504. Err.Raise 8732,,"Invalid control character"
  505. End If
  506.  
  507. esc = Mid(str, idx, 1)
  508. If esc <> "u" Then
  509. Select Case esc
  510. Case """" char = """"
  511. Case "\" char = "\"
  512. Case "/" char = "/"
  513. Case "b" char = b
  514. Case "f" char = f
  515. Case "n" char = n
  516. Case "r" char = r
  517. Case "t" char = t
  518. Case Else Err.Raise 8732,,"Invalid escape"
  519. End Select
  520. idx = idx + 1
  521. Else
  522. char = ChrW("&H" & Mid(str, idx + 1, 4))
  523. idx = idx + 5
  524. End If
  525. chunks.Add chunks.Count, char
  526. Loop
  527. ParseString = Join(chunks.Items, "")
  528. End Function
  529. Private Function SkipWhitespace(ByRef str, ByVal idx)
  530. Do While idx <= Len(str) And _
  531. InStr(Whitespace, Mid(str, idx, 1))> 0
  532. idx = idx + 1
  533. Loop
  534. SkipWhitespace = idx
  535. End Function
  536. End Class

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: cryptoinvest on February 17, 2014

Hi m8..im trying this but all i get is Microsoft VBScript compilation error : Expected statement. Could you plz help

You need to login to post a comment.