Multiple entry and exit of positions

Questions about MultiCharts and user contributed studies.
DanielMadan
Posts: 3
Joined: 08 Aug 2013
Has thanked: 3 times

Multiple entry and exit of positions

Postby DanielMadan » 08 Aug 2013

Hello everyone,

This is my first post in these forums, I'm a new user of Multicharts, I hope you'll find the time to help me with my issue.

I have currently coded a strategy which has multiple entries in a short amount of time. I would like to set a different stop loss for every position, yet I can't find a good way to do so.

Indeed, the global way of doing it does not help, as it closes all positions if the stop loss has been hit. Let's say I have 2 positions, A and B. I would like A to stop as soon as 2% profit or loss has been hit and B to stop at 2% as well, depending on their entry price.

Using this code, for a short strategy, and an amount of 250'000, this would close both positions as soon as the profit or loss occured in total.

if marketposition < 0 then begin

setstoploss(0.02*250000);

setprofittarget(0.02*250000);

end;


Since I'm not experienced with this language, I would like something that would do the following:

Setstoploss for position A to (0.02*250000) depending on the price I've bought A at

and Setstoploss for position B to (0.02*250000) depending on the price I've bought B at.

Obviously, I would need to generalize this to N positions.

Is there a function that allows me to set a stoploss or stopwin for each position that I enter that I am unaware of?

Thank you for your help and if you're missing info , please tell me.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Multiple entry and exit of positions

Postby furytrader » 08 Aug 2013

As far as I know, the "setstoploss" function is not customizable for specific entries within a trading model. However, you should be able to accomplish what you're seeking to do with EasyLanguage. Doing so requires you to keep track which entry trades have been triggered, and to only exit portions of your position at various stop levels.

To keep tracks of which entry trades have been triggered, there are a few ways to do this:

1) Watch how many total contracts (or shares) you have on using the CURRENTCONTRACTS keyword. For example, if you have only 100 shares on, this means that you've only triggered signal 1 and if you have 200 shares on, you have triggered signals 1 and 2, etc.; or

2) Use the keyword CURRENTENTRIES which tells you the number of open entries for the position; or

3) Use a tracking boolean variable that monitors whether the conditions to enter signal 1 (or 2 or 3) have been met, and set that variable to TRUE or FALSE, depending on whether you should have been filled for a particular signal.

This can be a little tricky at first, so you may have to experiment a bit to get it to work right. Option #3 above is probably the trickiest to program but it also gives you the greatest precision.

When it comes to exiting a particular position, be aware of a couple of things:

1) Use the TOTAL keyword when specifying the number of shares you want to exit. So, if you're long 500 shares and you only want to sell 100, make sure you write something like:

SELL 100 shares TOTAL Next Bar At 50 STOP;

If you do not use the "total" keyword, the exit quantity is apportioned to each open entry; with the "total" keyword, it is treated on a first-in, first-out basis.

2) When you enter a new position, you can assign a label to that entry, like:

BUY "L_BRKOUT" 100 SHARES AT 50 STOP;

When you exit a position, you can target a particular entry within your open position:

SELL "L_STOP" FROM ENTRY "L_BRKOUT" 100 SHARES AT 45 STOP;

See the help file on "BuyToCover" and "Sell" for more information on how to specify positions you are exiting.

Sorry if this is a little confusing. If someone has a better suggestion, please share it here.

DanielMadan
Posts: 3
Joined: 08 Aug 2013
Has thanked: 3 times

Re: Multiple entry and exit of positions

Postby DanielMadan » 09 Aug 2013

First, thank you for your help, it's much appreciated.

If I understand correctly, your solutions are based on a fixed total of shares (for number 1). Does it also work for a variable number of shares per position ?

Regarding solution 2), if your number of shares is variable, then I believe it's hard to exit the correct position because the price of the shares vary throughout positions and I'm not sure the CURRENTENTRIES will update itself corectly.

Regarding solution 3) is it true that you will have to exhaustively code as many entries and exits as you wish? I was aiming for something which would work for N solutions, and if N is very high then it would be troublesome.

Again, thank you for your help.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Multiple entry and exit of positions

Postby furytrader » 09 Aug 2013

For option #3 above (setting a tracking variable), yes, you would want to be able to monitor whether the market trades through your entry price level for each entry type you have. If the number of different entries get high (for example, more then three separate entry conditions within one strategy), you could use arrays and just build an array of entry points, stops, etc. Working with arrays can be a little tricky if you're not familiar with them.

As an alternative, you could break up your strategy into several strategies ... if and only if the entry criteria for one particular entry is not conditioned on whether another entry condition has already been met.

How many different potential entries does your strategy have?

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Multiple entry and exit of positions

Postby piranhaxp » 09 Aug 2013

I would recommend to set up each potential entry of several entries in one strategy to write a file via ELCollections and than read it back to separate each position. This solution lets control you partial fills for each entry and/or exit for every entry/exit rule and you can compare it via broker statement easily. Without looking to deep in this problem, I think it shouldn't be that difficult to write a txt-file like that and read it back, because it is well documented.

