SmartQuant Discussion

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

All times are UTC + 3 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Wed Mar 21, 2007 1:36 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6808
This Bollinger band strategy is intended for short intraday trades that last no longer than 5 or 10 minutes. It enters a trade if prices fall 3% below the lower Bollinger band, and exits a trade whena 1% profit target has been reached, or when two bars have passed since the trade was entered.
There are several interesting features of this system. It uses flags to say when a trade can be entered or exited (when two bars have passed). It uses the OnTrade event handler to look at trade data during bar formation intervals (so the strategy is working within two timeframes—trades and bars). And it uses SetStop method to set a stop reminder based on time (5 minutes) to remind the strategy to close the position automatically after 5 minutes have passed with an open position.

Code:
using OpenQuant.API;
using OpenQuant.API.Indicators;

using System.Drawing;

public class MyStrategy : Strategy
{
   [Parameter("Order quantity (number of contracts to trade)")]
   double Qty           = 100;
   
   [Parameter("Percent")]
   double Percent       = 3;
   
   [Parameter("Profit Target")]
   double ProfitTarget  = 1;
   
   [Parameter("Length of BBL", "BBL")]
   int    BBLLength = 10;
   
   [Parameter("Order of BBL", "BBL")]
   double BBLOrder  = 2;   
   
   BBL bbl;
   
   Order sellLimit;
   Order buyLimit;
   
   private int barsFromEntry = 0;
   
   public override void OnStrategyStart()
   {
      bbl = new BBL(Bars, BBLLength, BBLOrder);
         
      bbl.Color = Color.Yellow;
       
      Draw(bbl, 0);
   }   
   
   public override void OnBar(Bar bar)
   {
      if (bbl.Contains(bar.DateTime))
      {
         if (!HasPosition)
         {   
            // cancel previos buy limit
            if (buyLimit != null)
               buyLimit.Cancel();             
             
            // calculate limit price
            double buyPrice = bbl.Last * (1 - Percent / 100);
             
            // place new limit orders
            buyLimit = BuyLimitOrder(Qty, buyPrice, "Entry");             
            buyLimit.Send();
         }
         else
         {
            barsFromEntry++;
         
            // close position at the second bar after entry
            if (barsFromEntry == 2)
            {
               barsFromEntry = 0;               
               
               sellLimit.Cancel();
               
               Sell(Qty, "Exit (Second Bar After Entry)");
            }
         }
      }
   }
   
   public override void OnBarOpen(Bar bar)
   {
      // place limit order at the beginning of the next bar after entry
      if (barsFromEntry == 1)       
         PlaceSellLimit();
   }
   
   public override void OnOrderFilled(Order order)
   {
      if (order == sellLimit)
         barsFromEntry = 0;   
   }

   private void PlaceSellLimit()
   {
      // calculate price that satisfies the profit target
      double sellPrice = buyLimit.AvgPrice * (1 + ProfitTarget / 100);
             
      sellLimit = SellLimitOrder(Qty, sellPrice, "Exit (Profit Target)");
      sellLimit.Send();
   }
}




Top
 Profile  
 
 Post subject: cant see On trade event
PostPosted: Wed Jun 20, 2007 12:50 am 
Offline

Joined: Sun Jun 17, 2007 3:51 am
Posts: 3
Hi Anton :
PLease show me where the Ontrade event handler appears on this code(as explained in your message).
Thank you
jorge159


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 8:45 am 
Offline

Joined: Sun May 20, 2007 9:09 am
Posts: 351
not in the code how to show you? :lol:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 12:27 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6808
I see, thanks. I think this doc should be changed a bit.


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

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
I agree, and it's my fault. The second paragraph of the doc is wrong, and should not have been part of the cut and paste from an older version of the strategy. Here is what the doc should say (same as paragraph 1 above, but paragraph 2 has been cut out):

Code:
This Bollinger band strategy is intended for short intraday trades that last no longer than 5 or 10 minutes. It enters a trade if prices fall 3% below the lower Bollinger band, and exits a trade when a 1% profit target has been reached, or when two bars have passed since the trade was entered.


