- ago
I believe it should be a common case, but I couldn't find it in the Discussions.

I have a simple reverse strategy in code:
CODE:
if (HasOpenPosition(bars, PositionType.Long) || HasOpenPosition(bars, PositionType.Short)) {    //Check and exit if needed    ClosePosition(LastPosition, OrderType.Market, 0); } if (!HasOpenPosition(bars, PositionType.Long) && !HasOpenPosition(bars, PositionType.Short)) {    // Check and enter if needed    Transaction t = PlaceTrade(bars, TransactionType.Short / Long, OrderType.MarketClose, 0, "Short"); }

This code exits (long) on one bar, and enters short (if criteria is still met) next bar.

I need to close long and open short at the same first bar, but I don't see a way to understand that the long position is closed, and HasOpenPosition returns true until the next bar.

Can you please suggest any way to make the strategy truly reverse?

Thank you
0
246
Solved
3 Replies

Reply

Bookmark

Sort
- ago
#2
QUOTE:
I need to close long and open short at the same first bar,

You couldn't find answer in Discussion probably because this is more a "reversal strategy" than a "same bar exit" question. Check out Cone's post 8 in topic below for the answer:

Stop and Reverse strategy question
0
Best Answer
- ago
#3
So with regard to the provided link I should have my code modified as follows:

CODE:
if (HasOpenPosition(bars, PositionType.Long) || HasOpenPosition(bars, PositionType.Short)) { //Check and exit if needed ClosePosition(LastPosition, OrderType.Market, 0); // Check and enter if needed Transaction t = PlaceTrade(bars, TransactionType.Short / Long, OrderType.MarketClose, 0, "Short"); } if (!HasOpenPosition(bars, PositionType.Long) && !HasOpenPosition(bars, PositionType.Short)) { // Check and enter if needed Transaction t = PlaceTrade(bars, TransactionType.Short / Long, OrderType.MarketClose, 0, "Short"); }


Seems to work fine. Thank you, Eugene!
1

Reply

Bookmark

Sort