Exiting multiple positions using Stop orders
Author: Mouravyev
Creation Date: 12/16/2012 12:55 PM
profile picture

Mouravyev

#1
Good day!

Could you help me with the following issue?
I want to use stop orders (both for buy and sell).

I have a problem with exits.
Strategy uses pyramiding. For example, we have 4 active position. When exit signal appear we should close 2 active positions.
The strategy works OK if I use CloseAtMarket orders, but it works incorrect if I use Stop orders.

CODE:
Please log in to see this code.


The loop doesn't work in the code. Could you tell me what did I do wrong?
And the other question concerns LastActivePosition.ExitPrice. Why does the value always = 0?
I just need to use LastActivePosition.ExitPrice to calculate next position entry point. For instance,
BuyAtStop (i, LastActivePosition.ExitPrice + 3000)


profile picture

Cone

#2
AtMarket exit orders pretty much guarantee an exit, but AtStop orders require that the price reach the stop trigger on the specified bar to execute.

By "works incorrect" are you saying that you're sure that the price is reaching the stop trigger price but the positions are still not exited? Note that you can call PlotStops(); (call it just once, before the trading loop) to see the stop prices plotted on he chart.

Re: LastActivePosition.ExitPrice
By definition, if the position is active, there is no exit price. To get the exit price of a position, do something like this:

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

Eugene

#3
QUOTE:
For instance,
BuyAtStop (i, LastActivePosition.ExitPrice + 3000)

Note that "i" should be equal to bar+1 for stop orders to generate Alerts and to avoid introducing a peeking error in your code.
profile picture

Mouravyev

#4
Cone, Eugene thank you for your prompth replies!

I'm attaching the full code.


CODE:
Please log in to see this code.



this is the full code.
It works OK if I use AtMarket orders.
The problem occurs with stop orders.

The other problem is with ExitPrice which always returns 0.
double flagprice is initiated with the value of the very first bar.

Could you tell me where the mistake is, please?

Thank you in advance!
profile picture

Eugene

#5
QUOTE:
The other problem is with ExitPrice which always returns 0.

See Cone's reply above re: LastActivePosition.ExitPrice. It ought to be zero! See the QuickRef > Position object > ExitPrice property.

CODE:
Please log in to see this code.

You can't do that because "p" (LastPosition) is null. You assigned the property prior to taking any position. This is why your script errors out.

CODE:
Please log in to see this code.

This should be called only after your strategy actually made an entry or exit e.g.
CODE:
Please log in to see this code.

Otherwise you're risking to reset the flag even if a trade wasn't taken due to capital constraints.

I have a feeling that this could be coded in a more straightforward manner. Would you mind formalizing the rules in plain English?
profile picture

Mouravyev

#6
Eugene, sure!

Both entry and exit rules use stop orders.

Entry rules:

1) If we don't have active positions.
Entry signal: First Bar Close + 3000 (the very first bar when the system is launched)


2) Entry signal 2: Last EntryPrice + 3000 (number of max active positions is not limited)

3) Entry signal 3: Last ExitPrice + 3000 (number of max active positions is not limited)

Exit rules:

3) If we have 1 or 2 active positions:
Last EntryPrice - 3000 => Close all positions

4) If we have 3 or more active positions:
First Sell:
Last EntryPrice - 3000 => Close 2 Last Active Positions

If we still have active positions and the price continues to go down:
Last EntryPRice - 3000 => Close Last ActivePosition

If we closed 2 positions at first, then we've opened a new position and we received an exit signal after that, then we should close 2 active positions.

The thing is that every time we receive exit signal straigh after entry signal we should close 2 active positions.
Otherwise, we close only one active position (when we receive exit signal after another exit signal).






profile picture

Eugene

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

Mouravyev

#8
Euegene,

everything works perfect now!

Thanks a lot!
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).