(程式碼) Trend Follower


[LegacyColorValue = TRUE];

{ *****************************************************************************************
   
Study : Trend Follower


*******************************************************************************************}


Inputs: XMALen(4), VolLen(8), MultFact(2);
Vars: TopBand(0), BotBand(0), XAvg(0), FastMA(0), MedMA(0), SlowMA(0),
BullTrend(false), BearTrend(false);

{ Define initial Top and Bottom Band }
If BarNumber = 1 then Begin
TopBand = Open + Volatility( VolLen ) * MultFact;
BotBand = Open - Volatility( VolLen ) * Multfact;
End;

{ Define market mode with 3 line Moving averages }
XAvg = XAverage( Close , XMALen );
FastMA = Average( Close, 4 );
MedMA = Average( Close, 9 );
SlowMA = Average( Close, 18 );

BullTrend = FastMA > MedMA AND MedMA > SlowMA;
BearTrend = FastMA < MedMA AND MedMA < SlowMA;

{ Place long entry orders }
If XAvg > TopBand AND BullTrend then begin
Buy  this bar at close;
BotBand = TopBand;
TopBand = TopBand + Volatility( VolLen ) * MultFact;
End;

{ Place short entry orders }
If XAvg < BotBand AND BearTrend then begin
SellShort  this bar at close;
TopBand = BotBand;
BotBand = BotBand - Volatility( VolLen ) * MultFact;
End;

{ Place long and short exit orders }
If MarketPosition = 1 then
Sell next bar at BotBand stop;
If MarketPosition = -1 then
BuytoCover next bar at TopBand stop;