Anti-Volume Stop-Loss (AVSL)
alphaDescription
Experimental AVSL Stop-Loss Script
This script implements an experimental Anti-Volume Stop-Loss (AVSL) mechanism using Anti-Volume logic with Volume Price Confirmation Indicator (VPCI) and Bollinger Bands.
Key Features:
VPCI Calculation: Utilizes VWHMA (Volume-Weighted Hull Moving Average) and HMA (Hull Moving Average) for volume and price confirmation.
Dynamic Stop-Loss Adjustment: Calculates stop-loss levels dynamically based on VPCI, Volume Price Ratio (VPR), and a multiplier factor.
Z-score Analysis: Includes a Z-score calculation for detecting price anomalies and highlights crossovers.
Signal Generation: Generates long and short signals based on VPCI and Z-score conditions.
Plotting: Visualizes VPCI, Bollinger Bands, Z-score, and AVSL as dots on the chart.
Usage:
Signals: The script generates long and short trading signals based on overbought and oversold conditions.
Stop-Loss: The stop-loss is dynamically adjusted based on AVSL, ensuring a responsive risk management strategy.
HaasScript
-- DefineCommand: Set up the command for stop-loss generation
DefineCommand('AVSL_StopLoss', 'Stop loss generation using Anti-Volume logic with Plots')
-- Define parameters and inputs
local name = DefineParameter(StringType, 'name', 'Unique name of the indicator.', false, 'AVSL_Indicator', 'Text')
local chartIndex = DefineParameter(NumberType, 'chartIndex', 'The index on which to chart VPCI', false, 1, 'Number')
local chartIndexH = DefineParameter(NumberType, 'chartIndex-zscor', 'The index on which to chart AVSL', false, 2, 'Number')
InputGroupHeader('Stop Loss Settings')
local lenF = Input('Fast average', 12)
local srcF = Input('Fast Price type', ClosePrices())
local lenS = Input('Slow average', 26)
local srcS = Input('Slow Price type', ClosePrices())
local lenT = Input('Signal', 9)
local mult = Input('StdDev', 2.0)
local offset = Input('Offset', 2)
local bbLength = Input('BB Length', 20)
local bbMult = Input('BB Multiplier', 2.5)
local highlightBreaches = Input('Highlight BB Breaches?', true)
local vpcLength = Input('VPC Length', 20)
local shortTerm = Input('Short Term', 5)
local longTerm = Input('Long Term', 20)
local lengthMA = Input('VPCI MA Length', 8)
-- Data retrieval
local prices = ClosePrices()
local high = HighPrices()
local low = LowPrices()
local volume = GetVolume()
-- VPCI Calculation
local vpc = CC_VWHMA(prices, volume, vpcLength) - SMA(prices, vpcLength)
local vpr = CC_VWHMA(prices, volume, shortTerm) / SMA(prices, shortTerm)
local vm = SMA(volume, shortTerm) / SMA(volume, longTerm)
local vpci = CC_HMA((vpc * vpr * vm), lengthMA)
local vpciMA = CC_HMA(vpci, lengthMA)
-- BBands for VPCI
local vpciBB = BBANDS(vpci, bbLength, bbMult, bbMult, SmaType)
local upperBB = vpciBB[1]
local lowerBB = vpciBB[3]
-- Plotting VPCI and its Bollinger Bands
Plot(chartIndex, 'VPCI', vpci, White)
Plot(chartIndex, 'VPCI MA', vpciMA, Orange)
local upperPlot = Plot(chartIndex, 'Upper BB', upperBB, Red)
local lowerPlot = Plot(chartIndex, 'Lower BB', lowerBB, Green)
PlotBands(upperPlot, lowerPlot, SkyBlue(15))
-- Function to calculate dynamic stop-loss adjustment based on volume and price
local function PriceFun(VPC, VPR, VM, src)
local VPCI = VPC * VPR * VM
local lenV = (VPC < 0) and Ceil(Abs(VPCI - 3)) or Ceil(VPCI + 3) or 1
-- Ensure lenV is a number and within the bounds of src
lenV = Min(lenV, #src)
local priceAdjustments = HNC() -- Initialize the array for adjusted prices
local totalAdjustedPrice = 0.0
-- Iterate through calculated lenV to adjust prices dynamically
for i = 1, #lenV do
local adjustmentFactor = VPC -- Default to VPC if conditions below don't apply
if VPC > -1 and VPC < 0 then
adjustmentFactor = -1
elseif VPC < 1 and VPC >= 0 then
adjustmentFactor = 1
end
-- Calculate the adjusted price
local adjustedPrice = (src[i] or 0) * (1 / (adjustmentFactor or 1)) * (1 / (VPR or 1))
priceAdjustments = ArrayAdd(priceAdjustments, adjustedPrice) -- Appending each calculated price
totalAdjustedPrice = totalAdjustedPrice + adjustedPrice
end
-- Compute the average of adjusted prices and normalize it
local averageAdjustedPrice = totalAdjustedPrice / #priceAdjustments / 100
return averageAdjustedPrice
end
-- Calculations for stop-loss
local DeV = mult * vpci * vm
local AVSL = SMA(low - PriceFun(vpc, vpr, vm, low) + DeV, lenS)
-- Z-score Calculation for Price Anomalies
local zscore = CC_Z_Score(prices, 30, 10)
-- Plotting Z-score
Plot(chartIndexH, 'zscore', zscore, LineOptions(Red, Spiked, Solid, 1, 0, LeftAxis, '', true, false))
PlotHorizontalLine(chartIndexH, 'z-zero', Gray, 0, Dashed, LeftAxis)
-- Highlighting Z-score crossovers
if CrossOver(zscore, 0) then
PlotVerticalLine(chartIndexH, '', Purple, Time(), Dashed)
elseif CrossUnder(zscore, 0) then
PlotVerticalLine(chartIndexH, '', Cyan, Time(), Dashed)
end
-- Signal Logic based on VPCI and Z-score
local signal = SignalNone -- Initialize with no signal
-- Highlight Breaches and Determine Signal based on VPCI and its Bollinger Bands
if vpci > upperBB and zscore[1] > 1 then
if highlightBreaches then
PlotSignalBar(-4, Red) -- Highlight overbought condition
end
signal = SignalShort
elseif vpci < lowerBB and zscore[1] < -1 then
if highlightBreaches then
PlotSignalBar(-4, Green) -- Highlight oversold condition
end
signal = SignalLong
end
-- Optionally, you could also plot the VPCI signal directly as a shape on the chart
-- This is in addition to the BB breaches highlight
if signal == SignalLong then
PlotShape(0, ShapeTriangleUp, DarkGreen, 3, false) -- Indicate a long signal
elseif signal == SignalShort then
PlotShape(0, ShapeTriangleDown, Red, 3, true) -- Indicate a short signal
end
-- Plotting AVSL as Dots
local avslPlot = Plot(0, 'AVSL', AVSL, Red)
PlotShapes(avslPlot, ShapeCross, Green)
-- Declare stopLossTriggered as local variable
local stopLossTriggered = false
-- Determine if stop-loss is triggered based on AVSL
if signal == SignalLong and prices[#prices] < AVSL then
stopLossTriggered = true
elseif signal == SignalShort and prices[#prices] > AVSL then
stopLossTriggered = true
end
local result = {
signal = signal,
stopLossTriggered = stopLossTriggered
}
local dType = ListDynamicType
local dDescription = 'Custom object containing signals and stop-loss.'
local dSuggestions = 'Validate settings before use in trading algorithms.'
DefineOutput(dType, result, dDescription, dSuggestions)
local dIndex, dType, dName, dDescription, dSuggestions
-- Output index for signal
dIndex = 1
dType = EnumType
dName = 'signal'
dDescription = 'Trading signal based on VPCI and BBands.'
dSuggestions = 'Validate settings before use in trading algorithms.'
DefineOutputIndex(dIndex, dType, dName, dDescription, dSuggestions)
-- Output index for stopLossTriggered
dIndex = 2
dType = BooleanType
dName = 'stopLossTriggered'
dDescription = 'Flag to indicate if stop-loss is triggered.'
dSuggestions = 'Set to true if stop-loss condition is met.'
DefineOutputIndex(dIndex, dType, dName, dDescription, dSuggestions)
0 Comments
Sign in to leave a comment.
No comments yet. Be the first!