Replies: 10 comments
-
What we proved over the last 10 days was, list locking is not required generally speaking unless we are going to enumerate a list - while its been adding to. And if we do we can use finegrain techniques etc and you bool example is a good one. things to consider Solution Implemented thus far
Warnings of NT8 and Concurrent objects Conclusion this end
Conclusion of the conclusion: Yes Im in favor to R&D and test it out and compare one versus the other :-) Game Plan Ideas What game plan would make it an easy transition?
bool IsOrderEntryAnyActive Order OrderStoploss1 Order OrderTarget1 i think 1 dictionary which contains the structure for each trade is a potential but what of ad hoc user trades.
But the changes are now not so simple and much broader. i think if we think in terms of conccurency Lock or not to Lock There is better Trade Engine design i can think of methods where the orders submission need not live in a list at all - its just legacy migrated so within context of this engine what is worth changing and why and what is the impact etc Compromise
So a small change and experiment as surely we are both itching to know the difference and abilities |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
on that basis i have added this - public bool IsOrdeActiveConcDict - to control if we use this or the list [XmlIgnore] Instanstiate in Transition if (OrdersActiveConcDict.TryAdd(order.Id,order)) |
Beta Was this translation helpful? Give feedback.
-
This is way more fun than my job in hand will have to come back to this later on :-) Initial testing shows its a go :-) |
Beta Was this translation helpful? Give feedback.
-
Yikes! And to add more. What may be the most difficult aspect of Multi-Threading is NT does not tell us what objects they are locking on under the hood so when we execute API calls. If we can not lock on the same it is just a matter of time before exception or deadlock. In my fastest strategies I see this with calls to CBI objects. I figure its a good idea to follow NT's lead on use of ConDict. I have not yet found the original but these code was said to be against the pattern of how NT uses ConDict in SuperDom for adding a Volume column. pattern
"1) lock on the NT API element" is why I am liking the "out of band" "other thread" or soft lock approach I wrote for CancelAllOrders() approach to avoid slow downs, hangs and deadlocks and ensure the tick by tick order management.
Agreed. But then we are wholly dependent on API calls to get order Names & Status, open order quantity quite frequently and .... (see note above "What may be.. ").
I think there is power in tallying in and out to keep a short constant set of integers that describe ExpectedStrategyPosition Then for me it's
Yes. Small and simple while leaving existing code there just commented for easy rollback. Stage in..
Awesome! |
Beta Was this translation helpful? Give feedback.
-
just an FYI.. I read that public objects have additional layers of referencing for each call.. more complexity and slower. the reference here recommends the public properties does not host the list but calls an internal list. Internal calls call the internal list. Oh, .. might have lost that URL. Found a few references..
public IEnumerable GetFoo() If so public IsOrdeActiveConcDict might look something like private ConcurrentDictionary<string, Orders> ordersActiveConcDict; [XmlIgnore]
The easier path to start faster to Go//No Go test / assess concept viability and reliability might just be to start with the internal Dict. |
Beta Was this translation helpful? Give feedback.
-
Excellent call i was in a hurry now we know it is a compatible object we can whitebox the code and optimise it etc "I have low confidence this bit is accurate "IEnumerable<string, Order>"." Added to backlog: #31 (comment) |
Beta Was this translation helpful? Give feedback.
-
I think there is power in tallying in and out to keep a short constant set of integers that describe ExpectedStrategyPosition This does depend a lot on the layer putting on the orders
|
Beta Was this translation helpful? Give feedback.
-
To me the only advantage for the concDict thus far is enumerations without an explicit copy Soif there are still caveats to ConDict as opposed to the usage of a list with the correct logic to avoid problems then lists are fine We really dont care if a list changes as its read - within most contexts add/remove/contains but if concDicts are better then the list implementation for overhead and ease of use to prevent issues... |
Beta Was this translation helpful? Give feedback.
-
Good posts. All the whole you have had less trouble than I have with Lists in NT8 and that is a good thing. ConDict is slower than a List at Contains but faster and better (you are ensured one unique value when a list can have dupes) when you use TryGetValue rather than Contains. if (stopLossOrders.TryGetValue("Unique Stp order name", out Order)) |
Beta Was this translation helpful? Give feedback.
-
rom all the reading multiple threads even reading the same list with "contains" or even a .ToArray() call is like buying a lottery ticket.. best case it will happen rarely but eventual some hits
Over time (hopefully sooner than later) I recommend the replacement of Lists used outside a single thread be replaced with ConcurrentDictionaries.
ConcurrentDictionaries 200-400% slower to load each row than a list, but provide lock like security for read with no identifiable locks or locking if your the reader.
We read so much more than we write so the ConDict might be a massive win.
fyi .. .example use of ConcurrentDictionary in OnMarketData()
`
uint ui_volume ;
lock (e.Instrument.SyncMarketData)
{
ui_volume = (uint)e.Volume ;
_cdictionary.AddOrUpdate(e.Price, ui_volume, (price, oldVolume) => oldVolume + ui_volume);
}
`
NT Jim posted a good example use of Concurrent dictionary more complex than replacing the lists.. Using a data Class to add multiple items per dictionary row.
https://ninjatrader.com/support/forum/forum/ninjatrader-8/strategy-development/102909-imbalance-strategy?p=803902#post803902
For as fast as we add and remove orders manage order details in classes stuffed into dictionary would slow things down a lot and generates a lot heap GC pressure so not encouraged unless really needed but proves the ConDict is viable for far more than is needed here.
Rather, use of a number of ConcurrentDictionaries(, ) very similar as to how we use lists today would be very fast and simple to write and maintain.
just replacing the list looks much simpler than NT Jim's example..
much more like example from dotnetpearls
using System;
using System.Collections.Concurrent;
class Program
{
static void Main()
{
// New instance.
var con = new ConcurrentDictionary<string, int>();
con.TryAdd("cat", 1);
con.TryAdd("dog", 2);
}
Manage a simple ConDict(, ) for StopLoss Orders
private ConcurrentDictionary<string, Orders> activeOrders;
private ConcurrentDictionary<string, Orders>entryOrders;
private ConcurrentDictionary<string, Orders> stopLossOrders;
private ConcurrentDictionary<string, Orders> profitTargetOrders;
else if (State == State.DataLoaded)
{
stopLossOrders = new ConcurrentDictionary<<string, Order>>();
}
OBU(), OMD(),..
stopLossOrders.TryAdd("Unique Stp order name", "Working");
// Contains is slower, TryGetValue is awesome
if (!stopLossOrders.ContainsKey("Unique Stp order name"))
{
return;}
}
if (stopLossOrders.TryGetValue("Unique Stp order name", out Order))
{
do something
}
protected void CancelStopLossOrders(bool return)
{
if(stopLossOrders.Count > 0)
{
// No Lock needed... it is a ConcurrentDictionary. Yea!
foreach (Order o stopLossOrders.Keys)
{
If( IsOrderActiveCanCancel(o) )
CancelOrder(o);
stopLossOrders.Clear();
}
return true or false for success or fail ;
}
Order o;
stopLossOrderStatus.TryRemove("Unique Stp order name", out o )
TryRemove(searchKey, out CityInfo retrievedValue)
stopLossOrders.Clear()
a wee bit later... use ConDicts to provide Scalability for people to many low maintenance trading plans ...
// In this use case where the updates are less frequent use of a data class in a Dict is great.
public class StopLosslProftiTargetExitPlans
{
public double stopLoss1Distance = 0.0;
public double profitTarget1Distance = 0.0;
etc.
}
private ConcurrentDictionary<Key(int or string), StopLosslProftiTargetExitPlans> exitPlans;
else if (State == State.DataLoaded)
{
exitPlans = new ConcurrentDictionary<Key(int or string), StopLosslProftiTargetExitPlans>();
}
Beta Was this translation helpful? Give feedback.
All reactions