this may or may not work in tradestation
{ The IntrabarOrderGeneration attribute is set to false
in this strategy because strategy calculations depend on
end-of-bar prices. }
[IntrabarOrderGeneration = false]
inputs:
Price( 0.5 * ( H + L ) ),
N( 16 ),
NumDevsUp( 2 ),
NumDevsDn( 2 ),
TrailAmt( 0.5 ),
BandColor( Yellow ),
DataType( 1 ) ; { pass in 1 if you are working with
the entire population, and 2 if you are working
with a sample and want the standard deviation for
the population }
variables:
Divisor( 0 ),
Mean( 0 ),
SumSqr( 0 ),
AvgDiff( 0 ),
FracStandardDev( 0 ),
LowerBand( 0 ),
UpperBand( 0 ),
TL_ID1( -1 ),
TL_ID2( -1 ) ;
// AvgDiff = 0 ;
Divisor = Iff( DataType = 1, N, N - 1 ) ;
if Divisor > 0 then
begin
Mean = AdaptMovAvg_Fractal( Price, N ) ;
SumSqr = 0 ;
for Value1 = 0 to N - 1
begin
SumSqr = SumSqr + Square( Price[Value1] -
Mean ) ;
end ;
AvgDiff = SumSqr / Divisor ;
end ;
if AvgDiff > 0 then
FracStandardDev = SquareRoot( AvgDiff )
else
FracStandardDev = 0 ;
LowerBand = Mean - NumDevsDn * FracStandardDev ;
UpperBand = Mean + NumDevsUp * FracStandardDev ;
if CurrentBar > 1 and Low crosses over LowerBand then
{ CB > 1 check used to avoid spurious cross confirmation
at CB = 1 }
Buy ( "BBandLE" ) next bar LowerBand stop ;
if CurrentBar > 1 and High crosses under UpperBand then
SellShort next bar at UpperBand Stop ; { ie, don't
buy if next bar completely below current LowerBand,
but wait for next crossing condition - an example
of a non-persistent setup with an indefinite
stop/limit trigger }
SetStopShare ;
SetDollarTrailing( TrailAmt ) ;
SetProfitTarget ( 3 * TrailAmt ) ;
{ Use trendlines to "plot" the bands on the chart }
TL_ID1 = TL_New( Date[1], Time[1], UpperBand[1], Date,
Time, UpperBand ) ;
TL_SetColor( TL_ID1, BandColor ) ;
TL_ID2 = TL_New( Date[1], Time[1], LowerBand[1], Date,
Time, LowerBand ) ;
TL_SetColor( TL_ID2, BandColor ) ;