SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Thu Jul 09, 2020 8:04 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Dec 14, 2016 12:49 pm 
Offline

Joined: Tue Oct 13, 2009 12:19 pm
Posts: 270
Dear all,

I cannot get my strategy to execute orders on bars.

This is my Scenario:
Code:
         strategy = new MyStrategy(framework, "Debug");

         Instrument instrument = InstrumentManager.Instruments["SPY"];
         strategy.AddInstrument(instrument);

         DataSimulator.DateTime1 = new DateTime(2016, 6, 28).Add(new TimeSpan(9,59,7));
         DataSimulator.DateTime2 = new DateTime(2016, 6, 28).Add(new TimeSpan(10,02,01)); 
         //DataSimulator.BarFilter.Add(BarType.Time, 60);
         
         BarFactory.Clear();
         BarFactory.Add(instrument, BarType.Time, 60);
         BarFactory.Add(instrument, BarType.Time, 86400);

         Initialize();
         ExecutionSimulator.Connect();
         
         //ExecutionSimulator.CommissionProvider.Type = CommissionType.PerShare;
         //ExecutionSimulator.CommissionProvider.Commission = 0.01;
         //         ExecutionSimulator.FillOnBar = true; //otherwise it will only fill on trade.. which we do not have...
         StartStrategy();


this is my strategy:
Code:
protected override void OnStrategyStart()
        {
        }
      public Order entryIMLong;
        protected override void OnBar(Instrument instrument, Bar bar)
        {
         if (bar.Size == 60){
            Console.WriteLine("bar {5}- {4}: {0},{1},{2},{3}",bar.Open,bar.High,bar.Low,bar.Close,bar.CloseDateTime,bar.OpenDateTime);
            //entryIMLong = BuyLimitOrder(Instrument,101, bar.Close - 0.01,"LE.IM");
            entryIMLong = Buy(Instrument,101,"LE.IM");
            Console.WriteLine("2entryIMLong.Status " + entryIMLong.Status);
            Send(entryIMLong);
         }
        }


and this is the Output:
[code][/Building MyStrategy
12/14/2016 04:43:41.315 Scenario::StartStrategy Backtest
12/14/2016 04:43:41.315 StrategyManager::StartStrategy Debug
12/14/2016 04:43:41.319 DataSimulator::Subscribe SPY
12/14/2016 04:43:41.320 Data simulator thread started
EventBus::Dequeue Skipping: Bar [6/28/2016 09:59:00.000 - 6/28/2016 09:59:00.000] Instrument=9 Type=Time Size=60 Open=202.15 High=0 Low=0 Close=0 Volume=0 6/28/2016 09:59:00.000 <> 6/28/2016 09:59:07.000
bar 6/28/2016 9:59:00 AM- 6/28/2016 10:00:00 AM: 202.15,202.18,202.09,202.13
EventManager::OnException Exception occured in EventHandler - Bar [6/28/2016 09:59:00.000 - 6/28/2016 10:00:00.000] Instrument=9 Type=Time Size=60 Open=202.15 High=202.18 Low=202.09 Close=202.13 Volume=145529 - System.NullReferenceException: Object reference not set to an instance of an object.
at SmartQuant.ExecutionSimulator.Fill(Order order, Double price, Int32 size)
at SmartQuant.ExecutionSimulator.eHEqyMBqfP(Order , Bar )
at SmartQuant.ExecutionSimulator.NYoqOqJRej(Order )
at SmartQuant.ExecutionSimulator.Send(ExecutionCommand command)
at SmartQuant.OrderManager.Send(Order order)
at SmartQuant.Strategy.Buy(IExecutionProvider provider, Instrument instrument, Double qty, String text)
at SmartQuant.Strategy.Buy(Instrument instrument, Double qty, String text)
at OpenQuant.MyStrategy.OnBar(Instrument instrument, Bar bar)
at SmartQuant.Strategy.cBczL4u31v(Bar bar)
at SmartQuant.Strategy.cBczL4u31v(Bar bar)
at SmartQuant.StrategyManager.BqvUjmoROf(Bar )
at SmartQuant.EventManager.hopaLolsWC(Event )
at SmartQuant.EventManager.OnEvent(Event e)
EventManager::OnException Exception occured in EventHandler - Bar [6/28/2016 10:00:00.000 - 6/28/2016 10:01:00.000] Instrument=9 Type=Time Size=60 Open=202.13 High=202.21 Low=202.08 Close=202.14 Volume=289197 - System.NullReferenceException: Object reference not set to an instance of an object.
at SmartQuant.ExecutionSimulator.Fill(Order order, Double price, Int32 size)
at SmartQuant.ExecutionSimulator.eHEqyMBqfP(Order , Bar )
at SmartQuant.ExecutionSimulator.OnBar(Bar bar)
at SmartQuant.EventManager.hopaLolsWC(Event )
at SmartQuant.EventManager.OnEvent(Event e)
EventManager::OnException Exception occured in EventHandler - Bar [6/28/2016 10:01:00.000 - 6/28/2016 10:02:00.000] Instrument=9 Type=Time Size=60 Open=202.14 High=202.29 Low=202.13 Close=202.27 Volume=303696 - System.NullReferenceException: Object reference not set to an instance of an object.
at SmartQuant.ExecutionSimulator.Fill(Order order, Double price, Int32 size)
at SmartQuant.ExecutionSimulator.eHEqyMBqfP(Order , Bar )
at SmartQuant.ExecutionSimulator.OnBar(Bar bar)
at SmartQuant.EventManager.hopaLolsWC(Event )
at SmartQuant.EventManager.OnEvent(Event e)
12/14/2016 04:43:41.339 StrategyManager::StopStrategy Debug
12/14/2016 04:43:41.339 Data simulator thread stopped
12/14/2016 04:43:41.340 Data run done, count = 5 ms = 15 event/sec = 0
12/14/2016 04:43:41.342 Scenario::StartStrategy Done
code]