Should be something like this :

Code: Select all

if rule "xyz" and file position.txt = empty then
begin
buy next bar at abc stop;
write file for position1.txt //symbol, position, currentcontracts, entryprice etc.
control current position with broker statement // for partial fills or something like that
end;

constant read file position1.txt;

if position in position1.txt <> 0 then
begin
if rule "opx" then
begin
sell next bar at bla bla bla;
write file for position1.txt // symbol, position, currentcontracts, entryprice etc.
end;
end;
You can extend it with many different strategies you want to put together in one "overall" strategy.

You can do this for every specific entry-rule in one strategy to implement several strategies in one. It is just a constant write and read in a txt-file. I see a lot of problems with defining var's for each strategy in one overall strategy comparing them to separate each strategy. It could lead into a mess. Same with constant array-action which, right from my experience, could slow you down a lot over the day if not doing it right ....

Just take it as an idea ...

Regards.

Mike

DanielMadan
Posts: 3
Joined: 08 Aug 2013
Has thanked: 3 times

Re: Multiple entry and exit of positions

Postby DanielMadan » 09 Aug 2013

Furytrader, I did not decide how many entries I allowed, I wanted to know if there's a practical way to code 1000 individual entries and exits , each with their own stop loss.

Piranha, thanks for your idea, I'll look into it asap.

hilbert
Posts: 224
Joined: 17 Aug 2011
Has thanked: 76 times
Been thanked: 64 times

Re: Multiple entry and exit of positions

Postby hilbert » 26 Dec 2013

As far as I know, the "setstoploss" function is not customizable for specific entries within a trading model. However, you should be able to accomplish what you're seeking to do with EasyLanguage. Doing so requires you to keep track which entry trades have been triggered, and to only exit portions of your position at various stop levels.

To keep tracks of which entry trades have been triggered, there are a few ways to do this:

1) Watch how many total contracts (or shares) you have on using the CURRENTCONTRACTS keyword. For example, if you have only 100 shares on, this means that you've only triggered signal 1 and if you have 200 shares on, you have triggered signals 1 and 2, etc.; or

2) Use the keyword CURRENTENTRIES which tells you the number of open entries for the position; or

3) Use a tracking boolean variable that monitors whether the conditions to enter signal 1 (or 2 or 3) have been met, and set that variable to TRUE or FALSE, depending on whether you should have been filled for a particular signal.

This can be a little tricky at first, so you may have to experiment a bit to get it to work right. Option #3 above is probably the trickiest to program but it also gives you the greatest precision.

When it comes to exiting a particular position, be aware of a couple of things:

1) Use the TOTAL keyword when specifying the number of shares you want to exit. So, if you're long 500 shares and you only want to sell 100, make sure you write something like:

SELL 100 shares TOTAL Next Bar At 50 STOP;

If you do not use the "total" keyword, the exit quantity is apportioned to each open entry; with the "total" keyword, it is treated on a first-in, first-out basis.

2) When you enter a new position, you can assign a label to that entry, like:

BUY "L_BRKOUT" 100 SHARES AT 50 STOP;

When you exit a position, you can target a particular entry within your open position:

SELL "L_STOP" FROM ENTRY "L_BRKOUT" 100 SHARES AT 45 STOP;

See the help file on "BuyToCover" and "Sell" for more information on how to specify positions you are exiting.

Sorry if this is a little confusing. If someone has a better suggestion, please share it here.
I was trying to solve a similar problem of having multiple strategies in ON mode in a single chart and I independently implemented the same solution as suggested in above by furytrader. I used option 3 as furytrader describes above. I used boolean variables in my code that informed me whether a particular strategy order has been filled or not. I also used separate names for entry orders and associated exit orders (using sell from entry("") syntax). This approach works and gives a lot of flexibility.

Only downside is code gets longer since I am explicitly handling all entry and exit orders.

Important: When implementing above approach, avoid using any keywords supplied by multicharts like setstopprofit, setstoploss, entryprice, entrytime, marketposition etc. Since all these key words will have different values for different strategies/orders (that have been applied on the same chart) and those values will interfere. E.g. consider you have two strategies A and B on a chart, both ON. Strategy A generates a long order at price P(A)=1830, after sometime strategy B generates a long order at price P(B)=1832. Assume, profit target on strategy A is 10 points and profit target for strategy B is 4 points. Then if you code as:

Code: Select all

sell from entry ("Strategy B_LE") at entryprice + 4 limit;
it will result into a sell order at 1830+4 = 1834, whereas you want to sell at 1832+4 = 1836.
So, to accomplish this, you will not have to use any multicharts keyword like entryprice, entrytime etc. Rather you will have to store the entryprice for different strategies in different variables like StrategyA_entryprice, StrategyB_entryprice etc. and then code using these variables as:

Code: Select all

sell from entry ("Strategy B_LE") at StrategyB_entryprice + 4 limit;
Hope it helps. The other solution suggested by Piranha might also work but I have not tried it.


Return to “MultiCharts”