Hi,
I am trying to create a code that helps me with placing two orders.
- One sellshort x1% below the opening price
- one buy order X2% above the opening price.
When one of these is opened, the other one should be cancelled.
The order that is executed should have a SET TARGET %PROFIT of X3
and a SET STOP %LOSS X4.
Super grateful for any assistance!
There you go:
ONCE x1 = 0.20 //0.20%
ONCE x2 = 0.20 //0.20%
ONCE x3 = 0.50 //0.50%
ONCE x4 = 0.25 //0.25%
ONCE Distance = 0 //distance require by the broker, if any
IF Not OnMarket THEN
//
LongEntry = open + (open * x2 / 100)
IF close > (LongEntry + Distance*PipSize) THEN
BUY 1 Contract at LongEntry LIMIT
ELSIF close < (LongEntry - Distance*PipSize) THEN
BUY 1 Contract at LongEntry STOP
ELSE
//BUY 1 Contract at MARKET //enter at MARKET when the distance is not enough
ENDIF
//
ShortEntry = open - (open * x1 / 100)
IF close > (ShortEntry + Distance*PipSize) THEN
SELLSHORT 1 Contract at ShortEntry STOP
ELSIF close < (ShortEntry - Distance*PipSize) THEN
SELLSHORT 1 Contract at ShortEntry LIMIT
ELSE
//SELLSHORT 1 Contract at MARKET //enter at MARKET when the distance is not enough
ENDIF
ENDIF
SET TARGET %PROFIT x3
SET STOP %LOSS x4
//graphonprice LongEntry coloured("Green")
//graphonprice ShortEntry coloured("Red")
the pending orders that haven’t been triggered are automatically cancelled when a candle closes.
Be warned that it’s not possible to prevent both of them from being triggered, as PRT has no control over pending orders once they are sent to the broker, until the candle closes. If both pending orders are triggered, the latter one will close the first one triggered, as there cannot be opposite trades open at the same time.
Many thanks Roberto!
Seems to be working fine! When using this “live” (i.e. not back testing) I suppose I would be able to define “max position” as 1 contract which would in practice cancel the other pending order thus preventing both to be triggered. Would that be correct?
I never tried it, but I don’t think so, as max positions is a directive to ProRealTime. But PRT loses control over orders as soon as thay are sent to the broker.
You can have a try…