SmartQuant Discussion

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

All times are UTC + 3 hours




Post new topic Reply to topic  [ 35 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: Tue Mar 25, 2008 5:07 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
OpenQuant runs one instance of a strategy per instrument. This concept simplifies strategy development a lot since you always refer to one instrument (Strategy.Instrument) in your code and don't need to maintain arrays and tables holding different values (for example indicator) per instrument. It works pretty well for instrument independent staregies, i.e. for strategies that can run with one instrument, for example trend following or swing trading strategies, but you need to utilize certain techniques if you want to develop strategies that depend on two or more instruments, for example spread, pair trading. market neutral or correlation based strategies.

Assume we want to develop a strategy that buys qty of MSFT and sells qty of AAPL if MSFT / AAPL price ratio becomes larger that certain number X.

Code:
OnBar(Bar bar)
{
  Instrument instrument = Instruments["MSFT"];

  if (nstrument.Bar.Close / bar.Close > X)
  {
    Buy(qty);
    Sell(instrument, qty);
  }
}

You can also define instruments in the header of your strategy to speed things up

Code:
Instrument instrument1 = InstrumentManager.Instruments["MSFT"];
Instrument instrument2 = InstrumentManager.Instruments["AAPL"];

OnQuote(Quote quote)
{
  if (Instrument == instrument1)
    if (quote.Ask > instrument2.Quote.Bid * delta)
    {
       Buy(instrument1, qty);
       Sell(instrument2, qty);
     }
}

Note that you can also use OnBarSlice event handler.

Quote:
OnBarSlice fires whenever the market data provider BarFactory has finished emitting new bars for all the instruments in the current portfolio. BarFactory emits new bars sequentially when multiple instruments are being traded—first a bar for instrument 1, then a bar for instrument 2, and so on. After all individual bars have been emitted, BarFactory will fire OnBarSlice to tell strategy components that the end of the instrument list has been reached, and that all new bars for the current bar interval have been emitted. OnBarSlice solves the problem of strategy code not knowing when all the instrument bars for a correlation strategy are present and ready to be used. For example, if you try to do correlation calculations in the OnBar event handler for multiple instruments, your code will have to keep track of which instruments have been seen in the OnBar handler for the current bar time interval.


Code:
using System;
using System.Drawing;

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

public class MyStrategy : Strategy
{
   Instrument DELL;
   Instrument CSCO;

   TimeSeries spread_series;

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

   [Parameter("Length of SMA")]
   int SMALength = 20;

   [Parameter("Order of BBU")]
   double BBUOrder = 2;

   [Parameter("Order of BBL")]
   double BBLOrder = 2;

   // indicators
   BBU bbu;
   BBL bbl;
   SMA sma;

   public override void OnStrategyStart()
   {
      DELL = Instruments["DELL"];
      CSCO = Instruments["CSCO"];

      spread_series = new TimeSeries("DELL - CSCO", Color.Pink);

      // set up the moving averages
      sma = new SMA(spread_series, SMALength);
      sma.Color = Color.Yellow;
      Draw(sma, 2);
      // set up bollinger bands
      bbu = new BBU(spread_series, SMALength, BBUOrder);
      bbu.Color = Color.Green;
      bbl = new BBL(spread_series, SMALength, BBLOrder);
      bbl.Color = Color.Green;
      Draw(bbu, 2);
      Draw(bbl, 2);
      Draw(spread_series, 2);
   }

   public override void OnBarSlice(long size)
   {
      if (Instrument == CSCO)
      {
         double spread = CSCO.Bar.Close / DELL.Bar.Close;
         spread_series.Add(CSCO.Bar.DateTime, spread);
         
         if (bbl.Count == 0)
            return;
         
         if (!HasPosition)
         {
            if (spread < bbl.Last)
            {
               Buy(CSCO, Qty);
               Sell(DELL, Qty);
            }
         }
         else
         {
            if (spread > bbu.Last)
            {
               Sell(CSCO, Qty);
               Buy(DELL, Qty);
            }
         }
      } 
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2008 7:28 pm 
Offline

Joined: Thu Nov 06, 2008 3:13 pm
Posts: 8
Hi Anton,

Just started looking at OQ today. I like the fact you suppost multi instrument strategies. As part of that do you support Scatter plots?
ie plots where the y axis is one instrument and the x axis is another (rather than time).

As an example I would reffer you to the scatter plot functinality in excel which in addition to plots also provides regression (linear, poly and exponential) capabilities. It's an invaluable tool to the spread trader, please tell me you have this. :)

Thanks so much


Top
 Profile  
 
PostPosted: Mon Feb 02, 2009 11:10 am 
Offline

Joined: Thu Jan 15, 2009 1:25 pm
Posts: 14
I copied and paste the last code (the one with OnBarSlice event handler) and ran it. But the 3rd chart pad is blank (see attachement). Why the SMA, BBU, BBL, and spread were not shown? I'm niebie. Please forgive me if this question is too simple.

Thanks a lot!


Attachments:
File comment: strategy chart
pairtrade.JPG
pairtrade.JPG [ 70.32 KiB | Viewed 19848 times ]

_________________
Jack Lenard
Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2009 2:27 pm 
Offline
Site Admin

Joined: Thu Jul 17, 2003 10:39 am
Posts: 1478
Hi,

The reason is SMA,BBU and BBL are calculated from 'spread_series', but this series is empty for DELL and is populated in a case of CSCO only.
Please, look into code...
Code:
if (Instrument == CSCO)
{
     double spread = CSCO.Bar.Close / DELL.Bar.Close;
     spread_series.Add(CSCO.Bar.DateTime, spread);



Regards,
Alex

_________________
SmartQuant Development Team


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2009 2:33 pm 
Offline
Site Admin

Joined: Thu Jul 17, 2003 10:39 am
Posts: 1478
...


Attachments:
spread_chart.gif
spread_chart.gif [ 60.63 KiB | Viewed 19838 times ]

_________________
SmartQuant Development Team
Top
 Profile  
 
PostPosted: Sun Feb 22, 2009 4:40 pm 
Offline

Joined: Thu Jan 15, 2009 1:25 pm
Posts: 14
Is it possible to draw both stocks' candlestick charts together? Of course they can be into different panes.

_________________
Jack Lenard


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2009 9:56 pm 
Offline

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

in principle yes, but on a separate canvas that you invoke from the startegy code.

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 22, 2009 11:48 am 
Offline

Joined: Fri Apr 17, 2009 6:24 am
Posts: 21
then how to do that? to draw the all instruments bar on the separate chart
if i use GetBars what happen is they draw 2 different instruments on the same chart

public override void OnStrategyStart()
{
KOSPI = Instruments["KOSPI001"];
BarsKospi = new BarSeries("KOSPI001");
BarsKospi = GetBars(KOSPI,60);

ES = Instruments["ES001"];
BarsEs = new BarSeries("ES001");
BarsEs = GetBars(ES,60);
}

public override void OnBar(Bar bar)
{
if (Instrument == ES)
{
BarsEs.Add(bar);
}

if (Instrument == KOSPI)
{
BarsKospi.Add(bar);
}
}

any help is appreciated ^^

Regards,
lg4to32


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 19, 2009 7:59 pm 
Offline

Joined: Tue May 19, 2009 6:38 pm
Posts: 6
I've been using my trial time to see if I can work a pairs model...

Dr. Anton Fokin wrote:
Hi,

in principle yes, but on a separate canvas that you invoke from the startegy code.

Regards,
Anton


I got some of it going but I am having a hard time working the Charts and Canvas. So far I have the indicators I wants but the Bar chart at the top always has the first symbol only. Can I overlay to bar charts together and see prices of both instruments on the same chart. I would need an example or something I guess.

I tried the same approach as lg4to32 but it didn't work.


Top
 Profile  
 
PostPosted: Fri May 22, 2009 5:03 pm 
Offline

Joined: Thu Aug 21, 2008 5:16 am
Posts: 46
Anton,
I think the coding of this strategy has the same problem as the one I have on my market breadth indicator. How do we know the spread between "ES" and "GOOG" is calculated on the same bar?

Am I wrong on this? Please clarify. Thanks!

Tom


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 24, 2009 10:09 am 
Offline

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

you should calculate the spread in OnBarSlice event handler then.

Regards,
Anton


Top
 Profile  
 
 Post subject: Frame Work Problem?
PostPosted: Fri May 29, 2009 2:24 pm 
Offline

Joined: Thu Jan 15, 2009 1:25 pm
Posts: 14
It seems that for each instrument in a strategy, there is a separate of states that can not talk to the other clones related to other instruments. Then I don't know how to develop a strategy that involves multiple instruments and several orders that will be placed one by one. Following code won't work:
Code:
...
public class MyStrategy : Strategy
{
  int state=0;
//...

    public override void OnOrderFilled(Order order)
    {
        switch (state)
        {
            case 0:   //1st order is filled
                state = 1;
                //sth to do after the 1st order is placed;
                //...;
                break;
            case 1:   //2nd order is filled,
                state = 2;
                 //sth to do after the 2nd order is placed;
               //...;
                break;
            case 2:   //3rd order is filled,
                state = 2;
                 //sth to do after the 3rd order is placed;
            }
 }


if the 2nd order is on an instrument (instrument2) different from that (instrument1) in the 1st order, the member variable "state" will stay the original value 0 instead of the new value 1. actually there is a "state" with the new value 1, after the 1st order is filled. But that variable is in the variable set related to the instrument1 and can not be seen in this event "OnOrderFilled", because this is the event for instrument2, which has a completely different set of variables.

Is there any way to let the codes aware of something related to another instrument happened?

_________________
Jack Lenard


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 29, 2009 7:45 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
There is one instance of Strategy class for each instrument. The only way to pass data from one instance to another is to use static variables.

Regards,
Anton


Top
 Profile  
 
PostPosted: Sun Jul 05, 2009 1:12 pm 
Offline

Joined: Sat Jul 04, 2009 3:58 am
Posts: 10
For ex. we have two instruments with irregularities in bar series (liquid EURUSD and illiquid forex pair NOKEUR; sometimes we do not have bar for NOKEUR pair at the same time we always have one for EURUSD):

time\EURUSD OHLC bar\NOKEUR OHLC bar\ OnBarSlice event
10:00 \ yes \ yes \ yes
10:01\ yes \yes \ yes
10:02 \yes \ NO \ NO
10:03 \yes \yes \ yes

is this correct model "model" of onBarSlice event behavior?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 05, 2009 2:33 pm 
Offline

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

we will discuss it with Sergey on Monday.

Regards,
Anton


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