// Simiple MOVING AVERAGE MULTI TIMEFRAME BACKTEST
// Notes: all trades must be triggered on "Close", "0" days delay
// in the backtest setup.
// Parameters
LongPeriods=Param("Long
Periods",17,5,21,3);
LongPeriodsLTF=Param("Long
PeriodsLTF",9,5,21,3);
NumberofPositions=Param("Positions",2,2,6,1);
FirstProfitTarget=Param("FirstProfitTarget",0.004,0.0001,0.0045,0.0001);
SecondProfitTarget = Param("SecondProfitTarget",0.009,0.0001,0.0125,0.0001);
TrailingStop=Param("TrailStop",0.001,0.0001,0.003,0.0001);
TimeFrameSet
(
in5Minute);
// switch to 15
minute time frame
// Calculate
Moving
Averages
MAShort=MA(C,LongPeriods);
MALong=Ref(MAShort,-2);
// Check for Long or Short Signals
LongSignal = (C > MALong) AND (C > MAShort)
AND (MAShort > MALong);
ShortSignal = (C < MALong) AND (C < MAShort)
AND (MAShort < MALong);
TimeFrameRestore
();
// restore time frame to
original
TimeFrameSet (in15Minute); // switch to 15 minute time frame
// Calculate
Moving
Averages
MAShortLT=MA(C,LongPeriodsLTF);
MALongLT=Ref(MAShortLT,-1);
LongSignalLT = (C > MALongLT) AND (C > MAShortLT)
AND (MAShortLT > MALongLT);
ShortSignalLT = (C < MALongLT) AND (C < MAShortLT)
AND (MAShortLT < MALongLT);
SignalLatchLong = Flip(LongSignalLT,ShortSignalLT);
SignalLatchShort = Flip(ShortSignalLT,
LongSignalLT);
TimeFrameRestore
();
// restore time frame to
original
// Points only test is equivalent to trading just one contract. This can be
// easily accomplished using Futures mode of the backtester AND adding the
following one line to your formula:
MarginDeposit = 1;
PositionSize=NumberofPositions;
//Entry and Exit
EntryTimeCondition = IIf ( Minute() == 4 + 0*5, True,
IIf
(
Minute() ==
4 +
1*
5,
True,
IIf
(
Minute() ==
4 +
2*
5,
True,
IIf
(
Minute() ==
4 +
3*
5,
True,
IIf
(
Minute() ==
4 +
4*
5,
True,
IIf
(
Minute() ==
4 +
5*
5,
True,
IIf
(
Minute() ==
4 +
6*
5,
True,
IIf
(
Minute() ==
4 +
7*
5,
True,
IIf
(
Minute() ==
4 +
8*
5,
True,
IIf
(
Minute() ==
4 +
9*
5,
True,
IIf
(
Minute() ==
4 +
10*
5,
True,
IIf
(
Minute() ==
4 +
11*
5,
True,
False))))))))))));
//Entry only occurs at the end of each 5 minute bar.
//Bars must be setup in preferences for "Time stamp of compressed intraday
bars shows:
// START time of interval" as being checked off.
Cover = (TimeFrameExpand(LongSignal,
in5Minute) AND EntryTimeCondition)
OR (TimeFrameExpand(SignalLatchLong,
in15Minute) AND EntryTimeCondition)
OR TimeNum() > 155800;
Sell = TimeFrameExpand(ShortSignal,
in5Minute) OR TimeFrameExpand(SignalLatchShort, in15Minute)
OR TimeNum() > 155800;
Buy = TimeFrameExpand(LongSignal,
in5Minute) AND TimeFrameExpand(SignalLatchLong, in15Minute)
AND (TimeNum()>93300) AND (TimeNum()
<153000) AND EntryTimeCondition;
Short = TimeFrameExpand(ShortSignal,
in5Minute) AND TimeFrameExpand(SignalLatchShort, in15Minute)
AND (TimeNum()>93300) AND (TimeNum()
<153000) AND EntryTimeCondition;
// Trade Management
PriceatShort = 0;
LowSinceShort = 1000000;
PriceatBuy = 0;
HighSinceBuy = 0;
Exit = 0;
for
( i =
0; i <
BarCount;
i++)
{
if(PriceAtBuy == 0 AND Buy[i])
{
PriceAtBuy = BuyPrice[i];
BarCountBuy = BarCount;
}
if(PriceAtBuy > 0)
{
HighSinceBuy = Max(High[i-1] , HighSinceBuy);
if(Exit == 0
AND High[i] >= ((1 +
FirstProfitTarget)* PriceAtBuy))
{
// first profit target hit - Scale Out
Exit = 1;
Buy[i] = sigScaleOut;
BuyPrice[i] = ((1 + FirstProfitTarget)*
PriceAtBuy) ;
}
if(Exit == 0
AND High[i] >= ((1 +
SecondProfitTarget)* PriceAtBuy))
{
// Second profit target hit - Exit
Exit = 2;
Sell[i] = 1;
SellPrice[i] = ((1 + SecondProfitTarget)*
PriceAtBuy) ;
}
if((Low[i] <= (HighSinceBuy
*(1- TrailingStop))) AND (i !=
BarCountbuy))
{
// trailing stop hit - exit
Exit = 2;
Sell[i] = 1;
SellPrice[i] = (HighSinceBuy *(1-
TrailingStop));
}
if(Exit == 2)
{
Exit = 0;
PriceAtBuy = 0;
HighSinceBuy = 0;
}
}
if(PriceatShort == 0 AND Short[i])
{
PriceatShort = ShortPrice[i];
BarCountShort = BarCount;
}
if(PriceatShort > 0)
{
LowSinceShort = Min(Low[i-1] , LowSinceShort);
if(Exit == 0
AND Low[i] <= (PriceatShort *(1 -
FirstProfitTarget)))
{
// first profit target hit - Scale Out
Exit = 1;
Short[i] = sigScaleOut;
ShortPrice[i]=(PriceatShort *(1 -
FirstProfitTarget));
}
if(Exit == 0
AND Low[i] <= (PriceatShort *(1 -
SecondProfitTarget)))
{
// first profit target hit - Scale Out
Exit = 2;
Cover[i] = 1;
CoverPrice[i]=(PriceatShort *(1 -
SecondProfitTarget));
}
if((High[i] >= (LowSinceShort *
(1 + TrailingStop))) AND (i != BarCountShort))
{
// trailing stop hit - exit
Exit = 2;
Cover[i] = 1;
CoverPrice[i] = (LowSinceShort *(1+
TrailingStop));
}
if(Exit == 2)
{
Exit = 0;
PriceatShort = 0;
LowSinceShort = 1000000;
}
}
}
SetPositionSize
(
50,
spsPercentOfPosition*((
Buy ==
sigScaleOut)+(
Short
== sigScaleOut)));
//scale out of 50% of position
Mask = BarsSince(Sell) >= 5 AND BarsSince(Cover) >= 5;
Mask = IIf(IsNull(Mask), True, Mask); //
Convert Nulls to Trues
Buy = Buy AND Mask;
Short = Short AND Mask;