Description

300 Points

Categories & Tags

Comments (0)

0/2000

Loading comments…

Source code

// Custom Levels (300pt Range - Clean Day) - SXVNT SDK
// Shows 300-point range with three bases and specific level offsets for current day only

function calculate(bars, ctx) {
  // ==========================================
  // 1. INPUTS FOR CUSTOMIZATION
  // ==========================================
  const showTodayOnly = ctx.input('Show Today Only', true);
  const lineWidth = ctx.input('Line Width', 1, { min: 1, max: 5, step: 1 });
  
  // Color inputs for each level type
  const redColor = ctx.input('Red Lines Color', '#ff0000');
  const greenColor = ctx.input('Green Lines Color', '#00ff00');
  const pinkColor = ctx.input('Pink Lines Color', '#ff00ff');
  const blueColor = ctx.input('Blue Lines Color', '#0000ff');
  const blackColor = ctx.input('Black Lines Color', '#000000');
  const yellowColor = ctx.input('Yellow Lines Color', '#ffff00');
  
  // Blue circle opacity
  const blueCircleOpacity = ctx.input('Blue Circle Opacity', 0.3, { min: 0, max: 1, step: 0.1 });
  
  // ==========================================
  // 2. GET DAILY CLOSE FOR BASE CALCULATION
  // ==========================================
  // Request daily timeframe data for the close
  const daily = ctx.request('1D');
  if (daily.bars.length === 0) {
    ctx.log('No daily data available');
    return;
  }
  
  // Use the most recent daily close (confirmed bar)
  const lastDailyBar = daily.bars[daily.bars.length - 1];
  const dailyClose = lastDailyBar.close;
  ctx.log('Daily close:', dailyClose);
  
  // Calculate bases
  const baseCenter = dailyClose - (dailyClose % 100);
  const baseBelow = baseCenter - 100;
  const baseAbove = baseCenter + 100;
  
  ctx.log('Bases - Center:', baseCenter, 'Below:', baseBelow, 'Above:', baseAbove);
  
  // ==========================================
  // 3. DETERMINE WHICH BARS TO DRAW ON
  // ==========================================
  const last = bars.length - 1;
  if (last < 0) return;
  
  let startBar = 0;
  let endBar = last;
  
  if (showTodayOnly) {
    // Find today's bars by checking if they're within the last 24 hours
    const now = Date.now();
    const twentyFourHoursAgo = now - (24 * 60 * 60 * 1000);
    
    // Find the first bar within the last 24 hours
    for (let i = last; i >= 0; i--) {
      if (bars[i].time >= twentyFourHoursAgo) {
        startBar = i;
      } else {
        break;
      }
    }
    
    ctx.log('Today bars range:', startBar, 'to', endBar);
    
    // If no bars in last 24 hours, don't draw anything
    if (startBar > endBar) {
      ctx.log('No bars in last 24 hours');
      return;
    }
  }
  
  // ==========================================
  // 4. HELPER FUNCTION TO DRAW LEVELS
  // ==========================================
  function drawLevel(base, offset, color, style, width = lineWidth) {
    const levelPrice = base + offset;
    
    if (style === 'circle') {
      // Draw circle markers at each bar
      for (let i = startBar; i <= endBar; i++) {
        ctx.shape(i, 'circle', {
          color: color,
          size: 6,
          position: 'at',
          price: levelPrice
        });
      }
    } else if (style === 'cross') {
      // Draw cross markers at each bar
      for (let i = startBar; i <= endBar; i++) {
        ctx.shape(i, 'cross', {
          color: color,
          size: 8,
          position: 'at',
          price: levelPrice
        });
      }
    } else {
      // Draw horizontal line from start to end
      ctx.line(startBar, levelPrice, endBar, levelPrice, {
        color: color,
        lineWidth: width,
        lineStyle: 'solid',
        extend: 'right'
      });
    }
  }
  
  // ==========================================
  // 5. PLOT CENTER BASE LEVELS
  // ==========================================
  
  // RED (80) & GREEN (35)
  drawLevel(baseCenter, 80, redColor, 'line', 2);
  drawLevel(baseCenter, 35, greenColor, 'line', 2);
  
  // PINK (64 & 06)
  drawLevel(baseCenter, 64, pinkColor, 'line');
  drawLevel(baseCenter, 6, pinkColor, 'line');
  
  // BLUE (00, 10, 20, 50)
  // Blue 00 with circle style and opacity
  const blue00R = parseInt(blueColor.slice(1, 3), 16);
  const blue00G = parseInt(blueColor.slice(3, 5), 16);
  const blue00B = parseInt(blueColor.slice(5, 7), 16);
  const blue00WithOpacity = `rgba(${blue00R}, ${blue00G}, ${blue00B}, ${blueCircleOpacity})`;
  
  drawLevel(baseCenter, 0, blue00WithOpacity, 'circle');
  drawLevel(baseCenter, 10, blueColor, 'line');
  drawLevel(baseCenter, 20, blueColor, 'line');
  drawLevel(baseCenter, 50, blueColor, 'line');
  
  // BLACK (47, 72, 86)
  drawLevel(baseCenter, 47, blackColor, 'line');
  drawLevel(baseCenter, 72, blackColor, 'line');
  drawLevel(baseCenter, 86, blackColor, 'line');
  
  // YELLOW VOLATILITY (26, 42, 58, 94) - Cross style
  drawLevel(baseCenter, 26, yellowColor, 'cross');
  drawLevel(baseCenter, 42, yellowColor, 'cross');
  drawLevel(baseCenter, 58, yellowColor, 'cross');
  drawLevel(baseCenter, 94, yellowColor, 'cross');
  
  // ==========================================
  // 6. PLOT BELOW BASE LEVELS
  // ==========================================
  
  // RED (80) & GREEN (35)
  drawLevel(baseBelow, 80, redColor, 'line', 2);
  drawLevel(baseBelow, 35, greenColor, 'line', 2);
  
  // PINK (64 & 06)
  drawLevel(baseBelow, 64, pinkColor, 'line');
  drawLevel(baseBelow, 6, pinkColor, 'line');
  
  // BLUE (00, 10, 20, 50)
  drawLevel(baseBelow, 0, blue00WithOpacity, 'circle');
  drawLevel(baseBelow, 10, blueColor, 'line');
  drawLevel(baseBelow, 20, blueColor, 'line');
  drawLevel(baseBelow, 50, blueColor, 'line');
  
  // BLACK (47, 72, 86)
  drawLevel(baseBelow, 47, blackColor, 'line');
  drawLevel(baseBelow, 72, blackColor, 'line');
  drawLevel(baseBelow, 86, blackColor, 'line');
  
  // YELLOW VOLATILITY (26, 42, 58, 94)
  drawLevel(baseBelow, 26, yellowColor, 'cross');
  drawLevel(baseBelow, 42, yellowColor, 'cross');
  drawLevel(baseBelow, 58, yellowColor, 'cross');
  drawLevel(baseBelow, 94, yellowColor, 'cross');
  
  // ==========================================
  // 7. PLOT ABOVE BASE LEVELS
  // ==========================================
  
  // RED (80) & GREEN (35)
  drawLevel(baseAbove, 80, redColor, 'line', 2);
  drawLevel(baseAbove, 35, greenColor, 'line', 2);
  
  // PINK (64 & 06)
  drawLevel(baseAbove, 64, pinkColor, 'line');
  drawLevel(baseAbove, 6, pinkColor, 'line');
  
  // BLUE (00, 10, 20, 50)
  drawLevel(baseAbove, 0, blue00WithOpacity, 'circle');
  drawLevel(baseAbove, 10, blueColor, 'line');
  drawLevel(baseAbove, 20, blueColor, 'line');
  drawLevel(baseAbove, 50, blueColor, 'line');
  
  // BLACK (47, 72, 86)
  drawLevel(baseAbove, 47, blackColor, 'line');
  drawLevel(baseAbove, 72, blackColor, 'line');
  drawLevel(baseAbove, 86, blackColor, 'line');
  
  // YELLOW VOLATILITY (26, 42, 58, 94)
  drawLevel(baseAbove, 26, yellowColor, 'cross');
  drawLevel(baseAbove, 42, yellowColor, 'cross');
  drawLevel(baseAbove, 58, yellowColor, 'cross');
  drawLevel(baseAbove, 94, yellowColor, 'cross');
  
  // ==========================================
  // 8. ADD BASE LABELS FOR REFERENCE
  // ==========================================
  ctx.label(endBar, baseCenter, 'Center Base: ' + baseCenter.toFixed(2), {
    textColor: '#ffffff',
    color: 'rgba(0, 0, 0, 0.7)',
    size: 10,
    position: 'at'
  });
  
  ctx.label(endBar, baseBelow, 'Below Base: ' + baseBelow.toFixed(2), {
    textColor: '#ffffff',
    color: 'rgba(0, 0, 0, 0.7)',
    size: 10,
    position: 'at'
  });
  
  ctx.label(endBar, baseAbove, 'Above Base: ' + baseAbove.toFixed(2), {
    textColor: '#ffffff',
    color: 'rgba(0, 0, 0, 0.7)',
    size: 10,
    position: 'at'
  });
}