on the ExecutionSimulator, all false except FillOnBar is true and Queued is true.

This does not work for market or Limit orders...

I got Version: 1.0.6190

Thanks
Mike


Top
 Profile  
 
PostPosted: Thu Dec 15, 2016 9:16 am 
Offline

Joined: Tue Oct 13, 2009 12:19 pm
Posts: 270
Dear all,

I think I found the fix. Since I had already 1 min and daily bars in the database, I could have disabled barfactory. Which I did. And then a fresh start from the Framework and it works now. So, apparently, a bar Event was called twice, but sure did not look like it...
And then I also did a Buy and not a BuyOrder.... since also makes a change, since after Setting the buy order, I am still used Send(order);.....

Regards
Mike


Top
 Profile  
 
PostPosted: Thu Dec 15, 2016 10:39 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 571
Hello Mike,
I think problem occurred by same reason as with CommissionProvider, try add this code in Scenario:

Code:
         if (ExecutionSimulator.CommissionProvider == null)   
         {
            Console.WriteLine("CommissionProvider == null");
            ExecutionSimulator.CommissionProvider = new CommissionProvider();   
         }
         if (ExecutionSimulator.SlippageProvider == null)   
         {
            Console.WriteLine("SlippageProvider == null");
            ExecutionSimulator.SlippageProvider = new SlippageProvider();   
         }


Top
 Profile  
 
PostPosted: Thu Dec 15, 2016 12:40 pm 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 571
New setup available.
Please remove previous code(placed below) and run solution, it will help us understand where problems occur.

Code:
         if (ExecutionSimulator.CommissionProvider == null)   
         {
            Console.WriteLine("CommissionProvider == null");
            ExecutionSimulator.CommissionProvider = new CommissionProvider();   
         }
         if (ExecutionSimulator.SlippageProvider == null)   
         {
            Console.WriteLine("SlippageProvider == null");
            ExecutionSimulator.SlippageProvider = new SlippageProvider();   
         }


Top
 Profile  
 
PostPosted: Fri Dec 16, 2016 12:28 am 
Offline

Joined: Tue Oct 13, 2009 12:19 pm
Posts: 270
Dear all,

the slippage Provider was there. That was not the Problem:

the Problem is in the strategy code:

1. entryIMLong = Buy(Instrument,171,"LE.IM");
2. Console.WriteLine("2entryIMLong.Status " + entryIMLong.Status);
3. Send(entryIMLong);

Line 1, I issue a buy and then assign that to the variable entryIMLong.
On line 3, I send that order again... and that give this error message:

