Close all positions on the last bar
Author: algotradingwl
Creation Date: 3/22/2016 1:42 PM
profile picture

algotradingwl

#1
Plz tell me what is my mistake. All positions should be closed on the last bar (ExitAtMarket( bar + 1, p, "Exit at last bar") , but this is not happening:

CODE:
Please log in to see this code.
profile picture

Eugene

#2
Welcome to the forums Dima.

The thing is, the Position will never be closed because Bars.Count is dynamic (increasing with each new bar added) and your trade is an Alert for bar+1 (that new bar). So if you don't care about getting that alert (for you know that this Position is being closed anyway):
CODE:
Please log in to see this code.


Alternatively:
CODE:
Please log in to see this code.


As a side note, don't break the link between the IsLastPositionActive status check for the entries:

CODE:
Please log in to see this code.


Please refer to the WealthScript Programming Guide > Programming Trading Strategies > Peeking > Trading based on Position Active Status .
profile picture

algotradingwl

#3
Thank you for your attention! The code works correctly!
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).