Buy at BuyPrice, Don't if Low is Greater

Hello again All!

I'm wondering if there's perhaps a way to generate a list of buy signals, take the top ten, then if the buyPrice is greater than the next day's low, then the program buys, and if not, the buy is cancelled. My tested buyPrice is 0.8 * Close, but the trades go through regardless of the Low's price.

In case anyone has a better method: I'm looking to make a system that makes a limit order at the start of each day for ten stocks, then cancels the orders if they're not filled at the end of the day. So if anyone has a better way to interpret that, I'm curious. Below is the CBT attempt at it.


BuyPrice = 0.8 * Close;
Buy = Buyit; // previously declared in the code, just including essentials
Sell = Sellit; //same
Lower = Ref(Low, 1);

SetCustomBacktestProc("");
if(Status("action") == actionPortfolio)
{
	bo = GetBacktesterObject();
	bo.PreProcess();
	for(i=0; i<BarCount; i++)
	{
		x = bo.GetOpenPosQty;
		for(sig=bo.getfirstsignal(i); sig; sig=bo.getnextsignal(i))
		{
			if(x < 10 AND bo.Equity / 10 > bo.Cash)
			{
				if(sig.IsEntry())
				{
					x++;
					
					if(maxPrice[i] > Lower[i])
					{	
						printf("HOI");
						bo.EnterTrade(i, sig.Symbol, sig.IsLong, sig.Price, sig.PosSize, sig.PosScore);
					}
				}
			}
			else if(sig.IsExit())
			{
				bo.ExitTrade( i, sig.Symbol, sig.Price );
			}
		}
		bo.processtradesignals(i);
	}
	bo.postprocess();
}

Edit: Just so we're aware, I based this heavily off of pmxgs's code at the following link, which is in turn based off of the code from the introduction to CBT. backtestRegularRawMulti Mode in low-level CBT

Hi,
can you provide a code that can be copy&paste in an Amibroker session? People are wasting their time if you do not provide a testable code.
Why do you not work with SetOption( "MaxOpenPositions", 10);? Why can you not add the buyprice > low condition to your Buy definition?

1 Like

Sorry, here's a completed version. The MaxOpenPositions partially helps, but it does not ultimately solve the issue. If I add buyprice > low to the buy condition, the program will select other buys, based on prices in the future, and that's ultimately not what I'm looking for.

p = 0.001;
maxPrice = p * Close;
BuyPrice = p * Close;
SellPrice = Ref(Open,1); 


SetPositionSize(10, spsPercentOfEquity);



SetOption("InitialEquity", 1000 );
SetOption("MinShares", 1);


fast = EMA(Close, 50);
slow = EMA(Close, 200);


delta = (fast - slow) / (fast + slow) / 2;
PositionScore = 10 - delta;



//deltaNum = 0;
Buy =  delta < 0;
Sell = delta > 0;



Lower = Ref(Low, 1);

SetCustomBacktestProc("");
if(Status("action") == actionPortfolio)
{
	bo = GetBacktesterObject();
	bo.PreProcess();
	for(i=0; i<BarCount; i++)
	{
		x = bo.GetOpenPosQty;
		for(sig=bo.getfirstsignal(i); sig; sig=bo.getnextsignal(i))
		{
			if(x < 10 AND bo.Equity / 10 > bo.Cash)
			{
				if(sig.IsEntry())
				{
					x++;
					
					if(maxPrice[i] > Lower[i])
					{	
						printf("HOI");
						bo.EnterTrade(i, sig.Symbol, sig.IsLong, sig.Price, sig.PosSize, sig.PosScore);
					}
				}
			}
			else if(sig.IsExit())
			{
				bo.ExitTrade( i, sig.Symbol, sig.Price );
			}
		}
		bo.processtradesignals(i);
	}
	bo.postprocess();
}

Search the forum for "limit order" and you should find some helpful posts.

Dunno how I missed that key statement, should've just looked that up that specific sentence. Sorry for the trouble.

1 Like

See:

Ah, yeah. Ran into that right after I remembered the term was limit order. Sorry for the trouble with this, and thank you all for the help!

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