The goal is to sell at first positive open after buy. Using Norgate platinum EOD OLHC data. In backtesting, this code with any generic buy condition results in multiple exits for a loss (no same bar entry/exit ). Also several positions held beyond the first positive open. I would expect the position to either remain open or exit above buyprice.
What additional code is needed to accomplish the goal?
PO = Open > Buyprice;
Sell = PO;
Buyprice = Close;
Sellprice = Open;
First error in understanding: BuyPrice is INPUT array, not output.
- read the manual http://www.amibroker.com/guide/h_backtest.html and http://www.amibroker.com/f?applystop
- use ApplyStop profit target instead of your coding
One comment regarding your idea: exiting on first positive open is a recipe for guaranteed disaster as you are NOT accepting profits, but accepting huge drawdowns.
Instead of buyprice, try the valuewhen() function for tracking the close price at buy. Need Exrem() function for buy/sell for valuewhen() to get rid of excess buy signals.
That's not proper way. How is that supposed to work (for all kind of signals)?
Tomasz has given one correct way already -> ApplyStop.
2nd way -> looping.
Has been written multiple times already.