SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sat Aug 24, 2019 7:53 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Tue Dec 15, 2009 10:34 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Currently OQ does not provide an OnTradeSlice or OnQuoteSlice event hander to mimick the function of the existing OnBarSlice event handler.

This is not optimal because it prevents users from coordinating their strategy actions at the TradeSlice level, like they can do with OnBarSlice.

For example, I have N projects in 1 solution, and they all trade the same 1 instrument. Sometimes one strategy wants to buy quantity Qty1 contracts, while another one wants to sell a different quantity Qty2 of the same contracts. I need a method for combining the N strategy requests into one "net" buy or sell order to the broker.

The main advantages of an OnTradeSlice are that

(1) it would allow me to reduce my transaction commission costs, because I would only be trading the net quantity of contracts, instead of each quantity separately (and commission costs add up, as we all know...),

(2) it would allow me to avoid the error cases where an order for strategy A crosses an existing (outstanding) order sent from strategy B, and is therefore rejected by the broker execution provider (forcing me to write more error handling code... ugh)

(3) it would allow me to have a clear coordination point for coordinating all strategies after each new trade arrived and after all N strategies decided what to do with the trade.

(4) It would provide me with a coordination point where I could consider a final global position change action, depending on the values of all the "position change votes" sent in by the individual strategies. For example, I could reduce the total order sizes if margin limits were threatened, or could cut them all in half to actually trade at half the size of the sum of all the strategies, etc.

For example, after each new trade arrived, all strategies would decide their position changes, and register their orders with a global order manager object (that I would write). Then when an OnTradeSlice event occurred, it would guarantee that all strategies had placed their orders already, and one strategy could calculate a net position change and send it to the broker interface.

It seems to me that a similar OnQuoteSlice would also be useful and would also add to profit by reducing commission costs in a similar scenario. Indeed, it seems to be a required condition for coordinating (at the quote level) N strategies that trade the same Forex instrument, because Forex has no trades, and bars (even small ones) are often too slow.

If these new interfaces are not too difficult to provide (the OnBarSlice is a good existing model), maybe they could be added soon to an upcoming release (please :-)).

Otherwise there seems to be no good way to coordinate (at the trade or quote level) N strategies in 1 solution, for better strategy diversification.


Top
 Profile  
 
 Post subject: ++
PostPosted: Wed Dec 30, 2009 6:34 pm 
Offline

Joined: Wed Aug 27, 2003 2:43 am
Posts: 82
Location: Dallas
++

I found myself needing this functionality as well.

I wonder how this would affect performance.

Perhaps we could turn his feature on or off.

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 30, 2009 10:00 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
I doubt it would affect overall performance much -- it would amount to one event call on each quote/trade bar. Given the cpu work that indicators do, and that orders do, and all the memory allocations that are going on, it seems to me that OnTradeSlice and OnQuoteSlice would not add significant cpu.

Especially if their code handlers were not implemented in the user Strategy object...


Top
 Profile  
 
PostPosted: Wed Nov 30, 2011 5:23 pm 
Offline

Joined: Fri Sep 09, 2011 12:44 pm
Posts: 34
2 years passed, any chance we have onTradeSlice soon? :)


Top
 Profile  
 
PostPosted: Wed Nov 30, 2011 7:40 pm 
Offline

Joined: Fri Sep 09, 2011 12:44 pm
Posts: 34
BTW this is critical for pair trading users I think. It is nice we have the beautiful onBarSlice functionality described on a nice pair trading example, but without onTradeSlice it is still useless for real live pair trading :roll:, because one may want to exit the pair position on intraday basis (instead of EOD basis). So fresh prices needed for both legs anytime it changes, not only if new daily bar comes. Yep, one could use additional (like 1 mins) bars for this (maybe?) but this is not the same...onTradeSlice would do the job much better and for you it is just a hour of work :wink:


Top
 Profile  
 
PostPosted: Thu Dec 01, 2011 1:29 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
Perhaps this is just because we don't understand what TradeSlice really means...


Top
 Profile  
 
PostPosted: Thu Dec 01, 2011 2:28 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
I think to me OnTradeSlice would provide me with an execution access point -- coordinated between all strategies of course -- for each trade that came by in the input stream. In other words, it would give me a guaranteed way of chit-chatting between strategies, and updating my global state object after every trade that occurred. No danger of one strategy seeing 3 more trades into the future than the other strategies would see...


Top
 Profile  
 
PostPosted: Thu Dec 01, 2011 12:11 pm 
Offline

Joined: Fri Sep 09, 2011 12:44 pm
Posts: 34
Dr. Anton Fokin wrote:
Perhaps this is just because we don't understand what TradeSlice really means...


Same like what OnBarSlice is for OnBar, OnTradeSlice would be for OnTrade. So let's say you have strategy with two instruments - I1 and I2.
Let's say, I want to control the multi instrument strategy here just in the instance of the Strategy for the I1 (for instance, because I1 holds all kind of data). But I want to setup trading rules on prices of both I1 and I2 and in real time prices (using OnTrade or OnQuote).

Right now I cannot, because inside the Strategy instance linked to I1 I cannot get any event executed for I2 trade/quote changes, right?

Good example would be pair trading, which you use as the reference example for using onBarSlice.

In intra day pair trading, I need to trace realtime changes of ratio between two stock price. I can do it for instance in this way:

