Jump to content
Medved Trader Forums

Resampling candles


Recommended Posts

Hello!

 

Don't know who I was talking to via email, but I'm trying to build a multi timeframe indicator. It was suggested 

 

<Hm. The only way in MT to do SMA on multiple timeframes (I presume by that you mean multiple candle sizes) would be to run it on a 1 min candle chart and in the code accumulate those non-1-minute candles yourself and generate the SMAs>

 

I had the idea to use state keeping  to gather up buckets of 3 close values, then take the SMA of the 3rd one of each bucket to get the 3 minute candle close. Except since I'm doing this on the 1 min chart, it's not working out.

 

I can get the last of the FIFO que, but that's just the most recent one, and it's essentially just a 1 min chart 

 

I can get [3] bars ago, but that is the same problem, every minute will have a new one 

 

I also tried using a double.nan for minute candles not divisible by 3, but that's causing problems since for example the 3min candle at 15:30 is the same close as the 1min candle at 15:32

 

How could I resample the 1 min close to 3min (and higher?

 

Link to comment
Share on other sites

Here is some code that generates (on a 1-min candle paintbar scan) 5-min and 10-min candles as well:

// here is code that, when run on 1 min candles in MT, will generate 5-min and 10-min candles.
// so you can do your own things with it. The limitation of course is that you cannot use these
// new candles as inputs to indicators, but you can do your own (for example, use FIFO queues
// to make SMAs).
struct SimpleCandle
{
    public double open;
    public double high;
    public double low;
    public double close;
    public double vol;
    public DateTime timestamp;
    public SimpleCandle(double o, double h, double l, double c, double v, DateTime ts)
    {
        open = o;high=h;low=l;close=c;vol=v;timestamp=ts;
    }
}

Stack<SimpleCandle> min5;
Stack<SimpleCandle> min10;

private DateTime AlignTimestamp(int minutebound, DateTime timestamp)
{
    long ticks = timestamp.Ticks - timestamp.Ticks % (TimeSpan.TicksPerMinute*minutebound);
    return new DateTime(ticks);
}

private void CollectCandle(int NumMin, ref Stack<SimpleCandle> collection)
{
    var AlignedTimestamp = AlignTimestamp(NumMin, Timestamp);
    if (collection.Count>0)
    {
        var candle           = collection.Peek();
        if (candle.timestamp == AlignedTimestamp) // still in same 5 minutes
        {
            candle       = collection.Pop();
            candle.close = Close;
            candle.high  = Math.Max(High, candle.high);
            candle.low   = Math.Min(Low, candle.low);
            candle.vol   = candle.vol - CurrentState.prevBarVol + BarVolume;
            collection.Push(candle);
        }
        else // new candle into the queue
        {
            candle = new SimpleCandle(Open, High, Low, Close, BarVolume, AlignedTimestamp);
            collection.Push(candle);
        }
    }
    else // first candle into the queue
    {
        var candle = new SimpleCandle(Open, High, Low, Close, BarVolume, AlignedTimestamp);
        collection.Push(candle);
    }
}

public void MainCalculation() 
{
    CollectCandle(5, ref min5);
    CollectCandle(10, ref min10);
    CurrentState.prevBarVol = BarVolume;
    
    // at this point you can access 1 min candles (with the normal Close, Open etc indexed variables)
    // and the 5- and 10-min candles with the min5.ElementAt() and min10.ElementAt() variables
}

/// <summary>
/// INITIALIZES paintbar. Is called at start of paintbar calculation, should be used to initialize anything needed for the paintbar
/// NOTE: do NOT initialize the CurrentState here - the PaintbarClearState will be called AFTER this call, and will wipe it out.
/// </summary>
private void PaintbarInitialize()
{
    min5  = new Stack<SimpleCandle>(100);
    min10 = new Stack<SimpleCandle>(100);
}



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

/// <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
/// and initialize whatever variables you place into it. This is called AFTER PaintbarInitialize
/// </summary>
private void PaintbarClearState()
{
    CurrentState = new PaintbarState();
    min5.Clear();
    min10.Clear();
}

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

/// <summary>
/// Restores paintbar state (called internally).
/// </summary>
private void PaintbarRestoreState()
{
    CurrentState = SavedState;
}
Link to comment
Share on other sites

My follow up to this I guess is, is there any way to plot more than 2 values?

 

This indicator is a value and its SMA, so a pair of lines for setyvalue(this,that). I currently have pairs for the 1 min and 3 min but it seems like I can only get (this,that) but not (another,aswell) to plot at the same time? 

 

I plan on having pairs of lines for 1 min-15 min

Link to comment
Share on other sites

Posted (edited)
 
 

for anyone wondering , this is how i did it 

 

         // Calculate the 3m 
    if (Timestamp[-1].Minute % 3 == 0 )
    {
        threeminclose = Close[1];
        CurrentState.threeminbucket.Add(threeminclose);
     double threemin8 = CurrentState.threeminbucket.Average;
    double tmf = threeminclose -threemin8;
         CurrentState.SMA3of3mF.Add(tmf);
    double lowf3 = CurrentState.SMA3of3mF.Average;   
     if ( tmf >  lowf3)
         SetColor(0, Color.Green);
       else
           SetColor(1, Color.Red);
       SetYValue(tmf,lowf3);

    }

    
    else
    {
         threeminclose = Double.NaN;
    
    }

         // Calculate the 5m 
         if (Timestamp[-1].Minute % 5 == 0 )
    {
        fiveminclose = Close;
        CurrentState.fiveminbucket.Add(fiveminclose);
     double fivemin8 = CurrentState.fiveminbucket.Average;
    double fmf = fiveminclose -fivemin8;
         CurrentState.SMA3of5mF.Add(fmf);
    double lowf5 = CurrentState.SMA3of5mF.Average;   
     if ( fmf >  lowf5)
         SetColor(0, Color.Green);
       else
           SetColor(1, Color.Red);
       SetYValue(fmf,lowf5);

    }

    
    else
    {
         fiveminclose = Double.NaN;
    
    }

i had to do this for each timeframe

problem is, i  can only plot one timeframe at a time. thinking about just assigning colors or something if  2 lines is the limit 

Edited by zayjayspx
added code
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...