Jump to content
Medved Trader Forums

high / low lines from specific point in time on intraday chart


Recommended Posts

Double Today11amHigh      = 0;
Double Today11amLow       = 0;
int Today11amCandleNumber = -1;
Double Prev11amHigh       = 0;
Double Prev11amLow        = 0;
int Prev11amCandleNumber  = -1;
int DayNumber             = -1;


public void MainCalculation()
{
    var today = TradingDay;
    if (today.DayNumber!=DayNumber)
    {
        Prev11amHigh         = Today11amHigh;
        Prev11amLow          = Today11amLow;
        Prev11amCandleNumber = Today11amCandleNumber;
        Today11amHigh        = 0;
        Today11amLow         = 0;
        DayNumber            = today.DayNumber;
    }
    
    if (Today11amHigh==0 && Timestamp>=today.SessionStart.AddHours(1.5))
    {
        Today11amHigh         = High;
        Today11amLow          = Low;
        Today11amCandleNumber = CandleNumber;
    }
    if (Prev11amHigh!=0)
    {
        var highColor = GetNamedColor("High", SysColor.Positive);
        SetLine(1, highColor, Prev11amCandleNumber,Prev11amHigh, 100000, Prev11amHigh, 1, "11H", LineTextPosition.RightAbove);
        var lowColor = GetNamedColor("Low", SysColor.Negative);
        SetLine(2, lowColor, Prev11amCandleNumber,Prev11amLow, 100000, Prev11amLow, 1, "11L", LineTextPosition.RightBelow);
    }
}
Link to comment
Share on other sites

Thanks again; working great.   One variation...if I wanted to get the high from yesterday SINCE the 11am bar; would I add a loop into the code to keep checking the 5 minute bars from the prior day; or is there any easier way?

for instance, change addhours(1.5) to a variable and loop in 5 minute increments checking if the high is higher or the low is lower?




			
		
Link to comment
Share on other sites

Ok - first of all, documentation - yes. Either press F1 in MT or go to https://www.medvedtrader.com/trader/WebHelp/  - it is a pretty comprehensive help file for MT in general.

All the available variables and MT specific functions are found here: https://www.medvedtrader.com/trader/WebHelp/advanced_-_all_available_funct.htm

CandleNumber is recent so it has not been compiled into the help yet, I will ask Jerry to do that and put it up

 

Link to comment
Share on other sites

As to your question how to do the high/low not AT 11am but since 11am - it is a fairly small change in the paintbar code..

Double Today11amHigh          = 0;
Double Today11amLow           = 0;
int Today11amCandleNumberHigh = -1;
int Today11amCandleNumberLow  = -1;
Double Prev11amHigh           = 0;
Double Prev11amLow            = 0;
int Prev11amCandleNumberHigh  = -1;
int Prev11amCandleNumberLow   = -1;
int DayNumber                 = -1;


public void MainCalculation()
{
    var today = TradingDay;
    if (today.DayNumber!=DayNumber)
    {
        Prev11amHigh             = Today11amHigh;
        Prev11amLow              = Today11amLow;
        Prev11amCandleNumberHigh = Today11amCandleNumberHigh;
        Prev11amCandleNumberLow  = Today11amCandleNumberLow;
        Today11amHigh            = 0;
        Today11amLow             = 0;
        DayNumber                = today.DayNumber;
    }
    
    if (Timestamp>=today.SessionStart.AddHours(1.5))
    {
        if (High>Today11amHigh)
        {
            Today11amHigh             = High;
            Today11amCandleNumberHigh = CandleNumber;
        }
            
        if (Today11amLow==0 || Low<Today11amLow)
        {
            Today11amLow              = Low;
            Today11amCandleNumberLow = CandleNumber;
        }
    }
    if (Prev11amHigh!=0)
    {
        var highColor = GetNamedColor("High", SysColor.Positive);
        SetLine(1, highColor, Prev11amCandleNumberHigh,Prev11amHigh, 100000, Prev11amHigh, 1, "11H", LineTextPosition.RightAbove);
    }
    if (Prev11amLow!=0)
    {
        var lowColor = GetNamedColor("Low", SysColor.Negative);
        SetLine(2, lowColor, Prev11amCandleNumberLow,Prev11amLow, 100000, Prev11amLow, 1, "11L", LineTextPosition.RightBelow);
    }
}


 

Link to comment
Share on other sites

image.png

As for your other questions - in general, it is pretty inefficient to use loops in paintbar code, because it executes so often.

So you have to take advantage of the fact that at start the paintbar code is called for every candle, and then it is called for the last candle every time it updates with incoming data. Take a look at the code I provided here and see if you can understand how it works.

Link to comment
Share on other sites

Hey Mike, a few questions:

- what are the parameters for setline.    Are they listed somewhere?  

- also, trying to understand the code... when you run this as a paintbar and it goes bar by bar - do the values/variables carry over from one bar to the next bar?   I previously used another product and they did not but the code seems to be written that way ?

Link to comment
Share on other sites

SetLine parameters - in the help file. I just gave it to Jerry he will put it up on the web later today.

The variables - I guess you have to know C# a little. Variables that are defined inside the function are scoped in the function - they get reinitialized every time the function is entered. Variables defined outside the function stay there between function calls.

There are also "State Variables" you can have in paintbars - that is a bit more complicated. Basically the state is saved and restored for the same-candle calls. The code above didn't need that but some things that need to be done (for example, EMA calculations) need that. The way to do that and why it should be done is explained in help as well:

https://www.medvedtrader.com/trader/WebHelp/state_keeping.htm

Basically you can think of the paintbar as a "state machine". When the MainCalculation is called, it is given a "state" and it generates the new "state" for the next candle. When the MainCalculation is entered for the same candle again (which is what happens when the paintbar is calculated on the newly incoming data) it is again given a "state" for the previous candle. Example: for EMA calculation, you always need the previous candle's EMA. So if you put that in the State, the new candle's EMA can always be calculated from the State's EMA - and the State's EMA will stay the same (and contain the previous candle's EMA) no matter how many times the MainCalculation is called for this candle. When it is called for the next candle, the saved State will contain the previous candle's state again.

CurrentState structure can contain only simple variables. If you want to do state-machine with the FIFOQueues (for things like averages, standard deviation calcs, or stuff like looking at what some calculated value was N candles ago, use the StateFIFOQueue classes, and do the Save/Restore in the for them in the appropriate functions in the code - again, look in Help for examples.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...