Jump to content
Medved Trader Forums
Sign in to follow this  
Jason

Intermittent functionality with simple paintbar.

Recommended Posts

Sorry, not sure why this is happening but I often submit a post that ends up empty somehow which forces me to rewrite everything.

Okay, so because I need to use it for the paintbars I want to create I went back and tested SymbolData.High as I have encountered unexpected results in using it previously. Using the simple code below I am able to highlight the candle where the high of the day occurs for the symbol ONCE but cannot get it to work on any of the other symbols I've tested. I've tried it on TWLO, Z, HTZ, WTW, DF... I have tried it with and without state keeping, just in case, and cannot get it to work reliably. I don't know why it would matter but I've tested this on a 5 minute chart in after hourse trading. Also, the code has been the same across all tests and there are not any other indicators on the chart.

 




public void MainCalculation()
{
    if ( High == SymbolData.High )
    {
        SetColor(SysColor.Positive);
    }

}

 

Edited by Jason
Code wouldn't show up.

Share this post


Link to post
Share on other sites

Ok, it is a bit of a glitch, but - the SymbolData values are stored as Double in MT, and the Candle values are stored as Float (because they take half the space).

So I would suggest doing

if (Math.Abs(High-SymbolData.High)<0.00001)

 

 

 

 

 

Share this post


Link to post
Share on other sites

Well, I have tried making this change but am still getting very inconsistent results. On many symbols it works, but on others it doesn't. It works, for example, on ROKU, YELP, SQ, but not on XRAY, GOOGL or MCHP. I have tried using:

if ( High == (float)SymbolData.High )

which fixes the problem for GOOGL but not the others.

Here is the code as it stands now:

public void MainCalculation() { if ( Math.Abs(High-SymbolData.High)<0.00001 ) { SetColor(SysColor.Positive); } }

Share this post


Link to post
Share on other sites

Okay, I think I've figured out what's happening. When I use SymbolData.High it's pulling the high of the day as stored from all market activity that occurred from both before the opening bell and after the closing bell. Technically this makes sense, however, my intent is to test if the high of the day throughout the period from 9:30 AM to 4:00 PM is the same as the high of the first 5 minute candlestick. Is there an alternative to SymbolData.High which would present a better comparison to achieve this?

Share this post


Link to post
Share on other sites

Sure - you can calculate the high yourself instead of using the SymbolData.High. You're already checking the high of the first five minutes, so it is similar code.

1. Use Advanced mode.

2. In CurrentState, define

int LastDayNumber;

Float DayHigh;

3. in PaintbarClearState, set LastDayNumber to -1

4. 

var SessionInformation = GetTradingSessionInfo(Timestamp);   

if (SessionInformation.DayNumber != LastDayNumber)
{
    CurrentState.LastDayNumber = SessionInformation.DayNumber;

    CurrentState.DayHigh=High;

}

CurrentState.DayHigh = Math.Max(CurrentState.DayHigh, High);

if (High == CurrentState.DayHigh)

{

.....

}
        

Note: this will trigger at any time the high is the same as the daily high SO FAR in the session.

 

Share this post


Link to post
Share on other sites

Okay, thanks because that worked pretty nicely. It does trigger on the chart whenever a new high is reached but I have developed it into the code I have below and am encountering a new issue. I have tested and confirmed that the code below using just the comparison is able to highlight the first candlestick of the day when the high of that candlestick is equal to the high of the day. Now need to use further conditions to narrow down which candlesticks get highlighted given that the two are equivalent. Given that the high of the first candlestick is equal to the high of the day, I need the paintbar to highlight the candlestick that crosses up over an indicator like an EMA. When I eliminate the first requirement I can get it to highlight cross-overs and other conditions but as soon as I implement the requirement for the highs to be the same the paintbar doesn't display anything on the chart. I have tried compounding both conditions with "&&" in one line and separating them out into nested IF statements to no avail. Here is the code:

public void MainCalculation()
{
    var SessionInformation = GetTradingSessionInfo(Timestamp);

    if (SessionInformation.DayNumber != CurrentState.LastDayNumber)
    {
        CurrentState.LastDayNumber = SessionInformation.DayNumber;
        CurrentState.DayHigh       = High;
    }

    CurrentState.DayHigh = Math.Max(CurrentState.DayHigh, High);
    
    if (Timestamp <= SessionInformation.SessionStart.AddMinutes(5))
        {
            CurrentState.FirstCandleHigh = High;
        }
    
        if ((CurrentState.FirstCandleHigh == CurrentState.DayHigh))
        {
            if (Close.CrossesUp(EMA_10, 0))
            {
                    SetColor(SysColor.Positive);
            }
        }
}

/// <summary>
/// Is called at start of paintbar calculation, should be used to initialize anything needed for the paintbar
/// </summary>
private void PaintbarInitialize()
{
    CurrentState.FirstCandleHigh = 0.00;
}

