MicroTrendsLtd / NinjaTrader8

NinjaTrader8 Components Strategies and Trading tools
MIT License
72 stars 17 forks source link

locking patterns and avoiding them #18

Closed MicroTrendsTom closed 3 years ago

MicroTrendsTom commented 3 years ago

default -try keep fine grained Lock(objectLock) { }

Locking pattenrns with limited time... if(Monitor.TryEnter(objectLock, 250))//Don't wait more than 250ms { try { //do something } finally { Monitor.Exit(objectLock); } } else { //fallback code }

avoiding locking patterns allowing enumeration of a local list locking patterns var activeOrders= account.Orders.ToList().Where(o => o.Instrument == Instrument && Order..OrderIsWorking(o));

Now we can do what we want with acrtiveorders we got what it gave us at that point...
does this save locks and careful programming structure at a cost of creating new object and gc etc... but is that less than locks and the time to do it all?

i run this type of code with a system that tests over 1000 algos in parallel with 1000s of indicators running with out 1 hitch in 1 year i do use small fine grain locks such as

lock (account.Positions) position = account.Positions.FirstOrDefault(o => o.Instrument.FullName == Instrument.FullName);

jmscraig commented 3 years ago

Both test clients eventually locked up.

This one had a great run. image

"running with out 1 hitch in 1 year i do use small fine grain locks such as lock (account.Positions)"

Great!

I have been reading on this since I got up.

After trying to ferret out the object model I believe we should test

lock (Account.Orders) Account.CancelAllOrders(this.Instrument);

and if that does not work then capitulate and test.

lock (Account.All) Account.CancelAllOrders(this.Instrument);

MicroTrendsTom commented 3 years ago

A Question on locking basics my brains is down to one brain cell the other is on strike

public List OrdersActive { get { lock (ordersRT) return ordersRT; } }

so if we now do a lock somewhere else bool someFoo =false; lock(OrdersActive) { someFoo= OrdersActive.Count()>0; }

// is this not double locking... and is it needed...? is that not the same as the get lock? someFoo= OrdersActive.Count()>0;

Answered In Multi-threaded run: is it the double locking? .. No

The first lock expires on advent of the second curly bracket in the first block. The second lock is independent and required to prevent the first lock from taking place again while the second lock is in use.

An important question here that is a burden or a blessing is .. does the 'return ordersRT' return a copy? Blessing: If so (is a copy) that copy is not locked and independent. Use of it does not require locking if only one thread will use it at a time.

Burden: Copying is slow and loads up Garbage Collector work.

MicroTrendsTom commented 3 years ago

"running with out 1 hitch in 1 year i do use small fine grain locks such as BUT....... thinking about it the control center locks up and if you click on it, you get 2 verison of NT8 running in the process list... have to kill both so i have a scheduled tasks that checks for this every 1 minute and restarts it.. .it doenst happen a lot as i dont click on it during trading... user interaction seems to hang NT8 - so i let the algos run and really do not interfere

in its defence i am pulling loads and have implemented automation to a level never imagined by NT Picture removed due to NDA constraints - even though its mine - its exclusively provided to a small group of commercial clients only

this would take 8 hours to load unless it was done programmatically ;-) takes 15 to 20mins to proper start algos trading in realtime. not showboating but this a display of the power of decoupling it all and moving away from strategies as live trading tools for large scale true swing trading 100% unattanded even the server stops and starts and loads nt etc... the only thing i do is roll contracts and save workspaces - historical data for 65 futures contracts are replicated out from 1 main machine... each client only loads 1 day - but on a weekend cycle is will restart reboot NT8 and download 7. prior to the sunday open etc

so to me the role of a strategy is for backtesting -and maybe some realtime sunday to Friday attended interactive trading.. but when i got this beast strategies are only for testing hypothesis prior to running in large scale parrallel realtime tests.

So very much so we have great power in NT8 and we need not stay inside the box using strategies.....

jmscraig commented 3 years ago

Really amazing work.

Creation of something like this take years of constant thinking and hours and hours of market data analysis, experimentation and heads down coding.

Truly an impressive feat!

From decades of mission-critical system design and management I know how hard this was to pull off successfully and how much more work it is than would appear on the surface. Truly an impressive set of capabilities.


You have a really big head start on me here. I did not start focusing directly on this work until I exited my previous career via early retirement ( from Software and Systems Engineering, Enterprise Architecture, Advanced Analytics and Data Management at a Fortune 100 company).

Your work confirms a longer term vision I have been evolving, and doing deep technical research but have not yet started coding.

Develop the the real-time operational core independent of any strong dependencies NT (or any specific ) and above that in a modular (as loosely coupled as I can get it) layer on NT, Machine Learning, AI Learning and maybe other vendors products as they surface

Using each of those other components and vendor products just where they add tremendous value, avoiding areas of constraint and weakness.

But the core is fully independent, reliable, redundant, fast ... all the other great adjectives.

MicroTrendsTom commented 3 years ago

https://github.com/MicroTrendsLtd/NinjaTrader8/issues/27#issue-759740156