Hello!
I have a trading idea that I need help with.
I can’t get the partial close function to work with my compound effect, does anyone know what I’m doing wrong?
When i remove line 3 to 5 the partial close function works as it should see attached image.
thankful for any help 🙂
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
//amount of contract/lot/shares to open for each order
Amount=10
takeprofit = PATR*AverageTrueRange[NATR](close) //takeprofit in points
stoploss = SATR*AverageTrueRange[NATR](close) //stoploss in points
BreakevenAt = PATR*AverageTrueRange[NATR](close)/3//percent achieved of target to move stop to entry (breakeven)
PointsToKeep = 3 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
Lot2Close = Amount/2 //amount of contract/lot/shares quantity to close when breakeven occurs
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 093000
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
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0 OR OpenDayOfWeek = 1
if currentmonth = 4 then
longmonthcondition = 0
else
longmonthcondition = 1
endif
if currentmonth = 1 then
shortmonthcondition = 0
else
shortmonthcondition = 1
endif
// Conditions to enter long positions
indicator1 = Average[40,8](close) // 10,6
indicator2 = Average[40,3](close) // 50,7
c1 = (indicator1 crosses over indicator2)
indicator3 = Average[200,1](close)
if close > indicator3 then
tradeonlong = 1
else
tradeonlong = 0
endif
IF NOT LongOnMarket AND c1 and tradeonlong and timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry and longmonthcondition THEN
BUY amount CONTRACT AT MARKET
ENDIF
// Conditions to enter Short positions
indicator4 = Average[42,0](close) // 90,1
indicator5 = Average[29,0](close) // 50,4
c2 = (indicator4 crosses over indicator5)
if close < indicator3 then
tradeonshort = 1
else
tradeonshort = 0
endif
IF NOT ShortOnMarket AND c2 and tradeonshort and timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry and shortmonthcondition THEN
SELLSHORT amount CONTRACTS AT MARKET
ENDIF
// Stops and targets
NATR = 14 //ATR Period
SATR = 2 // ATR Multiplier for Stop
PATR = 3// ATR Multiplier for Profit
//Stop and Target
SET STOP LOSS SATR*AverageTrueRange[NATR](close)
SET TARGET PROFIT PATR*AverageTrueRange[NATR](close)
startBreakeven = takeprofit*(BreakevenAt/100)//how much pips/points in gain to activate the breakeven function?
// --- 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 LONGONMARKET AND breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
if countoflongshares=amount then
sell Lot2Close contract at market
endif
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 SHORTONMARKET AND breakevenLevel>0 THEN
EXITSHORT AT breakevenLevel STOP
if countofshortshares=amount then
exitshort Lot2Close contract at market
endif
ENDIF
// --- end of SELL SIDE ---
line 1-9 should be as below
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
Capital = 2000
Equity = Capital + StrategyProfit
Position = (0.03*equity)/stoploss
//amount of contract/lot/shares to open for each order
Amount=position
Without analysing your code really, it looks like you need to change line 3 to
Once Capital = 2000
Now you assign your capital once, and in the next call of your code (at the next bar(s)) the Capital will increase (or decrease 😉 ).
Regards,
Peter
thank you Peter ill try this 🙂
didn’t help :/
line 12 seems to not work
Lot2Close = Amount/2 not sure why
I added the ROUND() instruction to make sure there’s only 1 decimal place.
I also added MinLots to set a minimum number of lots to be traded (but I suggest that it be at least 2, or that the minimun lots to exit is allowed to be half that number, i.e. if you set a max 1 lotsize, then the broker must allow exiting 0.5 lots) and MinExit to exit partial poisitions.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
once Capital = 2000
once MinLots = 2 //minimum lotsize
once MinExit = 1 //minimum number of lots to exit
Equity = Capital + StrategyProfit
Position = max(MinLots,round((0.03*equity)/stoploss,1))
//amount of contract/lot/shares to open for each order
Amount=position
takeprofit = PATR*AverageTrueRange[NATR](close) //takeprofit in points
stoploss = SATR*AverageTrueRange[NATR](close) //stoploss in points
BreakevenAt = PATR*AverageTrueRange[NATR](close)/3//percent achieved of target to move stop to entry (breakeven)
PointsToKeep = 3 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
Lot2Close = max(MinExit,round(Amount/2,1)) //amount of contract/lot/shares quantity to close when breakeven occurs
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 093000
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
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0 OR OpenDayOfWeek = 1
if currentmonth = 4 then
longmonthcondition = 0
else
longmonthcondition = 1
endif
if currentmonth = 1 then
shortmonthcondition = 0
else
shortmonthcondition = 1
endif
// Conditions to enter long positions
indicator1 = Average[40,8](close) // 10,6
indicator2 = Average[40,3](close) // 50,7
c1 = (indicator1 crosses over indicator2)
indicator3 = Average[200,1](close)
if close > indicator3 then
tradeonlong = 1
else
tradeonlong = 0
endif
IF NOT LongOnMarket AND c1 and tradeonlong and timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry and longmonthcondition THEN
BUY amount CONTRACT AT MARKET
ENDIF
// Conditions to enter Short positions
indicator4 = Average[42,0](close) // 90,1
indicator5 = Average[29,0](close) // 50,4
c2 = (indicator4 crosses over indicator5)
if close < indicator3 then
tradeonshort = 1
else
tradeonshort = 0
endif
IF NOT ShortOnMarket AND c2 and tradeonshort and timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry and shortmonthcondition THEN
SELLSHORT amount CONTRACTS AT MARKET
ENDIF
// Stops and targets
NATR = 14 //ATR Period
SATR = 2 // ATR Multiplier for Stop
PATR = 3// ATR Multiplier for Profit
//Stop and Target
SET STOP LOSS SATR*AverageTrueRange[NATR](close)
SET TARGET PROFIT PATR*AverageTrueRange[NATR](close)
startBreakeven = takeprofit*(BreakevenAt/100)//how much pips/points in gain to activate the breakeven function?
// --- 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 LONGONMARKET AND breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
if countoflongshares=amount then
sell Lot2Close contract at market
endif
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 SHORTONMARKET AND breakevenLevel>0 THEN
EXITSHORT AT breakevenLevel STOP
if countofshortshares=amount then
exitshort Lot2Close contract at market
endif
ENDIF
// --- end of SELL SIDE ---
graph Amount
graph Lot2Close