Page 1 of 1

need hlp with code- multiple exit

Posted: Apr 30 2012
by justmake
Hello, guys,

I try to code a very simple multiple exit signal as below.
Try to exit part of the Long position every time price close below MA50.
The problem I have is that only the first time price close below MA50 will trigger a sell order.(see attached screenshot)

Also I set the POSITION LIMIT to 1,000,000 and ALLOW UP to 10 entry in signal property. So it shouldn't be a problem of executing those order.

Can someone let me know what's wrong with the code:

Code: Select all

vars: MALINE1(0),MALINE2(0);

MALINE1= averagefc(close,200);
MALINE2= averagefc(close,50);

If marketposition=0 and close > MALINE1 then buy 100000 contracts next bar at market;

if marketposition=1 and Close cross below MALINE2 then
sell 20000 contracts total next bar at MALINE2 LIMIT;

Re: need hlp with code- multiple exit

Posted: May 01 2012
by Henry MultiСharts
Hello Justmake,

When you are using partial exits you can exit with 1 order command only once.
If you need multiple exits – you need to send an individual “sell” command for each partial exit order that you are planning to use with the corresponding amount of contracts.

Re: need hlp with code- multiple exit

Posted: May 01 2012
by justmake
Thanks for the reply.
So I revised the code accordingly and add an separate sell command as code below. But it then execute the second sell command at the same bar as first sell command.
How do I code it so the second sell command will be executed when the price cross below MA50 "next time" ?
Please advise.

Code: Select all

vars: MALINE1(0),MALINE2(0);

MALINE1= averagefc(close,200);
MALINE2= averagefc(close,50);

If marketposition=0 and close > MALINE1 then buy 100000 contracts next bar at market;

if marketposition=1 and close cross below MALINE2 then
begin
sell 20000 contracts total next bar at MALINE2 LIMIT;
sell 20000 contracts total next bar at MALINE2 LIMIT;

end;

Re: need hlp with code- multiple exit

Posted: May 01 2012
by JoshM
Thanks for the reply.
So I revised the code accordingly and add an separate sell command as code below. But it then execute the second sell command at the same bar as first sell command.
How do I code it so the second sell command will be executed when the price cross below MA50 "next time" ?
Please advise.
I would advise to use a switch statement to submit an order depending on the number of cross-overs (so that the order is executed the next time on the next cross-over).

Perhaps something like the following (untested):

Code: Select all

Variables:
MALINE1(0),MALINE2(0), crossOverCounter(0);

MALINE1= averagefc(close,200);
MALINE2= averagefc(close,50);

If (MarketPosition(0) = 0) and (Close > MALINE1)
then buy 100000 contracts next bar at market;

if (MarketPosition(0) = 1) and (Close cross below MALINE2) then begin

crossOverCounter = crossOverCounter + 1;

switch (crossOverCounter) begin
case 1:
Sell ("XL 1") 20000 contracts next bar at MALINE2 limit;

case 2:
Sell ("XL 2") 20000 contracts next bar at MALINE2 limit;

case 3:
Sell ("XL 3") 20000 contracts next bar at MALINE2 limit;

case 4:
Sell ("XL 4") 20000 contracts next bar at MALINE2 limit;

case 5:
Sell ("XL 5") 20000 contracts next bar at MALINE2 limit;
end;

end;

// Reset variable
if (BarsSinceExit(1) < 3) and (crossOverCounter <> 0) then
crossOverCounter = 0;
Btw, these limit orders are only submitted for the next bar when the cross-over is active -- so they will be cancelled on the second bar after the cross-over.

Re: need hlp with code- multiple exit

Posted: May 01 2012
by justmake
Thanks Josh! The code is working as expected.
I don't have to keep scratching my head again:)