init() (system method) Overview:
is used to pass external parameters into the system. on_start() should be used for pre-running activity. Notice account, market, order, and service are not available in __init__()
Interface: def __init__(self,
params):
pass
Name | Type | Default | Information |
---|---|---|---|
self | object | required | The instance of Strategy (used by the system). |
params | kwargs | required | def __init__(self, \*\*params): if params is not None: for key, value in params.iteritems(): print '%s == %s' % (key, value)So can you see how we handled a keyworded argument list in our function. This is just the basics of \*\*kwargs and you can see how useful it is. Now lets talk about how you can use \*args and \*\*kwargs to call a function with a list or dictionary of arguments. |
Parameters with two leading asterisks like \*\*params, allow you to pass as many named keyword arguments to a function as you like. Such parameters MUST be last in the function definition, and invocation, and are customarily named \*\*kwargs (or \*\*kw), but the name can be whatever is desired. Here is an example to get you going with it:
Check params for a specific key and assign it to variable in self (so it can be used another method of the script). Notice the parameter is saved in a variable.
from cloudquant.interfaces import Strategy
class initExample(Strategy):
def __init__(self, **params):
self.your_dictionary_key = None
if 'your_dictionary_key' in params:
self.your_dictionary_key = params['your_dictionary_key']
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
# limit symbol universe to IBM AAPL
return symbol =='AAL'
def on_trade(self, event, md, order, service, account):
if self.your_dictionary_key != None :
print 'got parameter passed in - %s' % (self.your_dictionary_key)
service.terminate()
Console
got parameter passed in - your_key