Reputation: 109
I have a complex strategy that uses Bar States as triggers upon given indicator states. The challenge i am trying to solve is the prevention of simultaneous orders being executed either at the exact same moment of bar state or within the 1 bar and the trigger price might be a slight difference (e.g. 3 points).
For example: a White Marobozu bullish candle will often trigger at the same time a '3 Bull Soldiers' bar pattern - and the strategy executes 2x entries as both have triggered at the same time. However I only want 1 entry executed. This is a basic example - but there a many similar occurrences between different candle types.
I dont want to remove one or the other as there are many times they execute on their own.
The strategy parameters I have are as follows :
calc_on_every_tick=true, process_orders_on_close=true, max_labels_count=500, use_bar_magnifier = true, pyramiding=0)
In addition....if a entry condition and candle type is triggered on Bar[2] and another on Bar[1] and the price entry limit conditions are met for both in Bar[0] ...then again 2 entries are made on the single bar - but this time at a price difference of several points. This is despite the strategy order 'if statement' containing a not(isLong) command with isLong = strategy.position_size > 0.
I have pasted the strategy order code below .... if that helps. I have a moderate level of coding experience and have researched extensively for solutions and cannot find anything remotely close to solving this issue. I am hoping somebody here could point me in the direction of 'Look into this idea/solution' or how they may have constructed a custom script to prevent this concurrent simultaneous order entry on the same candle. This issue doesnt replicate over different bars due to the Pyramiding=0 command. But the pyramiding doesnt prevent same candle multiple order entries.
I hope I have explained this problem and needed solution clear enough. Thankyou so much for your time and attention to this :-)
Example order trigger and strategy order execution code 1:
L_BullEngulf = BullEngulfing and rsi_vwma_ind>DSmom and (DSmomUp or DSmomSigUp or DSmom>=DSmomSig) and BandsRestraint_Bullish and confirmed
plotshape(L_BullEngulf, color=color.new(color.rgb(139, 241, 6), transp = 0), location=location.belowbar, style=shape.arrowup, size=size.large, text = "L BullEngulf")
BullEngulfVal = ta.valuewhen(L_BullEngulf, high, 0)
BullEngulfPriceStop = BullEngulfVal + (syminfo.mintick*3)
BullEngulfPriceLimit = BullEngulfVal + (syminfo.mintick*4)
if inDateRange and L_BullEngulf and ( (isShort and C>strategy.opentrades.entry_price(0)) or not(isShort) ) and not(isLong)
strategy.order('L_BullEngulf', strategy.long, stop = BullEngulfPriceStop, limit = BullEngulfPriceLimit, comment='L_BullEngulf')
if ta.barssince(L_BullEngulf)>5 and C<=BullEngulfPriceStop
strategy.cancel("L_BullEngulf")
Example order trigger and strategy order execution code 2:
L_3BullSoldiers = (BullSoldiers3 and H<upperBandValue2 and (rsi_vwma_ind>DSmom or DSmomUp) and BandsRestraint_Bullish) or (BullSoldiers3 and H>upperBandValue2 and DSmom>DSmomSig and (rsi_vwma_ind>DSmom or rsi_vwma_ind>60)) and confirmed//2nd component for BO>upperBandValue2
plotshape(L_3BullSoldiers, color=color.new(color.rgb(139, 241, 6), transp = 0), location=location.belowbar, style=shape.arrowup, size=size.large, text = "L 3 Bull Soldiers")
BullSoldiersVal = ta.valuewhen(L_3BullSoldiers, high, 0)
BullSoldiersPriceStop = BullSoldiersVal + (syminfo.mintick*3)
BullSoldiersPriceLimit = BullSoldiersVal + (syminfo.mintick*4)
if inDateRange and L_3BullSoldiers and not(isLong) and ( (isShort and C>strategy.opentrades.entry_price(0)) or not(isShort) )
strategy.order('L_3BullSoldiers', strategy.long, stop = BullSoldiersPriceStop, limit = BullSoldiersPriceLimit, comment='L 3 Bull Soldiers')
if ta.barssince(L_3BullSoldiers)>5 and C<=BullSoldiersPriceStop
strategy.cancel("L_3BullSoldiers")
Upvotes: 1
Views: 452
Reputation: 109
After further extensive research and testing; I have finally found the solution to this problem and it has proven to work over the past 4 weeks.
When a strategy doesn’t respect its pyramiding setting, one solution is to place entry orders in an OCA (one cancels all) order group that uses the strategy.oca.cancel variable setting. So when one OCA order fills in TradingView, other orders that are triggered in the same bar and same moment from the same group are cancelled .
My entry order script has been modified as following to include 'oca_type=strategy.oca.cancel, oca_name="Short Entry" ' and now only ever triggers one order in the same bar irrespective of how many Candle Patterns trigger an execution in that same bar:
if inDateRange and (S_BearTrig_vwap or S_BearTrig_BD) and strategy.position_size==0 // and not(isShort) and ( not(isLong) ) //(isLong and C<strategy.opentrades.entry_price(0)) or
strategy.entry("S_Bear", strategy.short, stop = BearPriceStop, limit = BearPriceLimit, qty = 1, oca_type=strategy.oca.cancel, oca_name="Short Entry", comment="S_" + str.tostring(BearText) + str.tostring(BearTrig_Text)) //
Futher information regarding this can be found in the following link. https://www.tradingcode.net/tradingview/pyramiding-entry-orders/
Upvotes: 1
Reputation: 304
isLong = strategy.position_size > 0
, create another global variable which toggles between true and false in case of condition of buy or sell.
custom_is_long = true
//when any of the strategy triggers a buy signal.Upvotes: 2