Hi everyone, i have a problem, here is my system that i’m working on, is a Breokout of the first 15m bar, in a 5m timeframe default.
The problem is that the price limit is not respected, as you can see in the photo.
[attachment file=”81134″]
Here is the code:
DEFPARAM FLATBEFORE = 080000
DEFPARAM PRELOADBARS = 10000
DEFPARAM FLATAFTER = 210000
DEFPARAM CUMULATEORDERS = FALSE
once maxsetup = 0
once minSetup = 0
PROFITLONG=20
STOPLONG=50
PROFITSHORT=50
STOPSHORT=50
TIMEFRAME (15 MINUTES, UPDATEONCLOSE)
ONCE RES = 0
//ONCE SUPP= 0
if time=091500 then
res=high
supp=low
endif
TIMEFRAME (DEFAULT)
longok=(close[1]crosses over res) AND (close[1]> res)
shortok= (close[1] crosses under supp) and (close[1]<supp)
//BUY ORDERS
if not onmarket and TIME > 93000 AND TIME < 100000 and longok THEN
IF maxsetup = 0 THEN
maxsetup = res
endif
BUY 5 CONTRACT AT maxsetup stop
endif
//SELL ORDERS
if not onmarket and TIME > 93000 AND TIME < 100000 and shortok THEN
if minsetup = 0 then
minsetup = supp
endif
SELLSHORT 5 CONTRACT AT minsetup stop
ENDIF
IF LONGONMARKET THEN
SET TARGET PPROFIT PROFITLONG
SET STOP PLOSS STOPLONG
ELSE
IF SHORTONMARKET THEN
SET TARGET PPROFIT PROFITSHORT
SET STOP PLOSS STOPSHORT
ENDIF
ENDIF
if time>212300 then
exitshort at market
sell at market
endif
TIMEFRAME (DEFAULT)
////TRAILING STOP & BREAKEAVEN
//1////////////////////////////////////////////////////////
once trailinstop= 1 //1 on - 0 off
trailingstart = 35 //trailing will start @trailinstart points profit
trailingstep = 35 //trailing step to move the "stoploss"
//////////////
once breakeaven = 1 //1 on - 0 off
startBreakeven = 20 //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 5 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
///////////////////////////////
///partial close
once PCLOSE=0
// --- Partial Close settings ---
size = 5
mincontracts = 1
trigger = 22
pointback = 3
closepercent = 20
if not onmarket then
AllowPartialClose=0
endif
//////////////////////////////////////////////////////////
///2///////////////////////////////////////////////
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
//3////////////////////////////
//test if the price have moved favourably of "startBreakeven" points already
if breakeaven>0 then
IF onmarket 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
endif
//************************************************************************
//trailing stop function
if trailinstop>0 then
//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
endif
///////////////////////////////////////////////////////////////////////////////////////
//## -- PARTIAL CLOSE PRICE BACK FUNCTION -- ##
//## - BUY ORDERS
IF PCLOSE>0 THEN
if longonmarket then
//trigger for the partial closure function to start
if close-tradeprice>=trigger*pointsize then
AllowPartialClose = 1
endif
if AllowPartialClose then
//compute the maxprice reached
maxprice = max(maxprice,close)
//check to trigger a partial closure or not
if maxprice-close>=pointback*pointsize then
//close partially
sell max(mincontracts,size*(closepercent/100)) contracts at market
//reset the maxprice to the current price
maxprice = close
endif
endif
endif
//## - SELLSHORT ORDERS
if shortonmarket then
//trigger for the partial closure function to start
if tradeprice-close>=trigger*pointsize then
AllowPartialClose = 1
endif
if AllowPartialClose then
//compute the maxprice reached
minprice = min(minprice,close)
//check to trigger a partial closure or not
if close-minprice>=pointback*pointsize then
//close partially
exitshort max(mincontracts,size*(closepercent/100)) contracts at market
//reset the maxprice to the current price
minprice = close
endif
endif
endif
ENDIF
Your strategy works fine as you asked it to do!
As from the attached screenshot you can see that at 9:55 it enters a trade because the conditions were true in the setup bar at 9:50. Condition you asked to be met were that the previous candle (at 09:45) had crossed over RES, which it did!
Lines 19-20 can be written
longok=(close[1]crosses over res)// AND (close[1]> res)
shortok= (close[1] crosses under supp)// and (close[1]<supp)
since for a crossover/under to be true CLOSE must be above/under RES or SUPP.
The problem is, I guess, that you are referencing the previous candle ([1]) instead of the current one.
Your strategy works fine as you asked it to do!
As from the attached screenshot you can see that at 9:55 it enters a trade because the conditions were true in the setup bar at 9:50. Condition you asked to be met were that the previous candle (at 09:45) had crossed over RES, which it did!
Lines 19-20 can be written
|
|
longok=(close[1]crosses over res)// AND (close[1]> res)
shortok= (close[1] crosses under supp)// and (close[1]<supp)
|
since for a crossover/under to be true CLOSE must be above/under RES or SUPP.
The problem is, I guess, that you are referencing the previous candle ([1]) instead of the current one.
yes, it work fine, but i want that when the cross happens the buy/sell order must be at the limit price of the 15m bar of 9.15 that i supposed will be
BUY 5 CONTRACT AT maxsetup stop
but, price of traded are executed to the max of tha last bar.
the line 19 is ok, because the condition are 2, first the bar (1) have to cross, second the last bar have to close over or under the level, then the system have to put a limit order only at the PRICE of 9.15 (max and minimum)
Also in your test there is the difference i am talkin for. The price of “res” is different from the price of execution.
Sorry if I’m wrong, but since maxsetup=res and you are already above it because “longok” is true, why are you putting a pending STOP order? Buy at a level below the current price is made with a LIMIT order. Same goes for short orders.
Sorry if I’m wrong, but since maxsetup=res and you are already above it because “longok” is true, why are you putting a pending STOP order? Buy at a level below the current price is made with a LIMIT order. Same goes for short orders.
i tried also with LIMIT but price is not respected sometimes
Here the img of today[attachment file=”81201″]
Sorry if I’m wrong, but since maxsetup=res and you are already above it because “longok” is true, why are you putting a pending STOP order? Buy at a level below the current price is made with a LIMIT order. Same goes for short orders.
Hello, i’ve solved the issue, there was something wrong with the variable maxsetup/minsetup
IF NOT ONMARKET THEN
breakevenLevel=0
maxsetup=0
minsetup=0
ENDIF
//BUY ORDERS
if not onmarket and TIME > 93000 AND TIME < 100000 and longok THEN
IF maxsetup = 0 THEN
maxsetup = res
endif
BUY 5 CONTRACT AT maxsetup limit
endif
//SELL ORDERS
if not onmarket and TIME > 93000 AND TIME < 100000 and shortok THEN
if minsetup = 0 then
minsetup = supp
endif
SELLSHORT 5 CONTRACT AT minsetup limit
ENDIF
But there is still some problems with some orders, like in this. The 31/07/2018 at 9.40 am, the sistem should put an order long at 12.816,80 but that price was never touched in that bar and te sistem bid at 12.807
[attachment file=”81205″]
Any idea?
After line 21 insert these lines:
if OnMarket THEN
MinSetup=0
MaxSetup=0
ENDIF
otherwise they’ll never be reset and lines 25 and 33 will never be true again!
Also, despite not affecting the strategy itself, this code is logically WRONG (lines 19-20) as I said before:
longok=(close[1]crosses over res) AND (close[1]> res)
shortok= (close[1] crosses under supp) and (close[1]<supp)
GRAPH is your best friend when debugging.