Jump to content
Medved Trader Forums

Multiple candle sizes in the same paintbar/scan


Recommended Posts

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

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


/// <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;
}
  • Like 1
Link to comment
Share on other sites

 

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

how do i access 5 min close with this code

Edited by zayjayspx
Link to comment
Share on other sites

20 hours ago, zayjayspx said:
 

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

how do i access 5 min close with this code

min5.ElementAt(3).close  // will get you close for a 5 min candle 3 candles back

  • Like 1
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...