Jump to content
Medved Trader Forums
jasper

multiday FIFO Q in advanced state keeping paintbar

Recommended Posts

I am trying to code an indicator that paints the end of day vwap from the previous day as a line for the current day (on a 5 minute chart). I have managed to plot this on my chart, but cannot seem to carry forward the historical values for these lines. for example, it would be nice to have a FIFO Q(20) with the last 20 horizontal line values. Attached you should find an image of a chart with this paintbar.


private void PaintbarInitialize(){}
private struct PaintbarState{public double today;}
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(){

    var session = GetTradingSessionInfo(Timestamp);
    if (Timestamp == session.SessionStart){
        if (vwap[1] > .01 ) {CurrentState.today = vwap[1];}
        else if (vwap[2] > .01 ) {CurrentState.today = vwap[2];}
        else if (vwap[3] > .01 ) {CurrentState.today = vwap[3];}
        else if (vwap[4] > .01 ) {CurrentState.today = vwap[4];}
        else if (vwap[5] > .01 ) {CurrentState.today = vwap[5];}
        else if (vwap[6] > .01 ) {CurrentState.today = vwap[6];}
    }

    SetColorAndShape("Line", PBShape.Rectangle, SysColor.MainIndicator2);
    SetYValue(CurrentState.today);
    SetScanResult(CurrentState.today);

}

 

steps.jpg

Edited by jasper

Share this post


Link to post
Share on other sites

There is a better way to determine if the current candle is the first one of the session. Your method fails if the first candle's timestamp does not fall exactly on start of session.

 

1. Define DateTime SessionStart in PaintbarState.

2. In code:
 

  if (CurrentState.SessionStart != session.SessionStart) // this is the first candle of session
  {
     CurrentState.SessionStart = session.SessionStart;
     ....
  }

	 

 

Share this post


Link to post
Share on other sites

As for using FIFOQueue -

1. In order to use it, you have to declare it, initialize it in PaintbarClearState, and handle the saving/restoring it in the PaintbarSaveState/PaintbarRestoreState

An example of that is here - https://www.medvedtrader.com/trader/WebHelp/state_keeping.htm look at the second example.

2. In the example above, a new value is added to the FIFOQueue at every candle. You can do that only for the first candle of the day. That way you can keep the last 20 days' values in the queue.

I am also not sure what is the point of doing vwap[1], vwap[2], vwap[3] in your code. You could just save the previous candle's vwap in the PaintbarState, and just use that.

Share this post


Link to post
Share on other sites

Mike, thanks for your help. I really like your if statement to find the first bar of the day. 

When I use the FIFOQueue I don't get any results on the scanner or in my charts. Here is what I just tried:

FIFOQueue Qvwap; FIFOQueue SQvwap;
private void PaintbarInitialize(){}
private struct PaintbarState{public DateTime SessionStart;}
private PaintbarState CurrentState;
private PaintbarState SavedState;
private void PaintbarClearState()
{
    CurrentState     = new PaintbarState();  
    FIFOQueue Qvwap  = new FIFOQueue(100);
    FIFOQueue SQvwap = new FIFOQueue(100);
}
private void PaintbarSaveState()
{
    SavedState = CurrentState;
    Qvwap.CopyTo(SQvwap); 
}
private void PaintbarRestoreState()
{
    CurrentState = SavedState;
    SQvwap.CopyTo(Qvwap);
}
public void MainCalculation(){
    var session = GetTradingSessionInfo(Timestamp);
    
    if (CurrentState.SessionStart != session.SessionStart){
        CurrentState.SessionStart = session.SessionStart;
        Qvwap.Add(vwap[1]);
    }
    
    SetColorAndShape("Line", PBShape.Rectangle, SysColor.MainIndicator2);
    SetYValue(Qvwap.Last);
    SetScanResult(Qvwap[4]+", "+Qvwap.First);

Share this post


Link to post
Share on other sites

So i have it working, and like you say, First and Last are the only ones giving me a result. First and Last give me the same result (Last = First). when i try to result a FIFOQ.Sum, it also just results in the same value as FIFOQ.First, and when i result FIFOQ.Count it shows 1 leading me to think the Q is clearing with each new day and only holding on the the current days value?

Share this post


Link to post
Share on other sites

If there is only one element in the queue (that is, if you added to it only once), the First and Last would be same. Otherwise (and if the values you're adding are not all the same) they shouldn't be.

 

Please export the paintbar and send the file to support@medvedtrader.com. Will look at it.

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

×