Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simpler Program

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Simpler Program

    Hi Guys, I have this code that I'd like to make more efficient. Basically I have 20 instruments referencing the same piece of code. Here is an example for 2 instruments.

    PHP Code:
    if (TFTrendTypes.Balanced == TFtrendType)
        {        
                //b. Value area % at bottom        
                    
                if (DrawObjects["VH"] != null&&Close[0]>lineValhigh1)
                {
                graphics.DrawRectangle(new Pen(Color.White),  bounds.X +(bounds.Width/2), bounds.Bottom-50, 100, 40);                
                graphics.FillRectangle(new SolidBrush(Color.FromArgb(150,Color.Green.R,Color.Green.G,Color.Green.B)), bounds.X +(bounds.Width/2)+1, bounds.Bottom - 49, 99, 39);                            
                graphics.DrawString(abovevalue.ToString("N0")+"%", textFontHuge, textBrushStrong, bounds.X +(bounds.Width/2)-3, bounds.Bottom - 50, stringFormatCenter);        
                }    
                else if (DrawObjects["VH"] != null&&Close[0]<lineVallow1)
                {
                graphics.DrawRectangle(new Pen(Color.White),  bounds.X +(bounds.Width/2), bounds.Bottom-50, 100, 40);    
                graphics.FillRectangle(new SolidBrush(Color.FromArgb(150,Color.Red.R,Color.Red.G,Color.Red.B)), bounds.X +(bounds.Width/2)+1, bounds.Bottom - 49, 99, 39);                        
                graphics.DrawString(belowvalue.ToString("N0")+"%", textFontHuge, textBrushWeak, bounds.X +(bounds.Width/2)-3, bounds.Bottom - 50, stringFormatCenter);
                }
                
                else if (DrawObjects["VH"] != null&&Close[0]>=lineVallow1 && Close[0]<=lineValhigh1)
                    {                
                graphics.DrawRectangle(new Pen(Color.White),  bounds.X +(bounds.Width/2), bounds.Bottom-50, 100, 40);    
                graphics.FillRectangle(new SolidBrush(Color.FromArgb(250,Color.Yellow.R,Color.Yellow.G,Color.Yellow.B)), bounds.X +(bounds.Width/2)+1, bounds.Bottom - 49, 99, 39);                        
                graphics.DrawString(balancedvalue.ToString("N0")+"%", textFontHuge, textBrushNormal, bounds.X +(bounds.Width/2)-3, bounds.Bottom - 50, stringFormatCenter);
                    }                
        }        
                
        if (ESTrendTypes.Balanced == EStrendType)
        {        
                //b. Value area % at bottom        
                    
                if (DrawObjects["VH"] != null&&Close[0]>lineValhigh1)
                {
                graphics.DrawRectangle(new Pen(Color.White),  bounds.X +(bounds.Width/2), bounds.Bottom-50, 100, 40);                
                graphics.FillRectangle(new SolidBrush(Color.FromArgb(150,Color.Green.R,Color.Green.G,Color.Green.B)), bounds.X +(bounds.Width/2)+1, bounds.Bottom - 49, 99, 39);                            
                graphics.DrawString(abovevalue.ToString("N0")+"%", textFontHuge, textBrushStrong, bounds.X +(bounds.Width/2)-3, bounds.Bottom - 50, stringFormatCenter);        
                }    
                else if (DrawObjects["VH"] != null&&Close[0]<lineVallow1)
                {
                graphics.DrawRectangle(new Pen(Color.White),  bounds.X +(bounds.Width/2), bounds.Bottom-50, 100, 40);    
                graphics.FillRectangle(new SolidBrush(Color.FromArgb(150,Color.Red.R,Color.Red.G,Color.Red.B)), bounds.X +(bounds.Width/2)+1, bounds.Bottom - 49, 99, 39);                        
                graphics.DrawString(belowvalue.ToString("N0")+"%", textFontHuge, textBrushWeak, bounds.X +(bounds.Width/2)-3, bounds.Bottom - 50, stringFormatCenter);
                }
                
                else if (DrawObjects["VH"] != null&&Close[0]>=lineVallow1 && Close[0]<=lineValhigh1)
                    {                
                graphics.DrawRectangle(new Pen(Color.White),  bounds.X +(bounds.Width/2), bounds.Bottom-50, 100, 40);    
                graphics.FillRectangle(new SolidBrush(Color.FromArgb(250,Color.Yellow.R,Color.Yellow.G,Color.Yellow.B)), bounds.X +(bounds.Width/2)+1, bounds.Bottom - 49, 99, 39);                        
                graphics.DrawString(balancedvalue.ToString("N0")+"%", textFontHuge, textBrushNormal, bounds.X +(bounds.Width/2)-3, bounds.Bottom - 50, stringFormatCenter);
                    }                
        } 
    
    Instead of doing this 20 times I'd like to say :

    if (TFTrendTypes.Balanced == TFtrendType)

    reference that piece of code.

    if (TFTrendTypes.Balanced == TFtrendType)

    reference that same piece of code. So basically I only have the Value area % at bottom code put in once instead of 20 times and referenced each time. How would I do this?


    Thanks
    DJ

    #2
    DJ, to make this code better readable I think you could look into creating a custom method for the redundant operations and then only call this as needed - http://www.ninjatrader.com/support/h...s_explaine.htm

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    651 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    370 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    109 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    574 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    577 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X