DEV Community

Cover image for I decided to use Extension Methods in my new cryptocurrency trading application
zakwillis
zakwillis

Posted on

I decided to use Extension Methods in my new cryptocurrency trading application

Hi, this is a really short post. I wouldn't call it an epiphany, but I decided to use extension methods to add filtering and business logic to my application. Of course I use extension methods, but it isn't about them per se.

Before going any further - this is not a high frequency trading application (HFT). I won't need to care about performance in the typical way we think of automated trading but I am certain .Net Core and clever data modelling will save the day.

I have been into cryptocurrency for 5 years plus - too short tbf, but in that time I have come up with a very simple trading strategy. Don't worry, am not intending to sell some scam on here (or anywhere). However, I know I can improve my manual trading strategy using my programming skills. After all, I have worked in finance on countless projects at a high level within the financial space.

Part of the reason many financial people aren't involved in cryptocurrency is because it is still a taboo topic.

You can read a high-level overview of what I am looking into here.

https://www.inforhino.co.uk/article/Discussion/Insights%20and%20Thoughts/Automated-Cryptocurrency-Trading-Engine-in-dotnet----08-Aug-2022

Normally, I would have created various classes, injected them in. One cool thing you can do is to create an IList and add a series of logic tests, configuring which test you wish to perform - for example.

IList<ILogicTest> logicTests = new List<ILogicTest>()
{{new ShouldTrade()},{new ShouldNeverTrade()},{new BuyLikeYouNeedANewHandbag()}};
Enter fullscreen mode Exit fullscreen mode

Now, my application will still do something like the above in certain parts. This is a great way to avoid breaking the single responsibility principle. I have written about this before. However, how many ShouldTrade classes will we ever need? Some will say, but perhaps we want a different implementation - well why not create those different implementations but encapsulate the use of the extension method within there?

People will say - who cares? Many don't like Extension Methods - certainly we get collisions within the same namespaces, we can't inject in, but for pure evaluation on primitive types - why not?

I think that what I am trying to achieve is a little more clarity and less clutter. I don't know if this will work, but it is worth a try if it helps make the codebase simpler.

There are those claiming we can't unit test extension methods of course - of course we can.

for those that want to see a bit of C#

If you find any bugs - please let me know. I can think of lots of issues, but without further ado.

namespace IR.AssetTrader.Analyser.Helpers
{
    public static class AssetPairRateHelper
    {

        static public IEnumerable<AssetPairRate> GetAssetPairRatesByAsset(this IEnumerable<AssetPairRate> assetPairRates, string CodeLong, string CodeShort)
        {
            return assetPairRates.Where(x => x.LongAsset.Code == CodeLong && x.ShortAsset.Code == CodeShort);

        }


        static public IEnumerable<AssetPairRate> GetAssetPairRatesByAssetDate(this IEnumerable<AssetPairRate>  assetPairRates, string CodeLong, string CodeShort, DateTime Start,DateTime End )
        {
            return assetPairRates.Where(x => x.LongAsset.Code == CodeLong && x.ShortAsset.Code == CodeShort && x.EventTMS >= Start && 
            x.EventTMS <= End);

        }
        static public IEnumerable<AssetPairRate> GetAssetPairRatesByAsset(this IEnumerable<AssetPairRate> assetPairRates, AssetPair assetPair)
        {
            return assetPairRates.Where(x => x.LongAsset.Code == assetPair.LongAsset.Code && x.ShortAsset.Code == assetPair.ShortAsset.Code );

        }
        static public IEnumerable<AssetPairRate> GetAssetPairRatesByAssetDate(this IEnumerable<AssetPairRate> assetPairRates, AssetPair assetPair, DateTime Start, DateTime End)
        {
            return assetPairRates.Where(x => x.LongAsset.Code == assetPair.LongAsset.Code && x.ShortAsset.Code == assetPair.ShortAsset.Code && x.EventTMS >= Start &&
            x.EventTMS <= End);

        }
        static public AssetPair GetAssetPairFromAssetPairRates(this IEnumerable<AssetPairRate> assetPairRates, AssetPair assetPair)
        {

            var firstAsset = assetPairRates.Where(x => x.LongAsset.Code == assetPair.LongAsset.Code && x.ShortAsset.Code == assetPair.ShortAsset.Code).First();

            return new AssetPair { LongAsset = firstAsset.LongAsset, ShortAsset = firstAsset.ShortAsset };

        }
        static public AssetPair GetAssetPairFromAssetPairRates(this IEnumerable<AssetPairRate> assetPairRates, string CodeLong, string CodeShort)
        {
            var firstAsset = assetPairRates.Where(x => x.LongAsset.Code == CodeLong && x.ShortAsset.Code == CodeShort).First();

            return new AssetPair { LongAsset = firstAsset.LongAsset, ShortAsset = firstAsset.ShortAsset };


        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
sharonlow profile image
Sharonlow

That's quite cool, but I'm looking for another trading platform where it's possible to trade. So can anybody recommend something?

Collapse
 
zakwillis profile image
zakwillis

Hi there. Didn't see this until just now. So I will probably do a few more articles on this. I can't recommend a platform although I use quite a few. The reason is people should be as decentralised as possible, spreading the risk. Trading is a risky affair too, my focus is more on accumulating more coins and lowering risk.

Collapse
 
zakwillis profile image
zakwillis

So far, it is making sense. I find myself using these methods in different places, and this removes clutter from injecting in logic. I wouldn't want a codebase littered with extension methods - but...

Collapse
 
calrielder profile image
CalRielder

If to be honest, I fully comprehend need of the previous person. Actually, I had an experience with trading cryptocurrencies. By the way, before I began doing tat, I have been looking for a reliable long time. There were a lot of doubts and I was afraid that it would not work. However, once my friend told me about the best stock trading platform where I had an opportunity to read reviews about different platforms where it's possible to invest money. Ive chosen one and my income increased. I wish you the same.

Collapse
 
zakwillis profile image
zakwillis

That link gets stuck on a capchta, sadly.