- ago
Hi,

This is for backtesting only.

The blocks below work fine for sells where the limit is reached. What I want to do is if the sell doesn't happen by the 4th day after the buy, then sell at the closing price on the 4th day. The way this is laid out, the sells are using the open, instead of the close.

I just realized that even if it hits the limit, on the 4th day, it still shows the sell as being at the open for the day.

I've been playing with Trading Days in Trade, 1 Bar in Future, but not successfully.

Can this be done?
Thanks!

0
158
Solved
12 Replies

Reply

Bookmark

Sort
Glitch8
 ( 12.10% )
- ago
#1
You can do it using the Power Pack Blocks:
- Sell Next Bar at Market Close
- Bars since Entry/Exit Condition

0
- ago
#2
Thanks, Glitch. It works fine if I do Single Position:



But if I try Multiple Positions, it doesn't seem to be able to keep them apart. Stock bought on different days should exit on different days, not bunched together like this:



Am I missing something?
0
Cone8
 ( 6.32% )
- ago
#3
The rule is based on the "last Entry" not the entry of each position.

Try using "Trading Days in Trade" instead.
1
- ago
#4
Yes. I tried that, but it doesn't compile. Nothing in the Log Viewer. Is there some undocumented method to see why it won't compile?

0
Cone8
 ( 6.32% )
- ago
#5
Click the Open as C# Coded Strategy button, Compile.
We'll have to check that too.

... and it compiles fine for me. There must be something else in the Strategy that's broken... ah, multiple positions....
0
Glitch8
 ( 12.10% )
- ago
#6
It looks like that Power Pack Block was not coded to play nice with the multiple position setting. I'll fix it ASAP.
0
Cone8
 ( 6.32% )
- ago
#7
Glitch, but the whole point of this was not to use the LastOpenPosition. Make sure to fix it on a per-Position basis.
0
- ago
#8
Here's what I got when I compiled:

0
Cone8
 ( 6.32% )
- ago
#9
@steinh,
I'm not certain yet if it will be possible to fix rule in blocks for multiple positions, but you can do it by rearranging the C# code like this:

Find this section (you have to be precise):

CODE:
condition0 = false; {    tdays = bars.TradingDaysBetweenDates(bars.DateTimes[foundPosition0.EntryBar].AddMinutes(-bars.Scale.Interval), bars.DateTimes[index]);    if (tdays >= 3)    {       condition0 = true;    } } if (condition0) { foreach(Position foundPosition0 in OpenPositions) {    if (foundPosition0.PositionTag == 0)    {       Backtester.CancelationCode = 1;       ClosePosition(foundPosition0, OrderType.Market, 0, "Sell At Market (1)");    } } }


And rearrange it like this (you can just copy and paste this to replace the section above)

CODE:
foreach(Position foundPosition0 in OpenPositions) {    condition0 = false;    {       tdays = bars.TradingDaysBetweenDates(bars.DateTimes[foundPosition0.EntryBar].AddMinutes(-bars.Scale.Interval), bars.DateTimes[index]);       if (tdays >= 3)       {          condition0 = true;       }    }    if (condition0)    {       if (foundPosition0.PositionTag == 0)       {          Backtester.CancelationCode = 1;          ClosePosition(foundPosition0, OrderType.Market, 0, "Sell At Market (1)");       }    } }
0
Best Answer
- ago
#10
Thanks, Cone, but it sells at the open. The new code produces the same results as "Sell after 4 bars".

I tried using "OrderType.MarketClose", but it errors with
87: 'OrderType' does not contain a definition for 'MarketCLose'

I need it to sell at the closing price, as I'm trying to simulate doing a MOC for whichever stocks haven't already sold on the final day (4th day after entering the trade).

0
Cone8
 ( 6.32% )
- ago
#11
In C# code, case matters. You put "MarketCLose" with a capital "L" that should be lowercase: OrderType.MarketClose
0
- ago
#12
D'oh! Fat-finger coding.

That fixed it. Thanks!
0

Reply

Bookmark

Sort