Limting trade 1 Trade Per Day for Futures

Dear Friends,

Using the following code for Limit One Trade per day.
Result in Limited 1 Buy Trade + 1 Short Trade per day
How can I amend to be only 1 Buy trade or short trade per day only, and stop trade after closing the position for 1st trade?

Thanks!

// trades limit
N = 2;

Plot( Close, "Close", colorDefault, styleBar );

// identify new day
dn = DateNum();
newDay = dn != Ref( dn, -1 );

// buy and sell signals
Buy =Cover=  MACD() > Signal(); // sample repeated signals
// exit on signal vs macd crossover or last bar of the day
Short=Sell= Cross( Signal(), MACD() ) OR Ref( newday, 1 ); 

// remove redundant signals
Equity( 1 );

// modify Buy array and allow only first N signals
Buy = Buy AND Sum( Buy, BarsSince( newDay ) + 1 ) <= N;
Short = Short AND Sum( Short, BarsSince( newDay ) + 1 ) <= N;

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

if you use buy and short then you could first define a dummy array iBuy and iShort. See code

function SumSinceInclusive( condition, array )
{
    return SumSince( condition, array ) + ValueWhen( condition, array );
}

// number of trades limit per day
Ntrades = 4;

// identify new day
dn = DateNum();
newDay = dn != Ref( dn, -1 );

// buy and sell signals
iBuy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() ) OR Ref( newday, 1 );

iShort = Cross( Signal(), MACD() );
Cover =  Cross( MACD(),  Signal() ) OR Ref( newday, 1 );
//iShort = Cross( EMA( C, 20 ), C );
//Cover =  Cross( C,  EMA( C, 20 ) ) OR Ref( newday, 1 );

Buy = iBuy AND SumSinceInclusive( newDay, iBuy OR iShort ) <= Ntrades;
Short = iShort AND SumSinceInclusive( newDay, iBuy OR iShort ) <= Ntrades;

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

BuyPrice = SellPrice = ShortPrice = CoverPrice = C;

// back test for futures
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "FuturesMode", True );
SetOption( "PriceBoundChecking", False );
SetOption( "AllowSameBarExit", True );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 3 );
SetPositionSize( 1, spsShares );

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );
Plot( newDay, "", colorDarkBlue, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 5 );

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, IIf( Buy AND Cover, -35, -15 ) );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, IIf( Sell AND Short, -35, -15 ) );
PlotShapes( IIf( Buy, shapeSmallSquare, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeSmallSquare, shapeNone ), colorWhite, 0, SellPrice, 0 );

PlotShapes( IIf( Cover, shapeUpTriangle, shapeNone ), colorBrightGreen, 0, L, -15 );
PlotShapes( IIf( Short, shapeDownTriangle, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Cover, shapeSmallSquare, shapeNone ), colorWhite, 0, CoverPrice, 0 );
PlotShapes( IIf( Short, shapeSmallSquare, shapeNone ), colorWhite, 0, ShortPrice, 0 );

counter = SumSinceInclusive( newDay, Buy OR Short );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
ft = "Arial Black";
sz = 12;

for( i = fvb; i <= lvb; i++ )
{
    if( Buy[i] OR Short[i] )
    {
        str = "" + counter[i];

        if( Short[i] AND Sell[i] )
        {
            pos = H[i];
            dis = sz * 5;
        }

        if( Short[i] AND !Sell[i] )
        {
            pos = H[i];
            dis = sz * 3.5;
        }

        if( Buy[i] AND Cover[i] )
        {
            pos = L[i];
            dis = sz * -6;
        }

        if( Buy[i] AND !Cover[i] )
        {
            pos = L[i];
            dis = sz * -4.5;
        }

        PlotTextSetFont( str, ft, sz, i, pos, colorBlack, colorWhite, dis );
    }
}
1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.