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)..
I need to Count or Flag the 1st Exit of a Trade
-
- Posts: 742
- Joined: 09 Apr 2010
- Location: Texas
- Has thanked: 483 times
- Been thanked: 274 times
- Contact:
Re: I need to Count or Flag the 1st Exit of a Trade
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 :
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...
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.
Re: I need to Count or Flag the 1st Exit of a Trade
Set executions steps depended on your current position size.
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
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 ....
I hope you understand what I mean ... It's late here. Sorry.
Regards.
Ben
-
- Posts: 742
- Joined: 09 Apr 2010
- Location: Texas
- Has thanked: 483 times
- Been thanked: 274 times
- Contact:
Re: I need to Count or Flag the 1st Exit of a Trade
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
Steve
Re: I need to Count or Flag the 1st Exit of a Trade
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.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)..
Re: I need to Count or Flag the 1st Exit of a Trade
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
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
-
- Posts: 742
- Joined: 09 Apr 2010
- Location: Texas
- Has thanked: 483 times
- Been thanked: 274 times
- Contact:
Re: I need to Count or Flag the 1st Exit of a Trade
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-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
Thanks,
Steve
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: I need to Count or Flag the 1st Exit of a Trade
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.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 ?
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)
Code: Select all
PosTradeExitCategory(0, PosTradeCount(0) - 1)
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;
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;