Multiple Limit Orders

Questions about MultiCharts and user contributed studies.
Platinum
Posts: 8
Joined: 14 May 2020

Multiple Limit Orders

Postby Platinum » 06 Jun 2020

My strategy (not for autotrading - just backtesting) needs the following functionality:

Daily Chart

1. Buy a number of shares at the opening.
2. Place a sell limit order at the price of (opening + 2.5% ).
3. Do the same operation every day bar.

It is assumed that the limit orders placed are in force till the end of strategy backtesting and should be filled at the prices set.
But I'm getting the following (please see a screenshot).

For some reason all my limit orders "accumulate" and fill at one bar for the price which is lower than expected...

Here is my code example:

Code: Select all

Var: StocksToBuy(0); If (Barnumber>0) Then Begin StocksToBuy = 200; Buy StocksToBuy shares Next Bar at Market; Sell StocksToBuy shares Next Bar (close+0.025*close) Limit; End;
I've tried both [IntrabarOrderGeneration = true] and also dynamic names for limit orders with no luck.

Please advise.
Attachments
multiple limit orders.jpg
(43.34 KiB) Not downloaded yet

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

Re: Multiple Limit Orders

Postby JoshM » 06 Jun 2020

Code: Select all

Sell StocksToBuy shares Next Bar (close+0.025*close) Limit;
I cannot see the price axis so don't know for sure if the following applies to your code. But can it be that this limit order executes too late because it always takes the current bar's close as the limit price?

In other words, this limit order doesn't use the close price at the moment when the entry order is generated. Instead it uses the close each time the strategy calculates.

That can give a "moving target" that is hit at a later point than intended.

Platinum
Posts: 8
Joined: 14 May 2020

Re: Multiple Limit Orders

Postby Platinum » 06 Jun 2020

Thank you for your response. Yes I think you are right. I've made another screenshot with price axes.

BUT I thought that as soon as a limit order is placed its closing price doesn't change... So each limit order I place must have it's own closing price which is not recalculating on every future bar...

So can you please tell me how do I code such a simple strategy I've described in my first message? With multiple limit orders that keep their closing prices and not recalculated on every bar...
Attachments
multiple limit orders 2.jpg
(182.89 KiB) Not downloaded yet

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

Re: Multiple Limit Orders

Postby JoshM » 08 Jun 2020

BUT I thought that as soon as a limit order is placed its closing price doesn't change... So each limit order I place must have it's own closing price which is not recalculating on every future bar...
Well we can use the `Open` variable for your limit exits, since that variable will not change during the bar. Here's how the code for that looks like:

Code: Select all

[IntrabarOrderGeneration = true]; Variables: IntraBarPersist limitPrice(0), stocksToBuy(200); // Buy a number of shares at the opening if BarStatus(1) = 2 then begin Buy stocksToBuy shares next bar at market; end; // Place a sell limit order at the open price + 2.5% if MarketPosition = 1 then begin Sell stocksToBuy shares next bar at open * 1.025 limit; end;
Here's how it looks on the chart:

Image

There's of course still the issue that the position is not closed on all the days. But that's because there's only one exit condition (open price + 2.5%), which isn't achieved on down days.
Attachments
2020-06-08_18-56-09.png
(12.15 KiB) Not downloaded yet

Platinum
Posts: 8
Joined: 14 May 2020

Re: Multiple Limit Orders

Postby Platinum » 09 Jun 2020

Dear JoshM! Thank you for your help. This one works better BUT it is not working as it should...

Your code also "recalculates" the previous limit orders closing price...

All I want to get is: if I place a limit order - the system "remembers" its closing price and doesn't change it... until it reaches this price or the end of the strategy.

Just multiple limit orders where their closing prices don't change so they are waiting to close at their initial closing price.

So on the chart - there has to be no "red dotted lines" meaning that the previous limit orders close BELOW their initial closing prices.

Is there a way to release this simple task?

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

Re: Multiple Limit Orders

Postby JoshM » 09 Jun 2020

This one works better BUT it is not working as it should... Your code also "recalculates" the previous limit orders closing price...
I thought I coded the strategy like you wished. Since you asked:
So can you please tell me how do I code such a simple strategy I've described in my first message?
And that first message says:
Daily Chart

1. Buy a number of shares at the opening.
2. Place a sell limit order at the price of (opening + 2.5% ).
3. Do the same operation every day bar.

It is assumed that the limit orders placed are in force till the end of strategy backtesting and should be filled at the prices set.
Now I am confused. You do not want limit prices that are based on the opening of the bar + 2.5%? :?:

Plus now you talk about multiple limit orders that wait at their 'initial closing price' (?), which seems to me different from what you requested first. It's quite confusing to me!

Platinum
Posts: 8
Joined: 14 May 2020

Re: Multiple Limit Orders

Postby Platinum » 09 Jun 2020

Sorry for confusing you. English is not my native language.
Will try to explain again.

1st bar opening price is 100. We buy 200 shares @ 100 at bar opening and place a limit order @ (100+2.5%) = 102.5. This bar high was < 102.5 and this bar closing price is 98. So limit order remains unfilled.
2nd bar opening price is 98. We buy another 200 shares @ 98 and place another limit order @ (98+2.5%) = 100.45. This bar high was < 100.45 and its closing price is 99. We have now two unfilled limit orders. First @ 102.5 and second @100.45
3rd bar opening price is 99 We buy another 200 shares @ 99 and place a limit order @ (99+2.5%) = 101.475. And this bar high was 101.5. So our 2nd and 3rd limit orders are filled @ 100.45 and @ 101.475 correspondingly.

But our first limit order remains unfilled waiting for its price 102.5.... and so on.

Does it make sense?

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

Re: Multiple Limit Orders

Postby JoshM » 10 Jun 2020

Ah now I understand what you meant with the opening order.

But if you don't mind, I won't participate in this forum thread anymore. Reading, replying, figuring out what you meant, and coding already took a decent portion of my free time.

Perhaps someone else here on the forum has the time and motivation to develop this strategy for you. Good luck!

Platinum
Posts: 8
Joined: 14 May 2020

Re: Multiple Limit Orders

Postby Platinum » 15 Jun 2020

Dear Josh! Thank you for your help!

May be someone has an idea of how to code this type of strategy with multiple limit orders?


Return to “MultiCharts”