DEFPARAM CumulateOrders = False
// Timeframe : H1
tradingtime=(time >=090000 and time <=090000)
if time=090000 then
value2 = high
value1 = low
EntryPrice = (high) + (10* pipsize)
EntryPrice1 = (low) - (10 * pipsize)
StopLoss = range + (20 * pipsize) //10 pips below LOW
StopLoss1 = range + (20 * pipsize) //10 pips above HIGH
//takeprofit = range + (20 * pipsize) // TP LONG/SHORT
ENDIF
// Conditions to enter long positions
IF NOT LongOnMarket AND tradingtime and value2 THEN
BUY 0.5 CONTRACTS AT EntryPrice STOP
ENDIF
IF OnMarket THEN
SET STOP LOSS StopLoss
ENDIF
// Conditions to enter Short positions
IF NOT ShortOnMarket AND tradingtime and value1 THEN
SEllSHORT 0.5 CONTRACTS AT EntryPrice1 STOP
ENDIF
IF OnMarket THEN
SET STOP LOSS StopLoss1
ENDIF
// Stops and targets : Enter your protection stops and profit targets here
// points based STOP LOSS and TRAILING STOP
// initial STOP LOSS
//set target of positions at 40 points
SET TARGET PROFIT 200 //(''takeprofit'' must be here if you want to achive 1:1 and remove below coding if not breakeven)
//************************************************************************
//trailing stop function
trailingstart = 15 //trailing will start @trailinstart points profit
trailingstep = 5 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
The lines:
IF NOT LongOnMarket AND tradingtime and value2 THEN
IF NOT ShortOnMarket AND tradingtime and value1 THEN
use VALUE1 and VALUE2 as boolean variables, but they are NOT boolean variables as they are prices (high and low), so they will ALWAYS be true.
Can you explain what’s their purpose?
Moreover, the lines:
IF OnMarket THEN
SET STOP LOSS StopLoss
ENDIF
.
.
.
IF OnMarket THEN
SET STOP LOSS StopLoss1
ENDIF
will always be BOTH executed. Since they are read and executed sequentially, the last one written in the code will override all prior SET STOP LOSS instruction; in this case only StopLoss1 will always be the real stop loss.
You should make them different by executing one of them for LONG trades and the other one for SHORT trades, this way:
IF LongOnMarket THEN
SET STOP LOSS StopLoss
SET TARGET PROFIT StopLoss * 3 //1:3 ratio
ENDIF
.
.
.
IF ShortOnMarket THEN
SET STOP LOSS StopLoss1
SET TARGET PROFIT StopLoss1 * 3 //1:3 ratio
ENDIF
I also added the SET STOP PROFIT to 1:3 ratio, which you can change as best suits you.
DEFPARAM CumulateOrders = False
// Timeframe : H1
tradingtime=(time >= 090000 and time <=090000)
if time=090000 then
value2 = high
value1 = low
EntryPrice = (high) + (10* pipsize)
EntryPrice1 = (low) - (10 * pipsize)
StopLoss = range + (20 * pipsize) //10 pips below LOW
StopLoss1 = range + (20 * pipsize) //10 pips above HIGH
ENDIF
// Conditions to enter long positions
IF NOT LongOnMarket AND tradingtime THEN
BUY 1 CONTRACTS AT EntryPrice STOP
ENDIF
IF LongOnMarket THEN
SET STOP LOSS StopLoss
SET TARGET PROFIT StopLoss * 3 //1:3 ratio
ENDIF
// Conditions to enter Short positions
IF NOT ShortOnMarket AND tradingtime THEN
SEllSHORT 1 CONTRACTS AT EntryPrice1 STOP
ENDIF
IF ShortOnMarket THEN
SET STOP LOSS StopLoss1
SET TARGET PROFIT StopLoss1 * 3 //1:3 ratio
ENDIF
//************************************************************************
//trailing stop function
trailingstart = 15 //trailing will start @trailinstart points profit
trailingstep = 5 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
Hi Roberto,
Thank you very much as always. 🙂
I think I have removed everything and made it a bit clearer. Removed VALUE1 and VALUE2
Can I ask, If one of the orders got triggers long or short, will this not cancel the other order? Let’s say if the long order got triggered, short order should be cancelled and the long order SL should be the same within that tradingtime right?
if not both orders will get trigger. if the long order get triggered aggressively and hit the SL then short order will get triggered at the same time. is there a way to stop this?
Line 3 could simply read:
tradingtime=090000
To make sure pending orders not triggered are not placed again (they are automatically cancelled each bar, so you simply have to stop placing them when already at market), so replace both IF NOT LongOnMarket AND tradingtime THEN and IF NOT ShortOnMarket AND tradingtime THEN by:
IF NOT OnMarket AND tradingtime THEN
As to “if not both orders will get trigger. if the long order get triggered aggressively and hit the SL then short order will get triggered at the same time. is there a way to stop this?” the answer is NO, you can’t. If a pending order is triggered and hits the SL, the other order may also be triggered on the same bar, as all pending orders that haven’t been triggered are only cancelled when a bar closes.
Thank you Roberto for your kind help.
kinda makes sense now. I will still keep the time range, as I like to work with different time ranges. I really appreciate your help and sharing of knowledge!
DEFPARAM CumulateOrders = False
// Timeframe : H1
tradingtime=(time >= 090000 and time <=090000)
if time=090000 then
value2 = high
value1 = low
EntryPrice = (high) + (10* pipsize)
EntryPrice1 = (low) - (10 * pipsize)
StopLoss = range + (20 * pipsize) //10 pips below LOW
StopLoss1 = range + (20 * pipsize) //10 pips above HIGH
ENDIF
// Conditions to enter long positions
IF NOT OnMarket AND tradingtime THEN
BUY 1 CONTRACTS AT EntryPrice STOP
ENDIF
IF LongOnMarket THEN
SET STOP LOSS StopLoss
SET TARGET PROFIT StopLoss * 3 //1:3 ratio
ENDIF
// Conditions to enter Short positions
IF NOT OnMarket AND tradingtime THEN
SEllSHORT 1 CONTRACTS AT EntryPrice1 STOP
ENDIF
IF ShortOnMarket THEN
SET STOP LOSS StopLoss1
SET TARGET PROFIT StopLoss1 * 3 //1:3 ratio
ENDIF
//************************************************************************
//trailing stop function
trailingstart = 15 //trailing will start @trailinstart points profit
trailingstep = 5 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************