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

Having trouble retreiving variable from first paintbar calculation.

Recommended Posts

My goal is to store the high of the first 5 minute candlestick in a variable and then retrieve it for comparison but I'm stuck on this error message:

37, 5 CS0825 The contextual keyword 'var' may only appear within a local variable declaration

So far, this is the code I've worked up but through searching the forums I haven't been able to get any further than this.

Any help would be appreciated.


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

if (Timestamp <= SessionInformation.SessionStart.AddMinutes(5))
{
var FirstCandleHigh = High;
return;
}

else

if ((FirstCandleHigh == SymbolData.High) && (SymbolData.GapPercent <= -3) && Close.CrossesUp(EMA_10, 0))

{
SetColor(SysColor.Positive);
TriggerAlert("Reversal", @"Reversing");
SetScanResult("Reversal");
return;
}

}

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

///
/// Holds paintbar state - fill with variables needed to be preserved for next paintbar calc
///
private struct PaintbarState
{
var FirstCandleHigh;
}

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

///
/// Holds saved PB state - internal
///
private PaintbarState SavedState;

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

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

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

Share this post


Link to post
Share on other sites

ok

you cannot have

private struct PaintbarState
{
var FirstCandleHigh;
}

 

FirstCandleHigh has to be Double. You can only use var when you're assigning a value to it that is unambiguously of some type.

Other than that, sometimes you are referring to FirstCandleHigh instead of CurrentState.FirstCandleHigh - needs to be the second one.

Share this post


Link to post
Share on other sites

The Double part I didn't realize, but I suspected that's what I needed to do for the call. Thank you!

Share this post


Link to post
Share on other sites

Okay, I've changed the code as shown below, however, now I'm getting the following two errors:

37, 24    CS1002    ; expected    
37, 40    CS1519    Invalid token ';' in class, struct, or interface member declaration    

The parser seems to take issue with the following line in the PaintbarState struct:

    double CurrentState.FirstCandleHigh;

at the period and semicolon. Also, if I were to retrieve FirstCandleHigh from the first state would I need to manually call PaintbarSaveState() in the first part of the IF statement and read it back under the ELSE statement with something like SavedState.FirstCandleHigh?  Here is the full code:

 

public void MainCalculation()
{
    var SessionInformation = GetTradingSessionInfo(Timestamp);
    
    if (Timestamp <= SessionInformation.SessionStart.AddMinutes(5))
        {
            var CurrentState.FirstCandleHigh = High;
            return;
        }
        
    else
            
    if ((CurrentState.FirstCandleHigh == SymbolData.High) && (SymbolData.GapPercent <= -3) && Close.CrossesUp(EMA_10, 0))
        
        {
            SetColor(SysColor.Positive);
            TriggerAlert("Reversal", @"Reversing");
            SetScanResult("Reversal");
            return;
        }

}

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

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

/// <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();
}

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

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

 

Edited by Jason

Share this post


Link to post
Share on other sites

double CurrentState.FirstCandleHigh;   - should be

double FirstCandleHigh;

It is a Double variable inside a PaintbarState struct.

 

also

 

var CurrentState.FirstCandleHigh = 0.00;   - should not have var   - var is a declaration, and it is already declared.

Share this post


Link to post
Share on other sites

Okay, thank you very much for your patience.  For some reason I was thinking that I couldn't use an access modifier on a variable within a struct. Now I have another question for you. I'm currently trying to test this paintbar and flesh it out on chart data from a prior session a couple of days ago. From the help files I think I would need DayNumber for this but they state that DayNumber is " the # of the day in the list of trading sessions ." What list would this be though? Is it relative to the current day? Not sure how this is used. Or maybe there is an easier way? Timestamp is automatically going to refer to the current candlestick but is the session information being pulled internally from the current day or is it derived from the timestamp of the candlestick data that I'm feeding it on my 5 minute chart? If it's the latter then I must be making an error somewhere else since it should be showing up on my chart.

 

Thanks again!

Share this post


Link to post
Share on other sites

The # of the day is important because you can compare it to another day #. That is, if one (previous) call got you let's say 7 and now the call on current candle got you 8, that means this is a new day. The session is derived from the timestamp that you pass it - that is, it is the session inside which the timestamp fits.

Share this post


Link to post
Share on other sites

Okay, that being the case then the first timestamp the paintbar receives should be the timestamp of the first 5 minute candle on the chart that I have applied that paintbar to. And what about SymbolData.High? If the last trading session took place on, say, Friday and I used a paintbar that included SymbolData.High and applied it to the chart on, say, Sunday would it return the high of the day on Friday or return nothing since I was applying it on Sunday?

Share this post


Link to post
Share on other sites

SymbolData.High is the current High for the current day that is shown on your portfolio. All the SymbolData variables refer to the current, latest, received data. Not to anything on the chart.

 

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  

×