Guest User

Untitled

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