How can I rewrite this block of if / Else if into a more structured, condensed and optimal way. (C#)

In C# .NET. This block of if/else if works fine, but its getting cumbersome to maintain and read. I want a more optimized writing style.


if (Globals.SRSI < 10 && Globals.SRSI != 0 && Globals.ATR14 != 0) //Low Stochastic Entry Long Strategy
{ 
    dStochRSIPriceLong = markPrice - (0.1 * Globals.ATR14); 
    ECStrategy = "Stochastic Long"; 
} 
else if (Globals.RSI < 20 && Globals.RSI != 0 && Globals.ATR14 != 0) //RSI Rebound Long Strategy
{ 
    dRSIPriceLong = markPrice - (0.2 * Globals.ATR14); 
    ECStrategy = "Rebound Long"; 
}
else if (Globals.MACDLINEPrevious < Globals.MACDSigLINEPrevious && Globals.MACDLINE > Globals.MACDSigLINE) //MACD Crossover Long Strategy
{
    dMACDCrossoverLong = markPrice - (0.2 * Globals.ATR14);
    ECStrategy = "Crossover Long";
}
else if (Math.Abs(markPrice - Globals.SMA25) > 300 && Globals.PreviousCandleLo > Globals.SMA25 && markPrice > Globals.SMA25 && Globals.SMA25 > Globals.SMA50 && Globals.SMA50 > Globals.SMA100) //Runaway Long Strategy
{
    dLoCandleLong = Globals.PreviousCandleLo;
    ECStrategy = "Runaway Long";
}                                                                            
else if (markPrice > Globals.SMA25 && Globals.SMA25 > Globals.SMA50 && Globals.SMA50 > Globals.SMA100) //Knife Long Strategy
{ 
    dMA2PriceLong = Globals.SMA25; 
    ECStrategy = "Knife Long"; 
}  
else if (markPrice > Globals.SMA50 && Globals.SMA50 > Globals.SMA100) //Moving Average Momentum Long Strategy
{ 
    dMAPriceLong = Globals.SMA50; 
    ECStrategy = "Momentum Long"; 
}                               
else if (markPrice > Globals.SMA100) //MA100 Breakout Long Strategy
{ 
    dPullbacksPriceLong = Globals.SMA100; 
    ECStrategy = "Breakout Long"; 
}                                                      
dPositionEntryPriceLong = SearchArray(markPrice, new List<double> { dStochRSIPriceLong, dRSIPriceLong, dMACDCrossoverLong, dLoCandleLong, dMA2PriceLong, dMAPriceLong, dPullbacksPriceLong });


Obviously there will still be if/else but I am hoping to not having this repetition. I tried to use a list with index but got lost in the complication of it all because I don’t know how I can list a condition and action and then look it up.

New contributor

Harold is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

You can use a list of conditions and corresponding actions to make this block of if/else statements more structured, condensed, and easier to maintain. This can be achieved by creating a list of tuples or a custom class to hold the condition and action and then iterating through this list. Here’s one way to do it using a custom class:

public class Strategy
{
    public Func<bool> Condition { get; set; }
    public Action Action { get; set; }
}

public void DetermineStrategy(double markPrice)
{
    double dStochRSIPriceLong = 0, dRSIPriceLong = 0, dMACDCrossoverLong = 0;
    double dLoCandleLong = 0, dMA2PriceLong = 0, dMAPriceLong = 0, dPullbacksPriceLong = 0;
    string ECStrategy = string.Empty;

    var strategies = new List<Strategy>
    {
        new Strategy
        {
            Condition = () => Globals.SRSI < 10 && Globals.SRSI != 0 && Globals.ATR14 != 0,
            Action = () => { dStochRSIPriceLong = markPrice - (0.1 * Globals.ATR14); ECStrategy = "Stochastic Long"; }
        },
        new Strategy
        {
            Condition = () => Globals.RSI < 20 && Globals.RSI != 0 && Globals.ATR14 != 0,
            Action = () => { dRSIPriceLong = markPrice - (0.2 * Globals.ATR14); ECStrategy = "Rebound Long"; }
        },
        new Strategy
        {
            Condition = () => Globals.MACDLINEPrevious < Globals.MACDSigLINEPrevious && Globals.MACDLINE > Globals.MACDSigLINE,
            Action = () => { dMACDCrossoverLong = markPrice - (0.2 * Globals.ATR14); ECStrategy = "Crossover Long"; }
        },
        new Strategy
        {
            Condition = () => Math.Abs(markPrice - Globals.SMA25) > 300 && Globals.PreviousCandleLo > Globals.SMA25 && markPrice > Globals.SMA25 && Globals.SMA25 > Globals.SMA50 && Globals.SMA50 > Globals.SMA100,
            Action = () => { dLoCandleLong = Globals.PreviousCandleLo; ECStrategy = "Runaway Long"; }
        },
        new Strategy
        {
            Condition = () => markPrice > Globals.SMA25 && Globals.SMA25 > Globals.SMA50 && Globals.SMA50 > Globals.SMA100,
            Action = () => { dMA2PriceLong = Globals.SMA25; ECStrategy = "Knife Long"; }
        },
        new Strategy
        {
            Condition = () => markPrice > Globals.SMA50 && Globals.SMA50 > Globals.SMA100,
            Action = () => { dMAPriceLong = Globals.SMA50; ECStrategy = "Momentum Long"; }
        },
        new Strategy
        {
            Condition = () => markPrice > Globals.SMA100,
            Action = () => { dPullbacksPriceLong = Globals.SMA100; ECStrategy = "Breakout Long"; }
        }
    };

    foreach (var strategy in strategies)
    {
        if (strategy.Condition())
        {
            strategy.Action();
            break;
        }
    }

    dPositionEntryPriceLong = SearchArray(markPrice, new List<double> { dStochRSIPriceLong, dRSIPriceLong, dMACDCrossoverLong, dLoCandleLong, dMA2PriceLong, dMAPriceLong, dPullbacksPriceLong });
}
  1. The Strategy class encapsulates a condition and an action.
  2. A list of Strategy objects is created, each holding a condition and
    the corresponding action.
  3. The DetermineStrategy method iterates through the list and executes
    the action of the first strategy whose condition is met.

This approach makes it easier to maintain and read the conditions and actions. You can add, remove, or modify strategies without changing the structure of the DetermineStrategy method.

New contributor

Khach is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

I’d suggest making a class for each “strategy”; looping through a list of all the strategies; and letting each one determine if it is applicable and if so contribute its result to a list of results. Then interpret the list, or assert it is size 1.

interface IStrategy{
    bool IsApplicable(Globals globals);
    Result result(Globals globals);
}

class StochasticLongStrategy: IStrategy{
    bool IsApplicable(Globals globals){
        return Globals.SRSI < 10 && Globals.SRSI != 0 && Globals.ATR14 != 0;
    }
    Result result(Globals globals){
        return Result(.....)
    }
}


List<IStrategy> strategies = {new StochasticLongStrategy(),..........}
List<Result> strategyResults = new();

foreach (IStrategy strategy in strategies) {
    if (strategy.IsApplicable(globals)) strategyResults.Add(strategy.Result(globals))
}

......interpret strategyResults here

You can follow this pattern to separate your “rules” from the logical flow of the application.

I didn’t rewrite all of your code because if you’re taking this approach to make your code more expressive and readable then what you name the functions is really important.

// strategies / functions / rules
Func<bool> lowStoEntryLongStrat = () => Globals.SRSI < 10 && Globals.SRSI != 0 && Globals.ATR14 != 0;
Func<bool> rsiREboundLongStrat = () => Globals.RSI < 20 && Globals.RSI != 0 && Globals.ATR14 != 0;

// procedure
if (lowStoEntryLongStrat()) //Low Stochastic Entry Long Strategy
{
    dStochRSIPriceLong = markPrice - (0.1 * Globals.ATR14);
    ECStrategy = "Stochastic Long";
}
else if (rsiREboundLongStrat()) //RSI Rebound Long Strategy
{
    dRSIPriceLong = markPrice - (0.2 * Globals.ATR14);
    ECStrategy = "Rebound Long";
}
// ...

You should also consider looking at F# or other functional languages to store your formulas and calculations in a more readable way.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật