[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Same Bar Buy & Sell



PureBytes Links

Trading Reference Links

Hi

All seemed to be going Ok with the code below until I found that a 
Sell on the same bar as the Buy is not being actioned  and therefore 
the position remains open for ever.  In the cases of same bar extry & 
exit Sell is assigned the value of 1, but it doesn't get actioned..

Other exits seem to be fine.

Obviously, I have not got it right.  Any suggestions, please.

The following code has been simplified a little to remove clutter.

Thanks

Graham


//==================  Inital Equity & Margin  ==================
vCapital = 30000;						
								
					
//==================  Initial Trade Parameters  ==================
SetOption("AccountMargin", 10);					
	// Margin%					
SetOption("ActivateStopsImmediately", True);			// 
IntraDay Stops				
SetOption("AllowPositionShrinking", False); 			// 
Take partial trades if equity available
SetOption("AllowSameBarExit",True);				
	// Permit same bar exit for profit stops		
	
SetOption("CommissionMode", 0);					
	// 1=Percent trade size commission, 2= $ per trade	
SetOption("FuturesMode",False);					
						
SetOption("InitialEquity", vCapital );				
							
SetOption("InterestRate",0); 					
		// Set interest rate earned for free cash, zero to 
evaluate system	
SetOption("MaxOpenPositions", 6);				
		// Max no positions				
			
SetOption("MinPosValue",1);					
			// Min position value to make trade worthwhile
SetOption("MinShares", 1);					
			// Min No shares zero
SetOption("PriceBoundChecking", True ); 			
	// Price to stay in bar range				
SetOption("UseCustomBacktestProc", True);			
	// Use Custom backtest for reporting
SetOption("UsePrevBarEquityForPosSizing", True ); 	// Use last 
known bar of position sizing					
		

SetTradeDelays(0,0,0,0); 					
			// Intraday Entry & Exit signals, no forward 
references

vaValidEntry = Cross( Close, EMA( Close, 45 ) );

//==================  Limit Entry  ==================
vaLimitEntry = Close + (0.8 * (High - Low));

//==================  Stop Exit  ==================
vISATRPer = 30;	//15;
vISMult = 6;
vaIStopVal = ATR(vISATRPer) * vISMult;

//==================  Target Exit  ==================
vaTargetVal = Ref(H, -3);

//==================  Position Size  ==================
vMaxPosn = 10000;
vMaxRisk = 2000;
vMaxTOPc = 3;
vaPosnSize = vMaxRisk / Ref(vaIStopVal, -1) * Ref(vaLimitEntry, -1);
vaPosnSize = Min(vaPosnSize, vMaxPosn);
SetPositionSize(vaPosnSize, spsValue);

//==================  Trade  ==================
Sell = 0;
Buy = Ref(vaValidEntry,-1) AND Low <= Ref(vaLimitEntry, -1);
BuyPrice = Min(Open, Ref(vaLimitEntry, -1));
Sell = 0;

vaGapDown = GapDown();
vaGapUp = GapUp();
vaInitStop = Null;
vaSellStop =  0;
vaSellTarget = 0;
VEntryBar = 0;
vTempStop = 0;
vTempTarget = 0;
vTradeLength = 8;
vTrigger = 0;

for(vCnt = 1; vCnt < BarCount; vCnt++)
{
	if(vTempStop == 0
		AND Buy[vCnt] == 1)	// Entry Bar & 0 positions 
open
	{
		// Position Opened		
		vTempStop = BuyPrice[vCnt] - vaIStopVal[vCnt - 1];
		vTempTarget = High[vCnt - 3];
		vEntryBar = vCnt;
	}
	else
	{
		Buy[vCnt] = 0;	// Remove excess Buy signals
	}	// Not Entry Bar

	if(vTempStop == 0)
	{
		vaTargetVal[vCnt - 1] = Null;
	}
	else
	{
		if (vCnt - vEntryBar == vTradeLength + 1)
		{
			vTempStop = Max(vTempStop, BuyPrice
[vEntryBar]);
		}	// test # Bars Open

		if (vCnt > vEntryBar + 1)	// eliminate Entry Bar
		{
			if (vaGapDown[vCnt - 1] == 1)
			{
				vTempStop = Max(vTempStop, Low[vCnt - 
1]);
			}	// GapDown

			if (vaGapUp[vCnt - 1] == 1)
			{
				vTempStop = Max(vTempStop, High[vCnt -
 1]);
			}	// GapUp
		}	// Position Open & not Entry Bar

		vaInitStop[vCnt - 1] = vTempStop;		
		if(Low[vCnt] <= vaInitStop[vCnt - 1])
		{
			Sell[vCnt] = 1;
			SellPrice[vCnt] = Min(Open[vCnt], vaInitStop
[vCnt - 1]);
			vTempStop = 0;
		}	
		else
		{
			if(High[vCnt] >= vaTargetVal[vCnt - 1])
			{
				Sell[vCnt] = 1;
				SellPrice[vCnt] = Max(Open[vCnt], 
vaTargetVal[vCnt - 1]);
				vTempStop = 0;
_TRACE("Bar " + BarIndex() + " Buy " + Buy + " Sell " + Sell + " 
Target " + vaTargetVal + " Stop " + vTempStop);
			}
		}	// exits
	}	// Position Open
}	// for

// Remove excess signals					
			
Buy = ExRem(Buy, Sell);						
		//  Remove additional signals, 
Sell = ExRem(Sell, Buy);



Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/