New to Coding - Backtest Fails - Looking for advice

dc367719dc367719 Posts: 2
edited May 2018 in Support

Hello, I am really new to coding. I started off follow the Intermediate 1:entering and exiting positions video that Cloudquant has on YouTube. I made some small changes but nothing serious, just messing around trying to get a hang off it. The problem is that every time I try backtesting it fails. I was wondering if anyone had any advice? thank You

``from cloudquant.interfaces import Strategy

class CQ28dd599bc5be4438b5554bdb5401cb3d(Strategy):

@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
    etf=service.symbol_list.in_list(service.symbol_list.get_handle("82e8721e-d9fe-4ac4-b597-5197a4309a60"),symbol)
    etn=service.symbol_list.in_list(service.symbol_list.get_handle("9389dd95-ca7e-4fe2-b2a6-329ce7de4665"),symbol)
    return md.stat.prev_close>50.00 and md.stat.prev_close<100.00 and md.stat.avol>1000000 and md.stat.beta>1.50 and md.stat.dividend_percent<1.50

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

def on_minute_bar(self, event, md, order, service, account, bar):
    bardata=md.bar.minute(-1,include_extended=True).close
    if len(bardata)>0:
            if (1
            and event.timestamp>service.time(hour=9,minute=32,second=0,millisecond=0)
            and ent.timestamp<service.time(hour=13,minute=0,second=0,millisecond=0)
            and self.inPosition==0
            and bardat<md.stat.prev_close*0.99):

            order.algo_buy(self.symbol, algorithm="market", intent="init",order_quantity=100)
            self.inPosition=1
            print "Entering ",self.symbol,service.time_to_string(event.timestamp),round(bardata,2),round(md.stat.prev_close,2)
        if event.timestamp>service.time(hour=15,minute=40,second=0,millisecond=0) and self.inPosition==1:
            order.algo_sell(self.symbol, algorithm="market", intent="exit")
            self.inPosition=2
            print "Exiting ",self.symbol,service.time_to_string(event.timestamp),round(bardata,2),round(md.stat.prev_close,2)

``

Comments

  • ptunneyptunney Posts: 246

    Make sure you just run for 1 day and check the console as you are developing the script.

    The errors will appear there.

    The only errors I saw were
    1) indentation error 1.. the whole script should be indented after the class CQ28.... line
    2) indentation error 2 .. the if statement after checking the length of the bardata should be back by 1 tab (4 chars).. the rest of the lines after it are indented.. its a little odd to get used to..
    3) and ent.timestamp should be and event.timestamp
    4) and bardat<md should be and bardata<md

    It took me 4 separate submissions and 4 separate errors listed in the console to work each of these out but it only took 2 mins.
    So it is easy once you get used to the.. rinse.. repeat process.
    In time you will build your own library of routines and clone working routines from other scripts you like.
    It will get easier, I promise!

    And you are developing a skill that will be useful in other aspects of your working life!

    from cloudquant.interfaces import Strategy
    
    class test(Strategy):
    
        @classmethod
        def is_symbol_qualified(cls, symbol, md, service, account):
            etf=service.symbol_list.in_list(service.symbol_list.get_handle("82e8721e-d9fe-4ac4-b597-5197a4309a60"),symbol)
            etn=service.symbol_list.in_list(service.symbol_list.get_handle("9389dd95-ca7e-4fe2-b2a6-329ce7de4665"),symbol)
            return md.stat.prev_close>50.00 and md.stat.prev_close<100.00 and md.stat.avol>1000000 and md.stat.beta>1.50 and md.stat.dividend_percent<1.50
        def on_start(self,md,order,service,account):
            self.inPosition=0
        def on_minute_bar(self, event, md, order, service, account, bar):
            bardata=md.bar.minute(-1,include_extended=True).close
            if len(bardata)>0:
                if (1
                    and event.timestamp>service.time(hour=9,minute=32,second=0,millisecond=0)
                    and event.timestamp<service.time(hour=13,minute=0,second=0,millisecond=0)
                    and self.inPosition==0
                    and bardata<md.stat.prev_close*0.99):
                    order.algo_buy(self.symbol, algorithm="market", intent="init",order_quantity=100)
                    self.inPosition=1
                    print "Entering ",self.symbol,service.time_to_string(event.timestamp),round(bardata,2),round(md.stat.prev_close,2)
                if event.timestamp>service.time(hour=15,minute=40,second=0,millisecond=0) and self.inPosition==1:
                    order.algo_sell(self.symbol, algorithm="market", intent="exit")
                    self.inPosition=2
                    print "Exiting ",self.symbol,service.time_to_string(event.timestamp),round(bardata,2),round(md.stat.prev_close,2)
    
  • ptunneyptunney Posts: 246

    When reading an error in the console, you usually only really need to look at the last few lines to see where the error occurred.. see below..

Sign In or Register to comment.