Split buy order into two buy orders  [SOLVED]

Questions about MultiCharts and user contributed studies.
tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Split buy order into two buy orders

Postby tozwp » 24 Apr 2014

Stumped over how to code this. I have a strategy that takes partial profits. At a profit target, it closes 25% of the full trade. I was doing this by using one buy order for the full amount and then when a condition was met, selling 25% of the original position. The problem I am running into using this logic is that in backtesting, most of the time the closing of this 25% position happens at a higher price than would be possible. The signal runs with IOG on and the condition for partial profit is met after price has exceeded the target price. The sell order occurs within a conditional loop which waits for the price to meet the target but during backtesting, the target price is usually higher resulting in an unrealistic fill. What I would like to do is use two buy orders. One for 25% of the total position and one for 75% of the position. Then I could just issue a sell order for the 25% position at a limit price without using the conditional statements. I tried to do this by splitting up my buy order into two buy orders but it only buys the first order. This is what I tried:

Code: Select all

if TrendFlag and Flag and NoGap then begin
Buy("75") .75*size shares next bar at Trig Stop;
Buy("25") .25*size shares next bar at Trig Stop;
end;
The original buy just used one buy order nested in the logic. Written like this, only the "75" order is filled and the "25" order goes unfilled. Is this normal behavior and how do I place more than one buy order?

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

Re: Split buy order into two buy orders

Postby Andrew MultiCharts » 25 Apr 2014

Hello tozwp,

Please give an example of such trade. Specifically, i am interested in the number of entry contracts.

Also i recommend you to read this article to learn in details how orders can be filled in backtesting.

tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Re: Split buy order into two buy orders

Postby tozwp » 25 Apr 2014

Thanks Andrew. I didn't have both the Bar Magnifier and IOG box checked. I only had the Magnifier checked.

It looks better now but I am running into another issue. When a sell order for partial profits is made, it appears that no other sell orders will take place on the same bar. I have several instances on my charts where both the partial sell and the rest of the position should be sold on the same bar. The partial is sold and then the system exits on the next bar if the condition is still true. For instance, I have a partial sell at a price level. In the code I also have:

Code: Select all

SetStopShare;
SetBreakEven(R*1.2);
where R*1.2 is exactly the same level as the partial profit order. When the partial profit sell order is filled I should sell the rest at break even if price drops to that price. What I find is that the order fills on the next bar even though price retraced back through break even on the same bar that the partial profit was taken. This same behavior also occurs on bars that continue up and hit other profit targets within the same bar. For instance, if the order for partial profits is filled and the bar goes up, the next profit targets are hit but the order to sell there doesn't happen. If the conditions are still true on the next bar, the order is then filled. My question is, how do I get more than one sell order to take place on the same bar?

Should mention that this is all for historical backtesting. Not sure if this same behavior will take place for live trading also?

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

Re: Split buy order into two buy orders

Postby Andrew MultiCharts » 25 Apr 2014

It looks better now but I am running into another issue. When a sell order for partial profits is made, it appears that no other sell orders will take place on the same bar. I have several instances on my charts where both the partial sell and the rest of the position should be sold on the same bar. The partial is sold and then the system exits on the next bar if the condition is still true. For instance, I have a partial sell at a price level.
Could you give me the piece of code where both orders are generated?

tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Re: Split buy order into two buy orders

Postby tozwp » 25 Apr 2014

Code: Select all

if MP = 1 then sell .25*size shares from entry("Long") next bar at (1.2*R + Trig) limit;

if MP = 1 then sell from entry("Long") next bar at iStop Stop;

SetStopShare;
SetBreakEven(R*1.2);
The stop price for the middle line will vary depending on the current price. It will be the initial stop level (below the entry price) during the time that the partial profit is taken and when the SetBreakEven should be working. At higher R levels, iStop will move up and be higher than the break even price.

tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Re: Split buy order into two buy orders

Postby tozwp » 25 Apr 2014

Here is a chart showing the behavior. The code takes partial profits and then the price drops below the break even point. It then exits on the next bar at the open because it is below the break even. If I comment out the first line of the code above, it exits on the next bar after entry at break even. Shouldn't it both take profits and exit on the same bar?
Attachments
Capture.JPG
(39.16 KiB) Downloaded 791 times

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

Re: Split buy order into two buy orders

Postby Andrew MultiCharts » 25 Apr 2014

Please compare the generated prices for the exit orders in your code:

Code: Select all

