Multiple exit conditions

Questions about MultiCharts and user contributed studies.
kentai
Posts: 29
Joined: 02 Jun 2011
Has thanked: 9 times
Been thanked: 1 time

Multiple exit conditions

Postby kentai » 28 Dec 2011

Hi,

I have been trying to code something. I was trying to write a strategy which allows me to establish a few ways to exit after I establish a position. For example, I like to buy 20 contracts when the ES crossed above its 200-bar EMA and place a limit sell order to sell 10 contracts after it moves up by 3 points and sell the remaining 10 contracts when ES crosses below its 200-bar EMA. I tried to divide the entry into 2 parts like:

Code: Select all

Buy ("EMALE1") at next bar at Xaverage(C,200) limit;
Buy ("EMALE2") at next bar at Xaverage(C,200) limit;

Sell ("EMALX1") from entry ("EMALE1") at next bar at entryprice+3 limit;
Sell ("EMALX1") from entry ("EMALE1") at next bar at Xaverage(C,200) stop;


Apparently, the signal failed even after I change the position limits to Allow 3 entry orders in the same direction as the current held position regardless of the entry that generated the order.

User avatar
JoshM
Posts: 2089
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1485 times
Contact:

Re: Multiple exit conditions

Postby JoshM » 28 Dec 2011

kentai wrote:

Code: Select all

Sell ("EMALX1") from entry ("EMALE1") at next bar at entryprice+3 limit;
Sell ("EMALX1") from entry ("EMALE1") at next bar at Xaverage(C,200) stop;


I think there are two errors here:
    The second sell signal has the same order name as the first,
    The second sell signal sells from the first buy order (from which also the first sell signal sells; in other words, after the first sell signal is executed, there is not anything left from entry "EMALE1" to sell).

Something like this should work:

Code: Select all

// Entry
if (MarketPosition(0) = 0) and (Close > Average(Close, 200)) then begin

   Buy ("EMALE1") 10 contracts next bar at Xaverage(C,200) limit;
   Buy ("EMALE2") 10 contracts next bar at Xaverage(C,200) limit;

end;

// Exit
if (MarketPosition(0) = 1) then begin

   Sell ("EMALX1") from entry ("EMALE1") all contracts next bar at entryprice + 3 limit;
   Sell ("EMALX2") from entry ("EMALE2") all contracts next bar at Xaverage(C, 200) stop;

end;


Return to “MultiCharts”