Advertisement
Guest User

Malte

a guest
Aug 15th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.20 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sat Aug 13 13:10:37 2016
  4.  
  5. @author: Malte
  6. """
  7.  
  8. import requests
  9. import json
  10. import base64
  11. import time
  12. from datetime import datetime
  13.  
  14. urlbasis="http://stable.alpha-trader.com/"
  15. partnerID="XYZ"
  16. ############################################
  17. # helpfunctions #
  18. ############################################
  19. def masterUser():
  20.     #create header for the masterUser
  21.     UserName="Malte"
  22.     password="XYZ"
  23.     auth="Basic " + base64.encodestring(UserName+":" + password)
  24.     auth=auth.replace('\n', '')
  25.     header={
  26.        'authorization': auth,
  27.      'x-authorization': partnerID,
  28.      'cache-control': "no-cache",
  29.          }
  30.     return header
  31.     print(header)
  32.    
  33. def loginUser(name):
  34.     # login User and create header
  35.     UserName=name
  36.     password="pw!"+name
  37.     auth="Basic " + base64.encodestring(UserName+":" + password)
  38.     auth=auth.replace('\n', '')
  39.     header={
  40.        'authorization': auth,
  41.      'x-authorization': partnerID,
  42.      'cache-control': "no-cache",
  43.          }
  44.     return header
  45.    
  46. def companies(user, header=masterUser()):
  47.     # get all Companies of one user
  48.     url= urlbasis+"api/companies/ceo/username/"+user
  49.     response = requests.request("GET", url, headers=header)
  50.     parsed_json = json.loads(response.text)
  51.     numCompanies=len(parsed_json)
  52.     for i in range(0, numCompanies):
  53.         id=parsed_json[i]['id']
  54.         name=parsed_json[i]['name']
  55.         print(id)
  56.         print(name)
  57.  
  58. def companyID(name, header=masterUser()):
  59.     # get ID of a company
  60.     url=urlbasis+"api/search/companies/"+name
  61.     response = requests.request("GET", url, headers=header)
  62.     parsed_json=json.loads(response.text)
  63.     id=parsed_json[0]['id']
  64.     return id
  65.     print(id)
  66.    
  67. def companyCEO(name, header=masterUser()):
  68.     # get CEO of a company
  69.     url=urlbasis+"api/search/companies/"+name
  70.     response = requests.request("GET", url, headers=header)
  71.     parsed_json=json.loads(response.text)
  72.     ceo=parsed_json[0]['ceo']['username']
  73.     return ceo
  74.     print(ceo)
  75.    
  76. def companyAccount(name, header=masterUser()):
  77.     #get the securitiesAccountID of a company
  78.     url=urlbasis+"api/search/companies/"+name
  79.     response=requests.request("GET", url, headers=header)
  80.     parsed_json=json.loads(response.text)
  81.     accountID=parsed_json[0]['securitiesAccountId']
  82.     return accountID
  83.     print(accountID)
  84.    
  85. def dateTime(days):
  86.     # datetime helpfunction
  87.     now=datetime.now()
  88.     now=time.mktime(now.timetuple())
  89.     maturityDate=now+days*60*60*24
  90.     maturityDate=int(maturityDate)
  91.     maturityDate=str(maturityDate)+"000"
  92.     return maturityDate
  93.     print(maturityDate)    
  94.  
  95. def getOrderbook(securityIdentifier, header=masterUser()):
  96.     url=urlbasis+"api/orderbook/"+securityIdentifier
  97.     response=requests.request("GET", url, headers=header)
  98.     parsed_json=json.loads(response.text)
  99.     buyOrder=parsed_json['buyEntries'][0]['priceLimit']    
  100.     print(len(parsed_json['sellEntries']))
  101.    
  102. def getPrice(securityIdentifier, header=masterUser()):
  103.     url=urlbasis+"api/securityPrices/?securityIdentifier="+securityIdentifier
  104.     response=requests.request("GET", url, headers=header)
  105.     parsed_json=json.loads(response.text)
  106.     numValues=len(parsed_json)-1
  107.     price=parsed_json[numValues]['value']
  108.     return price    
  109.    
  110. ##############################################
  111. # functions #
  112. ##############################################
  113.    
  114. def registerUser(name):
  115.     # register new user
  116.     url=urlbasis+"user/register"
  117.     username=name
  118.     mail=name+"qwertzuiolfh@gmx.de"
  119.     password="pw!"+name
  120.     querystring={"username": username, "emailAddress": mail,"password": password}
  121.     header={
  122.             'X-authorization': partnerID,
  123.             'cache-control': "no-cache",
  124.             }
  125.     response=requests.request("POST", url, headers=header, params=querystring)
  126.     print(response.text)
  127.    
  128. def newCompany(CompName, UserName):
  129.     # found new company
  130.     url=urlbasis+"api/companies/"
  131.     querystring={"name": CompName, "cashDeposit": "50000"}
  132.     auth="Basic " + base64.encodestring(UserName+":"+"pw!"+UserName)
  133.     auth=auth.replace('\n', '')
  134.     headers = {
  135.      'authorization': auth,
  136.      'x-authorization': partnerID,
  137.      'cache-control': "no-cache",
  138.          }
  139.     response = requests.request("POST", url, headers=headers, params=querystring)
  140.     print(response.text)
  141.    
  142. def getStats(header=masterUser):
  143.     #get stats
  144.     url=urlbasis+"api/marketstatistics/"
  145.     response=requests.request("GET", url, headers=header)
  146.     print(response.text)    
  147.    
  148. def newBond(compName, numberOfBonds, faceValue, interestRate, maturityDate):
  149.     #issue new bond
  150.     maturityDate=dateTime(maturityDate)
  151.     compID=companyID(compName)
  152.     compCEO=companyCEO(compName)
  153.     header=loginUser(compCEO)
  154.     querystring={"companyId": compID, "numberOfBonds": numberOfBonds, "faceValue": faceValue, "interestRate": interestRate, "maturityDate": maturityDate}
  155.     url=urlbasis+"api/bonds/"
  156.     print(querystring)
  157.     response=requests.request("POST", url, headers=header, params=querystring)  
  158.     if str(response.status_code) == "201":
  159.         print("Bond erstellt")
  160.     else:
  161.         print("Bond nicht erstellt")
  162.        
  163. def newOrder(compName, securityIdentifier, action, type2, price, numberOfShares):
  164.     compID=companyAccount(compName)
  165.     user=companyCEO(compName)
  166.     header=loginUser(user)
  167.     querystring={"owner": compID, "securityIdentifier": securityIdentifier, "action": action, "type": type2, "price": price, "numberOfShares":numberOfShares}
  168.     url=urlbasis+"api/securityorders/?"
  169.     print(querystring)
  170.     response=requests.request("POST", url, headers=header, params=querystring)
  171.     print(response)
  172.        
  173. def pushSecurity2(pusher1, pusher2, securityIdentifier, minimum, maximum):
  174.     compID1=companyAccount(pusher1)
  175.     compID2=companyAccount(pusher2)
  176.     user1=companyCEO(pusher1)
  177.     user2=companyCEO(pusher2)
  178.     header1=loginUser(user1)
  179.     header2=loginUser(user2)
  180.     url=urlbasis+"api/securityorders/?"
  181.     i=0
  182.     price=round(getPrice(securityIdentifier)*1.2-0.01,2)
  183.     for i in range(1,10):
  184.         while price <= maximum :
  185.             if i % 2 ==0 :
  186.                 action="SELL"
  187.             else:
  188.                 action="BUY"
  189.             price2=str(price)    
  190.             newOrder(pusher1, securityIdentifier, action, "LIMIT", price2, "1")
  191.             if i % 2 ==1 :
  192.                 action="SELL"
  193.             else:
  194.                 action="BUY"
  195.             newOrder(pusher2, securityIdentifier, action, "LIMIT", price2, "1")
  196.             i=i+1
  197.             price=round(getPrice(securityIdentifier)*1.2-0.01,2)
  198.             time.sleep(2)
  199.         while price >= minimum:
  200.             if i % 2 ==0 :
  201.                 action="SELL"
  202.             else:
  203.                 action="BUY"
  204.             price2=str(price)    
  205.             newOrder(pusher1, securityIdentifier, action, "LIMIT", price2, "1")
  206.             if i % 2 ==1 :
  207.                 action="SELL"
  208.             else:
  209.                 action="BUY"
  210.             newOrder(pusher2, securityIdentifier, action, "LIMIT", price2, "1")
  211.             i=i+1
  212.             price=round(getPrice(securityIdentifier)*0.8+0.01,2)
  213.             time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement