# Need Help Getting Started on Hedge Algorithm

Hello All,

Hope this is not too out of bounds, but my name is Dave Somers and I am a beginner-intermediate Cypher developer & graph enthusiast from Las Vegas. I have a CS degree and am proficient in Python & C#, but am now adapting to this new language. I'm trying to write an algorithm to model hedging transactions. I know this algorithm can be done in other languages, but I am having great difficulty managing lists and coming up with a concise way to achieve this.

If possible, would anyone with solid Cypher experience be willing to help me properly structure some of these case & foreach statements to achieve my goal. Willing to pay for help as well or trade Poker Coaching for Cypher coaching, but at this point my learning curve is very steep for me to get this done. Thank you

Best,

Dave

Hello @somersdavid91

I would suggest to look at the official apoc or algorithm functions and procedures plugin for the Cypher language you might find what you need instead recreating the wheel again.

Sorry I don't have the link for them right now, but if you use Neo4j desktop they are in the plugin table for each dbms you select.

If not, I know Cypher by heart almost, maybe you can explain more about what's hedging transactions and how does it works?

Feel free to use arrows.app to

Thank you Tard, I've downloaded and have been looking for proper APOC functions but still coming up short; I believe my main issue is more of a lack of mastery of the syntax I believe, as I'm struggling to implement basic recursion..

As for the actual problem,
basically I need to search through a list of transactions: when I find a short transaction I need to find ample quantities of longs (if they exist) of the same security to see if there is a hedge and denote in the edge for which dates that trade is fully or partly hedged. Now one trade may be have 50 shares, 10 of which are hedged by 5 different transactions based on timing.
Another semi-critical question is whether or not this is efficient to do in graph. We believe so simply because the storing of the initial hedge relationship saves a lot of search time later down the algorithm.

My main problem right now is that I am unable to write a query to properly set up my lists:

So the pseudocode I have written is

Match (a: ShortTaxTrade) //for each short you find//
Match (b: LongTaxTrade) where a.AssetID = b.AssetID //find all of the Longs of the same asset//
with collect(b) as LongTrades //Ideally I would say while a.Quantity > 0 and then run this in loop//
foreach(b in LongTrades) //run through each eligible transaction one by one//
case
when a.Quantity > b.Quantity (create (b)-[x:Hedged_By]->(a) set a.Quantity = a.Quantity-b.Quantity, b.Quantity = 0, x.OpenHedgeDate = apoc.coll.min(a.OpenDate, b.OpenDate), x.CloseHedgeDate = apoc.coll.max(a.CloseDate, b.CloseDate), x.Quantity = a.Quantity //create relationship and put properties on edge//

else (create (a)-[x:Hedged_By]->(b) set a.Quantity = a.Quantity-b.Quantity, b.Quantity = 0, x.OpenHedgeDate = apoc.coll.min(a.OpenDate, b.OpenDate), x.CloseHedgeDate = apoc.coll.max(a.CloseDate, b.CloseDate), x.Quantity = b.Quantity;

This is actually only step one of the algo, but am having massive trouble even recurring to this point. Will continue to try to find a way, but any help is appreciated.