Advertisement
Guest User

Untitled

a guest
Jul 17th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.84 KB | None | 0 0
  1. import requests
  2. import base64
  3. import json
  4. import os
  5. import time
  6. import string
  7. import random
  8. import names
  9. from data import*
  10.  
  11. #content of data.py
  12. #egelauth="Basic XXXXXX"
  13. #egelaccountid="XXXXXX" #stable
  14. #i=0
  15. #password="XXX" #password for bots
  16. #urlbasis="http://stable.alpha-trader.com/"
  17. #partnerid="XXXXX" #stable
  18. #headeregel={
  19. # 'authorization': egelauth,
  20. # 'x-authorization': partnerid,
  21. # 'cache-control': "no-cache",
  22. # }
  23. #auth=""
  24.  
  25. def getPortfolio(securityAccount):
  26. url = urlbasis+"api/portfolios/"+securityAccount
  27. response = requests.request("GET", url, headers=headeregel, timeout=50)
  28. parsed_json = json.loads(response.text)
  29. return parsed_json
  30.  
  31. def getOrderbook(securityIdentifier):
  32. #timestamp = int(time.time())
  33. url = urlbasis+"api/orderbook/"+securityIdentifier
  34. #try:
  35. # response = requests.request("GET", url, headers=headeregel, timeout=50)
  36. # parsed_json = json.loads(response.text)
  37. #except:
  38. # parsed_json="[]"
  39. #print(parsed_json)
  40. response = requests.request("GET", url, headers=headeregel, timeout=50)
  41. parsed_json = json.loads(response.text)
  42. #timeneeded=int(time.time())-timestamp
  43. #print(timeneeded)
  44. #print("timeneeded")
  45. return parsed_json
  46.  
  47. def createUser(username, password):
  48. print("createUser")
  49. print("\n")
  50. url = urlbasis+"user/register"
  51. mail=username+"@gmx.de"
  52. companyname="comp"+username
  53. querystring = {"username":username,"emailAddress":mail,"password":password}
  54. headers = {
  55. 'x-authorization': partnerid,
  56. 'cache-control': "no-cache",
  57. }
  58. response = requests.request("POST", url, headers=headers, params=querystring)
  59. print(response.text)
  60. print("\n")
  61. print(querystring)
  62.  
  63. def createCompany(companyname, username, auth):
  64. print("createCompany")
  65. print("\n")
  66. url = urlbasis+"api/companies/"
  67. querystring = {"name":companyname,"cashDeposit":"50000"}
  68. headers = {
  69. 'authorization': auth,
  70. 'x-authorization': partnerid,
  71. 'cache-control': "no-cache",
  72. }
  73. response = requests.request("POST", url, headers=headers, params=querystring)
  74. print(response.text)
  75. print("\n")
  76.  
  77. def getcompanyIDbyUser(username):
  78. print("getcompanyIDbyUser")
  79. print("\n")
  80. url = urlbasis+"api/companies/ceo/username/"+username
  81. response = requests.request("GET", url, headers=headeregel)
  82. parsed_json = json.loads(response.text)
  83. companyid=parsed_json[0]['id']
  84. return companyid
  85.  
  86. def employEgelAsCeo(companyid, auth):
  87. print("employEgelAsCeo")
  88. print("\n")
  89. url = urlbasis+"api/polls/employceo"
  90. querystring = {"companyId":companyid,"dailyWage":"150"}
  91. print("test")
  92. print(auth)
  93. print("\n")
  94. print(querystring)
  95. response = requests.request("POST", url, headers=headeregel, params=querystring)
  96. print("test")
  97. #print(response.text)
  98. print("\n")
  99. url = urlbasis+"api/polls/"
  100. headers = {
  101. 'authorization': auth,
  102. 'x-authorization': partnerid,
  103. 'cache-control': "no-cache",
  104. }
  105. response = requests.request("GET", url, headers=headers)
  106. #print(response.text)
  107. #input()
  108. #print(json.loads(response))
  109. parsed_json = json.loads(response.text)
  110. #input()
  111. pollid=parsed_json[0]['id']
  112. print(pollid)
  113.  
  114. url = urlbasis+"api/polls/"+pollid
  115.  
  116. querystring = {"voices":"50000","votingType":"YES"}
  117. headers = {
  118. 'authorization': auth,
  119. 'x-authorization': partnerid,
  120. 'cache-control': "no-cache",
  121. }
  122. response = requests.request("POST", url, headers=headers, params=querystring)
  123. print(response.text)
  124. print("\n")
  125. #print(response.text)
  126.  
  127. url = urlbasis+"api/polls/execute/"+pollid
  128. response = requests.request("POST", url, headers=headeregel)
  129.  
  130. def getSecurityIdentifier(companyname):
  131. print("getSecurityIdentifier")
  132. print("\n")
  133. url = urlbasis+"api/search/companies/"+companyname
  134.  
  135. headers = {
  136. 'authorization': egelauth,
  137. 'x-authorization': partnerid,
  138. 'cache-control': "no-cache",
  139. }
  140. response = requests.request("GET", url, headers=headers)
  141. print(response.text)
  142. print("\n")
  143. parsed_json = json.loads(response.text)
  144. securityidentifier=parsed_json[0]['securityIdentifier']
  145. return securityidentifier
  146.  
  147. def getSecurityAccount(companyname):
  148. #print("getSecurityAccount")
  149. #print("\n")
  150. url = urlbasis+"api/search/companies/"+companyname
  151.  
  152. headers = {
  153. 'authorization': egelauth,
  154. 'x-authorization': partnerid,
  155. 'cache-control': "no-cache",
  156. }
  157. response = requests.request("GET", url, headers=headers)
  158. #print(response.text)
  159. #print("\n")
  160. parsed_json = json.loads(response.text)
  161. securityaccount=parsed_json[0]['securitiesAccountId']
  162. return securityaccount
  163.  
  164. def getCompanyIDbyName(companyname):
  165. print("getCompanyIDbyName")
  166. print("\n")
  167. url = urlbasis+"api/search/companies/"+companyname
  168.  
  169. headers = {
  170. 'authorization': egelauth,
  171. 'x-authorization': partnerid,
  172. 'cache-control': "no-cache",
  173. }
  174. response = requests.request("GET", url, headers=headers)
  175. print(response.text)
  176. print("\n")
  177. parsed_json = json.loads(response.text)
  178. securityaccount=parsed_json[0]['id']
  179. return securityaccount
  180.  
  181. def otcOrderSell(company1, company2):
  182. print("otcOrderSell")
  183. print("\n")
  184. url = urlbasis+"api/securityorders/"
  185. querystring = {"owner":getSecurityAccount(company1),"securityIdentifier":getSecurityIdentifier(company1),"action":"SELL","type":"LIMIT","price":"1","numberOfShares":"50000","counterparty":getSecurityAccount(company2)}
  186. headers = {
  187. 'authorization': egelauth,
  188. 'x-authorization': partnerid,
  189. 'cache-control': "no-cache",
  190.  
  191. }
  192. response = requests.request("POST", url, headers=headers, params=querystring)
  193. print(response.text)
  194. print("\n")
  195. querystring = {"owner":getSecurityAccount(company2),"securityIdentifier":getSecurityIdentifier(company1),"action":"BUY","type":"LIMIT","price":"1","numberOfShares":"50000","counterparty":getSecurityAccount(company1)}
  196. response = requests.request("POST", url, headers=headers, params=querystring)
  197. print(response.text)
  198. print("\n")
  199.  
  200. def intitiateLiquidationPoll(company):
  201. print("initiateliquidationpoll")
  202. print("\n")
  203. url = urlbasis+"api/polls/liquidation"
  204. querystring = {"companyId":getCompanyIDbyName(company)}
  205. headers = {
  206. 'authorization': egelauth,
  207. 'x-authorization': partnerid,
  208. 'cache-control': "no-cache",
  209. }
  210. response = requests.request("POST", url, headers=headers, params=querystring)
  211. print(response.text)
  212. print("\n")
  213.  
  214. #def acceptliquidationpoll(company)
  215. def sellowncompany(company, auth):
  216. print("sellowncompany")
  217. print("\n")
  218. url = urlbasis+"api/securityorders/"
  219. querystring = {"owner":getSecurityAccount(company),"securityIdentifier":getSecurityIdentifier(company),"action":"SELL","type":"LIMIT","price":"1","numberOfShares":"50000","counterparty":""}
  220. headers = {
  221. 'authorization': auth,
  222. 'x-authorization': partnerid,
  223. 'cache-control': "no-cache",
  224.  
  225. }
  226. response = requests.request("POST", url, headers=headers, params=querystring)
  227. print(response.text)
  228. print("\n")
  229.  
  230. def getlastpoll():
  231. print("getlastpoll")
  232. print("\n")
  233. url = urlbasis+"api/initiatedpolls/"
  234. response = requests.request("GET", url, headers=headeregel)
  235.  
  236. parsed_json = json.loads(response.text)
  237. #print(parsed_json.lentgh())
  238. pollid=parsed_json[-1]['id']
  239. return pollid
  240.  
  241. def votepoll (pollid):
  242. print("votepoll")
  243. print("\n")
  244. url = urlbasis+"api/polls/"+pollid
  245. querystring = {"voices":"50000","votingType":"YES"}
  246. response = requests.request("POST", url, headers=headeregel, params=querystring)
  247.  
  248.  
  249. def expoll (pollid):
  250. print("createUser")
  251. print("\n")
  252. url = urlbasis+"api/polls/execute/"+pollid
  253. response = requests.request("POST", url, headers=headeregel)
  254.  
  255. def id_generator(size=30, chars=string.ascii_uppercase + string.digits):
  256. return ''.join(random.choice(chars) for _ in range(size))
  257.  
  258. def randomName(capitalize):
  259.  
  260. bits=[]
  261. vowels="aeiou"
  262. letters="abcdefghijklmnopqrstuvwxyz"
  263. for ch in letters:
  264. for v in vowels:
  265. bits.append(ch+v)
  266. bits.remove("fu")
  267. bits.remove("hi")
  268. bits.remove("cu")
  269. bits.remove("co")
  270. bits.remove("mo")
  271. word=""
  272. rnd=len(bits)-1
  273. numOfBits=random.randint(2,3)
  274. for i in range(0,numOfBits):
  275. word=word+bits[random.randint(1,rnd)]
  276. word=word+letters[random.randrange(0,25)]
  277. if (capitalize==True):
  278. word=word.capitalize()
  279. return word
  280.  
  281. def otcOrderSellNormalAuth(company1, company2, auth):
  282. print("otcOrderSell")
  283. print("\n")
  284. url = urlbasis+"api/securityorders/"
  285. querystring = {"owner":getSecurityAccount(company1),"securityIdentifier":getSecurityIdentifier(company1),"action":"SELL","type":"LIMIT","price":"1","numberOfShares":"50000","counterparty":getSecurityAccount(company2)}
  286. headers = {
  287. 'authorization': auth,
  288. 'x-authorization': partnerid,
  289. 'cache-control': "no-cache",
  290.  
  291. }
  292. response = requests.request("POST", url, headers=headers, params=querystring)
  293. print(response.text)
  294. print("\n")
  295. querystring = {"owner":getSecurityAccount(company2),"securityIdentifier":getSecurityIdentifier(company1),"action":"BUY","type":"LIMIT","price":"1","numberOfShares":"50000","counterparty":getSecurityAccount(company1)}
  296. response = requests.request("POST", url, headers=headeregel, params=querystring)
  297. print(response.text)
  298. print("\n")
  299.  
  300. def foundCompanysAndSell():
  301. i=0
  302. runid=id_generator()
  303. while i<100:
  304. username=runid+"ZZZ"+str(i)
  305. #username="Baby"
  306. #username=names.get_first_name()
  307. ag=username
  308. auth=username+":"+password
  309. auth= base64.encodestring(auth)
  310. auth="Basic "+auth
  311. auth=auth.replace('\n', '')
  312. createUser(username)
  313. createCompany(ag, username, auth)
  314. print(username)
  315. #time.sleep(2)
  316. employEgelAsCeo(getcompanyIDbyUser(username), auth)
  317. #input("emplox")
  318. otcOrderSellNormalAuth(ag, "1GK6VPBI1LJ4E2142IJIDS8Q00PNPVZZZ0", egelauth)
  319. #input("otc")
  320. time.sleep(2)
  321. intitiateLiquidationPoll(ag)
  322. #input("liquidation")
  323. lastpoll=getlastpoll()
  324. votepoll(lastpoll)
  325. #input("input")
  326. expoll(getlastpoll())
  327. #input("input")
  328. i=i+1
  329. print(username)
  330. #input()
  331. #i=0
  332. #while i<100:
  333. # username=runid+"ZZZ"+str(i)
  334. # #username=names.get_first_name()
  335. # #username="Baby"
  336. # ag=username
  337. # auth=username+":"+password
  338. # auth= base64.encodestring(auth)
  339. # auth="Basic "+auth
  340. # auth=auth.replace('\n', '')
  341. # #createUser(username)
  342. # #createCompany(ag, username)
  343. # print(username)
  344. # #employEgelAsCeo(getcompanyIDbyUser(username))
  345. # otcOrderSellNormalAuth(ag, "Bender", auth)
  346. # #time.sleep(2)
  347. # intitiateLiquidationPoll(ag)
  348. # lastpoll=getlastpoll()
  349. # votepoll(lastpoll)
  350. # expoll(getlastpoll())
  351. # i=i+1
  352. # print(username)
  353.  
  354. def createAndSaveUsers(file):
  355. i=0
  356. usernames = [
  357. {'user' : '1', 'ag' : '1-AG'},
  358. {'user' : '1', 'ag' : '1-AG'},
  359. {'user' : '1', 'ag' : '1-AG'},
  360. {'user' : '1', 'ag' : '1-AG'},
  361. {'user' : '1', 'ag' : '1-AG'},
  362. {'user' : '1', 'ag' : '1-AG'},
  363. {'user' : '1', 'ag' : '1-AG'},
  364. {'user' : '1', 'ag' : '1-AG'},
  365. {'user' : '1', 'ag' : '1-AG'},
  366. {'user' : '1', 'ag' : '1-AG'}
  367. ]
  368. while i<10:
  369. username=names.get_last_name()
  370. ag=username+"-AG"
  371. auth=username+":"+password
  372. auth= base64.encodestring(auth)
  373. auth="Basic "+auth
  374. auth=auth.replace('\n', '')
  375. createUser(username)
  376. createCompany(ag, username, auth)
  377. usernames[i]['user']=username
  378. usernames[i]['ag']=ag
  379. i=i+1
  380.  
  381. with open(file, 'w') as f: #writing
  382. json.dump(usernames, f)
  383.  
  384. def loadUsers(file):
  385. data={}
  386. with open(file, 'r') as f:
  387. data = json.load(f)
  388. return data
  389.  
  390. def deleteAllOrders(securityAccount, auth):
  391. url = urlbasis+"api/securityorders/"
  392. querystring = {"owner":securityAccount}
  393. headers = {
  394. 'authorization': auth,
  395. 'x-authorization': "7cd22613-45f3-436a-8f84-fd1f47ed42b8",
  396. 'cache-control': "no-cache",
  397. }
  398. response = requests.request("DELETE", url, headers=headers, params=querystring)
  399.  
  400. def buySecurity(securityIdentifier, securityAccount, count, price, auth):
  401. url = urlbasis+"api/securityorders/"
  402. print(auth)
  403. querystring = {"owner":securityAccount,"securityIdentifier":securityIdentifier,"action":"BUY","type":"LIMIT","price":price,"numberOfShares":count,"counterparty":""}
  404. headers = {
  405. 'authorization': auth,
  406. 'x-authorization': partnerid,
  407. 'cache-control': "no-cache",
  408.  
  409. }
  410. response = requests.request("POST", url, headers=headers, params=querystring)
  411. print(querystring)
  412. print(response.text)
  413.  
  414. def sellSecurity(securityIdentifier, securityAccount, count, price, auth):
  415. url = urlbasis+"api/securityorders/"
  416. querystring = {"owner":securityAccount,"securityIdentifier":securityIdentifier,"action":"SELL","type":"LIMIT","price":price,"numberOfShares":count,"counterparty":""}
  417. headers = {
  418. 'authorization': auth,
  419. 'x-authorization': partnerid,
  420. 'cache-control': "no-cache",
  421.  
  422. }
  423. response = requests.request("POST", url, headers=headers, params=querystring)
  424. print(response.text)
  425.  
  426. def tradeSecurityOtc(securityIdentifier, securityAccount1, securityAccount2, count, price, auth, action):
  427. url = urlbasis+"api/securityorders/"
  428. querystring = {"owner":securityAccount1,"securityIdentifier":securityIdentifier,"action":action,"type":"LIMIT","price":price,"numberOfShares":count,"counterparty":securityAccount2}
  429. headers = {
  430. 'authorization': auth,
  431. 'x-authorization': partnerid,
  432. 'cache-control': "no-cache",
  433.  
  434. }
  435. response = requests.request("POST", url, headers=headers, params=querystring)
  436. print(response.text)
  437.  
  438. def authGen(username, password):
  439. print(username)
  440. print(password)
  441. auth=username+":"+password
  442. auth= base64.encodestring(auth)
  443. auth="Basic "+auth
  444. auth=auth.replace('\n', '')
  445. return auth
  446.  
  447. def getAllListings():
  448. url = urlbasis+"api/listings/"
  449. response = requests.request("GET", url, headers=headeregel)
  450. listings= json.loads(response.text)
  451. return listings
  452.  
  453. def getAllSpreads():
  454. url = urlbasis+"api/pricespreads/"
  455. response = requests.request("GET", url, headers=headeregel)
  456. listings= json.loads(response.text)
  457. return listings
  458.  
  459. def getCommitedCash(companyname):
  460.  
  461. url = urlbasis+"api/portfolios/"+getSecurityAccount(companyname)
  462. response = requests.request("GET", url, headers=headeregel)
  463. account= json.loads(response.text)
  464. cash=account['committedCash']
  465. return int(cash)
  466.  
  467. def getCash(companyname):
  468.  
  469. url = urlbasis+"api/portfolios/"+getSecurityAccount(companyname)
  470. response = requests.request("GET", url, headers=headeregel)
  471. account= json.loads(response.text)
  472. cash=account['cash']
  473. return int(cash)
  474.  
  475.  
  476. def getListingProfile(identifier):
  477. url = urlbasis+"api/listingprofiles/"+identifier
  478. response = requests.request("GET", url, headers=headeregel, timeout=50)
  479. parsed_json = json.loads(response.text)
  480. return parsed_json
  481.  
  482. def getAllBonds():
  483. url = urlbasis+"api/bonds/"
  484. response = requests.request("GET", url, headers=headeregel, timeout=5000)
  485. parsed_json = json.loads(response.text)
  486. return parsed_json
  487.  
  488. def buyBondsWithUsers(file):
  489. users=loadUsers(file)
  490. listings=getAllListings()
  491.  
  492. cash=1
  493. j=0
  494. i=0
  495. while i<10:
  496. #input()
  497. username=users[i]['user']
  498. ag=users[i]['ag']
  499. auth=authGen(username)
  500. print(ag)
  501. securityAccount=getSecurityAccount(ag)
  502. print(getCommitedCash(ag))
  503. #input()
  504. volume=1100
  505. if getCash(ag)-getCommitedCash(ag)>volume :
  506. cash=1
  507. else:
  508. cash=0
  509. while cash==1:
  510. if getCash(ag)-getCommitedCash(ag)>volume :
  511. cash=1
  512. else:
  513. cash=0
  514. if j<len(listings) and listings[j]['type']=="BOND":
  515. print (listings[j]['securityIdentifier'])
  516. print ("found bond")
  517. profile=getListingProfile(listings[j]['securityIdentifier'])
  518. print(profile['systemBond'])
  519. sysbond=0
  520. if profile['systemBond'] is not None:
  521. print ("system Bond")
  522. sysbond=1
  523. if sysbond==0:
  524. print "test1"
  525. try :
  526. print(profile['bond']['faceValue'])
  527. faceValue=profile['bond']['faceValue']
  528. url = urlbasis+"api/securityorders/"
  529. percent=100
  530. countShares=volume/(faceValue*percent)
  531. countShares=int(countShares)
  532.  
  533. #querystring = {"owner":securityAccount,"securityIdentifier":listings[j]['securityIdentifier'],"action":"BUY","type":"LIMIT","price":percent,"numberOfShares":countShares,"counterparty":''}
  534. querystring = {"owner":securityAccount,"securityIdentifier": "BOHDE7C3","action":"BUY","type":"LIMIT","price":110,"numberOfShares":10,"counterparty":''}
  535.  
  536. print (querystring)
  537. headers = {
  538. 'authorization': auth,
  539. 'x-authorization': partnerid,
  540. 'cache-control': "no-cache",
  541. }
  542. response = requests.request("POST", url, headers=headers, params=querystring)
  543. print(response.text)
  544. if response.text is not NONE:
  545. input()
  546. print(securityAccount)
  547. #input()
  548. #input()
  549. except :
  550. pass
  551. if j>len(listings):
  552. i=10
  553. cash=0
  554. print (getCash(ag)-getCommitedCash(ag))
  555. j=j+1
  556. i=i+1
  557.  
  558. def unCommittedCashOfUsers(file):
  559. users=loadUsers(file)
  560. listings=getAllListings()
  561. cash=1
  562. j=0
  563. i=0
  564. while i<10:
  565. username=users[i]['user']
  566. ag=users[i]['ag']
  567. auth=authGen(username)
  568. print(ag)
  569. print(getCash(ag))
  570. print(getCommitedCash(ag))
  571. i=i+1
  572. #print(getComittedCash("Bender"))
  573. def bondtest(file):
  574. file=file+".json"
  575. #createAndSaveUsers(file)
  576. buyBondsWithUsers(file)
  577.  
  578. def issueBond(ag, numberOfBonds, faceValue, interestRate, maturityDate, auth):
  579. url = urlbasis+"api/bonds/"
  580. querystring = {"companyId":getCompanyIDbyName(ag),"numberOfBonds":numberOfBonds,"faceValue":faceValue,"interestRate":interestRate,"maturityDate":maturityDate}
  581.  
  582. headers = {
  583. 'x-authorization': "7cd22613-45f3-436a-8f84-fd1f47ed42b8",
  584. 'authorization': auth,
  585. 'cache-control': "no-cache",
  586. }
  587.  
  588. response = requests.request("POST", url, headers=headers, params=querystring)
  589. print(response.text)
  590.  
  591. def getChats(auth):
  592. url = urlbasis+"api/chats"
  593. headers = {
  594. 'x-authorization': "7cd22613-45f3-436a-8f84-fd1f47ed42b8",
  595. 'authorization': auth,
  596. 'cache-control': "no-cache",
  597. }
  598. response = requests.request("GET", url, headers=headers)
  599. parsed_json = json.loads(response.text)
  600. #print(parsed_json.lentgh())
  601. return parsed_json
  602.  
  603. def quitChat(auth, chatId):
  604. url = urlbasis+"api/chats/quitchat/"+chatId
  605.  
  606. headers = {
  607. 'x-authorization': "7cd22613-45f3-436a-8f84-fd1f47ed42b8",
  608. 'authorization': auth,
  609. 'cache-control': "no-cache",
  610. 'postman-token': "cff2c724-ec2f-1a6e-4cd8-5b0bbbff3083"
  611. }
  612.  
  613. response = requests.request("PUT", url, headers=headers)
  614. print (response)
  615.  
  616. def quitAllChats(auth):
  617. chats=getChats(auth)
  618. l=0
  619. while l<len(chats):
  620. quitChat(auth, chats[i]['id'])
  621. l=l+1
  622.  
  623. #bondtest("johnsbots.json")
  624. #unCommittedCashOfUsers("users2.json")
  625. #foundCompanysAndSell()
  626. #password="plBL7r4aiU"
  627. #getAllListings()
  628. #createAndSaveUsers("users2.json")
  629. #employEgelAsCeo(getcompanyIDbyUser(username))
  630. #auth=egelauth
  631. #comp="Bis%20zum%20naechsten%20reset"
  632. #otcOrderSell(comp, "Bender")
  633. #intitiateLiquidationPoll(comp)
  634. #getlastpoll()
  635. #print(getCompanyIDbyName(comp))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement