need - help with code - multi-exit  [SOLVED]

Questions about MultiCharts and user contributed studies.
vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

need - help with code - multi-exit

Postby vking » 19 Sep 2011

Hello any one out there - need help with EL code - to help maintain the position in the order queue for multiple exits - if there is any alternate way to get this done. Thanks in advance.

For further information on this - here is the detailed discussion on this topic:

viewtopic.php?f=1&t=8810

- The main issue - When the first target is filled - the second/third target orders get cancelled and then re-submitted again at the same price.
- This is with allow unlimited orders in the same direction setting in strategy properties.

Code: Select all

[IntrabarOrderGeneration = true]

Vars: intrabarpersist MktPosition(0);
Vars: intrabarpersist AEP(0);
Vars: intrabarpersist CC(0);
Vars: OneTick(MinMove/PriceScale);


MktPosition=Marketposition;

if LastBarOnChart_s and barstatus(1)=2 and MktPosition=0 then begin
buy ("LE") 3 contracts next bar market;
end else begin
CC=CurrentContracts;
AEP=AvgEntryPrice;
if CC>=1 then sell ("TT") 1 contract next bar at AEP+(6*OneTick) limit; // Third Target
if CC>=2 then sell ("ST") 1 contract next bar at AEP+(4*OneTick) limit; // Second Target
if CC>=3 then sell ("FT") 1 contract next bar at AEP+(2*OneTick) limit; // First Target
end;

gmoney
Posts: 16
Joined: 17 Jun 2011
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: need - help with code - multi-exit

Postby gmoney » 19 Sep 2011

What I do is use multiple named entries, and then use "from entry" for each position. Basically I view them as 3 separate trades. For example you could adjust your code as follows:

Code: Select all

if LastBarOnChart_s and barstatus(1)=2 and MktPosition=0 then begin
buy ("LE1") 1 contracts next bar market;
buy ("LE2") 1 contracts next bar market;
buy ("LE3") 1 contracts next bar market;
end else begin
AEP=AvgEntryPrice;
sell ("TT") from entry ("LE3") next bar at AEP+(6*OneTick) limit; // Third Target
sell ("ST") from entry ("LE2") next bar at AEP+(4*OneTick) limit; // Second Target
sell ("FT") from entry ("LE1") next bar at AEP+(2*OneTick) limit; // First Target
end;

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: need - help with code - multi-exit

Postby vking » 19 Sep 2011

Thanks gmoney.

Though it doesn't give me the flexibility to manage the number of contracts - if it works out - until MC implements this feature request - I should be able to use this as a work around.

Going to check out and reply.

gmoney
Posts: 16
Joined: 17 Jun 2011
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: need - help with code - multi-exit

Postby gmoney » 19 Sep 2011

You can still manage the number of contracts. Instead of:

buy ("LE1") 1 contracts next bar market;

use:

buy ("LE1") 5 contracts next bar market;

or however many you want. If you don't specify the number of contracts for each exit, they will exit all contracts for that entry. Am I missing something???

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: need - help with code - multi-exit

Postby vking » 19 Sep 2011

gmoney - Unfortunately this is not working as expected.

- With SA/unlimited orders - it only generated one order entry only and then enters another order rather than filling all the 3 orders first.

does it fill 3 contract entries/exits for you simultaneously?

At a point to give up this automatic order management in multicharts until the feature I have requested is implemented as it gets really complicated (atleast for me) to handle the orders in this way rather than having the flexibility to enter/exit as needed(wanted to see if I could mimic unmanaged orders like in NT in MC but so far no luck).

Thanks.

gmoney
Posts: 16
Joined: 17 Jun 2011
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: need - help with code - multi-exit

Postby gmoney » 19 Sep 2011

It is working for me using limit orders for entry. I have not tried it with end of bar market orders.

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: need - help with code - multi-exit

Postby vking » 19 Sep 2011

I tried the following code. Unfortunately second/third targets still gets cancelled and re-submitted again at the same price(attached image).

