SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sun Dec 16, 2018 9:05 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Jul 04, 2018 12:56 pm 
Offline

Joined: Tue Oct 13, 2009 12:19 pm
Posts: 237
Dear Team,

I have a few strategies in OQ2014 which are all firing orders. And one point in time, I like to check all these orders and if certain things have happened. Each strategies order placing is in a independent thread.

When I foreach through framework.OrderManager.Orders, I some get excpetions, that the Collecition was modified.

What is your best bet on locking framework.OrderManager.Orders or maybe making a copy of framework.OrderManager.Orders (var oF = new List<Orders>(framework.OrderManager.Orders), so that it does have this exception when it gets modified from the other threads.

Thanks
Mike


Top
 Profile  
 
PostPosted: Fri Jul 06, 2018 10:00 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 443
Hello Mike,
Do you create threads in your code and why?
Common strategies are using single thread logic and ordermanager is threadsafe in this case.


Top
 Profile  
 
PostPosted: Fri Jul 06, 2018 10:07 am 
Offline

Joined: Tue Oct 13, 2009 12:19 pm
Posts: 237
yes, I create threads in my code.

I have 5 strategies, and on each OnBar Event, the last of the 5 strategies OnBar Event was maybe 2 sec after the first OnBar event. Since the other OnBar Event had all some complex processing logic. So, in PaperMode, I put the calculation into a new function, which is put into a new thread. Now, I will get each event fire within milliseconds... since the logic is processed in its own thread. These threads tough, place orders, so, the main thread, if he checks into: framework.OrderManager.Orders, which might change then.

Hope this helps.
Mike


Top
 Profile  
 
PostPosted: Fri Jul 06, 2018 12:04 pm 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 443
Ok, in case if you don't need orders from other strategies you can hold all orders in local List and use it locally.

In case when you plan to use orders from other strategies you should create thread-safe List/Dictionary and use it from all threads.
This thread-safe Dictionary can be placed in strategy.Global or as static field of strategy.
Each strategy should Add new orders to this Dictionary.

I think that copying orders from OrderManager can decrease performance of strategy so it is not good way.

Other way - iterating orders in OrderManager by OrderId.
In this case all you need is holding the latest value of OrderId in Global/Static.
And you can iterate like this:
for (int id = 0; id < latestOrderId; id++)
{
Order order = OrderManager.Get(id);
}
It will thread-safe


Top
 Profile  
 
PostPosted: Wed Dec 12, 2018 9:39 am 
Offline

Joined: Tue Oct 13, 2009 12:19 pm
Posts: 237
thanks for this so far.

Now, I came up with a different error with a similar setup:

To make use of the processing power, and be more efficient, the OnBar event of each strategy, I start a new thread called OnBarCalculations. In that function/thread, I calculate the values and if need, I send the order.
Now it can happen, that both of these threads running OnBarCalculations, come to the point, to place an order. Now, I had it a few times, that when this occurs at the identical time in each thread, that I get this error:
Code:
error in OnBarCalculations - entry Section: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Collections.Generic.List`1.Add(T item)
   at SmartQuant.OrderManager.alDTNHMUjj(Order  , Boolean  )
   at SmartQuant.OrderManager.Send(Order order)
   at SmartQuant.Strategy.Send(Order order)
   at OpenQuant.MyStrategyRefinitv.OnBarCalculations(Instrument instrument, Bar bar)

Can you do something on the OQ side, to make this possible, to make the OrderManager Concurrent?

What would you recommend in this setup?

Thanks
Mike


Top
 Profile  
 
PostPosted: Thu Dec 13, 2018 4:59 pm 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 443
We added some locks for OrderManager.
Not sure that it can be concurrent at this moment.


Top
 Profile  
 
PostPosted: Fri Dec 14, 2018 1:12 pm 
Offline

Joined: Tue Oct 13, 2009 12:19 pm
Posts: 237
thanks for that.

Hope you release a new version in the short term future.


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 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:  
Powered by phpBB® Forum Software © phpBB Group