/// <summary>
/// Holds paintbar state - fill with variables needed to be preserved for next paintbar calc
/// </summary>
private struct PaintbarState
{
    public double FirstCandleHigh;
    public int LastDayNumber;
    public double DayHigh;
}

/// <summary>
/// Holds current PB state - use to calc PB, changes to it carry over to next PB calc
/// </summary>
private PaintbarState CurrentState;

/// <summary>
/// Holds saved PB state - internal
/// </summary>
private PaintbarState SavedState;

/// <summary>
/// Is called at start of paintbar calculation, should be used to clear the paintbar state
/// </summary>
private void PaintbarClearState()
{
    CurrentState               = new PaintbarState();
    CurrentState.LastDayNumber = -1;
}

/// <summary>
/// Saves paintbar state (called internally).
/// </summary>
private void PaintbarSaveState()
{
    SavedState = CurrentState;
}

/// <summary>
/// Restores paintbar state (called internally).
/// </summary>
private void PaintbarRestoreState()
{
    CurrentState = SavedState;
}

 

Share this post


Link to post
Share on other sites

A few things. First, I am not sure if you mean to compare the highs for exact match. That is unlikely to happen. Maybe you mean the high is higher than the first candle high?

Second, I don't know if you really want to check for the "crosses up" condition. It is very unlikely that the candle that matches the first candle high will cross the EMA as well. You probably mean if Close is above the EMA?

And third - your code will only register the first five minutes' high if you use five-minute candles. If you use one-minute candles, you'd have to change the code. Do you only use this on five-min candles?

Share this post


Link to post
Share on other sites
47 minutes ago, Mike Medved said:

> A few things. First, I am not sure if you mean to compare the highs for exact match. That is unlikely to happen. Maybe you mean the high is higher than the first candle high?

Yes, an exact match. Because the point is to make sure that the cross up over the EMA is only detected in a chart where the price action up to that point in the day has moved down from the first candlestick in the session. In other words, the high of the first 5 minute candle is the highest price reached in the session up to the point of the EMA cross over.


>Second, I don't know if you really want to check for the "crosses up" condition. It is very unlikely that the candle that matches the first candle high will cross the EMA as well. You probably mean if Close is above the EMA?

The idea, was to store the high of the first 5 minute candlestick so that it could be retrieved and used later at some point. Then, to store the highest price reached for the day thus far. These two values wouldn't change so long as the price action never moved above the high of the first 5 minute candle. Those two values would be retrieved and compared later at the time of the EMA test. The code I use for the EMA crossover, when I test it independently works perfectly. I was hoping that the code wouldn't be requiring the EMA crossover to occur on the first 5 minute candlestick. If the high of the day and first candle high are variables that have been stored properly it shouldn't do that.

>And third - your code will only register the first five minutes' high if you use five-minute candles. If you use one-minute candles, you'd have to change the code. Do you only use this on five-min candles?

I was planning on designing it for the 5 minute chart as that is the most important to me and the parameterizing the code later to expand it's capabilities.

 

Edited by Jason

Share this post


Link to post
Share on other sites

"CrossUp" means previous candle's EMA is lower than the current candle's high and the current candle's EMA is higher than the high. Is that what you meant to do?

If you remove the "crossUp" - do you see a stretch in the chart where the paintbar's condition is satisfied? If there is, and you add the CrossUp, and the CrossUp condition is within that stretch, it should work.

 

Share this post


Link to post
Share on other sites

If I remove the condition for the crossup over the EMA the paintbar highlights just the first candlestick of the day. But because the high of that candlestick is equal to the high of the day for every candlestick in the chart the paintbar should be highlighting every candlestick. The comparison should return true for each candlestick. The chart that I'm using is for AMAT for Friday's activity. Here is the relevant code:

public void MainCalculation()
{
    var SessionInformation = GetTradingSessionInfo(Timestamp);

    if (SessionInformation.DayNumber != CurrentState.LastDayNumber)
    {
        CurrentState.LastDayNumber = SessionInformation.DayNumber;
        CurrentState.DayHigh       = High;
    }

    CurrentState.DayHigh = Math.Max(CurrentState.DayHigh, High);
    
    if (Timestamp <= SessionInformation.SessionStart.AddMinutes(5))
        {
            CurrentState.FirstCandleHigh = High;
        }
    
        if ((CurrentState.FirstCandleHigh == CurrentState.DayHigh))
        {

                    SetColor(SysColor.Positive);

        }
}

Share this post


Link to post
Share on other sites

Ok, the problem is that the line

if (Timestamp <= SessionInformation.SessionStart.AddMinutes(5))

should be

if (Timestamp < SessionInformation.SessionStart.AddMinutes(5))

Share this post


Link to post
Share on other sites

I just caught it myself in playing with it. The equals changes the value of FirstCandleHigh to the high of the second candlestick which borks everything else. Thanks for your patience.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×