EMC2
EMC2

Reputation: 21

Multiple time frames based off current time frame

Trying to set multiple timeframes to chart using the current timeframe as the base. Goal was to go twice current and half current. ie if I was in an hour chart my timeframes would the 2h/1h/and 30min and if I was in the 4hour my time frames would be 8h/4h/and 2h. Just trying to have it automatically change instead of changing them manually. any insights would be appreciated.

Upvotes: 1

Views: 2354

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8779

This code is taken from our FAQ here:

//@version=4
//@author=LucF, for PineCoders
study("Multiple of current TF")

resMult = input(2, minval = 1)

// Returns a multiple of current TF as a string usable with "security()".
f_MultipleOfRes( _mult) => 
    // Convert target timeframe in minutes.
    _TargetResInMin = timeframe.multiplier * _mult * (
      timeframe.isseconds   ? 1. / 60. :
      timeframe.isminutes   ? 1. :
      timeframe.isdaily     ? 1440. :
      timeframe.isweekly    ? 7. * 24. * 60. :
      timeframe.ismonthly   ? 30.417 * 24. * 60. : na)
      // Find best way to express the TF.
    _TargetResInMin     <= 0.0417       ? "1S"  :
      _TargetResInMin   <= 0.167        ? "5S"  :
      _TargetResInMin   <= 0.376        ? "15S" :
      _TargetResInMin   <= 0.751        ? "30S" :
      _TargetResInMin   <= 1440         ? tostring(round(_TargetResInMin)) :
      tostring(round(min(_TargetResInMin / 1440, 365))) + "D"

myRsi = rsi(close, 14)
plot(myRsi, color = color.silver)
// No repainting
myRsiHtf = security(syminfo.tickerid, f_MultipleOfRes(resMult), myRsi[1], lookahead = barmerge.lookahead_on)
plot(myRsiHtf, color = color.green)
// Repainting
myRsiHtf2 = security(syminfo.tickerid, f_MultipleOfRes(resMult), myRsi)
plot(myRsiHtf2, color = color.red)

Upvotes: 3

Related Questions