In [2]:
from api import*
myConfig = Config()
myConfig.view()
In [4]:
q1 = EventQueue()
q2 = EventQueue()
q = {'mkt': q1, 'bar': q2}
In [5]:
myapi = PyApi(Config(), q)
In [27]:
instrumentList = myapi.get_instruments()
print instrumentList['instruments'][0:3]
In [28]:
import json
print json.dumps(instrumentList['instruments'][0:3], sort_keys=True, indent=4)
In [8]:
print json.dumps(myapi.get_prices("EUR_USD"), sort_keys=True, indent=4)
In [10]:
print json.dumps(myapi.get_account_info(), sort_keys=True, indent=4)
In [11]:
print json.dumps(myapi.get_positions(), sort_keys=True, indent=4)
In [13]:
print json.dumps(myapi.get_orders(), sort_keys=True, indent=4)
In [15]:
print json.dumps(myapi.get_trades(), sort_keys=True, indent=4)
In [19]:
print json.dumps(myapi.get_positions(), sort_keys=True, indent=4)
myapi.place_order('USD_CAD', 'sell', 150, None, 'market') #fill USD_CAD long position
print '\n--------------AFTER--------------\n'
print json.dumps(myapi.get_positions(), sort_keys=True, indent=4)
In [22]:
from strat import *
class BuyAndHold(BaseStrategy):
"""
some parameters here.
"""
instrument = 'USD_CAD'
BHFlag = True
def __init__(self, api):
"""
override constructor.
"""
self.api = api
def on_bar(self, event):
"""
Strategy logics.
"""
if self.BHFlag: # If BHFlag == True, buy 100 CA dollar
self.market_buy(100)
self.BHFlag = 0 # then hold.
# Print account summary and bar data.
print '---------------------'
print json.dumps(self.api.get_account_info(), sort_keys=True, indent=4)
print '\n---------------------\n'
event.body.view()
print '---------------------'
In [ ]:
q1 = EventQueue()
q2 = EventQueue()
q = {'mkt': q1, 'bar': q2} # construct event queues
api = PyApi(Config(), q) # initialize api
mystrat = BuyAndHold(api) # initialize strategy
q1.bind('ETYPE_MKT', api.on_market_impulse) # let queues listen to some events
q2.bind('ETYPE_BAR', mystrat.on_bar)
q1.open() # start pushing event to strategies and other functions
q2.open()
api.make_stream('USD_CAD') # make connection to server.