limit order exits through loop

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

limit order exits through loop

Postby vking » 18 Jan 2011

MC Team - I am currently using MC 7 alpha and the following code doesn't compile in MC where as the same code compiles fine in TS 9.

Would you please check and confirm whether the orders through loop is not supported or not?

----
Input: CCTOBuy(10);

Vars: OKtoBuy(True);

If OKtoBuy=True then begin
OKtoBuy=False;
Buy CCToBuy contracts next bar market;
end;

If Currentcontracts > 0 then begin
Value70=Avgentryprice;
For value1=Currentcontracts downto 1 begin
Sell 1 contracts next bar at Value70+(value1*0.25) limit;
end;
end;
-----

error:
Please wait ....
------ Compiled with error(s): ------
Orders cannot be inside a loop.
errLine 13, errColumn 7, errLineEnd 13, errColumnEnd 7
causal study: (Function)

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: limit order exits through loop

Postby piranhaxp » 18 Jan 2011

Vking, as the log said, loops can't be defined in order statements. But checking your code, for what do you need the loop ?

Mike

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

Re: limit order exits through loop

Postby vking » 18 Jan 2011

piranhaxp - thanks for the reply.

Basically - In the process of building a little complex strategy - where the number of contracts are unknown ( can be any number ) - would like to scale out at fixed profit targets with limit prices.

As the number of contracts is unknown - and the average price is not known - can't issue multiple sell orders( if I have 100 contracts in hand - would have to issue 100 sell orders which is really not an optimal way) - wanted to issue limit order sell for each contract at Average entry price+onetick for all the contracts that are filled - through a loop.

If I can achieve this by some means - without actually hard-coding the number of sell orders - this would give me an option to expand further on this.

Appreciate if someone can recommend an elegant way to achieve this.

Thanks.

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: limit order exits through loop

Postby piranhaxp » 19 Jan 2011

VKing ... may I got you wrong, but please could you try the attached code. I did not tested, so would be nice to get an response from you.

Code: Select all

Input: CCTOBuy(10);

Vars: OKtoBuy(True);
Vars: Ordercount(0);

If OKtoBuy=True then begin
OKtoBuy=False;
Buy CCToBuy contracts next bar market;
end;

If Currentcontracts > 0 then begin
Value70=Avgentryprice;
For value1=Currentcontracts downto 1 begin
ordercount=1;
end;
end;

if ordercount=1 then begin
Sell 1 contracts next bar at Value70+(value1*0.25) limit;
end;
Regards.

Mike

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

Re: limit order exits through loop

Postby vking » 19 Jan 2011

Mike - Thanks for the reply.

Unfortunately - this would issue only one limit order sell for 1 contract and would leave rest of the 9 contracts in hand - with out any sell orders.

It would be nice to get 10 limit order exits issued each with one tick higher than the previous limit order - and all at the same time.

Any help?

Thanks.
Attachments
MC_Order_Issue1.png
Only one limit order exit
(3.31 KiB) Downloaded 1498 times

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: limit order exits through loop

Postby piranhaxp » 19 Jan 2011

Vking .... ah now I got you. Let's try something. If I have a solution I'mcoming back to you. May someone else find something.

Mike

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

Re: limit order exits through loop

Postby vking » 19 Jan 2011

thanks Mike.

Please note that the catch here is - the quantity defined here (10 in this case) - needs to be dynamically configurable and not necessarily a constant 10. That's the reason it is defined as input parameter.

Thanks again.

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

Re: limit order exits through loop

Postby vking » 19 Jan 2011

Bump.. if any one can help in this regard.

Thanks.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: limit order exits through loop

Postby janus » 20 Jan 2011

As far as I know you can't submit multiple orders of the same type at the same time. You have to spread them out over consecutive ticks with IOG turned on. I know, this is a pain and not really the same. Would gladly be proven wrong but if right then it's a major limitation for sophisticated traders. I'm not one of them so it doesn't concern me so much.

