Jump to content
Medved Trader Forums

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
Link to comment
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;
     ....
  }

	 

 

Link to comment
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.

Link to comment
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);

Link to comment
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?

Link to comment
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.

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...