QUOTE:
when do i have to use bar or bar+1 when i buy or sell a stock.
If
bar is the loop index and treated as the current bar (the most-recent complete bar) then you should use
bar+1 for all orders except possibly AtClose orders, which are a special case. It's actually very easy, but using the Open on the trade bar complicates matters because the Open price isn't directly accessible by the script when generating orders for the next bar (because the Open occurs on that bar). There's no problem backtesting it, but to trade it, you need to invent a real-time filter for the stop entry order.
If you want to BuyAtStop if the Low of the "current" bar is above the Open of the trade bar, here's how I'd write it:
CODE:
Please log in to see this code.
I don't follow what you're trying to accomplish in the exit logic. Exiting at stop on the Open of the same bar pretty much will guarantee that you stop out on the open. Got a link to Larry Williams Oops pattern?