Code:
Building MyStrategy
12/15/2016 16:22:55.643 Scenario::StartStrategy Backtest
12/15/2016 16:22:55.643 StrategyManager::StartStrategy Debug
hello world
12/15/2016 16:22:55.648 DataSimulator::Subscribe SPY
12/15/2016 16:22:55.648 Data simulator thread started
EventBus::Dequeue Skipping: Bar [6/28/2016 09:59:00.000 - 6/28/2016 09:59:00.000] Instrument=9 Type=Time Size=60 Open=202.15 High=0 Low=0 Close=0 Volume=0 6/28/2016 09:59:00.000 <> 6/28/2016 09:59:07.000
bar 6/28/2016 9:59:00 AM- 6/28/2016 10:00:00 AM: 202.15,202.18,202.09,202.13
2entryIMLong.Status PendingNew
EventManager::OnException Exception occured in EventHandler - Bar [6/28/2016 09:59:00.000 - 6/28/2016 10:00:00.000] Instrument=9 Type=Time Size=60 Open=202.15 High=202.18 Low=202.09 Close=202.13 Volume=145529 - System.ApplicationException: Can not send order that has been already sent 0 6/28/2016 10:00:00.000 Market Buy
   at SmartQuant.OrderManager.Send(Order order)
   at SmartQuant.Strategy.Send(Order order)
   at OpenQuant.MyStrategy.OnBar(Instrument instrument, Bar bar)
   at SmartQuant.Strategy.cBczL4u31v(Bar bar)
   at SmartQuant.Strategy.cBczL4u31v(Bar bar)
   at SmartQuant.StrategyManager.BqvUjmoROf(Bar  )
   at SmartQuant.EventManager.hopaLolsWC(Event  )
   at SmartQuant.EventManager.OnEvent(Event e)
bar 6/28/2016 10:00:00 AM- 6/28/2016 10:01:00 AM: 202.13,202.21,202.08,202.14
2entryIMLong.Status PendingNew
EventManager::OnException Exception occured in EventHandler - Bar [6/28/2016 10:00:00.000 - 6/28/2016 10:01:00.000] Instrument=9 Type=Time Size=60 Open=202.13 High=202.21 Low=202.08 Close=202.14 Volume=289197 - System.ApplicationException: Can not send order that has been already sent 1 6/28/2016 10:01:00.000 Market Buy
   at SmartQuant.OrderManager.Send(Order order)
   at SmartQuant.Strategy.Send(Order order)
   at OpenQuant.MyStrategy.OnBar(Instrument instrument, Bar bar)
   at SmartQuant.Strategy.cBczL4u31v(Bar bar)
   at SmartQuant.Strategy.cBczL4u31v(Bar bar)
   at SmartQuant.StrategyManager.BqvUjmoROf(Bar  )
   at SmartQuant.EventManager.hopaLolsWC(Event  )
   at SmartQuant.EventManager.OnEvent(Event e)
bar 6/28/2016 10:01:00 AM- 6/28/2016 10:02:00 AM: 202.14,202.29,202.13,202.27
2entryIMLong.Status PendingNew
EventManager::OnException Exception occured in EventHandler - Bar [6/28/2016 10:01:00.000 - 6/28/2016 10:02:00.000] Instrument=9 Type=Time Size=60 Open=202.14 High=202.29 Low=202.13 Close=202.27 Volume=303696 - System.ApplicationException: Can not send order that has been already sent 2 6/28/2016 10:02:00.000 Market Buy
   at SmartQuant.OrderManager.Send(Order order)
   at SmartQuant.Strategy.Send(Order order)
   at OpenQuant.MyStrategy.OnBar(Instrument instrument, Bar bar)
   at SmartQuant.Strategy.cBczL4u31v(Bar bar)
   at SmartQuant.Strategy.cBczL4u31v(Bar bar)
   at SmartQuant.StrategyManager.BqvUjmoROf(Bar  )
   at SmartQuant.EventManager.hopaLolsWC(Event  )
   at SmartQuant.EventManager.OnEvent(Event e)
12/15/2016 16:22:55.688 StrategyManager::StopStrategy Debug
12/15/2016 16:22:55.689 Data simulator thread stopped
12/15/2016 16:22:55.689 Data run done, count = 5 ms = 35 event/sec = 0
12/15/2016 16:22:55.691 Scenario::StartStrategy Done


If you remove line 3 from the code, so not sending the order twice on a bar, this works fine...
If you need further info, let me know.
So after all, my mistake. Thanks for your support and trying to fix that.
Mike
P.S.: seems like I cannot reproduce the original Problem...


Top
 Profile  
 
PostPosted: Fri Dec 16, 2016 11:20 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 571
Method Buy() means that order will be sent immediatelly.
You don't need send it again using Send(entryIMLong);
And error says about this, "System.ApplicationException: Can not send order that has been already sent 0 6/28/2016 10:00:00.000 Market Buy"


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 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