SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Tue Feb 10, 2026 8:05 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Mar 21, 2007 2:12 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
The concept behind this strategy is that sometimes the market panics over some news, and hammers down a stock price for a short time. After a few minutes or hours, people recover from their panic, realize that the stock price is too low, and buy long to bid the price back up. At the end of each trading day (we assume daily bars here, like Altucher did in his book), this strategy receives the daily bar in the OnBar event handler. If no position is open, the strategy uses today’s closing price to calculate a new limit price that is 5% lower than today’s close, and issues a limit buy order to be executed at market open the following day. In contrast, if a position is open at the end of the day, it means that a 5% gap happened sometime during today, and so the strategy immediately closes the position with a market order (which will be executed at market open the next morning). The thinking here is that the panic prices would have been corrected upward during the day to something more reasonable. 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("Buy when a stock drops this number of percents in one day")]
   double Percent = 5;

   [Parameter("Order quantity (number of contracts to trade)")]
   double Qty = 100;

   Order buyOrder;

   public override void OnBar(Bar bar)
   {
      // if we do not have a position, update the limit buy order to be 5% above today's close
      if (!HasPosition)
      {
         // cancel the old limit order (it's out of date now)
         if (buyOrder != null)
            buyOrder.Cancel();

         // issue a new buy order at 5% below today's close this order will execute tomorrow
         // if price is matched
         double buy_price = bar.Close * (1 - (Percent / 100));

         buyOrder = BuyLimitOrder(Qty, buy_price, "Entry");
         buyOrder.Send();
      }

      // else we opened a position today using our limit order from yesterday, so now close
      // the position at the end of today. We expect that such a big drop was freaky, and that
      // prices recovered during the day. If not, this order stops further losses.
      else
         Sell(Qty, "Exit");
   }
}



Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 12, 2007 10:02 pm 
Offline

Joined: Thu Jun 24, 2004 7:46 pm
Posts: 7
When I run this strategy with 60 second bars, I get the following exception (and OpenQuant crashes):


************** Exception Text **************
System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at OpenQuant.Projects.UI.ResultsWindow.DateTimeValueToString(Int64 value)
at OpenQuant.Projects.UI.ResultsWindow.UpdateSummary()
at OpenQuant.Projects.UI.ResultsWindow.UpdateView()
at OpenQuant.Projects.UI.ResultsWindow.instrumentListSource_SelectedInstrumentChanged(InstrumentEventArgs args)
at OpenQuant.Instruments.InstrumentListSource.set_SelectedInstrument(Instrument value)
at OpenQuant.Projects.UI.ResultsWindow.Init()
at OpenQuant.Projects.UI.ResultsWindow.UpdateGUI()
at OpenQuant.Projects.UI.ResultsWindow.OnInit()
at OpenQuant.Docking.OpenQuantDockableWindow.Init(SettingsList settings)
at OpenQuant.Docking.OpenQuantDockManager.OpenDockableWindow(Type windowType)
at OpenQuant.Projects.UI.ProjectExplorerWindow.ctxStrategy_ViewResults_Click(Object sender, EventArgs e)
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
**********************************

What am I doing wrong here?

Many thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 1:10 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
Hi,

this is a GUI problem. This should be fixed in the next update.

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 2:41 am 
Offline

Joined: Mon Dec 03, 2007 5:39 am
Posts: 9
If I want to run a strategy like this in paper or live trading mode, how would I configure the Market Data on the project? It seems to be expecting only one bar for each day.

I tested it with just the Daily Market Data, and it didn't get any bars for the whole day. Is the "Daily" one only good for simulation mode?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 2:56 am 
Offline

Joined: Mon Dec 03, 2007 5:39 am
Posts: 9
And what happens if you put multiple sources-- let's say bar every 12 minutes and every 5 minutes. Does it just hit every one of those? 5, 10, 12, 15 etc? Thanks.


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

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Yes, all the bars come in through the same event handler. Your strategy has to look at each bar size, and decide what to do with it.


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

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 20 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:  
Powered by phpBB® Forum Software © phpBB Group