{ Function: SupRes2 ("Support/Resistance") Locate the nearest price levels of support and resistance. Return the support and resistance prices through the argument list. The function fits a horizontal line to the highs and lows near the current close. The price below the close at which the line fits the best is the support line. The price above the close at which the line fits the best is the resistance line. The support and resistance levels are returned through the argument list. Returns: Average of support and resistance levels; Also, the support and resistance levels are returned through the argument list. Michael R. Bryant Breakout Futures www.BreakoutFutures.com Copyright 2003 Breakout Futures Feb 24, 2003 } Input: NBars (NumericSimple), { # bars to lookback in search } PriceRnge (NumericSimple), { # points to examine above/below close } PFilter (NumericSimple), { Include high/low within PFilter points } MinPoints (NumericSimple), { min # points for a fit } SupPrice (NumericRef), { located support price } ResPrice (NumericRef); { located resistance price } Var: iPrice (0), { price at current level } DtoLow (0), { distance from line to low } DtoHigh (0), { distance from line to high } Sum (0), { sum of weights } NormSum (0), { normalized sum } MinSum (999999),{ max sum of weights } NPoints (0), { # points in sum } SupportP (0), { best support price } ResistP (0), { best resistance price } NPInc (0), { number of price increments above/below close } istart (0), { first increment to start at } ip (0), { loop counter of price increments } ib (0); { loop counter for bars } NPInc = PriceRnge/(MinMove/PriceScale); istart = IntPortion(NPInc/3); { Search for resistance; loop over prices above close } MinSum = 999999; ResistP = MaxList(High, ResPrice); For ip = istart to NPInc Begin iPrice = Close + ip * (MinMove/PriceScale); Sum = 0; NPoints = 0; { Loop over bars } For ib = 1 to NBars Begin { Add up sum of squares of distances to price line } DtoLow = AbsValue(L[ib] - iPrice); DtoHigh = AbsValue(H[ib] - iPrice); If DtoLow <= DtoHigh and DtoLow <= PFilter then Begin NPoints = NPoints + 1; Sum = Sum + Square(DtoLow); end else If DtoHigh < DtoLow and DtoHigh <= PFilter then Begin NPoints = NPoints + 1; Sum = Sum + Square(DtoHigh); end; end; { loop ib } { Record iPrice if sum is highest so far } If NPoints >= MinPoints then Begin NormSum = SquareRoot(Sum/NPoints); If NormSum < MinSum then Begin MinSum = NormSum; ResistP = iPrice; end; end; end; { loop ip } ResistP = MaxList(High, ResistP); { make sure resistance >= high } { Search for support; loop over prices below close } MinSum = 999999; SupportP = MinList(Low, SupPrice); For ip = istart to NPInc Begin iPrice = Close - ip * (MinMove/PriceScale); Sum = 0; NPoints = 0; { Loop over bars } For ib = 1 to NBars Begin { Add up sum of squares of distances to price line } DtoLow = AbsValue(L[ib] - iPrice); DtoHigh = AbsValue(H[ib] - iPrice); If DtoLow <= DtoHigh and DtoLow <= PFilter then Begin NPoints = NPoints + 1; Sum = Sum + Square(DtoLow); end else If DtoHigh < DtoLow and DtoHigh <= PFilter then Begin NPoints = NPoints + 1; Sum = Sum + Square(DtoHigh); end; end; { loop ib } { Record iPrice if sum is highest so far } If NPoints >= MinPoints then Begin NormSum = SquareRoot(Sum/NPoints); If NormSum < MinSum then Begin MinSum = NormSum; SupportP = iPrice; end; end; end; { loop ip } SupportP = MinList(Low, SupportP); { make sure support <= Low } SupPrice = SupportP; ResPrice = ResistP; SupRes2 = (SupportP + ResistP)/2.; { Indicator: SupRes Indicator-2 Plot the support and resistance given by the SupRes function. Michael R. Bryant Breakout Futures www.BreakoutFutures.com Copyright 2003 Breakout Futures Feb 24, 2003 } Inputs: NBars (30), { # bars to lookback in search } PriceRnge (3), { # points to examine above/below close } PFilter (1.0), { points this close to line } MinPoints (4); { need at least this many points in fit } Var: SupPrice (C), { located support price } ResPrice (C); { located resistance price } { Call SupRes function to find nearest support/resistance } {Value1 = SupRes(NBars, PriceRnge, ProxWght, SupPrice, ResPrice);} Value1 = SupRes2(NBars, PriceRnge, PFilter, MinPoints, SupPrice, ResPrice); Plot1(SupPrice, "Support"); Plot2(ResPrice, "Resistance");