Next bar execution is fine
In that case it is simply solved by
If Not OnMarket then
// Do your Buy or ShortSell thing
Endif
with the notice that this is slightly more complicated because now you need to keep track of your last position (Short vs Long). Only when you know that, you can “invert”, right ?
So I think it will be more easy for you to do it in the same bar (because you know in your sequential code (which is executed from top to bottom) when you did higher-up.
I am not so sure whether I should pre-cook that code, because quite some more is involved than just “do it”. I will give you a snippet (implied to execute in the same bar), though, so you can see what’s involved the least (as how I see it) :
if SellBuy then // 11-02-2018,PS. Sell / Buy again right away ?
// Apply the regular means to determine the direction.
// Note that this could change later hence is *not* really
// redundant from this similar determination elsewhere.
if DroppingShortTerm and not JustSoldShort then // 27-05-2018,PS, JustSoldShort. 28-05-2018,PS, ShortTerm version (Test !).
// 08-02-2018,PS, But we also want a CLEAR rise.
// This works, but does not work out for the better (too short in
// the market).
// 09-05-2018,PS, There's a kind of major bug in this SellBuy code now, because
// it does not take into account the Trend (which is new since a few days).
// Also, the sheer reason the system just has sold could be buggy (or unclear)
// in itself and THAT implies that while we were e.g. dropping and just sold,
// changing the direction could be blatantly wrong. [...]
If TrendNet = -1 Then // 09-05-2018,PS, This If. Trend is Dropping indeed ?
if [my condition to enter the market in the opposite direction] then
SELLSHORT BuySellAmountForReal SHARES AT MARKET
JustSoldShort = 1 // 27-05-2018,PS.
JustSoldLong = 0 // 27-05-2018,PS, Reset.
endif
endif
endif
endif // SelBuy ?
If you want to do this cross bars (thus exit in the one second and enter in the next second) then focus on either
- the StrategyProfit PRT constant
- define variables with Once so you can track a last occurred value (e.g. Once LastTradeDirection)
Regarding the latter, a variable declared with Once, will appear to have its last assigned value in the previous bar (-call) in the new bar. In my example JustSoldShort and JustSoldLong would be suitable for that. But/and, you would need to reset them at the end of the code, if no trade happened, because else it would not be an “invert” really. … which IMO it already is not if you apply this cross bars.
Hope this helps …
Peter