SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Wed Oct 23, 2019 6:32 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Tue Mar 12, 2019 12:46 pm 
Offline

Joined: Thu Feb 05, 2015 11:15 am
Posts: 39
Hi,

I found that if sellside strategy emits events (i.e. bar), buyside strategy will not receive them in backtest mode. If it is true, how to implement backtesting with history bar data for synthetic instrument from sellside strategy?

Example codes:

It is to subscribe a synthetic instrument with ID = 261, which has only one leg.
It is shown in console that sellSideBar has been emit but buyside does not receive them.

Code:
using System;

using SmartQuant;

namespace OpenQuant
{
    public partial class MyScenario : Scenario
    {
        public MyScenario(Framework framework)
            : base(framework)
        {
        }

        public override void Run()
        {
            strategy = new Strategy(framework, "meta");
               
            BarFactory.Clear();
            Instrument instrument1 = InstrumentManager.GetById(261);
            Strategy buyside = new MyBuySideStrategy(framework, "MyBuySideStrategy");
            strategy.AddStrategy(buyside);
            buyside.Instruments.Add(instrument1);

            MySellSideStrategy sellSideStrategy = new MySellSideStrategy(framework, "MySellSideStrategy");
            strategy.AddStrategy(sellSideStrategy);
            buyside.ExecutionProvider = sellSideStrategy;
            buyside.DataProvider = sellSideStrategy;
            //buyside.DataSimulator = sellSideStrategy;
            instrument1.ExecutionProvider = sellSideStrategy;
            instrument1.DataProvider = sellSideStrategy;
            BarFactory.Add(instrument1, BarType.Time, 86400);
            IHistoricalDataProvider historyProvider = ProviderManager.GetHistoricalDataProvider(60);
            BarSeries dailySeries = DataManager.GetHistoricalBars(historyProvider, instrument1.Legs[0].Instrument, DateTime.Now.Date.AddDays(-30), DateTime.Now.Date, BarType.Time, 86400);

            BarFactory.Add(instrument1.Legs[0].Instrument, BarType.Time, 86400);
            sellSideStrategy.AddInstrument(instrument1.Legs[0].Instrument);
            DataSimulator.Series.Add(dailySeries);
            Console.WriteLine("Data simulator add {0} daily series count = {1}, {2}-{3}", instrument1.Legs[0].Instrument.Symbol, dailySeries.Count, dailySeries.FirstDateTime, dailySeries.LastDateTime);
            buyside.AddInstrument(instrument1);
            BarFactory.Add(instrument1, BarType.Time, 86400);
            DataSimulator.SubscribeAll = false;
            ExecutionSimulator.FillOnBar = true;
            Console.WriteLine("Start");
            StartBacktest();
        }
    }
}


Code:
using System;

using SmartQuant;

namespace OpenQuant
{
    public class MySellSideStrategy : SellSideInstrumentStrategy
    {
        private Instrument sellSideInstrument;
      public MySellSideStrategy(Framework framework, string name)
            : base(framework, name)
        {

        }

        protected override void OnSubscribe(Instrument instrument)
        {
                Console.WriteLine("OnSubscribe {0}", instrument.Symbol);
                sellSideInstrument = instrument;
                foreach (Leg leg in instrument.Legs)
                {
                    Console.WriteLine("Add daily bar {0}", leg.Instrument.Symbol);
                    BarFactory.Add(leg.Instrument, BarType.Time, 86400);
                    Console.WriteLine("Add instrument {0}", leg.Instrument.Symbol);
                    AddInstrument(leg.Instrument);
                }
        }

        protected override void OnBar(Instrument instrument, Bar bar)
        {
            Bar sellSideBar = new Bar(bar.DateTime, bar.CloseDateTime, 261, bar.Type, bar.Size, bar.Open, bar.High, bar.Low, bar.Close, bar.Volume);
            Console.WriteLine("EmitBar {0}", sellSideBar);
            EmitBar(sellSideBar);
        }
    }
}