Code:
public override void OnTrade(Trade trade) {
    if (Instrument!=i1) return; // we do something only for the primary instrument
    double ratio=trade.Price/Instruments[i2].Trade.Price;
    Console.WriteLine("ratio: "+ratio);
    // ... process pair trading rules
}


Code above works, but it only catches new price data of the primary instrument, not of the secondary one. With OnTradeSlice I could receive prices of both instruments into single strategy instance.

Another strategy example could be index arbitrage strategy where I for instrance trade QQQQ based on prices of N stocks of the index. I also need to trace realtime prices of N stocks, but I maintain the strategy within the Strategy object linked to QQQQ only, right? I cannot do it because I cannot react there on realtime price changes in the whole stock pool.


Top
 Profile  
 
PostPosted: Thu Dec 01, 2011 12:21 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
It has nothing to do with OnXXSlice. There is no time "Slice" in case of trades or quotes. It can only be applied to time bars.

I think you are talking about a different strategy model where you don't have one startegy instance per instrument but one strategy instance for all instruments. I.e. OnBar/OnTrade/OnQuote handlers receive events for all instruments

OnTrade(Instrument instrument, Trade trade);

Correct?


Top
 Profile  
 
PostPosted: Thu Dec 01, 2011 3:56 pm 
Offline

Joined: Fri Sep 09, 2011 12:44 pm
Posts: 34
Dr. Anton Fokin wrote:
It has nothing to do with OnXXSlice. There is no time "Slice" in case of trades or quotes. It can only be applied to time bars.

I think you are talking about a different strategy model where you don't have one startegy instance per instrument but one strategy instance for all instruments. I.e. OnBar/OnTrade/OnQuote handlers receive events for all instruments

OnTrade(Instrument instrument, Trade trade);

Correct?


Yes, that would do the job too.


Top
 Profile  
 
PostPosted: Thu Dec 01, 2011 7:24 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Well, I think I would like to disagree to the general point in the previous post. To me, it has to do with execution points in each strategy instance -- synchronized so that every strategy instance is guaranteed an execution slice after every trade, before any of the other strategy instances gets to see the next trade. I don't think Anton's code example immediately above OnTrade (inst, trade) can guarantee what OnXXXSlice could guarantee.

Suppose you have N strategies reading the same input stream of trades. OnTrade in each strategy will see that each strategy sees every trade, but how can OnTrade provide a way for all strategies to have a synchronized chit chat with each other (or with a global data store) after each trade? OnTrade cannot.

In contrast, OnTradeSlice could make the guarantee. When each strategy received an OnTradeSlice event, it could be guaranteed that all strategies had seen the most recent trade, and NO strategy had yet seen the next trade in the input stream.

This way, the first OnTradeSlice event handler could gather up all the knowledge required from all the strategies, do whatever, and then set a flag so that all other OnTradeSlice events in all other strategies would simply return, because the global processing work had already been done for the most recent trade event.


Top
 Profile  
 
PostPosted: Fri Dec 02, 2011 11:07 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
This is something too complex I think and this is something which would only work in a completely synchronous world. In real world Strategy A can start processing a new trade while strategy B is still busy processing previous trade in OnTrade.

In principle I think you can organize this kind of logic yourself. Assume you have N strategies. Write a class with a static method that would trigger event when it gets N "I am done on this trade" calls from strategies.


Top
 Profile  
 
PostPosted: Fri Dec 02, 2011 11:20 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Ah yes, I see how anyone could do that. We could have our OnTrade handlers call the static method with the trade object, the static guy counts the incoming calls (he would have to know how many strategies were running), and then would eventually fire the MyTradeSlice event while passing the current incoming trade object. The corresponding event handlers in each of the strategies would then execute, safe in the knowledge that all strategies had seen this trade (but not necessarily had processed it).

Really nice idea, Anton. Now why couldn't I have imagined that solution...

And why couldn't I just do that with OnBarSlice? (Did you implement the existing OnBarSlice event this same way, with a static method of some kind?)

Thanks


Top
 Profile  
 
PostPosted: Fri Dec 02, 2011 11:46 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
OnBarSlice was introduced for a different purpose. Assume you ask BarFactory to emit time bars and now it's 10:00 and the BarFactory has two bars ready to be emitted for instruments 1 and 2. If it emits the bar for Instrument 1 first, then this instrument's OnBar is triggred but Instrument2.Bar (last bar of instrument 2) is not updated yet, although the bar for instrument 2 is ready.

So that you get wrong results if you calculate Instrument1.Bar.Close / Instrument2.Bar.Close in Instrument 1 OnBar. Thus you need to be notified when Instrument.Bar is updated for all instruments to do this kind of calculations. This is done with OnBarSlice.

Indeed OnBarSlice is emitted by the BarFactory itself. There is no special object with static methods.


Top
 Profile  
 
PostPosted: Fri Dec 02, 2011 6:30 pm 
Offline

Joined: Fri Sep 09, 2011 12:44 pm
Posts: 34
It seems my problem can be solved this way:
- I will store the main strategy instance into static Global hashtable under some code
- I put the trading logic into some public method - tradeLogic()
- when I receive onTrade in the secondary instance, I will just get the primary strategy instance from the hashtable and call tradeLogic() on the primary instance

So far seems to do the trick, I hope I won't find any issue with this approach.


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

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