MA PERFECT ORDER CODE

Posted By: Sage

MA PERFECT ORDER CODE - 12/06/21 18:09

I'd love to code a moving average strategy. I saw it in a book and it goes

"Buy when 10SMA is above 20, 20 above 50 and 50 above 100. Exit when 10SMA crosses under the 20 SMA. (The reverse for a short trade)
In the book, 200 SMA was utilized but I cut it out personally.

I tried coding it this way but doesn't seem to work, guess I need a correction


function run()
{

SMA10 = series(SMA(seriesC(),10));
SMA20 = series(SMA(seriesC(),20));
SMA50 = series(SMA(seriesC(),50));
SMA100 = series(SMA(seriesC(),100));

if(crossOver(SMA10, SMA20) && crossOver(SMA20,SMA50) && crossOver(SMA50,SMA100))
enterLong();

if(crossUnder(SMA10, SMA20) && crossUnder(SMA20,SMA50) && crossUnder(SMA50,SMA100))
enterShort();

if(crossUnder(SMA10,SMA20)) exitLong();
if(crossOver(SMA10,SMA20)) exitShort();

}


Would be looking forward for the correct code. Thanks!
Posted By: jmlocatelli

Re: MA PERFECT ORDER CODE - 12/06/21 18:24

Your strategy says buy when 10SMA IS above 20SMA and 20SMA IS above 50SMA and so forth. It does not say buy when CROSS above.
The code should be:

if(SMA10[0]>SMA20[0] and SMA20[0]>SMA50[0] and SMA50[0]>SMA100[0])
enterLong();
Posted By: Sage

Re: MA PERFECT ORDER CODE - 12/07/21 00:41

Thank you
© 2024 lite-C Forums