Simple cross over trading system

I am trying to write back testable trading system, Please help on following code.
I am struggling with following.

1.) Stoploss on signal's previous day's low and target next sell signal
2.) Condition1:- Say ema13 and ma34 cross over where when ema13>ma34
Condition2:- when condition1 satisfies, the high of the bar should be long entry if it's crossed by the next bar
If the next bar doesn't crosses the first signal bar when ema13>ma34 then entry gets revised to the next bar high

Say first bar(signal bar) high was 202 and next bar high is 201.4 now our entry gets above 201.4 but again if next bar is unable to cross 201.4 the entry has to be again revised to next bar high
If high doesn't breaks means trade not entered

https://forum.amibroker.com/t/edge-in-a-systematic-trading/11770?u=sandeep

_SECTION_BEGIN("13-34 Crossover Trading System");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g f%%) {{VALUES}}", O, H, L, C ));

//Initial Parameters

SetTradeDelays( 0, 0, 0, 0 );
SetOption( "InitialEquity", 500000);
SetOption("FuturesMode" ,False);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",50);
SetOption("AccountMargin",100);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(150,spsShares);
SetOption( "AllowPositionShrinking", True );
RoundLotSize=1;

//Parameters

MALength1 = 13;
MALength2 = 34;

//prevL = Ref( L, -1 ); // use previous bar low
//prevH = Ref( H, -1 ); // use previous bar high

//Buy-Sell Logic

//Buy = Cross(ema( C, MALength1 ),ema( C, MALength2 ));
//Sell  =Cross( ema( C, MALength2 ), ema( C, MALength1 )) ;


Bcond1 = Cross(ema( C, MALength1 ),ema( C, MALength2 ));
Scond1 =Cross( ema( C, MALength2 ), ema( C, MALength1 )) ;

Bcond2=H > Ref(H,-1);
Scond2=L < Ref(L,-1);

Buy = Ref( Bcond1, -1) AND Bcond2;
Sell = Ref(scond1,-1) AND scond2;
*/
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = Cover = 0;


BuyPrice=C;
SellPrice=C;
ShortPrice=C;
CoverPrice=C;


Plot( Close, "Price", colorWhite, styleCandle );
Plot(ema( C, MALength1 ),"FastEMA",colorRed);
Plot(ema( C, MALength2 ),"SlowEMA",colorBlue);

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

Filter = Buy OR Sell ;
AddColumn(Buy,"Buy");
AddColumn(Close,"RATE",1.2,colorBlack, IIf(Buy,colorAqua,colorPink));



_SECTION_END();

Please help , I have tried but not getting desired result.

maybe like this?

_SECTION_BEGIN( "13-34 Crossover Trading System" );

SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g f%%) {{VALUES}}", O, H, L, C ) );

//Initial Parameters

SetTradeDelays( 0, 0, 0, 0 );
SetOption( "InitialEquity", 500000 );
SetOption( "FuturesMode" , False );
SetOption( "MinShares", 1 );
SetOption( "CommissionMode", 2 );
SetOption( "CommissionAmount", 50 );
SetOption( "AccountMargin", 100 );
SetOption( "RefreshWhenCompleted", True );
SetPositionSize( 150, spsShares );
SetOption( "AllowPositionShrinking", True );
RoundLotSize = 1;

//Parameters
MALength1 = 13;
MALength2 = 34;

Bcond1 = Cross( ema( C, MALength1 ), ema( C, MALength2 ) );
Scond1 = Cross( ema( C, MALength2 ), ema( C, MALength1 ) );

inlong = Flip( Bcond1, Scond1 );
inshort = Flip( Scond1, Bcond1 );

Bcond2 = H > Ref( H, -1 );
Scond2 = L < Ref( L, -1 );

Buy = inlong AND Bcond2 AND BarsSince( Bcond1 ) >= 1;
Sell = inshort AND Scond2 AND BarsSince( Scond1 ) >= 1;
BuyPrice = Max( O, Ref( H, -1 ) );
SellPrice = Min( O, Ref( L, -1 ) );

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = Cover = 0;

Plot( Close, "Price", colorWhite, styleCandle );
Plot( ema( C, MALength1 ), "FastEMA", colorRed );
Plot( ema( C, MALength2 ), "SlowEMA", colorBlue );

/* Plot Buy and Sell Signal Arrows */
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -40 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -50 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -45 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 40 );
PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 50 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -45 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

Filter = Buy OR Sell ;
AddColumn( Buy, "Buy" );
AddColumn( Close, "RATE", 1.2, colorBlack, IIf( Buy, colorAqua, colorPink ) );

_SECTION_END();
5 Likes

ohh.. I am glad !! solution from LEGEND !!!!!!!

Thank you sir for your time , I appreciate !!!

Sir , only missing stoploss ( Stoploss on signal's previous day's low)

Thanx again

ok didn't see that. This is a but tricky because on some bars you get both a buy and a sell signal. So this is hard to solve just by using arrays (or at least for me).

so below I add an exit loop. I assume that when you both get a buy signal and a sell signal on the same bar you only take the sell signal. Is this what you meant?

_SECTION_BEGIN( "13-34 Crossover Trading System" );

SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g f%%) {{VALUES}}", O, H, L, C ) );

//Initial Parameters
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "InitialEquity", 500000 );
SetOption( "FuturesMode" , False );
SetOption( "MinShares", 1 );
SetOption( "CommissionMode", 2 );
SetOption( "CommissionAmount", 50 );
SetOption( "AccountMargin", 100 );
SetOption( "RefreshWhenCompleted", True );
SetPositionSize( 150, spsShares );
SetOption( "AllowPositionShrinking", True );
RoundLotSize = 1;

//Parameters
MALength1 = 13;
MALength2 = 34;

Bcond1 = Cross( ema( C, MALength1 ), ema( C, MALength2 ) );
Scond1 = Cross( ema( C, MALength2 ), ema( C, MALength1 ) );

inlong = Flip( Bcond1, Scond1 );
inshort = Flip( Scond1, Bcond1 );

Bcond2 = H > Ref( H, -1 );
Scond2 = L < Ref( L, -1 );

Buy = inlong AND Bcond2 AND BarsSince( Bcond1 ) >= 1;
BuyPrice = Max( O, Ref( H, -1 ) );
Sell = ( ( inshort AND Scond2 AND BarsSince( Scond1 ) >= 1 ) OR L < Ref( L, -1 ) );
SellPrice = Min( O, Ref( L, -1 ) );

BuyHelp = BuyHelpPrice = 0;
SellHelp = SellHelpPrice = 0;

slip = 0;
for( i = 0; i < BarCount; i++ )
{
    if( Buy[i] )
    {
        BuyHelp[i] = 1;
        BuyHelpPrice[i] = BuyPrice[i] + slip;

        for( j = i + 1; j < BarCount; j++ )
        {
            if( Sell[j] )
            {
                SellHelp[j] = 1;
                SellHelpPrice[j] = SellPrice[j] - slip;
                i = j;
                break;
            }
            else
                if( j == BarCount - 1 )
                {
                    i = BarCount;
                    break;
                }
        }
    }
}

Buy = BuyHelp;
BuyPrice = BuyHelpPrice;
Sell = SellHelp;
SellPrice = SellHelpPrice;

Short = Cover = 0;

Plot( Close, "Price", colorWhite, styleCandle );
Plot( ema( C, MALength1 ), "FastEMA", colorRed );
Plot( ema( C, MALength2 ), "SlowEMA", colorBlue );

/* Plot Buy and Sell Signal Arrows */
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -40 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -50 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -45 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 40 );
PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 50 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -45 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

Filter = Buy OR Sell ;
AddColumn( Buy, "Buy" );
AddColumn( Close, "RATE", 1.2, colorBlack, IIf( Buy, colorAqua, colorPink ) );

_SECTION_END();
1 Like

There is no loop required. Also (if using loop) there isn't nested loop required.

As for no loop... you just need ApplyStop function
See KB article here.

So since you use long only:

/// APPLYSTOP SNIPPET TO BE INCLUDED INTO YOUR CODE!
/// @link http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/
// define stop level
stopLevelLong = Ref( L, -1 ); // use previous bar low

// calculate stop amount
stopAmountLong = BuyPrice - stopLevelLong;

// make sure stop-amount is at least one tick
stopAmountLong = Max( TickSize, BuyPrice - stopLevelLong );

// assign stop amount conditionally by checking if there is a Buy signal on given bar
stopAmount = stopAmountLong;
ApplyStop( stopTypeLoss, stopModePoint, stopAmount, True );

To evaluate stops in your plot and explorer you need to use Equity() functon e.g.

if ( Status("action") == actionIndicator OR Status("action") == actionExplore) {
	eq = Equity(1, 0);
	
	Plot( Close, "Price", colorWhite, styleCandle );
	Plot( ema( C, MALength1 ), "FastEMA", colorRed );
	Plot( ema( C, MALength2 ), "SlowEMA", colorBlue );

	/* Plot Buy and Sell Signal Arrows */
	PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -40 );
	PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -50 );
	PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -45 );
	PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
	PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 40 );
	PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 50 );
	PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -45 );
	PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

	Filter = Buy OR Sell ;
	AddColumn( Buy, "Buy" );
	AddColumn( Close, "RATE", 1.2, colorBlack, IIf( Buy, colorAqua, colorPink ) );
}
4 Likes

yes I developed my own "style" maybe better to learn the "official" scripting language

with respect to the system. I suggest trying out the sort of the opposite. A simple long state like:

Bcond1 = Cross( C, ema( C, MALength1 ) );
Scond1 = Cross( ema( C, MALength1 ), C );

inlong = Flip( Bcond1, Scond1 );
inshort = Flip( Scond1, Bcond1 );

then buy when you cross lower than the previous day and sell if you croos higher (see chart). This way the slippage is in you favor and you can use slip = 0.

Below the chart with the basic idea. The blue area is the area where you can take long trades. Didnt test it extensively but it seems to give positive results

sandeep

1 Like

just looked at it again. Forget about this "just do the opposite" idea :slight_smile: The playback showed I made a mistake. I think I made this mistake about a 1000 times before.

2 Likes