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!
"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!