I have fixed the original doc too.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 6:23 am 
Offline

Joined: Sat May 24, 2008 2:30 pm
Posts: 12
Hi,

I actually borrowed James Altucher's book from the library and photocopied the pages for this system from the book for later reference.

Looking at it now, he says to BUY when the stock falls 3% below its lower band and hold until at least the end of the 5-min period where the stock was bought.

And to SELL when the stock hits a 1% profit target or at the end of the second bar after the stock was bought.

Ok, what I understand from the above is that the maximum profit per trade is capped at 1% and that longest holding time is 10min. Is this correct?

Now, the confusing part is that some of his examples show significantly >1% profit, eg. ORCL 3.3%, AMAT 6.15%

And, the system stats show 113 trades over 15 months with 92% hit rate which is excellent.
BUT the average win is 3.22% (how come its more than 1% when you have the profit target) and the average holding time for a loss is 5.44 bars (I thought maximum holding time was 10mins??)

Can anybody explain these anomalies?

Thanks.

Nizar.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 7:27 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
(sigh) I can see another glitch from the cut and paste of the doc, which I will change. For example, there is certainly no SetStop timer action in the code to match the doc, so the cut-and-pasted doc must be changed.

I can't speak to exactness, but it seems to me that Altucher worked with daily strategies, not intraday strategies (which is what the (now incorrect) doc from the QD product was talking about). So that's one big difference.

Another is that even if you have a profit target of 1%, it's just a target that issues an exit order when 1% is reached. The actual trade price might be more or less than 1%.

I don't have Altucher's book in front of me, so I can't even comment on whether the code that we're looking at is supposed to come from Altucher's book.

The way things work is that the strategy code is always orginally written by the SQ guys. Then I try to figure out what they're doing, and then try to tidy up and document the code so that people have a better chance of understanding the algorithm.

I know that the original code strategies from a year or two ago were modelled somewhat after his book, but it looks to me like the code changed along the way somewhere from my original doc to the current code, which is why the cut-and-paste from QD to OQ doc is not right (I missed the change.)

The code you're looking at is for 5-minute bars, for sure. Here's what my copy of the entire doc says for this strategy:

Quote:
4.7.2 Bollinger Bands with 5-Minute Bars
This Bollinger band strategy is intended for short intraday trades that last no longer than 5 or 10 minutes. It enters a trade if prices fall 3% below the lower Bollinger band, and exits a trade when a 1% profit target has been reached, or when two bars have passed since the trade was entered.


I hope this helps. It's good that you're trying to understand the details of the code and how the algorithm is supposed to work (vs how it actually works). Because we all know that the devil is in the details in computerized trading.[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 11:54 am 
Offline

Joined: Sat May 24, 2008 2:30 pm
Posts: 12
kkkwj,

have you (or anybody else) backtested (or traded) this code on NASDAQ stocks?
how has it performed?

thanks,

Nizar.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 14, 2009 9:15 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
I haven't seriously backtested any of the example strategies. I only tried them out on the default data set when I first started programming in QD/OQ. Mostly I used the example strategies for coding examples.


Top
 Profile  
 
PostPosted: Fri Nov 26, 2010 11:07 pm 
Offline

Joined: Fri Nov 26, 2010 10:41 pm
Posts: 1
Hi i´m trying to run a simulation at this strategy and i'm having an outofmemory exception.
I´m running 30 instruments for an year and the error comes when reaches the 3rd month. i already tried to change bar array to 200 and trade array to 200 and still stops at the same point. I haven´t change a single line of code in strategy.... How to solve that ????

Thank´s Vitor Dantas


Top
 Profile  
 
PostPosted: Sat Jan 22, 2011 2:47 pm 
Offline

Joined: Sat Jan 22, 2011 2:44 pm
Posts: 1
A bull would tend to buy calls, at least for the stocks they are bullish on. In their world view, there should be a time while the call is still active when the stock price is higher than the strike price. Thus by exercising the call they get a break on the purchase price.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 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