Hi,
if i have a popgun i wanna go short if one of the next 5 bars is lower than the last bar of the popgun is, the opposite for long. SL under the last bar of the popgung -5 points, long the opposite.
I this dont happend the order should be cancel.
How can i code this?
PG = High[2]>High[1] and Low[2]<Low[1] and High>High[1] and Low<Low[1]
JSParticipant
Senior
Hi @killerplatuze
Try this one…
DefParam CumulateOrders=False
//PopGun
If High[7]>High[6] and Low[7]<Low[6] and High[5]>High[6] and Low[5]<Low[6] then
If Low[4]<Low[5] or Low[3]<Low[5] or Low[2]<Low[5] or Low[1]<Low[5] or Low<Low[5] then
SellShort 1 contract at Market
Set Stop Price Low[5]+5
EndIf
If High[4]>High[5] or High[3]>High[5] or High[2]>High[5] or High[1]>High[5] or High>High[5] then
Buy 1 contract at Market
Set Stop Price High[5]-5
EndIf
EndIf
This doesn’t work. The first 3 bars are the Popgun, if the conditions are true. The 3th bar is the the triggerbar, the next 5 bars are the bars which can break out.
But it works with OR.
But i have a problem with TP and SL. The SL should be 5 points below the triggerbar if long.
The TP should be one ATR of the triggerbar.
So i need help again with TP and SL, thx a lot.
DefParam CumulateOrders=False
PG = High[2]>High[1] and Low[2]<Low[1] and High>High[1] and Low<Low[1]
TP = AverageTrueRange[1]
//PopGun
If PG then
If High[3] OR High[4] OR High[5] OR High[6] OR High[7] then
Buy 1 contract at Market
Set Stop Price Low[2]-5
Set Target Profit TP
EndIf
Endif
JSParticipant
Senior
You have 3 bars for the PopGun and 5 bars for the breakout, a total of 8 bars…
From right to left B[0] B[1] B[2] B[3] B[4] B[5] B[6] and B[7] (total 8 bars)
“If High[3] or High[4]” and so on is not a valid condition because they are all true…
There is a High[3] (true) or there is a High[4] (true) and so on…
The conditions in the code match your story…
You can put the Stop wherever you want but your trigger bar is B[5] and not B[2]
If you want to use the ATR for your TP, this ATR will have to be calculated over a certain (lookback) period…
Thx, i have seen my mistake.
But if i use your code the result is wrong, as you can see it at the attached picture
The buy for the first PG is on Bar too late, the buy for the second bar ist 2 bars too late.
JSParticipant
Senior
Unfortunately it doesn’t work that way, the order is…
- B[5] is the trigger bar
- B[4] is the breakout bar, here is the condition true and then the next bar…
- Buy when B[3] opens…
This is the order in which PRT processes the market orders…
JSParticipant
Senior
Alternatively, you can use Stop orders instead of market orders…
Buy 1 contract at High[5] STOP
These orders are executed immediately…
This Order must be canceled if no bar gets a trigger, how can in do this?
JSParticipant
Senior
These STOP orders are “pending” orders and they start at the beginning of the new bar and are valid until the end of that bar…
So, when the condition (PopGun) is true then these pending orders are started /placed and when not hit in the next five bars (under a certain conditions) then this “pending STOP order” will not be placed afterwards …
So, you don’t have to cancel anything…
For example:
PopGun is true extra condition is true place the pending stop orders pending stop order is hit extra condition is no longer true no more placement of the pending STOP orders…
JSParticipant
Senior
I think you can reduce the code to this…
DefParam CumulateOrders=False
//PopGun
If High[7]>High[6] and Low[7]<Low[6] and High[5]>High[6] and Low[5]<Low[6] then
Buy 1 contract at High[5] STOP
SellShort 1 contract at Low[5] STOP
Set Stop Price Low[5]+5
Set Stop Price High[5]-5
EndIf
JSParticipant
Senior
I think you’ve found a nice pattern…
Excellent Risk/Reward ratio
Excellent Profit/Loss ratio
%profit trades is bad but that’s because there are a lot of small losses that are offset by relatively large profits…
This can probably be improved…
Something is wrong, the trade starts much too late
JSParticipant
Senior
You must go back to the first code and replace the market orders with STOP orders…
(The last code only looks at the PopGun)
DefParam CumulateOrders=False
//PopGun
If High[7]>High[6] and Low[7]<Low[6] and High[5]>High[6] and Low[5]<Low[6] then
If Low[4]<Low[5] or Low[3]<Low[5] or Low[2]<Low[5] or Low[1]<Low[5] or Low<Low[5] then
sellShort 1 contract at Low[5] STOP
Set Stop Price Low[5]+5
EndIf
If High[4]>High[5] or High[3]>High[5] or High[2]>High[5] or High[1]>High[5] or High>High[5] then
Buy 1 contract at High[5] STOP
Set Stop Price High[5]-5
EndIf
EndIf
This is the code, same result. Trade starts much too late. Its crazy.
JSParticipant
Senior
Hi,
You can try this one…
The problem is that your PopGun in its current form does not indicate a direction (Long or Short)…
DefParam CumulateOrders=False
//PopGun
If High[2]>High[1] and Low[2]<Low[1] and High>High[1] and Low<Low[1] then
Buy 1 contract at High STOP
Set Stop Price High-50
SellShort 1 contract at Low STOP
Set Stop Price Low+50
EndIf