need hlp with code- multiple exit

Questions about MultiCharts and user contributed studies.
justmake
Posts: 42
Joined: 21 Dec 2010
Has thanked: 15 times
Been thanked: 1 time

need hlp with code- multiple exit

Postby justmake » 30 Apr 2012

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;
Attachments
avg.gif
(17.34 KiB) Downloaded 298 times

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: need hlp with code- multiple exit

Postby Henry MultiСharts » 01 May 2012

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.

justmake
Posts: 42
Joined: 21 Dec 2010
Has thanked: 15 times
Been thanked: 1 time

Re: need hlp with code- multiple exit

Postby justmake » 01 May 2012

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;

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: need hlp with code- multiple exit

Postby JoshM » 01 May 2012

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.

justmake
Posts: 42
Joined: 21 Dec 2010
Has thanked: 15 times
Been thanked: 1 time

Re: need hlp with code- multiple exit

Postby justmake » 01 May 2012

Thanks Josh! The code is working as expected.
I don't have to keep scratching my head again:)


Return to “MultiCharts”