Triple MA Crossover

#1
Hello all

I am new to Amibroker and I am trying to get a system together. I have been reading Bandys book on Quantitative trading systems and I am trying (desperately) to get my code to work. Here is the description of the triple-MA setup I would like to test.

1)* Use the Slowest MA as a filter
This is the first part.* The slow MA acts as a "trade" or "don't trade" filter.*
The idea to test is that I turn off all trades when price action is close to, ie touching, the slow MA. I only trade when there is "white space" between the price (shown as a bar chart) and the slow MA.Specifically, I am only allowed to go long when the lows of the past 10 days are all above the 200 day moving average. And, I am only allowed to go short when the highs of the past 10 days are all below the 200 day moving average.

2)* Take buy/sell and short/cover signals from the two shorter MA's.
When in long mode (determined by the slowest MA), you would take crossovers of the fastest and medium MA's as your buy/sell signals. When in long mode, there is no shorting.* Only long positions are allowed.

Therefore, the signals are:
(a)* Buy when the fast MA crosses above the medium MA ... ie Cross (MAFast, MAMed)
(b)* Sell when the fast MA crosses below the medium MA ... ie Cross (MAMed, MAFast)
When in short mode;
Short = Sell
Cover = Buy

3)* Every open position must be closed.
I'm not sure the current code matches up buy's and sells. Every buy must be followed by a sell.* And you can't initiate a short until a previous long position has been closed with a sell.*This is important.* I don't want orphaned open long positions because the proper "sell" signal didn't show up.

4)* I probably want to limit this to one open position.
Makes more sense to do that i think.* I can't see how I would have multiple open positions.* You're either in or out, long or short.* If the system says go long or short, we'd put the entire part of the portfolio set aside for index/currency into that trade.* Otherwise it sits in cash.

Now the problem is that the code complies and buys and sell signals are in the chart but the buys and sells are not where they are meant to be. There are buys going in when the Slow MA has been penetrated by a Low in the past 10 days. I was hoping that you guys might be able to tell me what is going wrong? Am I using the LLV function correctly? Is my logic completely screwed?

Help ! :)



######################################
CODE
######################################
// Triple Moving Average Crossover System
//----------------------LIBRARY------------------------------

// Set up the lengths for the moving averages
LengthFast = Optimize("FastMA",3,1,50,2);
LengthMed = Optimize("MedMA",20,1,100,2);
LengthSlow = Optimize("SlowMA",100,1,150,3);
LengthLow = Optimize("LowL",10,1,30,1);
LengthHigh = Optimize ("HighH", 10,1,30,1);

// The Moving Average Calculations
MAFast = EMA(C,LengthFast);
MAMed = EMA(C,LengthMed);
MASlow = EMA(C,LengthSlow);

// 10 Day high/low
TenDayLow = LLV(L,10);
TenDayHigh = HHV(H,10);


//-----------------BUY & SELL LOGIC-------------------------
*for( i = 1; i < BarCount; i++ )
{
*** if ( TenDaylow * > MASlow )
*** {
*** *** Buy = Cross ( MAFast, MAMed);
*** }
*** if ( TenDaylow * > MASlow )
*** {
*** *** Sell = Cross ( MAMed, MAFast);
*** }
}

//----------------------STATEMENTS---------------------------
//* Plots the price series on the chart
Plot( Close, "Price",colorBlack,styleBar);

// Plot the MA Lines
Plot (MAFast, "MAFast", colorRed,styleLine);
Plot (MAMed, "MAMedium", colorBlue, styleLine);
Plot (MASlow, "MASlow", colorGreen,styleLine);

// Plot the Buy and Sell arrows
BuyArrow = Buy * shapeUpArrow;
SellArrow = Sell * shapeDownArrow;
PlotShapes(BuyArrow, IIf(Buy, colorGreen,colorBlack), 0 , IIf(Buy,Low,High));
PlotShapes(SellArrow, IIf(Sell, colorDarkGreen,colorBlack), 0 , IIf(Sell,High,Low));

//-----------------------EQUITY CURVE---------------------
// Compute the Equity for the single ticker if
e = Equity();
Maxe = LastValue(Highest(e));

// Plot the Equity Curve

Plot(e, "Equity", colorBrown,styleLine|styleOwnScale,0,Maxe);
Plot(1000,"",colorBlue,styleLine|styleOwnScale,0,Maxe);
//---------------------------END------------------------------
 

rajeshn2007

Well-Known Member
#2
hi Rajeev,

Before going in detail of your strategy, here are some codes to make use of.

remove excessive buy/sell signals, so that you get a single long/short signal.

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
 
#3
Rajesh

That's great it has cut down some of the clutter, thanks a lot

I'm still not sure why the LLV is not working! there are still positions going in when there is a Low in the last 10 days crossing the SlowMA

any ideas?

Thanks again

Rajeev
 

vinodkiyer

Well-Known Member
#5
Try This instead of the above:

// Triple Moving Average Crossover System
//----------------------LIBRARY------------------------------

// Set up the lengths for the moving averages
LengthFast = Optimize("FastMA",3,1,50,2);
LengthMed = Optimize("MedMA",20,1,100,2);
LengthSlow = Optimize("SlowMA",100,1,150,3);
LengthLow = Optimize("LowL",10,1,30,1);
LengthHigh = Optimize ("HighH", 10,1,30,1);

// The Moving Average Calculations
MAFast = EMA(C,LengthFast);
MAMed = EMA(C,LengthMed);
MASlow = EMA(C,LengthSlow);

// 10 Day high/low
TenDayLow = LLV(L,10);
TenDayHigh = HHV(H,10);
Longbase=HHV(MASlow,10);
Longs=TenDayLow>Longbase;
Shortbase=LLV(MASlow,10);
Shorts=TenDayHigh<Shortbase;


//-----------------BUY & SELL LOGIC-------------------------
Buy = Longs && Cross ( MAFast, MAMed);
Sell = Shorts && Cross ( MAMed, MAFast);


//----------------------STATEMENTS---------------------------
// Plots the price series on the chart
Plot( Close, "Price",colorBlack,styleBar);

// Plot the MA Lines
Plot (MAFast, "MAFast", colorRed,styleLine);
Plot (MAMed, "MAMedium", colorBlue, styleLine);
Plot (MASlow, "MASlow", colorGreen,styleLine);

// Plot the Buy and Sell arrows
BuyArrow = Buy * shapeUpArrow;
SellArrow = Sell * shapeDownArrow;
PlotShapes(BuyArrow, IIf(Buy, colorGreen,colorBlack), 0 , IIf(Buy,Low,High));
PlotShapes(SellArrow, IIf(Sell, colorDarkGreen,colorBlack), 0 , IIf(Sell,High,Low));

//-----------------------EQUITY CURVE---------------------
// Compute the Equity for the single ticker if
e = Equity();
Maxe = LastValue(Highest(e));

// Plot the Equity Curve

Plot(e, "Equity", colorBrown,styleLine|styleOwnScale,0,Maxe);
Plot(1000,"",colorBlue,styleLine|styleOwnScale,0,Maxe);
//---------------------------END------------------------------
 

Similar threads