Custom Classes and Inputs Functionality

Brandon_JohnsonBrandon_Johnson Posts: 8
edited March 2017 in Under The Hood

Writing classes that define an init() method which require a parameter input causes an input query to be displayed when the strategy is selected for backtesting. Here, I've defined a parameter called length that is intended to control the length of a list being used by my strategy.

Attempting to run the backtest with the default parameter in place throws the following exception.

Was this functionality intended? What type of object is expected to be provided in the backtest's input line?

Is there a preferred way to pass a user value to a variable to be used in a strategy?

Here is the sample code showing how the error was generated and how the parameter might be used to control the length of a metaclass inheriting from a list object.

`from cloudquant.interfaces import Strategy

user defined classes: This is where my 'length' parameter default was defined and picked up by the backtest module.

class smartList(list):

#initialize method with default variable:
def __init__(self, length = 30):        
    self.length = int(length)

def append(self, item):        
    list.append(self, item)        
    if len(self) > self.length: self[:len(self)-self.length]=[]  

class computeObj(object):

#initialize method:
def __init__(self):        
    self.obj = smartList(30)                

strategy definition: Where/How is the class name used to avoid clashing with cloned scripts using the same class name?

class This_is_my_strategy(Strategy):

#initialization method:
def __init__(self): 

    #initialize user defined class to member variable
    self.computer = computeObj()  

#static method: 
@staticmethod #decorator
def in_symbol_universe(symbol, md, service): 
    #single symbol example:
    if symbol == 'AAPL':
        return True
    else:
        return False 

#run once per day    
def on_start(self, md, order, service, account):        
    pass

#run on market trade event
def on_trade(self, event, md, order, service, account):
    pass   

#run on order fill
def on_fill(self, event, md, order, service, account):     
    pass  

#run when service.terminate() called
def on_finish(self, md, order, service, account):
    pass`

Comments

Sign In or Register to comment.