Code:
using System;

using SmartQuant;

namespace OpenQuant
{
    public class MyBuySideStrategy : InstrumentStrategy
    {
      public MyBuySideStrategy(Framework framework, string name)
            : base(framework, name)
        {
        }

        protected override void OnStrategyStart()
        {
            Group("Bars", "Pad", 0);
        }

        protected override void OnBar(Instrument instrument, Bar bar)
        {
            Console.WriteLine("OnBar {0}", bar);
            Log(bar, "Bars");
        }
    }
}


Top
 Profile  
 
PostPosted: Fri Mar 15, 2019 9:40 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 536
Hello,
Scenario looks properly, but you don't need AddInstrument in sellside, it will be doing during request from buyside.
You can look to our examples with sellside: Spread, VWAP, etc.


Top
 Profile  
 
PostPosted: Fri Mar 15, 2019 11:00 am 
Offline

Joined: Thu Feb 05, 2015 11:15 am
Posts: 39
skuvv wrote:
Hello,
Scenario looks properly, but you don't need AddInstrument in sellside, it will be doing during request from buyside.
You can look to our examples with sellside: Spread, VWAP, etc.


Spread and VWAP created trade, bid, ask to barfactory to create bars for buyside. The difference is I would like to use sellside to create bar history, so that buyside should receive bar from sellside not bar factory in backtest. I am not sure if it is possible due to the architecture?

I also tried to use barInupt.bar and set barFactoryItem size and instrument, but not work.

Will it be different using Strategy_?


Top
 Profile  
 
PostPosted: Fri Mar 15, 2019 12:54 pm 
Offline

Joined: Thu Feb 05, 2015 11:15 am
Posts: 39
BTW, I found in MySellSideStrategy, sometimes sellSideInstrument is null when onbar() triggered, which should have value after sellside is subscribed. Is it something about the multiple thread architecture?


Top
 Profile  
 
PostPosted: Fri Mar 15, 2019 2:14 pm 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 536
ericwang wrote:
BTW, I found in MySellSideStrategy, sometimes sellSideInstrument is null when onbar() triggered, which should have value after sellside is subscribed. Is it something about the multiple thread architecture?

This usually happens with a wrong subscription.
Check it during OnSubscribe()


Top
 Profile  
 
PostPosted: Mon Mar 18, 2019 6:27 am 
Offline

Joined: Thu Feb 05, 2015 11:15 am
Posts: 39
I tried with Strategy_ and facing error with no debug information:

Code:
EventManager::OnException Exception occured in EventHandler - Bar [12/10/2018 00:00:00.000 - 12/10/2018 00:00:00.000] Instrument=153 Type=Time Size=86400 Open=3160.2 High=0 Low=0 Close=0 Volume=0 - System.NullReferenceException: Object reference not set to an instance of an object.
   at SmartQuant.Strategy_.StrategyManager_.w6RuNp8S9C(Bar  )
   at SmartQuant.EventManager.rprmT88ahM(Event  )
   at SmartQuant.EventManager.OnEvent(Event e)


Top
 Profile  
 
PostPosted: Mon Mar 18, 2019 9:54 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 536
ericwang wrote:
I tried with Strategy_ and facing error with no debug information:

Code:
EventManager::OnException Exception occured in EventHandler - Bar [12/10/2018 00:00:00.000 - 12/10/2018 00:00:00.000] Instrument=153 Type=Time Size=86400 Open=3160.2 High=0 Low=0 Close=0 Volume=0 - System.NullReferenceException: Object reference not set to an instance of an object.
   at SmartQuant.Strategy_.StrategyManager_.w6RuNp8S9C(Bar  )
   at SmartQuant.EventManager.rprmT88ahM(Event  )
   at SmartQuant.EventManager.OnEvent(Event e)

Looks like strategy or subscription added wrong.
If our examples are not helping you, please send me your solution (based on Strategy_ ) PM.
There is no need for trading logic, just code for reproduction error.


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