Stop Loss to trigger new trade
Forums › ProRealTime English forum › ProOrder support › Stop Loss to trigger new trade
- This topic has 15 replies, 3 voices, and was last updated 2 years ago by JS.
-
-
11/30/2021 at 11:48 AM #182518
Hello,
Forgive me, still very much learning. If a trade closes because of a stop loss, I want to open a new trade in the opposite direction immediately.
1sec timeframe, I have my script set to 1 trade per trading window, unless that trade went to stop loss, then 1 more trade is allowed in the opposite direction.
What am I doing wrong?
12345678910111213//check profitif strategyprofit<>strategyprofit[1] thenif close < tradeprice AND count =1 thenSELL 1.5 PERPOINT AT MARKETcount = count + 1SET STOP LOSS 6elsif close>tradeprice AND count=1 thenBUY 1.5 PERPOINT AT MARKETcount = count + 1SET STOP LOSS 6endifendifMany thanks
11/30/2021 at 12:00 PM #182519Hi Joeyp,
You can’t do that within the same bar (hence same call of your code) with a formal Set Stop Loss command.
If you keep track of the StopLoss yourself (If Price < StopLossFigure) and Sell (when Long), you can buy in the same bar when further down the line you have a Buy command again.You can keep the formal Set Stop Loss command (at the very end of your code) for safety, if it is only a little further (deeper) from what you coded “manually” per what I told above.
If you need a more literal example, just let me know.
Peter11/30/2021 at 12:12 PM #182521You will know that you managed by not being able to zoom in such that two separate orders appear. See attachment 1 and 2. There’s always the little “2”, telling you that it happening within the same bar (hence second in your case).
Of course you can also observe it in the closed positions list (attachment 3).Notice that it is also possible – when e.g. 10 Long – to sell (or Exit Short) 20. This may save some on minimum Brokerage fees per quantity threshold. If you pay per spread, this does not matter (that I know).
11/30/2021 at 12:18 PM #182525you can buy in the same bar when further down the line you have a Buy command again.
Apologies – in your case this should read :
you can invert in the same bar when further down the line you have an Sell
Notice that it is also possible – when e.g. 10 Long – to sell (or Exit Short) 20.
That too was wrong (should be Sell Short). But I assume you got the gist anyway.
Short command again (when you were Long).11/30/2021 at 12:30 PM #182526Wow, I couldn’t make that more messy (now a quote was injected in the middle of a sentence. 🙁 )
Here the first sentence as how it should be (I hope) :
Apologies – in your case this should read :
you can invert in the same bar when further down the line you have an Sell Short command again (when you were Long).11/30/2021 at 12:56 PM #18252711/30/2021 at 2:17 PM #182530Next bar execution is fine
In that case it is simply solved by
123If Not OnMarket then// Do your Buy or ShortSell thingEndifwith 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) :
1234567891011121314151617181920212223242526272829if 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] thenSELLSHORT BuySellAmountForReal SHARES AT MARKETJustSoldShort = 1 // 27-05-2018,PS.JustSoldLong = 0 // 27-05-2018,PS, Reset.endifendifendifendif // 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 …
Peter11/30/2021 at 3:43 PM #18253412345678910111213//check profitif strategyprofit<>strategyprofit[1] thenif close < tradeprice AND count =1 thenSELLSHORT 1.5 PERPOINT AT MARKETcount = count + 1SET STOP LOSS 6elsif close>tradeprice AND count=1 thenBUY 1.5 PERPOINT AT MARKETcount = count + 1SET STOP LOSS 6endifendifUse SellShort for short positions (not Sell).
Buy versus Sell
SellShort versus ExitShort
11/30/2021 at 5:35 PM #18254012/01/2021 at 6:59 AM #182557Been trying the following, it works, except if a trade was open and closed in the same bar. Any ideas? Do I need to count +1 bar before executing a new trade?
123456789If StrategyProfit < StrategyProfit[1] AND Not OnMarket thenIf LongOnMarket[1] AND count=1 thenSellshort 1.5 PERPOINT AT MARKETcount = count + 1Elsif count=1 thenBUY 1.5 PERPOINT AT MARKETcount = count + 1EndifEndif12/01/2021 at 7:48 AM #182558Turnaround12345If Not OnMarket and LongOnMarket[1] thenSellshort 1.5 PERPOINT AT MARKETElseBUY 1.5 PERPOINT AT MARKETEndifIt is not necessary to count.
Keep the program order straight:
You can start with “DefParam CumulateOrders = false”
- Conditions for going Long / Short
- StopLosses
- and then your “turnaround” strategy.
12/01/2021 at 1:06 PM #182600Hi JS,
I’m counting because I only want 2 trades max per trading window, if trade 1 is a loss, trade 2 is triggered, if trade 1 is profit, no trade 2.
I’ve got my code setup as you say, with:
“DefParam CumulateOrders = false”
- Conditions for going Long / Short
- StopLosses
- and then “turnaround” strategy.
However, my turnaround strategy as above works, EXCEPT when trade 1 has opened and closed in the same bar (1sec timeframe) which I need to fix.
Sorry I wasn’t clearer, any help much appreciated.
12/01/2021 at 1:56 PM #182604EXCEPT when trade 1 has opened and closed in the same bar (1sec timeframe)
Hi Joeyp, … are you sure those trades actually happened within that second ?
As you wil have seen, I am doing 1 second TF and I can’t imagine it to happen, except for when I do things really wrong, and it also needs to be two subsequent orders in the same direction. Hence :
A buy followed by a sell in the same bar (second) would cancel out, and the program will not put through an order, net. Regarding this, notice that the orders will only be given at the end of the bar you’re in (at least that has always been my thinking), which is why all ends up with a net result. Thus :The text I quoted, could be about you abusively putting a buy and a sell in the same bar (thus within the same call of your code). This is different from an opened and closed trade as such.
Also notice this part :
1234567891011121314// Let's say Count is 1.// Let's also say LongOnMarket[1] = 0 (false).// Lat's also assume that ShortOnMarket[0] (!) is True.If LongOnMarket[1] AND count=1 then // This will be false.Sellshort 1.5 PERPOINT AT MARKETcount = count + 1Elsif count=1 then // This will be True.BUY 1.5 PERPOINT AT MARKETcount = count + 1Endif// Result : You bought 1.5 pp against a Short position of (undoubtedly) 1.5pp. Net this leaves zero position.// But it wouldn't close and open in the same bar - just saying.OK ?
12/01/2021 at 2:08 PM #18260512/01/2021 at 4:58 PM #182630I am sorry, but I can’t reason how this would emerge.
Of course with a real invert this would happen in the same second. This also would happen with a semi-invert (Sell Short 1 when being Long 1) which ends up at net 0.- With a real invert I would not be able to spread this over two bars;
- With Buying 1 in the one bar (call) and inverting that with 2 in the next bar, I would not be able to do that in the same second.
I’m afraid it is still your code with an odd “structure” ten things happening you don’t expect and also can’t follow (map to what really happened). Thus see my previous post where I try to point out easily possible anomalies, just because of not-so-decent coding.
Meanwhile I can guarantee you that all can work as you intend, as I do it all the time myself (in Live).Don’t hesitate to post more (questions) !
-
AuthorPosts
Find exclusive trading pro-tools on