(程式碼) Mov Avg Crossover


[LegacyColorValue = TRUE];

{ *****************************************************************************************
   
Study : Moving Average Cross Over

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


Input: FastLen(9), SlowLen(18), ChLen(12), TrailBar(8), Initial(2), ReBars(15), ReTrailBar(10), Reentry(1);
Vars: FastMA(0), SlowMA(0),LEntryPrice(0), SEntryPrice(0), LCount(-999), SCount(-999), 
ReEntryCount(0), CurrentPosition(0);

FastMA = Average( Close , FastLen );
SlowMA = Average( Close , SlowLen );

{ Order Placement for Long Positions }
If FastMA crosses over SlowMA and barnumber > 1 then Begin
LEntryPrice = Highest( High , TrailBar )[1] * 1.005;
LCount = BarNumber;
End;
commentary( barnumber - lcount , lentryprice );  
If MarketPosition <> 1 AND BarNumber < LCount + ChLen then
Buy ("Cross Over Buy") Initial Shares next bar at LEntryPrice Stop;

{ Order Placement for Short Positions }
If FastMA crosses under SlowMA and barnumber > 1 then Begin
SEntryPrice = Lowest( Low , TrailBar )[1] *.995;
SCount = BarNumber;
End;

If MarketPosition <> -1  AND BarNumber < SCount + ChLen then
SellShort ("Cross Under Buy") Initial Shares next bar at SEntryPrice Stop;

{ Trailing Stop while in Position }
If MarketPosition = 1 then begin
LCount = -999;
Sell ("LongTStop") next bar at Lowest( Low , TrailBar ) Stop;
End;

If MarketPosition = -1 then Begin
SCount = -999;
BuytoCover ("ShortTStop") next bar at Highest( High , TrailBar ) stop;
End;

{ Reentry Technique }
CurrentPosition = MarketPosition;

If CurrentPosition = 0 AND CurrentPosition[1] = -1 then
ReEntryCount = 1;
If CurrentPosition = 0 AND CurrentPosition[1] = 1 then
ReEntryCount = 1;

If MarketPosition = 0 AND MarketPosition(1) = 1 AND ReEntryCount < ReBars then Begin
ReEntryCount = ReEntryCount + 1;
Buy ("Long ReEntry") ReEntry Shares next bar at Highest( High , ReTrailBar ) Stop;
End;

If MarketPosition = 0 AND MarketPosition(1) = -1 AND ReEntryCount < ReBars then Begin
ReEntryCount = ReEntryCount + 1;
SellShort ("Short ReEntry") ReEntry Shares next bar at Lowest( Low , ReTrailBar ) Stop;
End;