Wabash Simple Order Question

ptunneyptunney Posts: 246
edited October 2018 in Quick Question

A simple script to start off our Wabash students...

from cloudquant.interfaces import Strategy

class testwabash(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return md.stat.prev_close>10 and md.stat.prev_close<11

    def on_start(self, md, order, service, account): 
        self.position = 0

    def on_trade(self, event, md, order, service, account):
        if md.L1.last>10.90 and event.timestamp<service.time(9, 32, 0, 0) and self.position==0:
            order.send(self.symbol, 'buy', 10, type='MKT') 
            self.position = 1
            print self.symbol,md.L1.last,md.L1.last_size, service.time_to_string(event.timestamp,format="%Y-%m-%d %H:%M:%S.%f") 
        if event.timestamp>service.time(9, 33, 0, 0) and self.position == 1:
            myshares=account[self.symbol].position.shares
            order.send(self.symbol, 'sell', myshares, type='MKT') 
            self.position = 2


Comments

  • ptunneyptunney Posts: 246
    edited October 2018

    The students also expressed an interest in VANGUARD ETFs and a little confustion surrounding how "tabbing in" works in Python.

    So I modified the code as follows.
    I also added a lot of comments and explanation of how commenting and tabbing works...

    from cloudquant.interfaces import Strategy
    
    class testwabash2(Strategy):
    
        # Putting a # in your code means, "anything after this is just a comment.. do not run!"
        # note how each of the callbacks are tabbed in 1 tab (or 4 spaces) under our class.
        # all our code will be tabbed in like this
    # comments do not have to be tabbed in...
    
        @classmethod
        def is_symbol_qualified(cls, symbol, md, service, account):
    # you can also use # to "comment out" code that you do not want to run but may wish to add back in at a later time.
    # putting a hash at the start of a line of code still means "this is a comment, do not run", as we have with the line below from our original script.
    #        return md.stat.prev_close>10 and md.stat.prev_close<11 
    # you can also place a # at the end of a line of code to place comments there as below. In this case the code will run, the comment will not...
            etf_list_guid = "82e8721e-d9fe-4ac4-b597-5197a4309a60" # search under documentation main page for ETF and you will see this. It is the list of ETF symbols on each backtest date.
            etf = service.symbol_list.in_list(service.symbol_list.get_handle(etf_list_guid),symbol) # this takes that list reference (GUID) and the symbol and returns a True of False if the symbol is or is not in the list.
            if etf and symbol.startswith('VT'): # We want ETFs that start with VT for Vanguard ETFs. A check of the internet revealed a python command "startswith". A nice easy solution.
                print symbol,etf # this prints the name of everything that qualified for the last if statement.. so ETFs starting with VT
                return True # This returns a True for all that meet the previous if statement.
            else:  
                return False # If it is not an etf or it is an ETF that does not start with VT then return false, do not run that symbol.
    
        def on_start(self, md, order, service, account): 
            # anything tabbed in (4 spaces in python) like this code below, will only run when the function above it is called
            self.position = 0 # first time through we set this status flag for each symbol to zero
    
        def on_trade(self, event, md, order, service, account):
            # So this tabbed in code will only run when a qualified symbol (ETF starting VT) receives a trade.
            mytime = service.time_to_string(event.timestamp,format="%Y-%m-%d %H:%M:%S.%f") # If you may use a value a lot, just capture it into a variable, makes code easier to read as well 
    #        print mytime,self.symbol,md.L1.last,md.stat.prev_close*0.995,md.L1.last>md.stat.prev_close*0.995, event.timestamp<service.time(9, 35, 0, 0), self.position==0
    
            # An if statement MUST end with a colon. Anything that is True will run the code that is tabbed in underneath it. False it skips to the next code that is not tabbed in.
            # In this case we are just putting in a simple qualfier just to play with the system, most recent trade must above 95% of the previous close price.
            # and it must be before 9:35am and we must not be in a postion. Note how when we set a value for a variable we use one = sign but when we do a comparison, as below, we MUST use two == signs.
            if md.L1.last>md.stat.prev_close*0.995 and event.timestamp<service.time(9, 35, 0, 0) and self.position==0: 
                # if the if statement above was true then we will do this code, place an order, set our position flag to 1 and print some info to the console.
                order.send(self.symbol, 'sell', 100, type='MKT') 
                self.position = 1
                print mytime,"entering",self.symbol,md.L1.last,md.L1.last_size
    
            # Because of the time check, this if and the previous if cannot both be true. If it is after 15:35 and we are in a position then exit.
            if event.timestamp>service.time(15, 35, 0, 0) and self.position == 1:
    #            myshares=account[self.symbol].position.shares #If I was using thin symbols I may not get complete fills so I would want to exit however many I was holding. But Vanguard ETFs should fill 100 with no problems.
                order.send(self.symbol, 'sell', 100, type='MKT') 
                self.position = 2
                print mytime,"exiting ",self.symbol,md.L1.last,md.L1.last_size
    
Sign In or Register to comment.