0% found this document useful (0 votes)
20 views4 pages

Advanced Array Techniques

Pine script array tutorial

Uploaded by

Esaro Logics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Advanced Array Techniques

Pine script array tutorial

Uploaded by

Esaro Logics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//@version=5

indicator("Advanced Array Techniques", overlay=false)

// ==========================================
// SECTION 1: ADVANCED ARRAY INITIALIZATION
// ==========================================

// Dynamic size initialization based on conditions


var int dynamicSize = ta.ema(volume, 14) > ta.ema(volume, 28) ? 20 : 10
var float[] dynamicArray = array.new_float(dynamicSize, 0.0)

// Multi-dimensional array simulation using 1D arrays


var int ROWS = 3
var int COLS = 3
var float[] matrix = array.new_float(ROWS * COLS, 0.0)

// Helper function to access 2D position in 1D array


get2DIndex(row, col) => row * COLS + col

// ==========================================
// SECTION 2: ADVANCED ARRAY OPERATIONS
// ==========================================

// Circular buffer implementation


var float[] circularBuffer = array.new_float(10, 0.0)
var int bufferIndex = 0

updateCircularBuffer(value) =>
array.set(circularBuffer, bufferIndex, value)
bufferIndex := (bufferIndex + 1) % array.size(circularBuffer)
circularBuffer

// Custom moving window with weighted values


var float[] weightedWindow = array.new_float(5, 0.0)
var float[] weights = array.new_float(5, 0.0)

// Initialize weights (example: exponential weights)


array.set(weights, 0, 1.0)
array.set(weights, 1, 0.8)
array.set(weights, 2, 0.6)
array.set(weights, 3, 0.4)
array.set(weights, 4, 0.2)

// Calculate weighted average


calculateWeightedAverage() =>
float sum = 0.0
float weightSum = 0.0
for i = 0 to array.size(weightedWindow) - 1
sum += array.get(weightedWindow, i) * array.get(weights, i)
weightSum += array.get(weights, i)
sum / weightSum

// ==========================================
// SECTION 3: ADVANCED ARRAY ALGORITHMS
// ==========================================

// Implementing Quick Sort


quickSort(float[] arr, int low, int high) =>
if low < high
// Partition
float pivot = array.get(arr, high)
int i = low - 1

for j = low to high - 1


if array.get(arr, j) <= pivot
i += 1
float temp = array.get(arr, i)
array.set(arr, i, array.get(arr, j))
array.set(arr, j, temp)

float temp = array.get(arr, i + 1)


array.set(arr, i + 1, array.get(arr, high))
array.set(arr, high, temp)

pivotIndex = i + 1

// Recursive calls
quickSort(arr, low, pivotIndex - 1)
quickSort(arr, pivotIndex + 1, high)

// Binary Search implementation


binarySearch(float[] arr, float target) =>
int left = 0
int right = array.size(arr) - 1

while left <= right


int mid = (left + right) / 2
float midVal = array.get(arr, mid)

if midVal == target
left := mid
break
else if midVal < target
left := mid + 1
else
right := mid - 1

left

// ==========================================
// SECTION 4: ADVANCED DATA STRUCTURES
// ==========================================

// Stack implementation using array


var float[] stack = array.new_float(0)

stackPush(float value) =>


array.push(stack, value)

stackPop() =>
float result = na
if array.size(stack) > 0
result := array.pop(stack)
result

stackPeek() =>
float result = na
if array.size(stack) > 0
result := array.get(stack, array.size(stack) - 1)
result

// Queue implementation using array


var float[] queue = array.new_float(0)

queueEnqueue(float value) =>


array.push(queue, value)

queueDequeue() =>
float result = na
if array.size(queue) > 0
result := array.get(queue, 0)
array.remove(queue, 0)
result

// ==========================================
// SECTION 5: PRACTICAL APPLICATIONS
// ==========================================

// Price action pattern detection


var float[] pricePattern = array.new_float(5, 0.0)

updatePricePattern() =>
array.shift(pricePattern)
array.push(pricePattern, close)

// Check for specific pattern (example: three rising closes)


bool isRising = true
for i = 1 to array.size(pricePattern) - 1
if array.get(pricePattern, i) <= array.get(pricePattern, i-1)
isRising := false
isRising

// Volume profile using arrays


var float[] volumeProfile = array.new_float(10, 0.0)
var float[] pricePoints = array.new_float(10, 0.0)

updateVolumeProfile() =>
float maxPrice = ta.highest(high, 20)
float minPrice = ta.lowest(low, 20)
float priceRange = maxPrice - minPrice
float increment = priceRange / 10

// Initialize price points


for i = 0 to 9
array.set(pricePoints, i, minPrice + (i * increment))

// Update volume profile


for i = 0 to 9
if close >= array.get(pricePoints, i) and close < array.get(pricePoints, i
+ 1)
array.set(volumeProfile, i, array.get(volumeProfile, i) + volume)

// ==========================================
// SECTION 6: TESTING AND VISUALIZATION
// ==========================================

// Update and test implementations


if barstate.islast
// Test circular buffer
updateCircularBuffer(close)

// Test weighted window


array.shift(weightedWindow)
array.push(weightedWindow, close)
float weightedAvg = calculateWeightedAverage()

// Test price pattern


bool risingPattern = updatePricePattern()

// Update volume profile


updateVolumeProfile()

// Visualize results
label.new(bar_index, high,
text="Weighted Avg: " + str.tostring(weightedAvg) + "\n" +
"Rising Pattern: " + str.tostring(risingPattern),
color=color.blue,
textcolor=color.white)

// Plot volume profile


for i = 0 to 9
plot(array.get(volumeProfile, i), "Volume " + str.tostring(i),
color=color.from_gradient(i, 0, 9, color.red, color.green))

You might also like