SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Fri Sep 25, 2020 6:39 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Problem with backtesting
PostPosted: Tue Feb 01, 2011 1:52 pm 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
Hello,

I have a very simple strategy which buy every other bar and sell every other bar. I have 1 min data for AAPL from January 18 till now. AAPL is the only stock in the strategy.
I specified startDate 1/19/2011 and endDate 2/1/2011. I should have the first buy order at 1/19/2011 at 9.31
For some reason I have first buy 1/25/2011 2:09PM . After 1/25/2011 3:22 no orders until first sell order 1/27/2011 9:35. Then everything OK until 1/25/2011 12:02PM
What is wrong?

There is the code


Code:
   private bool    isTimeGood()
   {
      bool returnValue = false;
      if (Bar.BeginTime.Hour >= 9 && Bar.BeginTime.Hour < 16)
      {
         if (Bar.BeginTime.Hour == 9)
         {
            if (Bar.BeginTime.Minute > 32)
            {
               returnValue = true;
            }
         }
         else
         {
            returnValue = true;
         }
      }
      if(DateTime.Now.DayOfWeek == DayOfWeek.Saturday ||DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
         returnValue= false;
      return returnValue;
   }

   public override void OnBar(Bar bar)
   {
      if(isTimeGood())
      {   
         if(!this.HasPosition )
         {
            Buy(100);
         }
         else
         {
            Sell(100);
         }
      }
   }


Top
 Profile  
 
PostPosted: Wed Feb 02, 2011 4:32 am 
Offline

Joined: Mon Sep 08, 2008 2:20 am
Posts: 40
Location: Toronto, Ontario, Canada
Try this...
(Be sure to disable this when running live if you're not EST)

Code:

   public override void OnBar (Bar bar)
   {
      
      if(DurationWithinWorkingHours(bar.DateTime))
      {
         //do your magic   
      }
      else
      {
         //Cancel any open order (not filled)
         foreach (Order order in Orders)
            if (!order.IsDone)
               order.Cancel();   
         //Now close any open position
         //Hint: Just check if your buy order and sell order != null  (or you can check the IsFilled and IsDone properties)
         
            
      }
   }

   public bool DurationWithinWorkingHours(DateTime currentTime)
   {
      DateTime start = new DateTime(1999,1,1,09,35,00);     //Allow entry at 935
      DateTime end = new DateTime(1999,1,1,16,00,00);        //Exit everything at 400
      DateTime current = new DateTime(1999,1,1,currentTime.Hour,currentTime.Minute,currentTime.Second);
       
      if (current >= start && current <= end)
         return true;
      return false;
       
   }




Top
 Profile  
 
PostPosted: Wed Feb 02, 2011 5:16 am 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
I tried your idea and I have exactly the same results except now I have 1 more Buy 1/27/2011 at 4.01PM which is after market.
There us a code

Code:
Code:
public override void OnBar(Bar bar)
   {
      if(DurationWithinWorkingHours(bar.DateTime))
      {
         if(!this.HasPosition )
         {
            Buy(100);
         }
         else
         {
            Sell(100);
         }
      }
      else
      {
         //Cancel any open order (not filled)
         foreach (Order order in Orders)
            if (!order.IsDone)
               order.Cancel();   
         //Now close any open position
         //Hint: Just check if your buy order and sell order != null  (or you can check the IsFilled and IsDone properties)
         
           
      }

   }
   public bool DurationWithinWorkingHours(DateTime currentTime)
   {
      DateTime start = new DateTime(1999,1,1,09,35,00);     //Allow entry at 935
      DateTime end = new DateTime(1999,1,1,16,00,00);        //Exit everything at 400
      DateTime current = new DateTime(1999,1,1,currentTime.Hour,currentTime.Minute,currentTime.Second);
       
      if (current >= start && current <= end)
         return true;
      return false;
       
   }



Top
 Profile  
 
PostPosted: Wed Feb 02, 2011 6:02 am 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
Looks like this is a problem with the data. On the chart I can see gap in data after January 25. There is no data for January 26here is no data before January 25.

Looks like a very serious problem with the data integrity. Is there a way how to check data integrity automatically without charting? For me that kind of problems like a stop sign.

Paul.


Top
 Profile  
 
PostPosted: Thu Feb 03, 2011 12:04 am 
Offline

Joined: Mon Sep 08, 2008 2:20 am
Posts: 40
Location: Toronto, Ontario, Canada
Quote:
Looks like this is a problem with the data. On the chart I can see gap in data after January 25. There is no data for January 26here is no data before January 25.


Where are you getting the data from? Yahoo? Google? IQFeed? Interactive Brokers?
Have you tried re-loading the data?

Quote:
Looks like a very serious problem with the data integrity. Is there a way how to check data integrity automatically without charting? For me that kind of problems like a stop sign.


To verify you can just dump it to excel and count rows... You know the tick size and the trading hours so just multiply it out and make sure the number of bars is correct. (For example 30 second bars for 1 day of e-mini is 800 ticks)
(Of course I'm assuming you're only having problems with a single instrument...)

I download data directly from Interactive Brokers and haven't had any issues.
Best bet is to re-get the data and try it again.

(If you PM me the duration, timeframe and instrument I can send u a csv dataset to try out)

CS


Top
 Profile  
 
PostPosted: Thu Feb 03, 2011 3:04 am 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
I used IQFeed and I tried to reload.

Paul


Top
 Profile  
 
PostPosted: Thu Feb 03, 2011 6:59 am 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
I have to use IQFeed. It is not my choice.

Paul.


Top
 Profile  
 
PostPosted: Thu Feb 03, 2011 6:08 pm 
Offline

Joined: Mon Sep 08, 2008 2:20 am
Posts: 40
Location: Toronto, Ontario, Canada
How do you download the tick data?

I'm using QCollector to get all my historical data.
http://www.mechtrading.com/index.htm

QCollector will dump all the data to a directory (csv txt file), then I load the directory in to OpenQuant.
(I use it this way so I can read the text files in to other applications (Matlab))

Are you running something similar?
(I'm guessing you use IQFeed for historical as well as live feeds)

There is a plugin for IQFeed for openquant but I haven't tried it out yet.
(I just use IB for live data... I don't really run more than 5 strategies at a time)

Here's the link for the install directions:
http://www.smartquant.com/forums/viewtopic.php?f=41&t=8506


Top
 Profile  
 
PostPosted: Thu Feb 03, 2011 10:02 pm 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
I have to use IQFeed plugin. I've written this simple strategy to check the data integrity to make sure I have all the data without gaps.
In real time trading I will use IQFeed plugin and if I will have a problem with it then I am in trouble.

Paul.


Top
 Profile  
 
PostPosted: Mon Feb 07, 2011 7:21 pm 
Offline

Joined: Mon Sep 08, 2008 2:20 am
Posts: 40
Location: Toronto, Ontario, Canada
If you woudn't mind posting the script that you're using I can take a look at it for you.

=D


Top
 Profile  
 
PostPosted: Mon Feb 07, 2011 8:54 pm 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
There is my code

Code:
  private bool    isTimeGood()
   {
      bool returnValue = false;
      if (Bar.BeginTime.Hour >= 9 && Bar.BeginTime.Hour < 16)
      {
         if (Bar.BeginTime.Hour == 9)
         {
            if (Bar.BeginTime.Minute > 32)
            {
               returnValue = true;
            }
         }
         else
         {
            returnValue = true;
         }
      }
      if(DateTime.Now.DayOfWeek == DayOfWeek.Saturday ||DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
         returnValue= false;
      return returnValue;
   }

   public override void OnBar(Bar bar)
   {
      if(isTimeGood())
      {   
         if(!this.HasPosition )
         {
            Buy(100);
         }
         else
         {
            Sell(100);
         }
      }
   }


Top
 Profile  
 
PostPosted: Mon Feb 07, 2011 9:15 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
Perhaps it's a good idea to pass bar to IsTimeGood as argument ...

Regards,
Anton


Top
 Profile  
 
PostPosted: Mon Feb 07, 2011 10:36 pm 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
Anton,

Thank you for reply. But even without function isTimeGood there will be gaps.

Try this code:


Code:
public override void OnBar(Bar bar)
   {
         if(!this.HasPosition )
         {
            Buy(100);
         }
         else
         {
            Sell(100);
         }
   }



There is no place for a bug in the code. Just data problems.

Regards,
Paul.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group