How to exit from multiple entries

Questions about MultiCharts and user contributed studies.
Zheka
Posts: 223
Joined: 13 Jan 2016
Has thanked: 8 times
Been thanked: 53 times

How to exit from multiple entries

Postby Zheka » 02 Jan 2018

I would like to have the same 'time exit" of N-bars for each entry in a complex position. But of course, a code like below would generate a compile error of "Orders cannot be inside a loop".

Code: Select all

for ii=0 to posTradecount(0)-1
begin
if currentbar-PosTradeentryBar(0,ii)=TimeStop then
Sell ("TimeBarsLX"+Numtostr(ii,0) ) from entry ("Buy"+Numtostr(ii,0)) next bar at market ;
end;
Is there a way to achieve the above?

And what's the rationale for not allowing orders inside the loop?

User avatar
Anna MultiCharts
Posts: 560
Joined: 14 Jul 2017
Has thanked: 42 times
Been thanked: 140 times

Re: How to exit from multiple entries

Postby Anna MultiCharts » 10 Jan 2018

Hello, Zheka!

I’m afraid that is not possible to generate orders in a loop, this is pre-determined behavior.
You can specify the required number of lines for exits or use the SameExitFromOneEntryOnce attribute to achieve your goal.

wilkinsw
Posts: 662
Joined: 21 Apr 2013
Has thanked: 154 times
Been thanked: 104 times

Re: How to exit from multiple entries

Postby wilkinsw » 11 Jan 2018

You could easily achieve what you need by flagging your exit condition using an array.

Code: Select all

array: exitarray[50](0)

for ii=0 to posTradecount(0)-1 begin
exitarray[ii]=0
end;

for ii=0 to posTradecount(0)-1
begin
if currentbar-PosTradeentryBar(0,ii)=TimeStop then
exitarray[ii]=1 ;
end;

//then you have to list all the exit lines unfortunately
if exitarray[{x}]=1 then Sell ("TimeBarsLX"{+x}) from entry ("Buy"{+x}) next bar at market ;
If the above isn't right it's not a million miles off.

I'm guessing handling orders outside of loops is a safety measure that won't be taken away any time soon.

Zheka
Posts: 223
Joined: 13 Jan 2016
Has thanked: 8 times
Been thanked: 53 times

Re: How to exit from multiple entries

Postby Zheka » 11 Jan 2018

Brute force rules! :-))


Return to “MultiCharts”