Modify limitprice during backtesting procedure

I have a strategy that works with limit orders on the next. The strategy has between zero and 10 amount of signals per day. I want to be able to change the limit value of the trade depending on how many signals there are at a given moment.

I created a custom backtester and I can get the amount of signals per day. But when I try to change the value of the buy limit it does not work.

My code on the backtester:

StaticVarSet(Ticker + "BuyLimit", BuyL);

...

for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
{ 
	if (sig.IsEntry() && sig.IsLong())  
	{
	
		LimitValue = StaticVarGet(sig.Symbol + "BuyLimit");
		LowP = Foreign(sig.Symbol, "Low");
		LimitValue = LimitValue - (Reduction * SignalAmount);
		
														
		if ( LimitValue[i] < LowP[i])
		{				
			sig.Price = -1;
		}

Without the reduction it works just fine, its once i try to modify the value of "LimitValue" in the custom backtester that it goes wrong. Im not sure it is even possible what I am trying to achieve. Any help would be welcome

You haven't really provided enough details for anyone to help you. How is Reduction calculated? When you say the "backtester goes wrong", what does that mean? Are you updating sig.Price with the revised LimitValue?

1 Like

The reduction is simply ATR(14) * TotalSignals

I am not updating the sig.Price. Am I supposed to? I assumed the backtester did that itself

LimitPrice is array (AFAICS).
And Reduction is array type.

But if you have arrays then rather use subscript [ i ] within BarCount loop.


Foreign should not be used in CBT.
Better use Static var.

Please follow this advice: How to ask a good question

In short, you need to provide copy-pasteable minimum working example of the problem you are having. The portion that you sent is not meeting this criteria.

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

Oh yes you do update it:

		if ( LimitValue[i] < LowP[i])
		{				
			sig.Price = -1; // HERE you are updating it
		}

And why do you assume that backtester should modify the prices you specified ? Backtester does exactly what YOU told it to do, nothing more.

2 Likes

Yes, if you are using bo.ProcessTradeSignals(), then of course you'd have to update the price inside the signal object (i.e. sig.Price) because how would the backtester magically know you're using your own variable named LimitValue? If you're using a low-level CBT (calling bo.EnterTrade() and bo.ExitTrade() instead of bo.ProcessTradeSignals()), then you just have to pass the correct price when you're entering the trade. But once again, you haven't given us enough information to really know what you're doing so that leaves us guessing.

Thx for all the replies. Indeed updating the sig.Price was the issue. I updated the code to:

StaticVarSet(Ticker + "BuyLimit", BuyL);

...

for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
{ 
	if (sig.IsEntry() && sig.IsLong())  
	{
	
		LimitValue = StaticVarGet(sig.Symbol + "BuyLimit");
		LowP = Foreign(sig.Symbol, "Low");
		LimitValue[i] = LimitValue[i] - (Reduction * SignalAmount);
												
		if ( LimitValue[i] < LowP[i])
		{				
			sig.Price = -1;
		}
        else
        {
            sig.Price = sig.Price - (Reduction * SignalAmount);
        }

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