Addendum: Actually, after thinking it through for a minute, I don't think even my workaround would work. Someone else might know. Given how MC manages orders, it can be problematical. I can understand why it's done the way it is - I think. It avoids a situation where catastrophic never ending orders are submitted. Still, I prefer a simpler alternative where an order can be placed once off and then left on the broker's end without the need to keep "refreshing" it from MC to keep it alive. It can have it's dangers as I said but that's why we have trial runs. Of course we would then need to cancel or modify orders using extra commands. All this is exactly like what one would do with a manual trading platform, which is why I and most other traders I know prefer it this way. Anyway, we just have to workaround such "limitations" the best way we can. MC suits me fine for now.

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: limit order exits through loop

Postby piranhaxp » 20 Jan 2011

Janus, still thinking about VKing's issue from time to time. I'm with you. The only one way I see is to export 'currentcontracts' variable with 'entryprice' into a 3rd party solution and putting the separated orders from there via macro/algo externally in the market. Then sending status of orders back into MC in variables to compare 'currentcontracts and 'entryprice' with MC's status. But I'm afraid in "fast markets" this could lead into huge problems. In "normal" markets I see no problems. But MC's API should/could handle it.

Mike

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: limit order exits through loop

Postby janus » 20 Jan 2011

Here's another workaround that should work. Given all your exit orders are very unlikely to be hit in one tick, you could submit the closest one first. When it's hit, you then submit the next one. You keep doing this until none are left or your strategy changes. Same logic applies to stops as well as limit orders. In a very fast moving market, the actual level exited will slip but there's nothing one can do about that other than to "press the panic button" and exit all positions immediately.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: limit order exits through loop

Postby janus » 20 Jan 2011

piranhaxp, I was planning to do what you suggested some time ago with IB's api calls. However, I then started to trade more simply so it obviated the need for such fancy approaches. Still, I might do it one day just for fun - when I have the time. I like to keep things simple - MC makes things far too complex for my liking. I spend far too much time thinking the way it works and writing code that's more complex than is required. I've done so much work now it's too late to change to another platform.

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

Re: limit order exits through loop

Postby vking » 20 Jan 2011

Mike/Janus - thanks for the replies. Appreciate it.

- Related to issuing orders at different time - though this would work - I would lose the ability to stay in the order queue if I issue the orders at a later stage.

- with external control - not sure whether it would be possible to perform - replay like features etc to test this out

- with the order limitations that this is presenting - I am doing manual coding - if currentcontracts=1 - issue one sell statement, and so on for 20 contracts 20 sell statements. The problem with this approach - can't scale more than 20 contracts and not a generic solution.

I guess - this is what I would have to get adjusted to :)

Thanks.
Last edited by vking on 20 Jan 2011, edited 1 time in total.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: limit order exits through loop

Postby janus » 20 Jan 2011

Yes, vking, being in the order queue ASAP is important. That also drove me to my earlier plan to develop my own external code using IB's api's to manage my orders, and just use MC for charting and front-end coding for my rules.

Yet another approach worth thinking about is to use multiple studies. So if you want 10 exit orders then have 10 studies. Not sure if this will work the way we would want it to if they are all applied on the same instrument. If it does work then you could use global variables to communicate the necessary details. The problem there is the execution order of the studies can be indeterminate, perhaps especially on multi-core processes. The api approach is by far the best. As I said, I might one day do it for fun. Perhaps others have already done this and are offering the solution for sale. I haven't bothered to check.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: limit order exits through loop

Postby janus » 20 Jan 2011

- with the order limitations that this is presenting - I am doing manual coding - if currentcontracts=1 - issue one sell statement, and so on for 20 contracts 20 sell statements. The problem with this approach - can't scale more than 20 contracts and not a generic solution.

I guess - this is what I would have to get adjusted to :)
I've done the exact same thing. See attached code. It includes a lot of other stuff I use to override orders using my own external DLL programs to block, supplement or cancel automatically generated orders. MC v7 will provide some of these features.