if MP = 1 then begin
print(File("C:\sell_limit_exit.txt"), "======================", NewLine,
"The date is ", DateToString(DateToJulian(Date)), ",", NewLine,
"time is ", TimeToString(ELTimeToDateTime(Time)), ",", NewLine,
"bar # is ", Symbol_CurrentBar, ",", NewLine,
"current bar open is ", open, ",", NewLine,
"current bar high is ", high, ",", NewLine,
"current bar low is ", low, ",", NewLine,
"current bar close is ", close, ",", NewLine,
"market position on chart is", MarketPosition, ",", NewLine,
"MP = 1, so at close of this bar (or at current tick if IOG=TRUE) the limit order to sell at ", 1.2*R + Trig, " is generated by the script", NewLine,
"to be executed at bar # ", Symbol_CurrentBar+1, " if IOG=FALSE. If IOG=TRUE, it can be filled at next intrabar tick");
sell .25*size shares from entry("Long") next bar at (1.2*R + Trig) limit;
end;

if MP = 1 then begin
print(File("C:\sell_stop_exit.txt"), "======================", NewLine,
"The date is ", DateToString(DateToJulian(Date)), ",", NewLine,
"time is ", TimeToString(ELTimeToDateTime(Time)), ",", NewLine,
"bar # is ", Symbol_CurrentBar, ",", NewLine,
"current bar open is ", open, ",", NewLine,
"current bar high is ", high, ",", NewLine,
"current bar low is ", low, ",", NewLine,
"current bar close is ", close, ",", NewLine,
"market position on chart is", MarketPosition, ",", NewLine,
"MP = 1, so at close of this bar (or at current tick if IOG=TRUE) the stop order to sell at ", iStop, " is generated by the script", NewLine,
"to be executed at bar # ", Symbol_CurrentBar+1, " if IOG=FALSE. If IOG=TRUE, it can be filled at next intrabar tick");
sell from entry("Long") next bar at iStop Stop;
end;

print(File("C:\breakeven_stop_exit.txt"), "======================", NewLine,
"The date is ", DateToString(DateToJulian(Date)), ",", NewLine,
"time is ", TimeToString(ELTimeToDateTime(Time)), ",", NewLine,
"bar # is ", Symbol_CurrentBar, ",", NewLine,
"current bar open is ", open, ",", NewLine,
"current bar high is ", high, ",", NewLine,
"current bar low is ", low, ",", NewLine,
"current bar close is ", close, ",", NewLine,
"market position on chart is", MarketPosition, ",", NewLine,
"On every code calculation the BrekEven stop order to exit at ", EntryPrice, " is generated by the script", NewLine,
"to be executed only if you have an open position at bar # ", Symbol_CurrentBar+1, " if IOG=FALSE. If IOG=TRUE, it can be filled at next intrabar tick");
SetStopShare;
SetBreakEven(R*1.2)

tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Re: Split buy order into two buy orders

Postby tozwp » 25 Apr 2014

Pasted it into my current code and it is generating three gigantic files. I'm going to have to figure out how to only run this on the trade of interest.

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

Re: Split buy order into two buy orders

Postby Andrew MultiCharts » 25 Apr 2014

Please add time-based conditions to do that.

tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Re: Split buy order into two buy orders

Postby tozwp » 25 Apr 2014

Andrew, I created those files and looked at them. On the entry bar, the entry and stops are shown correctly. On the next bar the stops are also shown correctly. On the chart, the partial sell is shown at the correct price. In the file the market position continues to show "1" as it should because the position has only been reduced. At the end of the bar the sell limit price is still shown but the sell has taken place a few minutes before the close of the bar. The breakeven stop exit price is calculated correctly in the printout but it never executes on the chart. At the end of the second bar, the close is below the breakeven stop and nothing happens. The breakeven stop only triggers on the beginning of the next bar when it opens below the BE trigger.

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

Re: Split buy order into two buy orders

Postby Andrew MultiCharts » 25 Apr 2014

Could you provide us with following files (please send us them to support@multicharts.com)?

tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Re: Split buy order into two buy orders

Postby tozwp » 25 Apr 2014

All items sent Andrew. Thanks for your help.

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

Re: Split buy order into two buy orders

Postby Andrew MultiCharts » 05 May 2014

Did not hear anything back from you after i sent you last email with prints for you code. Please let us know if you have any further questions.

tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Re: Split buy order into two buy orders

Postby tozwp » 05 May 2014

Sorry Andrew, I didn't know that you were looking for that output. I'll see if I can generate it later for you.

From what I've found, when there are orders that should be filled in the same hourly bar, only the first partial profit order is filled and the other order won't fill until the next bar regardless of whether it is a limit order or if I use SetBreakEven. I've been working on a workaround that uses 1 minute bars instead. Assuming conditions that would trigger both the partial fill order and the break even order don't happen in the same minute, this should be a bandaid solution. I've run the same code in TS and it will exit multiple times on the same bar. The behavior in MC doesn't seem to be correct.

Have you tried running the code on your end? It should result in the same issues that I have been seeing.

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

Re: Split buy order into two buy orders  [SOLVED]

Postby Andrew MultiCharts » 06 May 2014

Please provide me with print files demonstrating that the required number of profit for activating breakeven stop order is satisfied at a particular code calculation and then the breakeven is not executed at a bar, where it should be.


Return to “MultiCharts”