Hi guys,
I’m trying to define the exit parameters of my strategy to be the following:
Exit on the next open if we’ve achieved a close 0.3% higher than the trade bars’ close.
I’m having trouble with this because a simple take profit won’t do. I’m trying to exit on the next bar once we’ve achieved that 0.3% profit target. Here’s my autogenerated code:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = MoneyFlowIndex[4]
c1 = (indicator1 = 0)
IF c1 THEN
BUY 1 SHARES AT MARKET
ENDIF
// Conditions to exit long positions
percent = close / close[-1] - 1
c2 = (percent >= 0.3)
IF c2 THEN
SELL AT MARKET TOMORROWOPEN
ENDIF
I’ve trying looking in the documentation, but I’ve only been able to define the percent parameter, not the “exit on the next bar once we’ve achieved 0.3% profit” part.
Let me know what you think,
Aleks
fixed my syntax, apologies for the spam.
c2 = (percent >= 0.003)
AleksLi – Welcome to the forums.
Please always use the ‘Insert PRT Code’ button when posting code to make it more readable for others. I have tidied up your posts 🙂
Glad you fixed your own problem however there is an issue in your code.
You cannot use [-1 ] after CLOSE as it must be a positive integer value. CLOSE[1] is the previous bars close. If you want to compare current price to the price your trade opened at then you need to use TRADEPRICE or POSITIONPRICE. I usually use the latter to avoid any confusion as TRADEPRICE can also be the last price you sold at.
Also it is simpler just to do the exit price calculation as part of the condition.
// Conditions to exit long positions
c2 = close >= positionprice * 1.003
IF c2 THEN
SELL AT MARKET TOMORROWOPEN
ENDIF
Thank you very much for your reply Vonasi!
One issue I have though- for some reason after inserting the positionprice variable, the backtest doesn’t generate any positions? I’ve tried removing selling rules altogether and it generated a buy signal.
For reference, here’s my latest code:
// Definition of code parameters
DEFPARAM CumulateOrders = false // Cumulating positions deactivated
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 143000
timeEnterBefore = time >= noEntryBeforeTime
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryAfterTime = 210000
timeEnterAfter = time < noEntryAfterTime
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions to enter long positions
indicator1 = MoneyFlowIndex[4]
c1 = (indicator1 = 0)
IF c1 THEN
BUY 1 SHARES AT MARKET
ENDIF
// Conditions to exit long positions
c2 = close >= positionprice * 1.003
IF c2 THEN
SELL AT MARKET TOMORROWOPEN
ENDIF
I’ve tried fiddling around with brackets, and that hasn’t helped. I’ve also used other rules to exit positions (like my first attempt) and that generated backtest results.
Sorry – I was coding without thinking too hard about it!
The problem is that your c2 condition is true when you are not on the market because POSITIONPRICE = 0 and so this cancels out any buy orders. The addition of an ONMARKET condition should solve the problem.
Try this (not tested):
defparam cumulateorders = false
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 143000
timeEnterBefore = time >= noEntryBeforeTime
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryAfterTime = 210000
timeEnterAfter = time < noEntryAfterTime
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions to enter long positions
indicator1 = MoneyFlowIndex[4]
c1 = (indicator1 = 0)
IF c1 THEN
BUY 1 SHARES AT MARKET
ENDIF
// Conditions to exit long positions
c2 = close >= positionprice * 1.003
IF onmarket and c2 THEN
SELL AT MARKET TOMORROWOPEN
ENDIF
It worked! Thank you so much for taking the time to look into this. Much appreciated 🙂