The following tends to work, but if I can only get the proceeds from shorting made available for long positions, and never do both at the same time, this may be profitable(?) I think what's needed is a way to identify whether the LastPositionActive == True is a short or long position. Either that or a dummy variable (0,1) would need to be set to 1 if currently in a short position, and simply don't go long when this variable is equal to one. Then, zero it out when covering -- and only go long when it's equal to zero.
Is there an internal function which reveals whether the last active position is a short or long?
FYI, changing the short and cover to i+2 does nothing.
Also, I do have logical for always exiting a long position if timenow > 1555, but I am not sure if an open short also needs to be closed after 1555(?).
CODE:
Please log in to see this code.
Size:
Color:
QUOTE:
a way to identify whether the LastPositionActive == True is a short or long position.
For
exiting a Position:
1. Strike F11 to bring up the QuickRef tool
2. Expand the "Position Property" category
3. See
PositionType Property
Size:
Color:
Here's an equity curve based on the above code and 1-min bars for Bank of America (BAC), one of the most liquid equities. Those are some huge corrections on the right side, and the shorting seems to turn a profit but going long seems to cause a slow leak somehow. You can also see the straight lines on shorting when long is active and straight lines on long when shorting is active -- so the simple dummy indicator (e.g., int CurrentlyShorting = 0 or 1) is working when placed into the logical statements when opening and closing positions.
The code above is being employed to generate this equity curve, so is there something that's off? Also, would this suggest adding a trend line of using e.g. a cobination of a short-lag 5-bar EMA and a longer e.g. 20-bar EMA to prevent going long when in a downtrend?
If shorting seems to generate revenue, why is going long not working when it's the exact opposite of going short. Maybe the head-fakes are triggering longs at the wrong times.
Size:
Color:
I have been attempting to exit short positions which go bad using a combination of Bars.Open[i], Bars.High[i], Bars.Low[i], Bars.Close[i], and LastPosition.EntryPrice when PositionType is a short, but the code seems to ignore the logical statement. Given the short position, which goes bad in the attached image, is there a sure fire way to exit such positions? With the amount of capital used (i.e, thousands of shares) such positions can lose $1-2k.
I can use an optimizable delta or something, but I think I am not picking off the information from bar i before the short position is entered on bar + 1. You can see that bar i was red, but bar i+1 and bar i+3 had very large positive returns. This is also followed by a run of positive returns a few minutes later.
I am considering using a median instead of EMA, either that or a low-lag MA from TASC (Ehlers). Is there a median function?
I am also not sure if setting the lastlongentryprice and lastshortentryprice with i+1 is optimal(?), or whether I need to fetch from the current ith bar (?)
CODE:
Please log in to see this code.
Size:
Color:
There are numerous aspects of this code I'd like to clarify before making a step forward:
* First and foremost, that short at stop entry after covering is puzzling me. If it were a market entry or the exit was made at market I'd understand it's a stop-and-reverse move. Anyway, you cannot do this without rewriting the Strategy for the multi-position template as this seems possible. So is this a single-position system or can it take multiple positions?
* You can't access a value at i+1 without first checking if the bar is not the last one - or you get a runtime error when processing for Alerts:
CODE:
Please log in to see this code.
** But why make an assumption there at all? Not only it's incorrect, the price level itsel would not match the actual entry price. So the correct way would be to use the .EntryPrice property.
* Be careful not to use this incorrect (reverse) order of lines if you get to query the "lp" in the future inside the in IsLastPositionActive block:
CODE:
Please log in to see this code.
So for single-position system I'd use this:
CODE:
Please log in to see this code.
And for multi-position scenario I'd take this:
CODE:
Please log in to see this code.
Size:
Color:
The intent is a single-position stategy, and thanks much for looking at the code. Hopefully, going back to EntryPrice will make things more straightforward. Using the Bars.Low[i] and high really affected the equity curve.
FYI-GA is probably better for difficult N-P hard type problems, but have you ever considered implementing PSO, as a more straightforward greedy hill-climbing heuristic? PSO is fast and doesn't stagnate like GA - at least for convex objective functions. GA is still best for Rastrigin and Ackley type functions.
Size:
Color:
Glad to help you.
We don't favor offtopic on the forum (like PSO is in this topic) but if you search the site you shall find it implemented by community member LenMoz with some discussion threads.
Size:
Color: