Buy from multiple intraday bars for certain cash value

Hello, i am working on 5 mins bars and i want my code to buy 5% from multiple bars till it reach 50000$ then it should sell and it can't sell before it bought all the amount or if the day ended.
Here is part of my code.
my problem is i don't know how to keep buying till i reach the 50000$.

SetPositionSize(5,spsPercentOfEquity);
Buy = iif(buySignal,sigScaleIn,0);
NewDay = Day()!= Ref(Day(), -1) ; 
buy=exrem(buy,newday);

https://www.amibroker.com/guide/a_custombacktest.html
http://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/

does it have to be CBT can't it be done by the normal backtesting ?

No, because of

Have you checked out second link completely?
There are example codes.

@mennatareeq,

See

1 Like

Hi, so i have been trying out to understand the codes from the CBT but my code still doesn't work,
What i want to do is that it should buy from multiple tickers (5 minutes) till it bought with a money amount of 50000, and if it reach that value or the day ends it should wait for the next sell signal to sell all , then it should start all over again


PositionSize = -5;
setCustomBacktestProc("");

if (Status("action") == actionPortfolio)
{
    bo = GetBacktesterObject();    //  Get backtester object
    bo.PreProcess();    //  Do pre-processing
    for (i = 0; i < BarCount; i++)    //  Loop through all bars
    {
        for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
        {    //  Loop through all signals at this bar
            if (sig.IsEntry())    //  If this signal is a long entry (ie. buy)
            {
                evol = Foreign("~evol_"+sig.Symbol, "V");   //  Get stock's composite volume array
                psize = sig.PosSize;    //  Get position size specified in AFL code
                if (psize < 0)    //  If it's negative (a percentage of equity)
                    psize = (-psize/100) * evol;  //  Convert to dollar value using current equity
                scnt = psize / sig.Price;    //  Calculate number of shares for position size
				psize = scnt * sig.Price;    //  Calculate new position size

				D = IIf (Day()!= Ref(Day(), -1),1,0);
				if (bo.Equity - bo.Cash >= 50000 OR D[i] > 1)    //  If position size is greater than $50,000
					{
					psize = 0;
					Buy[i] = 0; 
					}  
				else
				{
					
					Sell[i] = 0;
				}
                sig.PosSize = psize;    //  Set modified position size back into object
                
            }
            else
            {
				Buy[i] = 0;
            
            }

        }    //  End of for loop over signals at this bar

        bo.ProcessTradeSignals(i);    //  Process trades at this bar
    }    //  End of for loop over bars
    bo.PostProcess();    //  Do post-processing
}

There are multiple errors in your CBT code.

Sort out your licence badge for the forum (as noted in the above post from @TrendSurfer) and we can assist you with a solution.

1 Like

so no one will help me out if i am working on a free version ?

Should they? You haven't paid the owner for his product that you are using!

Only paid-up forum members can post in this section of the forum.

I am not stealing from him or something ! He made it available for free and i am using the free version

@mennatareeq what free version? The free 30 day trial version? You have been a member of this forum for 13 months, how is your version "free"? Also the forum is for support of version 6.0 and later. The forum users are paying to ensure future updates, improvements and development of the software. Why would they support you if you refuse to support the community.

4 Likes