Hi, I have the code which I use to trade the FTSE 1 Hour, but would like to modify the break even code to the following conditions:
If after 1 hour (i.e. during the 1st candle), the break even function is not activated, set stop to b/e on the next candle ASAP.
Currently, the b/e is only activated once the candle has closed (and conditions met) but looking for a more ‘real time’ break even code.
Thanks for the help.
defparam cumulateorders=false
// --- settings
size = 1 //size of orders
maxDayOrder = 1 //max orders per day
startBreakeven = 11 //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 6 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
TakeProfit = 30 //takeprofit in points/pips
StopLoss = 45 //stoploss in points/pips
// --- end of settings
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 090000
timeEnterBefore = time >= noEntryBeforeTime
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryAfterTime = 160000
timeEnterAfter = time < noEntryAfterTime
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
//max orders each day
if intradaybarindex=0 or day<>day[1] then
orderscount=0
endif
allowtrading=orderscount<maxDayOrder and timeenterbefore and timeenterafter and not daysforbiddenentry
//indicators
ema5=average[5,1](close)
ema17=average[17,1](close)
ema62=average[62,1](close)
ignored, ignored, ignored, ignored, BuySignal, SellSignal, ignored = CALL "WA Explosion COMBO"[150, 30, 15, 15]
irsi=rsi[7](close)
//orders launch
buysig = ema5>ema17 and ema62<ema5 and BuySignal>=260 and irsi>=83
sellsig = ema5<ema17 and ema62>ema5 and SellSignal>=260 and irsi<=22
if allowtrading then
if buysig then
buy size contract at market
orderscount=orderscount+1
endif
if sellsig then
sellshort size contract at market
orderscount=orderscount+1
endif
endif
set target pprofit takeprofit
set stop ploss stoploss
// --- breakeven function
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// --- BUY SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
ENDIF
// --- end of BUY SIDE ---
// --- SELL SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
EXITSHORT AT breakevenLevel STOP
ENDIF
// --- end of SELL SIDE ---
Decisions are made at the close of a candle and orders placed at the open of the following candle so if you want to make decisions and place orders mid way during a 1 hour candle candle then you need to use MTF within your strategy and run it on a faster time frame.
Thanks. Is there not a way for the b/e stop to work off the 1min candle through monitoring that time frame as an example.
Yes – use multi time frame and trade on the 1 minute chart. You enter on the hourly time frame and then check on the minute timeframe whether you are enough points in profit and then start your breakeven by placing your stop orders at the close of every minute candle.
The downside to this is that you have very little data to backtest on due to the faster timeframe.
Separate the breakeven function from the strategy code into 2 different timeframes:
defparam cumulateorders=false
// --- settings
size = 1 //size of orders
maxDayOrder = 1 //max orders per day
startBreakeven = 11 //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 6 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
TakeProfit = 30 //takeprofit in points/pips
StopLoss = 45 //stoploss in points/pips
// --- end of settings
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 090000
timeEnterBefore = time >= noEntryBeforeTime
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryAfterTime = 160000
timeEnterAfter = time < noEntryAfterTime
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
timeframe(1 hour, updateonclose)
//max orders each day
if intradaybarindex=0 or day<>day[1] then
orderscount=0
endif
allowtrading=orderscount<maxDayOrder and timeenterbefore and timeenterafter and not daysforbiddenentry
//indicators
ema5=average[5,1](close)
ema17=average[17,1](close)
ema62=average[62,1](close)
ignored, ignored, ignored, ignored, BuySignal, SellSignal, ignored = CALL "WA Explosion COMBO"[150, 30, 15, 15]
irsi=rsi[7](close)
//orders launch
buysig = ema5>ema17 and ema62<ema5 and BuySignal>=260 and irsi>=83
sellsig = ema5<ema17 and ema62>ema5 and SellSignal>=260 and irsi<=22
if allowtrading then
if buysig then
buy size contract at market
orderscount=orderscount+1
endif
if sellsig then
sellshort size contract at market
orderscount=orderscount+1
endif
endif
set target pprofit takeprofit
set stop ploss stoploss
timeframe(1 minute)
// --- breakeven function
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// --- BUY SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
ENDIF
// --- end of BUY SIDE ---
// --- SELL SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
EXITSHORT AT breakevenLevel STOP
ENDIF
// --- end of SELL SIDE ---
The breakeven levels would check for update every 1 minute now, so you have to launch the strategy on the 1-minute timeframe.