How to close at same day when i entry

Hello.

I have a big problem. When i codding mi ALF, y need to cover, same day as i sell.

cover = C
coverprice = C

dont work, because it exit at next trading day.

How can i do it ?

Thanks for all!

There is too much missing information.
Do you trade daily of intraday?

If you trade daily, your system will be in real trading very prone to slippage and/or catching the correct open price.

For EOD stuff, you could try trading on Open:

SetOption("AllowSameBarExit",true);//
SetTradeDelays(1,1,1,1);
BuyPrice=O; 
SellPrice=O; 

And then as a start, try looking at the applystop function, type n-bar.

https://www.amibroker.com/guide/afl/applystop.html

Hi. This should get you going. I use this in my own code.



// Market Start/Stop variables
symbol = Name();											// symbol we are trading/looking at
CloseAtEOD = ParamToggle( "Close At EOD", "Off|On", defaultval = 0);		// close the open orders at EOD or not

DOW = Now( 9 );												//day of the week
WeekEnd = IIf( ( DOW == 1 OR DOW == 7 ), 1, 0 );
MarketOpen = 093000;						// 09:30:00;
MarketEntryOffVal = 153000;					// don't go long at 15:30 or more
MarketCloseMinusOneMinute = 155900;			// 15:59:00 - EOD close of orders if the flag is set on
MarketClose = 160000;						// 20:00:00 or 163000 or 160000

// Time: Now( 4 )  format = 4 - returns TIMENUM number with current time 
MarketON = (Now( 4 ) >= MarketOpen) AND (Now( 4 ) < MarketClose) AND NOT WeekEnd;
MarketOFF = Now( 4 ) >= MarketClose ;									//day's over
MaketEntryOFF = Now( 4 ) >= MarketEntryOffVal ;							//day's over for entries
MarketCloseAllPos = Now( 4 ) > MarketCloseMinusOneMinute ;				// one minute to close all positions

// have we enabled auto trading on this contract
ibc = GetTradingInterface( "IB" );
StaticVarSet( "IBControllerConnectionStatus", ibc.IsConnected() );		// get the connection status, 2 is OK
LastTWSMsg = ibc.getLastError( 0 );
if ( StaticVarGet( "IBControllerConnectionStatus" ) == 2 OR StaticVarGet( "IBControllerConnectionStatus" ) == 3 ) // connected to TWS with no error messages
{
	if( ibc.GetPositionSize( symbol ) > 0 ) 							// Set the flag if we have an open position 
	{ 
		MarketPosition = 1; 											// we are long
		
	} else (ibc.GetPositionSize( symbol ) == 0 ) {

		MarketPosition = 0; 											// we are flat

	}
}


if (LastValue( MarketON ) )
{
	SetChartBkColor( colorGreen );										// turn this Green for live
} else {
	SetChartBkColor( colorYellow );										// turn this Yellow for a closed market
}
			

index = BarCount - 1; 													// this is the bar index we are working on	
if (LastValue(MarketON))												// the market is on
{
	if (MarketPosition[index] == 0)												// if we are NOT Long - Flat, then look for a long
	{
		if (MaketEntryOFF[index] AND CloseAtEOD[index])							// turn off long entries at the defined point if CloseAtEOD is on
		{
			_TRACE ("<" + symbol + "> Market is NOT accepting entry orders - MaketEntryOFF <" + MaketEntryOFF[index] + ">, CloseAtEOD <" + CloseAtEOD[index] + ">");

			// DO NOT ENTER TRADES HERE

		} else {

			_TRACE ("<" + symbol + "> Market is OPEN - MaketEntryOFF <" + MaketEntryOFF[index] + ">, CloseAtEOD <" + CloseAtEOD[index] + ">");

			// OPEN AND MANAGE TRADES HERE

		}
	}
}

// Exit signals
if (MarketPosition[index] == 1)												// if we are Long check for SELLS or Stops
{
	// now exit the long positions if we are closing at the EOD		
	if ( MarketCloseAllPos AND CloseAtEOD  )			//  If we have turned on the close at the EOD, overide the entry and exits to exit
	{
		_Trace ("Market is NOT accepting entry orders - MaketEntryOFF <" + MaketEntryOFF[index] + ">, CloseAtEOD <" + CloseAtEOD[index] + ">");
		
		// CLOSE YOUR OPEN TRADES HERE IF End of DAY
		
	} else {
	
		// MANAGE YOUR EXITS HERE
	
	}  
4 Likes
FTT = 091500;   // Trading start time			
LTT = 140000;   //Last trading time
Exit = 150000;    //Exit time intraday

Buy=  (buy conditaion) AND TimeNum() <= LTT AND TimeNum() >= FTT  ; 
Short =  (short condition) AND TimeNum() <= LTT AND TimeNum() >= FTT ;
Sell=   TimeNum() >= Exit ;
Cover=   TimeNum() >= Exit;