Jump to content
Medved Trader Forums

Bars Since True


Recommended Posts

Hey guys,


Recently I inquired about the possibility of a "Bars Since True" looping function, where it reports the # of bars since a particular chart event, which I guess would be a timespan data type.  In case it wasn't on the todo list, I figured I would ask if there is a way to do it without the simplified looping function?  I have snippets from other loops, and in a previous thread the "Ago" was mentioned, but I can't seem to put it together.  One use would be to compare the recency of particular chart events, for example:

If Bars since "Outside bar (engulfing)" > Bars since "Inside bar"
{
SetColor(Color.Green);
}
 

 

Link to comment
Share on other sites

Well, the way to do this would be - have a state variable, let's call it Boom (as in "something happened"). Set it to 9999999 or something to begin with.

Any time an event happens (like "Outside bar (engulfing)") - set CurrentState.Boom to 0. If the event did NOT happen, do CurrentState.Boom++

This way you can check the CurrentState.Boom to see how far back the event happened.

Link to comment
Share on other sites

Thanks Mike for the tip.  I put together most of it, but I'm not quite sure how to declare a state variable called Boom.  I googled but didn't find any good examples.  What I have is below:



public void MainCalculation()
{
//declare state variable Boom;
    if (High>High[1] && Low < Low[1])
    {
        CurrentState.Boom = 0;
    }
    else
    {
        CurrentState.Boom++;
    }
    if (CurrentState.Boom > 5)
    {
        SetColor(Color.Green);
    }
}

Link to comment
Share on other sites

There is an option in the ribbon menu to add State to the indicator.

That adds a bunch of code, including the CurrentState and a few state-initializing, saving and restoring functions.

You put the Boom into the PaintbarState definition:

 

/// <summary>
/// Holds paintbar state - fill with variables needed to be preserved for next paintbar calc
/// </summary>
private struct PaintbarState
{
   int Boom;  // <--- this is what you add
}

 

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

I really worked on that help file to make it as comprehensive as possible :)

Link to comment
Share on other sites

I was getting an "inaccessible due to it's protection level" error, so I put "public" in front of "int Boom;" and it worked.  You suggested setting the state variable to 99999, which I'm not sure where to put, but it seems to work anyway.  Thanks again.  I posted the full code of my original example idea in case it helps anyone.

private void PaintbarInitialize()
{ }

private struct PaintbarState
{
     public int OutsideBar;
     public int InsideBar;
}

private PaintbarState CurrentState;
private PaintbarState SavedState;
private void PaintbarClearState()
{
    CurrentState = new PaintbarState();
}

private void PaintbarSaveState()
{
    SavedState = CurrentState;
}

private void PaintbarRestoreState()
{
    CurrentState = SavedState;
}

public void MainCalculation()
{

    if (High>High[1] && Low < Low[1])
    {
        CurrentState.OutsideBar = 0;
    }
    else
    {
        CurrentState.OutsideBar++;
    }
    if (High<High[1] && Low > Low[1])
    {
        CurrentState.InsideBar = 0;
    }
    else
    {
        CurrentState.InsideBar++;
    }

    if (CurrentState.OutsideBar < CurrentState.InsideBar)
    {
        SetColor(Color.Green);
    }
}

Link to comment
Share on other sites

You're right, I forgot the public part. And setting it to 999999 would be in PaintbarClearState - but it only affects the candles from the beginning of the chart to the first occurrence of your condition - basically if you don't set it that way, it will assume that the candle right before the beginning of the chart satisfied the condition.

 

As you can see, having C# available to program paintbars/scans is very versatile. Hope the help file clarified things.

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.

×
×
  • Create New...