PS: Haven't tried the limit order entries yet.. would try it out but don't have much hope as I am trying to solve the exit issue :(

Code: Select all

[IntrabarOrderGeneration = true]

Vars: intrabarpersist MktPosition(0);
Vars: intrabarpersist AEP(0);
Vars: intrabarpersist CC(0);
Vars: OneTick(MinMove/PriceScale);

if LastBarOnChart_s and barstatus(1)=2 and MktPosition=0 then begin
// buy ("LE") 3 contracts next bar market;
buy ("LE") 3 contracts next bar market;
//buy ("LE2") 1 contracts next bar market;
//buy ("LE3") 1 contracts next bar market;
end;

MktPosition=Marketposition;
if MktPosition<>0 then begin
CC=CurrentContracts;
AEP=AvgEntryPrice;
// if CC>=1 then sell ("TT") 1 contract next bar at AEP+(6*OneTick) limit; // Third Target
// if CC>=2 then sell ("ST") 1 contract next bar at AEP+(4*OneTick) limit; // Second Target
// if CC>=3 then sell ("FT") 1 contract next bar at AEP+(2*OneTick) limit; // First Target
sell ("TT") 1 contract from entry ("LE") next bar at AEP+(6*OneTick) limit; // Third Target
sell ("ST") 1 contract from entry ("LE") next bar at AEP+(4*OneTick) limit; // Second Target
sell ("FT") 1 contract from entry ("LE") next bar at AEP+(2*OneTick) limit; // First Target
end;
Attachments
OrderExitIssue2.jpg
(114.24 KiB) Downloaded 3013 times

gmoney
Posts: 16
Joined: 17 Jun 2011
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: need - help with code - multi-exit

Postby gmoney » 19 Sep 2011

The entries and exits need to be individually named (LE1, LE2, LE3). See code example I posted before. The code you used is not what I was suggesting.

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: need - help with code - multi-exit

Postby vking » 19 Sep 2011

gmoney - thanks. I am checking further on this!!

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: need - help with code - multi-exit

Postby vking » 19 Sep 2011

gmoney - Unfortunately this didn't work. If you happen to have a demo account - would you please see if it works for you or any other suggestions?

Attached is the image showing the orders are being cancelled and sent at the same price again.

Thanks.

Code: Select all

[IntrabarOrderGeneration = true]

Vars: intrabarpersist MktPosition(0);
Vars: intrabarpersist AEP(0);
Vars: intrabarpersist CC(0);
Vars: OneTick(MinMove/PriceScale);

if MktPosition=0 then begin
buy ("LE1") 1 contracts next bar market;
buy ("LE2") 1 contracts next bar market;
buy ("LE3") 1 contracts next bar market;
end;

MktPosition=Marketposition;
if MktPosition<>0 then begin
CC=CurrentContracts;
AEP=AvgEntryPrice;
sell ("TT") 1 contract from entry ("LE3") next bar at AEP+(6*OneTick) limit; // Third Target
sell ("ST") 1 contract from entry ("LE2") next bar at AEP+(4*OneTick) limit; // Second Target
sell ("FT") 1 contract from entry ("LE1") next bar at AEP+(2*OneTick) limit; // First Target
end;
Attachments
OrderExitIssue3.jpg
(185.5 KiB) Downloaded 3031 times

gmoney
Posts: 16
Joined: 17 Jun 2011
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: need - help with code - multi-exit

Postby gmoney » 20 Sep 2011

I ran the code in your latest post and had the same issue. The other exit orders were cancelled when the 1st exit was filled but then immediately reissued the orders.

Also, the entries did not fill simultaneously. This is an issue that needs to be corrected. Have you posted a bug report on this?

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: need - help with code - multi-exit

Postby vking » 20 Sep 2011

gmoney - As per MC - this is not a bug but current way of handling the orders(always gets submitted as OCO).

They are going to consider this feature for future versions(no ETA on this).

viewtopic.php?f=1&t=8810 -> thread

https://www.multicharts.com/pm/viewissu ... _no=MC-454 -> PM request

Was trying to see - if there are any alternate way to get this done without losing the position in the queue - but seems to be a limitation with power language. Not sure how this is done using DOM/master exit strategy ( it works as intended - but same functionality can't be reproduced using powerlanguage/autotrading unfortunately).

Thanks

gmoney
Posts: 16
Joined: 17 Jun 2011
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: need - help with code - multi-exit

Postby gmoney » 20 Sep 2011

Ok thanks for the info.

The "bug" I was referring to wasn't the exits but the entry orders not ocurring simultaneously. This may not be a bug per se, but it isn't executing as intended - the intention/desire is to have all the entries occur simultaneously.

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: need - help with code - multi-exit

Postby vking » 20 Sep 2011

you can get the multiple entries done by the setting "allow unlimited entries" at the strategy format/properties. this would allow multiple entries to be submitted at the same time.

Only issue is with exits - with any combination/setting - I am unable to let them stay in the queue when other exit orders are filled - though the condition for them to stay in the queue is still valid - it seems to be the way power language orders gets executed and at present there seems to be a limitation until this feature gets implemented.

thanks.

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

Re: need - help with code - multi-exit

Postby Henry MultiСharts » 20 Sep 2011

Hello Vking.
Glad that you have figured it out. Vote for the feature request if you would like to have such functionality in the future. Please let me know if you need any help further.

Gmoney, please check that you have enabled "Multiple entry orders in the same direction as the currently held position" in the Format tab->Strategy properties->Properties tab.

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: need - help with code - multi-exit

Postby vking » 20 Sep 2011

Thanks Henry. I have opened this PM request couple of months back with the hope to have it implemented for 7.1. Apparently this is going to be considered some point in the future( No ETA).

Thanks.

Hello Vking.
Glad that you have figured it out. Vote for the feature request if you would like to have such functionality in the future. Please let me know if you need any help further.

Gmoney, please check that you have enabled "Multiple entry orders in the same direction as the currently held position" in the Format tab->Strategy properties->Properties tab.

gmoney
Posts: 16
Joined: 17 Jun 2011
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: need - help with code - multi-exit

Postby gmoney » 20 Sep 2011

I do have "Allow unlimited entries and exits per bar" and "Allow up to ___ entry orders in the same direction...." checked.

Per the chart screenshot attached, there were multiple entries on one bar but they were not simultaneous. You can see them being entered real time, plus you can see in the tracking window that they are not generated simultaneously. For some reason there is a delay between orders....and it's not just the fills, the orders aren't being sent at the same time. Please see attached pics. Are there any other settings that need to be adjusted?
Attachments
Tracking order times.PNG
(71.11 KiB) Downloaded 3016 times
Chart strategy settings.PNG
(94.51 KiB) Downloaded 3010 times

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: need - help with code - multi-exit

Postby vking » 20 Sep 2011

with OEC - orders were entered at the same time for me(attached screenshot). Just the exits are an issue for me.

not sure - if the MB order interface is different and behaves differently.

Thanks.
Attachments
OrderExitIssue3.jpg
(185.5 KiB) Downloaded 3013 times

gmoney
Posts: 16
Joined: 17 Jun 2011
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: need - help with code - multi-exit

Postby gmoney » 20 Sep 2011

I ran it on minute bars too. Not getting multiple entries at same time. See screenshot below.
Attachments
Orders staggered not simultaneous.png
(35.59 KiB) Downloaded 3008 times

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

Re: need - help with code - multi-exit

Postby Henry MultiСharts » 21 Sep 2011

Gmoney, please go to the format signals->signal name->Format->Properties->select IOG mode #1 (limit each order command in this signal to one entry and one exit per bar).
In strategy properties->Properties->Allow up to 3 entry orders.
Please let me know if this solution helped you or not.

gmoney
Posts: 16
Joined: 17 Jun 2011
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: need - help with code - multi-exit

Postby gmoney » 21 Sep 2011

Thank you for the suggestion. I did the following: "format signals->signal name->Format->Properties->select IOG mode #1 (limit each order command in this signal to one entry and one exit per bar). In strategy properties->Properties->Allow up to 3 entry orders." However the orders are still not simultaneous.

On a 1 minute chart the entries did happen on the same bar but were separated about 1-2 seconds each.

On a 50 tick chart the entries were delayed 1-2 seconds as well. The delay in plotting entries on the chart was even greater.

See screenshots attached. Following is the exact code I am using.

Code: Select all

[IntrabarOrderGeneration = true]

Vars: intrabarpersist MktPosition(0);
Vars: intrabarpersist AEP(0);
Vars: intrabarpersist CC(0);
Vars: OneTick(MinMove/PriceScale);

if MktPosition=0 then begin
buy ("LE1") 1 contracts next bar market;
buy ("LE2") 1 contracts next bar market;
buy ("LE3") 1 contracts next bar market;
end;

MktPosition=Marketposition;
if MktPosition<>0 then begin
CC=CurrentContracts;
AEP=AvgEntryPrice;
sell ("TT") 1 contract from entry ("LE3") next bar at AEP+(6*OneTick) limit; // Third Target
sell ("ST") 1 contract from entry ("LE2") next bar at AEP+(4*OneTick) limit; // Second Target
sell ("FT") 1 contract from entry ("LE1") next bar at AEP+(2*OneTick) limit; // First Target
end;
Attachments
Tracking order times 1 min and 50 tick.PNG
(49.43 KiB) Downloaded 3007 times
Orders staggered on 50 tick.png
(26.58 KiB) Downloaded 3004 times

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

Re: need - help with code - multi-exit

Postby Henry MultiСharts » 23 Sep 2011

Gmoney, unfortunately we failed to reproduce the issue in our environment.
Please come to our live chat Monday-Friday 6:30 am - 2 pm EST.
I would like to connect to your PC and ivestigate the issue.

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: need - help with code - multi-exit  [SOLVED]

Postby Andrew MultiCharts » 15 Oct 2013

Please read about this feature scheduled for MC 9.1: https://www.multicharts.com/pm/viewissu ... no=MC-1497

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: need - help with code - multi-exit

Postby vking » 15 Oct 2013

Andrew - Certainly looks like the right direction to take in this regard. I have pretty much given up hope for several months now as this was not being prioritized.

Would would wait for 9.1 beta 1 to see how it gets implemented.

Thanks


Return to “MultiCharts”