I need to Count or Flag the 1st Exit of a Trade

Questions about MultiCharts and user contributed studies.
sptrader
Posts: 710
Joined: 09 Apr 2010
Location: Colorado
Has thanked: 470 times
Been thanked: 261 times
Contact:

I need to Count or Flag the 1st Exit of a Trade

Postby sptrader » 07 Jul 2016

Say I go long 4 ES contracts using chart trader. I want to sell half at the first profit target and the rest at the second profit target(using PL)... How do I flag or count, the "filled" profit targets ? If this first target is hit, how can I use that to activate the 2nd target ?
I tried "exitstoday" but that only counts fully closed positions....I need something like exits-this-trade... or flag = 1 , if 1st target has been hit..
Any suggestions would be greatly appreciated...(not a pro programmer)..

sptrader
Posts: 710
Joined: 09 Apr 2010
Location: Colorado
Has thanked: 470 times
Been thanked: 261 times
Contact:

Re: I need to Count or Flag the 1st Exit of a Trade

Postby sptrader » 07 Jul 2016

I think I may have figured it out in an easy but crude way.. Untested, but this is what I came up with. Since the flag needs to be set after the 1st profit target, I think this will work... not very sophisticated but :

Code: Select all

flg = iff(positionprofit(0) > 0,1,0);


So the flag = 0 until the 1st profit is taken, then it equals 1, to alert only the 2nd exit to take control.. (but what if I use 3 or 4 profit targets ?) ... that's why I say "not very sophisticated or flexible"...

Other suggestions-ideas welcome...
Last edited by sptrader on 08 Jul 2016, edited 1 time in total.

User avatar
bensat
Posts: 331
Joined: 04 Oct 2014
Has thanked: 46 times
Been thanked: 104 times

Re: I need to Count or Flag the 1st Exit of a Trade

Postby bensat » 07 Jul 2016

Set executions steps depended on your current position size.

Code: Select all

If mp = 1 and currentcontracts = 4 then sell 1 contract at 2090 ....

If mp = 1 and currentcontrats = 3 then sell 1 contract at 2091 ....

if mp = 1 and currentcontracts = 2 then sell 1 contract at ....
Or set a var and count for each sell you did from the long and place your exit order for the next target when the count changes.

I hope you understand what I mean ... It's late here. Sorry.

Regards.

Ben

sptrader
Posts: 710
Joined: 09 Apr 2010
Location: Colorado
Has thanked: 470 times
Been thanked: 261 times
Contact:

Re: I need to Count or Flag the 1st Exit of a Trade

Postby sptrader » 08 Jul 2016

Ben: thanks for the reply- one of the problems is that I "build a position" using chartTrader and it's not always the same size, so my "current contracts" may vary.. I do like your idea of "counting" the number of exits, although I'm not sure how to code something like that...if you could give me a simple example, I'd really appreciate that....
Steve

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 80 times
Contact:

Re: I need to Count or Flag the 1st Exit of a Trade

Postby tony » 08 Jul 2016

Say I go long 4 ES contracts using chart trader. I want to sell half at the first profit target and the rest at the second profit target(using PL)... How do I flag or count, the "filled" profit targets ? If this first target is hit, how can I use that to activate the 2nd target ?
I tried "exitstoday" but that only counts fully closed positions....I need something like exits-this-trade... or flag = 1 , if 1st target has been hit..
Any suggestions would be greatly appreciated...(not a pro programmer)..
You could hold a value at the moment MP size changes. For example if MP=4 and MPP=2 then var1=1; this would set and hold var1= 1 at the moment market position went from long 4 lots to long 2 lots for example. MPP is a variable = MP and placed AFTER the last end; in your signal. This way it holds that previous tick value for MP on the next signal calculation.

sptrader
Posts: 710
Joined: 09 Apr 2010
Location: Colorado
Has thanked: 470 times
Been thanked: 261 times
Contact:

Re: I need to Count or Flag the 1st Exit of a Trade

Postby sptrader » 08 Jul 2016

Tony: Yet another good idea to try- thanks !

User avatar
bensat
Posts: 331
Joined: 04 Oct 2014
Has thanked: 46 times
Been thanked: 104 times

Re: I need to Count or Flag the 1st Exit of a Trade

Postby bensat » 09 Jul 2016

