SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sun Nov 29, 2020 2:00 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 42 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: Fri Nov 30, 2007 4:17 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
The code below demonstrates how to use strategy timer to cancel a limit order if the order is not filled within 20 seconds.

Note how Clock.Now is used to obtain current strategy time. This technique works in both simulation and live trading modes.

Code:
using OpenQuant.API;

public class MyStrategy : Strategy
{
   private Order order;
   private bool entry = true;
   
   public override void OnBar(Bar bar)
   {
      if (HasPosition)
         ClosePosition();

      if (entry)
       {
         order = LimitOrder(OrderSide.Buy, 100, bar.Close - 0.03);                     
         order.Send();
            
         AddTimer(Clock.Now.AddSeconds(20));

         entry = false;
      }
   }
   
   public override void OnTimer(DateTime signalTime)
   {   
      if (!order.IsDone)
         order.Cancel();

      entry = true;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 2:18 pm 
Offline

Joined: Fri Nov 09, 2007 9:44 pm
Posts: 46
It's great that your posting these, please continue.

Concerning OnTimer:

say I want to simulate a delay in execution, say 4 seconds, how do i do that? ive tried barfilter and the likes but that doesnt work for me. i then thought about using ontimer, but thats problematic because i can only have one timer at a time and im working on 1 second bars sometimes. plus im not sure if it is possible to access an order from previous bars, since my orders will be overwritten i'm suppose. how cani get out of this dilemma anton?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 3:51 pm 
Offline

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

could you explain what you want to achieve? What is delay in execution?

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 4:30 pm 
Offline

Joined: Fri Nov 09, 2007 9:44 pm
Posts: 46
sorry if i didn't explain myself well. i meant a four seconds delayed execution. sorry


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 5:21 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
Do you want to submit an order four seconds after some event or you want to submit a market order and received fill with four second delay (in the simulation mode)?

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 5:40 pm 
Offline

Joined: Fri Nov 09, 2007 9:44 pm
Posts: 46
the latter is what im trying to do. though id be interested in how to do the former as well (in the case that the event might happen with relatively high frequency and three different events might trigger different orders four seconds later). thanks anton


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 5:52 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
It was discussed several times on SQ forums. I think the outcome is that there is no continuous time axis in the simulation mode. Thus it's hard to understand what four seconds execution delay really is. Though we can think a bit more about it.

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 6:13 pm 
Offline

Joined: Fri Nov 09, 2007 9:44 pm
Posts: 46
it's really not so hard because the trade data comes with a time stamp doesn't it?

something like this wouldnt work?
on order push on queue (or orderlist)
while trade.datetime >= queue.order.datetime + delay){
order.execute
pop queue
}

im curious

also (i'm not sure), in the case that this wouldn't work, a crude approximation could be using 1 sec bars and executing after x bars. in fact this should definitely be a feature (using a lower resolution barseries as fill data, plus having the option to fill not only at nextbar close, but rather at x bars)

please look into this, it should really be no big deal and add a some more realism to high frequency strategies. (btw does anybody know how to get millisecond timestamps from the exchanges?)
cheers


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 4:36 am 
Offline

Joined: Thu Mar 16, 2006 12:15 pm
Posts: 184
Into the same direction:

I am using quote data for filling. But filling on neither lastquote nor nextquote is the most realistic thing.
Most realistic filling would be on the current quote x-milliseconds after e.g. an onbar event has been shooted. x depends on the data latency you have - broker...

_________________
Expect the unexpected. May your MM/RM be with you.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 06, 2008 12:49 pm 
Offline

Joined: Thu Jan 31, 2008 11:39 am
Posts: 166
Just one question: If the Order is filled within the 20 seconds, how can I remove the timer to have another one ready?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 11:42 am 
Offline

Joined: Thu Mar 20, 2008 4:41 pm
Posts: 273
Location: Sweden
flotschie wrote:
Just one question: If the Order is filled within the 20 seconds, how can I remove the timer to have another one ready?

There is a removetimer function. For some reason it takes a dateTime as paramter, not sure why, but it may be optional. Try it. :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 11:44 am 
Offline

Joined: Thu Mar 20, 2008 4:41 pm
Posts: 273
Location: Sweden
The original question, how do I cancel an order at a specific time?
I want to do
Code:
AddTimer((new TimeSpan(0,16,00,0,0) - Bar.DateTime.TimeOfDay));

Which in my head takes the time 16:00, subtracts the current time, say 12:00, and sets a timer for the difference, that is 4 hours.
But the TimeSpan can't be converted to a DateTime, so it doesn't work.

I also tried

Code:
DateTime someTime = new DateTime(Clock.Now.Year, Clock.Now.Month, Clock.Now.Day);
TimeSpan someSpan = new TimeSpan(16, 0, 0);
someTime.Add(someSpan);
AddTimer(someSpan);


Same result.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 2:17 pm 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi,

The following code works fine on my machine, the timer fires exactly at 17-00:


Code:
using System;
using System.Drawing;

using OpenQuant.API;
using OpenQuant.API.Indicators;

public class MyStrategy : Strategy
{
   bool done = false;
   
   public override void OnBar(Bar bar)
   {
      if (!done)
      {
         done = true;
         AddTimer(bar.DateTime.Date.Add(new TimeSpan(17, 0, 0)));
      }
   }

   public override void OnTimer(DateTime dateTime)
   {
      Console.WriteLine("OnTimer at " + Clock.Now);         
   }
}


Regards,
Sergey.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 2:26 pm 
Offline

Joined: Thu Mar 20, 2008 4:41 pm
Posts: 273
Location: Sweden
Works great, thank you :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 10:20 pm 
Offline

Joined: Tue Apr 29, 2008 11:00 pm
Posts: 43
Can you modify a timer? Say have a rolling 2 minute delay?

I'd like to clean up my method for making sure I'm getting bars/trades for all my instruments.

Right now I add a 3 minute timer on every bar/trade and see if the datetime of the last bar/trade came after I added the timer. It works but definatly takes alot of cycles that aren't necessary.

What do you use?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 42 posts ]  Go to page 1, 2, 3  Next

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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