Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Sat Aug 13 13:10:37 2016
- @author: Malte
- """
- import requests
- import json
- import base64
- import time
- from datetime import datetime
- urlbasis="http://stable.alpha-trader.com/"
- partnerID="XYZ"
- ############################################
- # helpfunctions #
- ############################################
- def masterUser():
- #create header for the masterUser
- UserName="Malte"
- password="XYZ"
- auth="Basic " + base64.encodestring(UserName+":" + password)
- auth=auth.replace('\n', '')
- header={
- 'authorization': auth,
- 'x-authorization': partnerID,
- 'cache-control': "no-cache",
- }
- return header
- print(header)
- def loginUser(name):
- # login User and create header
- UserName=name
- password="pw!"+name
- auth="Basic " + base64.encodestring(UserName+":" + password)
- auth=auth.replace('\n', '')
- header={
- 'authorization': auth,
- 'x-authorization': partnerID,
- 'cache-control': "no-cache",
- }
- return header
- def companies(user, header=masterUser()):
- # get all Companies of one user
- url= urlbasis+"api/companies/ceo/username/"+user
- response = requests.request("GET", url, headers=header)
- parsed_json = json.loads(response.text)
- numCompanies=len(parsed_json)
- for i in range(0, numCompanies):
- id=parsed_json[i]['id']
- name=parsed_json[i]['name']
- print(id)
- print(name)
- def companyID(name, header=masterUser()):
- # get ID of a company
- url=urlbasis+"api/search/companies/"+name
- response = requests.request("GET", url, headers=header)
- parsed_json=json.loads(response.text)
- id=parsed_json[0]['id']
- return id
- print(id)
- def companyCEO(name, header=masterUser()):
- # get CEO of a company
- url=urlbasis+"api/search/companies/"+name
- response = requests.request("GET", url, headers=header)
- parsed_json=json.loads(response.text)
- ceo=parsed_json[0]['ceo']['username']
- return ceo
- print(ceo)
- def companyAccount(name, header=masterUser()):
- #get the securitiesAccountID of a company
- url=urlbasis+"api/search/companies/"+name
- response=requests.request("GET", url, headers=header)
- parsed_json=json.loads(response.text)
- accountID=parsed_json[0]['securitiesAccountId']
- return accountID
- print(accountID)
- def dateTime(days):
- # datetime helpfunction
- now=datetime.now()
- now=time.mktime(now.timetuple())
- maturityDate=now+days*60*60*24
- maturityDate=int(maturityDate)
- maturityDate=str(maturityDate)+"000"
- return maturityDate
- print(maturityDate)
- def getOrderbook(securityIdentifier, header=masterUser()):
- url=urlbasis+"api/orderbook/"+securityIdentifier
- response=requests.request("GET", url, headers=header)
- parsed_json=json.loads(response.text)
- buyOrder=parsed_json['buyEntries'][0]['priceLimit']
- print(len(parsed_json['sellEntries']))
- def getPrice(securityIdentifier, header=masterUser()):
- url=urlbasis+"api/securityPrices/?securityIdentifier="+securityIdentifier
- response=requests.request("GET", url, headers=header)
- parsed_json=json.loads(response.text)
- numValues=len(parsed_json)-1
- price=parsed_json[numValues]['value']
- return price
- ##############################################
- # functions #
- ##############################################
- def registerUser(name):
- # register new user
- url=urlbasis+"user/register"
- username=name
- mail=name+"qwertzuiolfh@gmx.de"
- password="pw!"+name
- querystring={"username": username, "emailAddress": mail,"password": password}
- header={
- 'X-authorization': partnerID,
- 'cache-control': "no-cache",
- }
- response=requests.request("POST", url, headers=header, params=querystring)
- print(response.text)
- def newCompany(CompName, UserName):
- # found new company
- url=urlbasis+"api/companies/"
- querystring={"name": CompName, "cashDeposit": "50000"}
- auth="Basic " + base64.encodestring(UserName+":"+"pw!"+UserName)
- auth=auth.replace('\n', '')
- headers = {
- 'authorization': auth,
- 'x-authorization': partnerID,
- 'cache-control': "no-cache",
- }
- response = requests.request("POST", url, headers=headers, params=querystring)
- print(response.text)
- def getStats(header=masterUser):
- #get stats
- url=urlbasis+"api/marketstatistics/"
- response=requests.request("GET", url, headers=header)
- print(response.text)
- def newBond(compName, numberOfBonds, faceValue, interestRate, maturityDate):
- #issue new bond
- maturityDate=dateTime(maturityDate)
- compID=companyID(compName)
- compCEO=companyCEO(compName)
- header=loginUser(compCEO)
- querystring={"companyId": compID, "numberOfBonds": numberOfBonds, "faceValue": faceValue, "interestRate": interestRate, "maturityDate": maturityDate}
- url=urlbasis+"api/bonds/"
- print(querystring)
- response=requests.request("POST", url, headers=header, params=querystring)
- if str(response.status_code) == "201":
- print("Bond erstellt")
- else:
- print("Bond nicht erstellt")
- def newOrder(compName, securityIdentifier, action, type2, price, numberOfShares):
- compID=companyAccount(compName)
- user=companyCEO(compName)
- header=loginUser(user)
- querystring={"owner": compID, "securityIdentifier": securityIdentifier, "action": action, "type": type2, "price": price, "numberOfShares":numberOfShares}
- url=urlbasis+"api/securityorders/?"
- print(querystring)
- response=requests.request("POST", url, headers=header, params=querystring)
- print(response)
- def pushSecurity2(pusher1, pusher2, securityIdentifier, minimum, maximum):
- compID1=companyAccount(pusher1)
- compID2=companyAccount(pusher2)
- user1=companyCEO(pusher1)
- user2=companyCEO(pusher2)
- header1=loginUser(user1)
- header2=loginUser(user2)
- url=urlbasis+"api/securityorders/?"
- i=0
- price=round(getPrice(securityIdentifier)*1.2-0.01,2)
- for i in range(1,10):
- while price <= maximum :
- if i % 2 ==0 :
- action="SELL"
- else:
- action="BUY"
- price2=str(price)
- newOrder(pusher1, securityIdentifier, action, "LIMIT", price2, "1")
- if i % 2 ==1 :
- action="SELL"
- else:
- action="BUY"
- newOrder(pusher2, securityIdentifier, action, "LIMIT", price2, "1")
- i=i+1
- price=round(getPrice(securityIdentifier)*1.2-0.01,2)
- time.sleep(2)
- while price >= minimum:
- if i % 2 ==0 :
- action="SELL"
- else:
- action="BUY"
- price2=str(price)
- newOrder(pusher1, securityIdentifier, action, "LIMIT", price2, "1")
- if i % 2 ==1 :
- action="SELL"
- else:
- action="BUY"
- newOrder(pusher2, securityIdentifier, action, "LIMIT", price2, "1")
- i=i+1
- price=round(getPrice(securityIdentifier)*0.8+0.01,2)
- time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement