SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sun Sep 24, 2017 6:01 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Thu Sep 29, 2016 5:37 am 
Offline

Joined: Thu Sep 17, 2015 5:52 am
Posts: 133
Hello, Two related questions.

1. In OQ2014 how do I use a strategy .CS file to plot data from two different instruments onto the same chart?

For instance, I made the following changes to the MyStragey.CS file of OQ2014's SMA Crossover example in order to plot AAPL's Ask price MSFT's Ask price on the same chart, but it dident work. Any idea what I did wrong? (Note: if you need to take a closer look, the full solution file is attached with //new part written next to any changes I made.

-Declared instrument1 and instrument2 in the header.
-Created the accordingly groups.
-Added the following code.

Code:
protected override void OnAsk(Instrument instrument, Ask ask)
      {
         if (Instrument == instrument1)
         {
            Log(instrument1.Ask , instrument1AsksGroup);
         }   
         if (Instrument == instrument2)
         {
            Log(instrument2.Ask , instrument2AsksGroup);
         }
      }



2. I'm looking to trade two separate instruments via a strategy .CS file in OQ2014 (rather then via the "synthetic instrument" tactic shown in OQ2014's "Spread" example). Is the "How To develop spread/pair trading strategies in OpenQuant" from OQ's FAQ the best way to learn how to do this in OQ2014? If so, what differences should I look out for between OQ and OQ2014 while reading the mention FAQ?

Thanks


Attachments:
SMACrossover.zip [14.6 KiB]
Downloaded 51 times
Top
 Profile  
 
PostPosted: Thu Sep 29, 2016 11:04 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 367
Hello.
1)
First thing, is that your instrument1 and instrument2 not assigned(will be equal null)

Second thing,
Code:
protected override void OnAsk(Instrument instrument, Ask ask)
      {
         if (Instrument == instrument1)
         {
            Log(instrument1.Ask , instrument1AsksGroup);
         }   
         if (Instrument == instrument2)
         {
            Log(instrument2.Ask , instrument2AsksGroup);
         }
      }

It will not work, because you use type of strategy = InstrumentStrategy and it has strategy instance for each instrument.
At result each instance will be trigger one of two Ask groups:
Instance with AAPL will trigger only for
Code:
if (Instrument == instrument1)

Instance with MSFT will trigger only for
Code:
if (Instrument == instrument2)


Third thing, is that you create Ask groups with different selector and this groups will be plotted at different instruments.

2)If you want trade two or more instruments from one strategy better will be change a type of strategy from InstrumentStrategy to Strategy.


Top
 Profile  
 
PostPosted: Thu Sep 29, 2016 8:05 pm 
Offline

Joined: Thu Sep 17, 2015 5:52 am
Posts: 133
Thanks Skuvv,

I changed it from InstrumentStrategy to Strategy and it compiled fine.

However, when I try to assign the instruments and change the selector I get an error message when I press compile. Not sure what I'm doing wrong.

Any chance you can post a version of post 1's Solution file with the updates you mentioned?


Top
 Profile  
 
PostPosted: Wed Oct 05, 2016 7:22 pm 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 367
I prepared solution with two different approaches.
The first(MyStrategy) will plot the Asks only triggered by Instrument associated for current instance of strategy.
The second(MyStrategy2) will plot the Asks at any new tick from both instruments.

Attachment:
SMACrossoverTwoInstruments.zip [20.85 KiB]
Downloaded 63 times


Top
 Profile  
 
PostPosted: Tue Oct 11, 2016 1:41 am 
Offline

Joined: Thu Sep 17, 2015 5:52 am
Posts: 133
Great. This explains a lot. Thanks!


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

Joined: Tue Oct 13, 2009 12:19 pm
Posts: 195
Hi,

can you write out the reasos, why it makes sense, to trade two instruments with same strategy as "Strategy" and not "InstrumentStrategy"?

What is the logic behind it?

What are Advantages and Disadvantages?

Thanks
Mike


Top
 Profile  
 
PostPosted: Thu Jan 26, 2017 1:33 am 
Offline

Joined: Thu Sep 17, 2015 5:52 am
Posts: 133
Hi Skuvv,

How do I log data from two separate instruments side by side in Strategy Monitor?

I tried altering the solution file you posted in post 4 to do this and could not get it to work. I think my issue lies in how I'm adding the Strategy Monitor groups in private void AddGroups()

For instance, I tried to log your solution file's ask1 data via the code below and Strategy Monitor ended up blank. The last line of the code below is slashed off because the strategy is not an InstrumentStrategy. I'm guessing I need to replace this slashed of line with something else (what if so)?
Quote:
private void AddGroups()
{
ask1MonitorGroup = new Group("Ask1");
ask1MonitorGroup.Add("LogName", "Ask1");
ask1MonitorGroup.Add("StrategyName", "MyStrategy2");
// ask1MonitorGroup.Add("Symbol", Instrument.Symbol);


Thanks


Top
 Profile  
 
PostPosted: Tue Jan 31, 2017 11:12 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 367
Assuming you using Strategy.

You can use Log without handling Group, directly by group name.
Examples demo strategies in fresh install or in [Install folder]\Setup\Documents\Solutions after update.

Code:
         Group("Ask1monitor", "LogName", "Ask");
         Group("Ask1monitor", "StrategyName", "MyStrategy");
         Group("Ask1monitor", "Symbol", Instrument1.Symbol);

And next
Code:
Log(ask, "Ask1monitor");

At result you will have group "Ask1" for charts and group "Ask1monitor" for strategy monitor.
And description of fields for Strategy Monitor:
Attachment:
StrategyMonitor.png
StrategyMonitor.png [ 18.23 KiB | Viewed 1377 times ]


Top
 Profile  
 
PostPosted: Fri Feb 03, 2017 6:36 am 
Offline

Joined: Thu Sep 17, 2015 5:52 am
Posts: 133
Great, that works, thanks.


Top
 Profile  
 
PostPosted: Tue Feb 07, 2017 5:55 am 
Offline

Joined: Thu Sep 17, 2015 5:52 am
Posts: 133
Hi Skuff,

I really like the feature you added that allows logging "without handling Group, directly by group name."

Can this feature be used in a strategy = Strategy type strategy  to plot bars from two separate instruments on the same chart?

I tried to do this via the code below but the chart showed up blank

Code:
    public class MyStrategy2 : Strategy
    {
        public Instrument Instrument1;
        public Instrument Instrument2;
        private BarSeries instrument1bars;
        private BarSeries instrument2bars;
      
        public MyStrategy2(Framework framework, string name)
            : base (framework, name)
        {
        }
        protected override void OnStrategyStart()
        {
         instrument1bars = new BarSeries();
         instrument2bars = new BarSeries();
         
         Group("instrument1bars", "Pad", 0);
         Group("instrument1bars", "StrategyName", "MyStrategy2");
         Group("instrument1bars", "SelectorKey", "Two Instruments");
         Group("instrument1bars", "Color", Color.Blue);
         
         Group("instrument2bars", "Pad", 0);
         Group("instrument2bars", "StrategyName", "MyStrategy2");
         Group("instrument2bars", "SelectorKey", "Two Instruments");
         Group("instrument2bars", "Color", Color.Green);
        }
      
      protected override void OnBar(Instrument instrument, Bar bar)
      {
         if (instrument == Instrument1
            && bar.Size == 60)
         {
            instrument1bars.Add(bar);
            Log(bar, "instrument1bars");
         }
         if (instrument == Instrument2
            && bar.Size == 60)
         {
            instrument2bars.Add(bar);
            Log(bar, "instrument2bars");    
         }
      }



Top
 Profile  
 
PostPosted: Wed Feb 08, 2017 4:11 pm 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 367
I think that Instrument1 and Instrument2 = null, check this first.
If you are setting them from scenario you should use attribute:
Code:
[Parameter]


Top
 Profile  
 
PostPosted: Wed Apr 05, 2017 8:30 pm 
Offline

Joined: Thu Sep 17, 2015 5:52 am
Posts: 133
Hi Skuvv, I checked and they are not Null. I also used the [Parameter] attribute in the scenario file and tried everything else I can think of. The chart is still showing up blank.
Would really appreciate if you could post a solution file that does this.


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