# SellPrice and CoverPrice wrong inside the loop

Hello!

I´m having an issue with SellPrice and CoverPrice and I just can´t figure how to fix it.

Here´s part of the code to you understand:

``````TPBuy1 = 1300;

for( i = 0; i < BarCount; i++ )
{
if( newDay[ i ] ) { tradeCount = 0; onBuy=0; onShort=0; }

Sell[i]=1;
}

...
``````

But let´s say that the values of the bar/candle where the conditions are met have is:

Open = 1100
High = 1500
Low = 1000
Close = 1200

When I run the backtest the Exit Price is the High (1500) and not the SellPrice (TPBuy1) = 1300

I want the Exit Price = SellPrice (TPBuy1) which is 1300 ion this example.

The same thing occurs in the opposite side: The CoverPrice is the Low instead of the price that I specified.

How can I solve this? What is wrong inside the loop?

(I want to keep the TPBuy1 outside the loop because there´s a lot of calculations, it´s just an example here)

Looks like the SellPrice is being completely ignored... Even if I put the price, the execution is still on wrong price:

``````if(onBuy AND High[i]>=TPBuy1[i]) {
SellPrice[i]=1300;
Sell[i]=1;
}
``````

What is wrong with the code? Why the SellPrice is being ignored? I need to add some parameters ou something is missing?

Thanks!

First, please review this post for ideas on how to find the error in your coding: How do I debug my formula?. Since you didn't show us all your code, there's really no way for anybody to guess where your mistake is.

Second, AmiBroker's default behavior is to force all your trade prices to be within the Low-High range of prices for that bar on which you enter or exit. That means if you specify a SellPrice which is above the High then AmiBroker will use the High price to exit the trade. If you specify a SellPrice which is below the Low, then AmiBroker will use the Low price to exit the trade. That may provide you with some clues about where to look for the problem.

Third, it seems likely that you could implement your logic with array formulas instead of using a loop. That would be much more efficient than the looping approach.

Fist, I am using _TRACE to debug this for a week. And since I don´t have a clue about why the SellPrice is being ignored inside the loop, I am here asking for help. I will not show all my code, beceuse the problem is inside an if statement, and it´s just a question why the SellPrice is being ignored...

Second, you said:

That means if you specify a SellPrice which is above the High then AmiBroker will use the High price to exit the trade. If you specify a SellPrice which is below the Low, then AmiBroker will use the Low price to exit the trade

This is not the case. If you read what i wrote you will see that my SellPrice is BETWEEN high and low of the bar/candle. Not above the high, not below the low.

Third, I´m using the loop because of this:
This is what I need. So, I will use the loop.

Thanks again.

In fact I DID read what you wrote, and I looked at the code that you posted. It does not appear to match up with the results you are getting, which suggests there's something else going on with the code which you chose NOT to post. So you have three choices: