Long on at close sell atthe open and short at the open and cover

Hi All,

I am trying to do , in theory , a very basic System , however I am not able to come up with the code.
Plain Rules:
Long on the close of today´s bar.
Sell: on open of tommorrow´s bar; then reverting with short on the same open and Cover on the same day on the close.

I enter long on the close of March 23 and I sell it at the open price of MArch 24 and on March 24 a enter short on the open and close on the 24th at the close.
It is a stop-reverse system.

I read the manual " REsolving Same bar, same symbols signal conflicts" and also the guide " short trade suport" but I am not able to get what I have explained about. I tried with the AllowsamebarExit, with the Holdminbars and with the tradedelay and I am not able to get it.
This is the basic code that I Have to starts , I am sure there is a way to do it but I do not see.
Thanks a lot in advance

t//Long: Buy en el Close  y Sell en el Open next Day
//Short: Short en el Open y Cover en el Close same day





OwnCapital 			= 100000; 		// Initial capital
SetBacktestMode(backtestRegular);					 // Backtest mode - raw used so that Amibroker doesn't automatically filter out excessive signals
SetOption("InitialEquity",OwnCapital);					 // Initial capital, using OwnCapital variable from part 1
//SetOption("AllowSameBarExit",true);					 // Disable same bar exit as the strategy exits at open of the following day
 SetPositionSize(100,spsPercentOfEquity); //we invest 100% of equity - compounding 
ype or paste code here

That would account for two trades per single bar and that is only doable using custom backtester. Regular backtest only allows one trade per bar (you can have TWO signals entry and exit OR exit and entry on single bar, in your example you have THREE in single bar - exit, entry and exit).

If you use custom backtester you can go circles and have hundreds of same bar trades if you wish.

The CBT code below handles scalein and exit on same bar. adapt this to your needs.

SetCustomBacktestProc( "" );
_TRACE( "!CLEAR!" );
_SECTION_BEGIN( "Price" );
SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
PlotOHLC( O, H, L, C, "Traded", colorRed, styleBar , Null, Null, 0, 1, 1 );

dt = DateNum();
tnum = timenum();

Sell = 0;
Buy = 0;

Buy = IIf( tnum == 110000, 1, Buy );

buy = IIf( tnum == 134500, sigscaleout, buy );

/*Scalein and Sell occurs in same bar. This is the scenario : bar opened above the stop price. 
We scaled-In hoping to see a price rise but the price dropped and stop was hit. So scaleIn happened first and the stop was hit.*/

buy = IIf( tnum == 151500, sigscaleIn, buy );
BuyPrice = 345.35;

Sell = IIf( tnum == 151500, 1, sell ); //ohh stop hit
SellPrice = 344.15;

SetPositionSize( 10, spsPercentOfEquity );
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut  ) ); // scale out 50% of position
SetPositionSize( 50, spsshares * (Buy == sigScaleIn));
//_TRACE("buy="+Buy+", sell="+Sell);

if( Status( "action" ) == actionPortfolio )
    bo = GetBacktesterObject(); // Get backtester object
    //bo.Backtest( True ); // Run backtests with no trade listing
    stat = bo.GetPerformanceStats( 0 ); // Get Stats object for all trades

    dt = DateTime();
    thisBarDate = DateTimeConvert( 0, dt );
    for( i = 0; i < BarCount; i++ )
        formatedDateTime = DateTimeFormat( "%d-%m-%Y %H:%M:%S", dt[i] );

        for( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )

            if( sig.Type == 1 || sig.Type == 3 || ( sig.Type == 5 && openPosition ) ) //if buy, short or scalein

                _TRACE( "sig.type = " + sig.Type + " for "
                        + sig.Symbol + " sig.PosSize =" + sig.PosSize + ",  sig.Price = " + sig.Price + ", Buy[i]="+Buy[i]+", at " + formatedDateTime );
                        //This scenario (described above) has to be handled explicitly using low-level programming
                       if(Buy[i]==sigScaleIn && Sell[i])
						bo.ScaleTrade(i, sig.Symbol, true, sig.Price, sig.PosSize);
						bo.ExitTrade(i, sig.Symbol, SellPrice[i]);


        bo.ProcessTradeSignals( i );



Thanks a lot Thomas for your answer

Thanks so much Algo Enthusiastic. I still do not know well how to code the CBT, so I really appreciate you provide me with the CBT code . GREATTTTTT

Thanks again. Unfortunatelly the link to the turorial does not work . I get the followin message "The transfer you requested has been deleted". Any other link to get such tutorial??

Actually due to severe restrictions on the forum it does not allow sharing file links. If you share your email by pm i shall send it to you.


What do you mean by "pm"? and thanks a lot for the PDF about the CBT

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