Open and Close at the same bar with AtClose orders
Author: MDA
Creation Date: 2/15/2017 1:37 PM
profile picture

MDA

#1
Hello,

how can I Open and Close at the same bar with AtClose orders? This is for a reallocation. Example, every end of month sell all position an reopen this position at close with the actually %size of Equity.

MDA
profile picture

Eugene

#2
Hi,

Clarification required as probably I misunderstand you. Assuming daily scale, you buy the same symbol with BuyAtClose and issue an immediate SellAtClose. What'd be the purpose of doing this in a Strategy, as it only leads to a loss due to trading costs?
profile picture

MDA

#3
Hi Eugene,

A simple example. I have two positions with 50/50 size from the equity. Next month, one position has risen more than the other. I now have 60/40 size. Now I want to reallocate 50/50. I thought for a simple code, I sell everything and buy new 50/50 back. In reality, I sell only one part and buy only part of the other to get to 50/50.

MDA
profile picture

Cone

#4
That keeps it simple and certainly a good way to do it. Example below.

Install Community.Components to use the DateOfNextTradingDay method to assist finding the last trading day of the month.

CODE:
Please log in to see this code.
profile picture

MDA

#5
Hi Cone,

thanks for feedback.

CODE:
Please log in to see this code.


this does'nt make a trade. the change of month was not the problem. If I close and open next day at market, then works my code. only atClose is the problem. with atclose I have a close end of moth and a open end of next month.


MDA
profile picture

Eugene

#6
QUOTE:
this does'nt make a trade.

Matthias, how do you run the strategy? Sure I get some entries if I run Cone's code.
profile picture

MDA

#7
hmm, OK the complete code from Cone works, but with the same problem ... the open is end of next month with me

profile picture

Eugene

#8
That's because there is not enough capital to execute all signals. Try it in Raw Profit mode to make sure the code works as intended.
profile picture

MDA

#9
ahhhh yes, in RAW Profit Mode works the code, hmmm but I need the Portfolio Simulation Mode ... I test with Margin Factor 10:1 for enough Money ... with "atMarket" oders for next day works the Code correct, only with "atClose" not
profile picture

Eugene

#10
profile picture

MDA

#11
Sorry, I have tried workaround 1 and 2. I do not get it to work as it should.
profile picture

Eugene

#12
Then your position sizing chosen within Position Options is not appropriate for the selected DataSet, if you wanted to capture all signals. Try a much smaller position size, use Raw Profit, or execute the system in single-symbol mode.
profile picture

MDA

#13
I have still one idea ... is there a code for with "SellAtMarket" and "SellAtLimit" how this Code:

CODE:
Please log in to see this code.



When I Change Buy and sell then I get error messages
profile picture

Cone

#14
I'm not sure where your snippet came from, but here's the correction for that BuyAtMarket method:

CODE:
Please log in to see this code.


Now, the reason for doing that for BuyAtMarket was to put the precise entry price to enter at limit on the open in order to not skip trades for the backtest. It makes no sense to do that for exit orders. If you SellAtMarket (or ExitAtMarket), no resizing is required... the number of shares in the position that you specify will be exited. Ok?
profile picture

MDA

#15
Hi Cone,

thanks for help. " pos = LastPosition " that was my mistake. I had previously experimented with sellatlimit and forget to delete.

With Buyatlimit (the Code above) works for me when I increase the margin factor. But then too many positions are opened. I just can not get any further.
profile picture

Eugene

#16
You mean that MF 10:1 causes too many positions to be opened? How about a smaller MF, like 1.10 or 1.15, for example?
profile picture

MDA

#17
with small margin factor I have the old Problem, the trades only open end of next month. really a simple Job. Close end of month five position and open same time five position with actually ROC Ranking. Regrettable that it is so complicated.
profile picture

Eugene

#18
I'm trying to communicate the point that your position size should be in line with the number of candidates your strategy is creating on this DataSet. Five positions and a 20% equity size means a lof of skipped trades that "Skipped trade solution" and "For last position, use all what's left" combined (from Position Options) should minimize. It should rather be 15% and a MF of 1.15, IMHO.
profile picture

MDA

#19
Hi Eugene,

thank you for your help. Sorry, I just can not get it.

MDA
profile picture

Cone

#20
If you like, please post your code with a detailed description of what you want it to do. I'm sure we can make it work ;)
profile picture

MDA

#21
Hi Cone,

here is the code:

CODE:
Please log in to see this code.


The Positions Settings are in the below in the picture.

I tried everything. I have not managed to close 5 positions at the close and open at the same time.

MDA
profile picture

LenMoz

#22
Did you intend the trailing semicolon on the line below?
CODE:
Please log in to see this code.
profile picture

Eugene

#23
Matthias,

The "Skipped trade solution" cannot help in your case. It's for AtMarket orders only.

The more symbols your DataSet contains, the more skipped trades will there be. On a 5-symbol DataSet, at 15% equity and Margin Factor = 1.5 you can virtually get rid of them.

P.S. There's a slight logic error:
CODE:
Please log in to see this code.
profile picture

MDA

#24
Yes, that ";" was wrong. But that is not the reason for my problem. I need a simple ranking system without lists programming for closing trading.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).