Hello everyone!
iv recently started to learn how to use simpel code and trying to creat some algos based on what i use when im daytrading.
this is one of my scripts (very very simpel) that iv tried on dax 2 min backtesting 200k bars
can i backtest it longer somehow? tryed not to curvefit it but it seems that it cant be helped to curvefit it abit.
rn the stoploss and take profit are the same 1:1 but copied the rolling stopploss from this forum, can i make it better somehow? i wanna minimize the drawdowns even if it draw down the gains (not to much i hope hehe)
here is the code:
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions to enter long positions
indicator1 = Momentum[1000](close)
c1 = (indicator1 CROSSES OVER 270)
IF c1 AND not daysForbiddenEntry THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
indicator2 = Momentum[1000](close)
c2 = (indicator2 CROSSES UNDER 0)
IF c2 THEN
SELL AT MARKET
ENDIF
// Stops and targets
SET STOP pLOSS 100
SET TARGET pPROFIT 100
//%trailing stop function
trailingPercent = .35
stepPercent = .01
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit
trailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoploss
endif
//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 THEN
newSL = tradeprice(1)+trailingstep
ENDIF
//next moves
IF newSL>0 AND close-newSL>trailingstep THEN
newSL = newSL+trailingstep
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep
ENDIF
//next moves
IF newSL>0 AND newSL-close>trailingstep THEN
newSL = newSL-trailingstep
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
Looks good on DJI 1 min at spread = 5 … see attached.
Try and get the Short side working?
thx allot grahal! ye i cant have it the same atleast and im to afraid to curvefit it cus its all i read about hehe, any ideas how i would go about it without fixing it for the curv?
Below doesn’t look right?
Substitute DJI Price in and check it out.
trailingPercent = .35
stepPercent = .01
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit
trailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoploss
endif
Below doesn’t look right?
I checked … it does look right after all! 🙂
im to afraid to curvefit it cus its all i read about hehe,
I agree! 🙂
I try and keep the curve fitting in step with the current curve / market action!
I’m going to see how attached works out on Demo Forward Test next week.
thx allot m8! also woundering how to go about to test stuff on 1m bars? i have some 1min systems that cant realy be backtested correctly
i wanna minimize the drawdowns even if it draw down the gains (not to much i hope hehe)
Well, one way to get lower the draw-down would be to check if the momentum is coming from below and not just going down and below your 270-level and up again. For example 5 periods
c1b = (indicator1[5] < 270)
Of course, this is not a guarantee to avoid the mentioned idea above, and it will make fewer trades and less gain. And also – another variable could mean bigger chance for curve fit. But in the backtest I did (did not optimize the variable, just tested with the 5-period), it cut the drawdown in half.
Mattias – Please be more careful when using the ‘Quote’ facility. You included all your own text within the quote which made your post very difficult to understand. I have edited your post to tidy it up. You have five minutes after pressing submit to delete or edit your posts if they do not look correct. Please double check your future posts and correct any errors before leaving the page.
thx for all your insights! trying them all out. rn i run 10 versions of the system.
i have some questen for one of my other system, rn im building a HF algo (over 300 trades per day) i wanna limit it to the swedish open of omx 09-1730 gmt+1
is this code right for it? cus it doesent seem to work right for me
DEFPARAM FLATBEFORE = 090000
// Cancel all pending orders and close all positions at the "FLATAFTER" time
DEFPARAM FLATAFTER = 173000
// 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 = 173000
timeEnterAfter = time < noEntryAfterTime
Sounds like all you need is FlatBefore and FlatAfter and not the rest of the code you show above?
https://www.prorealcode.com/documentation/flatbefore/
Personally I prefer to use something like
CTime = Time >= 090000 and Time < 173000
(with CTime as an entry condition, obviously)
This way new trades can only be opened during market hours, but can stay open after the close.
This also allows you to optimize the Time variables, which you can’t do when using DEFPARAM FLATBEFORE / FLATAFTER
thx grahal and nonetheless!
nonetheless code was what i was after and it worked great. the only problem i have left now is that the code is very sensetive so sometimes the broker dont allow some of the orders so it turns of the bot, i dont wanna change the code so is there any adds that prevent that from happening
What kind of rejection are you getting?
(go Trading > Order List > Cancelled/Rejected then click in the Status column)
Your trading system was stopped because it tried to place a stop order below the minium distance required by your broker.