System uses high prices instead of closing for executions

I have a code working on weekly closing. Basically, some buy signals on the stock along with specific conditions for foreign stock, in my case the benchmark index. My prob is when I use the setforeign function and use tradeprices as true to backtest, I get buy and sell signals at HIGH PRICE and NOT at close despite defining 0 trade delays and buyprice = sell price = close;

this is the code I am working with:

SetForeign("NIFTY",fixup = True, tradeprices = True); 

marketup = TimeFrameExpand(RSI(14),inweekly) >  40 

AND TimeFrameExpand(C, inweekly) > TimeFrameExpand(EMA(C,34), inweekly);

RestorePriceArrays(tradeprices);

If I change the tradeprices to false then I get the correct trade prices on the backtest (i.e. at CLOSE) but then the system DOES NOT use previous bar equity for new positions as programed SetOption( "UsePrevBarEquityForPosSizing", True ); and reduces the exposure of my portfolio dramatically.

I have ATR based SL along with other multiple sell signals for my system. Does that matter? Happy to send the entire code if needed.

I would really appreciate if anyone can help me resolve this issue pls.

Thanks

The (incomplete) code you have there is totally wrong.

You use time frame expand without any time frame set before. Also time frame expand is supposed to be set after restoring.

Please read below article showing how to use time compression of foreign symbols.

Also please re-read time frame compression tutorial again.
http://www.amibroker.com/guide/h_timeframe.html


Then as for trade price setting...
It is for cases of trade signals within set foreign/restore routine.

Look at second example here
https://www.amibroker.com/guide/afl/setforeign.html

You dont have any Buy Sell rules within Set foreign. So set trade prices to false.

You trade the stock but not the index.

1 Like

hey mate. time frame compression has been a struggle and for very little for me. i modified my code to a single time frame (ie not using the function) and now the setforeign works fine with tradeprices=true. Sure i was making some mistake with time frames, will keep working on trying to understand more on how that works. thanks for your help again. cheers.