SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Mon Sep 24, 2018 3:20 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Four Down Days and Long
PostPosted: Wed Mar 21, 2007 2:10 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6808
The concept of this strategy is to open a long position after a major market index has had 4 down days in a row. The theory is that 4 days of market momentum in a major market index is hard to maintain, and that an up day is soon to follow. You might have to wait a day or two for it to show up, but it will show up eventually. This implementation buys at the market open on day 5 after 4 down days, and doesn’t wait around for multiple days for an up day. It just automatically issues a market sell order at the end of day 6 (when day 6 bar is received). The sell order will actually be executed by the exchange when the market opens on day 7. Notice how the timing of strategy decisions and actions is affected by when the bars arrive, and when the orders are executed. With daily bars, the strategy receives the daily bar at the end of each trading day, in the OnBar event handler. The OnBar event handler is executed on the trailing edge of each bar, at time that corresponds to the end of the trading day for daily bars. If you want to do something on the leading edge of a bar—meaning at the market open before the daily bar is constructed—then you should put your code in an OnBarOpen event handler.

Code:
using OpenQuant.API;

public class MyStrategy : Strategy
{
   [Parameter("Order quantity (number of contracts to trade)")]
   double Qty = 100;

   [Parameter("Number of consecutive down closes")]
   int ConsClosesCount = 4;

   int count;
   double prevClose;

   public override void OnStrategyStart()
   {
      prevClose = -1;
      count = 0;
   }

   public override void OnBar(Bar bar)
   {
      // we need at least one bar to go by before we do anything
      if (prevClose != -1)
      {
         // if we don't have a position open, increment the count of down days (or reset it to zero)
         if (!HasPosition)
         {
            if (prevClose > bar.Close)
               count++;
            else
               count = 0;

            // if this is the fourth (consClosesCount is equal to 4 by default) down day,
            // issue a market order to open a long position tomorrow morning, on day 5
            if (count == ConsClosesCount)
               Buy(Qty, "Entry");
         }

         // else if we have a position open, close it now (today is the day after the trade was
         // entered), so now is actually end of day 6, and the trade will be executed by the market
         // on open day 7.
         else
            Sell(Qty, "Exit");
      }

      // today's close now becomes the previous close for the down day calculation
      prevClose = bar.Close;
   }
}



Top
 Profile  
 
 Post subject: live trading a portfolio
PostPosted: Mon Jan 07, 2008 7:09 am 
Offline

Joined: Thu Dec 27, 2007 7:27 pm
Posts: 1
i would like to use this technique to be scanning a large number of stocks
instead of only one ( a portfolio).
would you be able to show how this could be done scanning
these stocks live and trading automatically.

is this possible with smartquant?


thanks


Top
 Profile  
 
PostPosted: Thu Jun 26, 2008 1:23 am 
Offline

Joined: Tue Jun 24, 2008 11:16 pm
Posts: 7
I was going through the OpenQuant Strategy Development guide and, as an exercise, I coded my own version of the Four Down Days and Long strategy.

When I compared its behavior to that of the above sample code I found one difference that I'm not able to explain. I ran the Sample strategy on only MSFT (the data that was included in the installation download). The closing values for MSFT for the period of interest are:

(Year = 1998)
4/21 - 23.51
4/22 - 24.50
4/23 - 23.42 Down day 1
4/24 - 22.83 Down day 2
4/27 - 22.38 Down day 3
4/28 - 22.27 Down day 4 (submit buy order)
4/29 - 22.43 (submit sell order)
4/30 - 22.33 Down day 1
5/1 - 22.21 Down day 2
5/4 - 21.82 Down day 3
5/5 - 21.75 Down day 4 (submit buy order)
5/6 - 21.40 Down day
5/7 - 20.66 Down day
5/8 - 21.25

The sample code listed above does NOT generate the 5/5 buy order and I cannot figure out why. I thought it might be something related to when the down day counter is reset....but since we have a string of 6 down days, if the order wasn't sent in on 5/5, then it would be on 5/6 or 5/7.....but this order is completely missed.

I'm hoping someone can explain why.

Thanks in advance......

Also, just FYI, I've included my code below (that does submit the buy order on 5/5/98).

public class MyStrategy : Strategy
{
double Qty = 100;

double numDownDays = 4;
double numHoldDays = 1;

double cnt = 0;
double holdcnt = 0;
double prevClose;

public override void OnStrategyStart()
{
prevClose = -1;
}

public override void OnBar(Bar bar)
{
//Have to let one bar pass.
if (prevClose != -1)
{
if (HasPosition)
{
holdcnt++;
if (holdcnt == numHoldDays)
{
Sell(Qty,"Exit");
holdcnt = 0;
}
}
else
{
if (bar.Close < prevClose)
cnt++;
else
cnt = 0;

if (cnt >= numDownDays)
{
Buy(Qty,"Entry");
cnt = 0;
}
}
}
prevClose = bar.Close;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 1:26 am 
Offline

Joined: Tue Jun 24, 2008 11:16 pm
Posts: 7
I just noticed that a date in my above post was transformed into a smiley face.

That line is supposed to read:

...does submit the buy order on 5 / 5 / 98 ).

Thx.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2008 7:12 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
I came across this posting just now. Sorry for taking so long to respond, I thought these forums were inactive since the Quanthouse move. The reason why the May 5 Buy order is not issued is because of a combination of two things.

(1) An "==" test is performed, instead of a >=. Equals is not bad, but you must make sure the count starts below the ConsCloseCount and increments upward in order to catch it with an == test. (2) The code does not reset the count when the buy order is issued, so count gets incremented _past_ the == test, so the test fails, and no successive Buy order is issued.

Here's a code change that will issue the May 5 Buy order.

Code:
if (count == ConsClosesCount) {
     Buy(Qty, "Entry");
     count = 0;            // reset count each time you buy stock
}


I fixed it in the doc too. One of the SQ guys will have to fix the source code.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 02, 2010 1:50 pm 
Offline

Joined: Wed Feb 03, 2010 8:24 am
Posts: 1
Good post. I would like to use this technique to be scanning a large number of stocks. That's really good.


Top
 Profile  
 
PostPosted: Thu Mar 10, 2011 2:30 pm 
Offline

Joined: Tue Feb 15, 2011 3:07 pm
Posts: 1
Location: USA
infinite123 wrote:
i would like to use this technique to be scanning a large number of stocks
instead of only one ( a portfolio).
would you be able to show how this could be done scanning
these stocks live and trading automatically.

is this possible with smartquant?


thanks

Why this scan only one stock. I think there is any problem in your scanning because when I scan this many stock scanned. So, check your link and use smartquant for this purpose.

_________________
Beacon Hill Homes for Sale


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

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 2 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