You can grab your current position from the broker with <MarketPosition_at_Broker> ... So if you bought 4 contracts in the chart trader and your current position is 4 contracts long in any symbol, you should see 4 contracts from <MarketPosition_at_Broker> when you call it.

Your exit algo could implement an input for to <START> the exit algo and for how many contracts you want to exit. A different value of <MarketPosition_at_Broker> which should by less then the number before assures you to have get filled for one order. So if this value changes you set the next order etc. So many ideas to play with. You could make a button in Excel to set a cell value from 0 (OFF) and 1 (ON) to start the algo etc etc.

From your history of quotes & posts in this forum I am very confident you can realize a counter very easily. I feel just a little bit offended from your demand to offer a written solution as you show yourself here as a PL expert.

Hmmmmm .....


Regards.

Ben

sptrader
Posts: 710
Joined: 09 Apr 2010
Location: Colorado
Has thanked: 470 times
Been thanked: 261 times
Contact:

Re: I need to Count or Flag the 1st Exit of a Trade

Postby sptrader » 10 Jul 2016

You can grab your current position from the broker with <MarketPosition_at_Broker> ... So if you bought 4 contracts in the chart trader and your current position is 4 contracts long in any symbol, you should see 4 contracts from <MarketPosition_at_Broker> when you call it.

Your exit algo could implement an input for to <START> the exit algo and for how many contracts you want to exit. A different value of <MarketPosition_at_Broker> which should by less then the number before assures you to have get filled for one order. So if this value changes you set the next order etc. So many ideas to play with. You could make a button in Excel to set a cell value from 0 (OFF) and 1 (ON) to start the algo etc etc.

From your history of quotes & posts in this forum I am very confident you can realize a counter very easily. I feel just a little bit offended from your demand to offer a written solution as you show yourself here as a PL expert.

Hmmmmm .....


Regards.

Ben
Ben : please don't take it as a "demand" for code, I only asked for an example of what you meant , to clarify it for me. I'm sure that you're a far better programmer...but you're right I can probably handle a simple counter. I just wasn't quite sure how to set it up... Your answer gives me some additional clarity-
Thanks,
Steve

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1504 times
Contact:

Re: I need to Count or Flag the 1st Exit of a Trade

Postby JoshM » 20 Jul 2016

Say I go long 4 ES contracts using chart trader. I want to sell half at the first profit target and the rest at the second profit target(using PL)... How do I flag or count, the "filled" profit targets ? If this first target is hit, how can I use that to activate the 2nd target ?
Monitoring the strategy's market position for changes is of course one way to implement that. But I think there's also a drawback with that method. Say your position changes from 4 to 2.

In that case we still don't really know what happened. Perhaps a profit target is hit, a stop-loss order executed, or the strategy closed two contracts with a market order based on some risk calculation.

If you want to know what caused a position (or part of a position to be close), you can use the PosTradeExitCategory() keyword. A related keyword is PosTradeExitName() which returns the name of the exit order.

The first keyword works like this:

Code: Select all

PosTradeExitCategory(PosAgo, TradeNumber)
Now to have it return the reason of the most recent exit order of the current, open position we use it like:

Code: Select all

PosTradeExitCategory(0, PosTradeCount(0) - 1)
Sidenote: PosTradeCount(0) returns the number of entries in the current position. Since the PosTrade* keywords require that value to be zero-based, we subtract 1 from it to get the recent exit.

If you want to access information about the first exit of the currently open position, it would be:

Code: Select all

PosTradeExitCategory(0, 0)


Since `PosTradeExitCategory()` returns a value of 2 when the recent exit was a limit order, we can use it like:

Code: Select all

if (PosTradeExitCategory(0, PosTradeCount(0) - 1) = 2) then begin

Print("The recent exit was a limit order");

end;
For even more control/information about that recent exit, we can use `PosTradeExitName()` also:

Code: Select all

if (PosTradeExitCategory(0, PosTradeCount(0) - 1) = 2) then begin

if (PosTradeExitCategory(0, PosTradeCount(0) - 1) = "XL Take Profit") then
Print("The exit limit order was my profit target")
else if (PosTradeExitCategory(0, PosTradeCount(0) - 1) = "ATR Exit") then
Print("The exit limit order was something else");

end;
I'm not sure if you need all of these keywords for more information about the recent exit of a position, but if you want, it's possible in PowerLanguage.


Return to “MultiCharts”