Which brings me back to my comment about how orders are managed. I like simplicity. If I want to sell short 10 new contracts, is just say "sell 10 contracts". If I then want to sell 10 more I prefer a system that uses the same statement. If I then want to take profits on 5 contracts I would prefer to say "buy 5 contracts". If I then want to go net long 10 contracts while I'm short 15, I would prefer to say "buy 25 contracts". Just my simple nature and how I traded manually for many years in the past phoning my orders in to my broker, before I started using computers. Even when I used computer platforms I continued to manage my orders the exact same way. It's only when I came across MC that I had to change my thinking. Still not used to it - I don't think I will ever get used to it. That could be the real reason I will eventually develop my own code to place orders using IB's api's - perhaps.
Attachments
code.txt
(27.45 KiB) Downloaded 400 times

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

Re: limit order exits through loop

Postby vking » 20 Jan 2011

Janus - thanks a lot for posting the code. Really appreciate it.

I totally agree with you about the simplicity of placing the orders.

Related to the code you have posted - would be going over to see if I can learn any tips :)

Thanks once again.

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Re: limit order exits through loop

Postby Dave Masalov » 21 Jan 2011

MC Team - I am currently using MC 7 alpha and the following code doesn't compile in MC where as the same code compiles fine in TS 9.

Would you please check and confirm whether the orders through loop is not supported or not?
Orders inside a loop are not supported in MultiCharts. This was always the case. Earlier versions of TS had the same limitation. TS 9 behaves the following way: entries inside a loop are not supported ("Orders cannot be inside a loop"), exits can be coded within a loop, but in the end only the last exit is generated. Could you tell us how this "improvement" can be useful? You can simply remove your exits from the loop in MC code.

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

Re: limit order exits through loop

Postby vking » 21 Jan 2011

Dave - Thanks for the reply.

From my point of view - If I have 10 contracts(configurable) - I would like to issue a limit order sell and stay in the Sell order queue - and stay there until a condition changes - which would force me to cancel the order.

limitations I see with current way of order handling:
- I am being forced to issue multiple sell orders though my requirement is to issue each contract sell order with 1 tick higher than the previous tick. This is not a scalable approach and what if I have to deal with 100s of contracts - I would be forced to issue 100 individual sell orders
- as a work around - you might say - monitor the current contracts and issue an order later, but with this approach - I would have to come to order queue - only after the first contract is closed( would be late to the order queue and less chances or getting filled at the limit order I pick)
- With the help of loop - based on the currentcontracts in hand - I would have the ability to issue the orders as needed with one single line of code.

Following are other Order limitations that I could think of (unrelated to loop)
- even better - if the order entry supports this kind of format - where stringvariable is manipulated by someother means but as far as I know EL doesn't allow this.
buy ({SringVariable}) 2 contracts next bar market; -- This is not allowed
buy ("string") 2 contracts next bar market -- This is allowed
- There is no issue and stay in the queue concept - assuming order gets issued every bar if the conditions are met - this way - we would lose the ability to stay in the queue
- There is no word to "cancel all the orders in the queue"

PS: Please note that like all the features and ease that EL brings to the table but wishing for few advanced order entry handling features in EL.

Thanks.

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: limit order exits through loop

Postby piranhaxp » 21 Jan 2011

VKing ..... which platform are you using for execution ?

I stick with my proposal to "export" or call it "transfer" your currentcontracts variable and your averageprice to the external platform with execution script inside and transfering the orderstatus back to mc to compare position status. Should be easy going.

Mike

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

Re: limit order exits through loop

Postby vking » 21 Jan 2011

Mike - I currently use NT for order entry - but I am trying to see if I can use MC itself for charting as well as order entry through strategies.

By using external DLL - I woudn't have the ability to replay etc to make sure the orders being placed/replaced/cancelled etc as I would expect them to be.

Thanks

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

Re: limit order exits through loop

Postby vking » 21 Jan 2011

And I have also made a feature request to have a simulator in MC(similar to NT which would work in realtime as well as during replay) - which would allow to experiment much better with different order styles and this would bring lot of good possibilities in many areas.

Thanks


Return to “MultiCharts”