Hi there,
I am running the code below, which is meant to exit a short position if it closes above the 50EMA, and then quit the code.
The command to exit the short and then quit is at the bottom of the code below. Strangely, it successfully quits the code, meaning it must be running the code within this IF statement, but it does not exit the short position. Instead it quits the code and leaves the position open. Can you help me figure out why?
Many thanks
Chris
//-------------------------------------------------------------------------
// Main code : MegaTrend1
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------------
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
//Define the indicators
i1 = close
i2 = ExponentialAverage[20](close)
i3 = ExponentialAverage[50](close)
//define the conditions for actions
c1 = (i1 > i2) //close above the 20MA
c2 = (i1 < i2) //close below the 20MA
c3 = (I1 < i3) //price closes under the 50MA
c4 = (i1 > i3) //price closes above the 50MA
Trend = CALL "UpTrend/DownTrend" //call the up/down trend indicator
UpTrend = (Trend = 1) //uptrend in action
DownTrend = (Trend = -1) //downtrend in action
//Money Management
Capital = 35000 //Starting pot of £35k
Risk = 0.01 //1% risk on each trade
IF UpTrend then
StopLoss = close-i3 // Initial stop loss at 50EMA
ENDIF
IF DownTrend then
StopLoss = i3-close
ENDIF
//Calculate position size
equity = Capital + StrategyProfit //Initial capital + current profits
maxrisk = round(equity*Risk) //Equity times 1% rounded to whole number
PositionSize = abs(round((maxrisk/StopLoss)/PointValue)*pipsize)
// Conditions to enter positions
IF NOT ONMARKET THEN
IF c1 THEN
IF UpTrend THEN
BUY PositionSize PERPOINT AT MARKET
SET STOP pLOSS StopLoss
ENDIF
ENDIF
IF c2 THEN
IF DownTrend THEN
SELLSHORT PositionSize PERPOINT AT MARKET
SET STOP pLOSS StopLoss
ENDIF
ENDIF
ENDIF
// Conditions to exit positions
IF LONGONMARKET THEN
IF c3 THEN
SELL AT MARKET
QUIT
ENDIF
ENDIF
IF SHORTONMARKET THEN
IF c4 THEN
EXITSHORT AT MARKET
QUIT
ENDIF
ENDIF
Hi Munkanu
Try below and let us know how you get on.
IF SHORTONMARKET AND c4 THEN
EXITSHORT AT MARKET
QUIT
ENDIF
Cheers
GraHal
Hi GraHal,
Many thanks for your reply. I have tried this and still get the same result – the code quits (meaning the IF statement seems to be working) but the short position is left open (meaning the EXITSHORT command) does not seem to be working. Very odd.
Not sure what to try next as the code looks fine, can anyone help? Am I missing an argument on the EXITSHORT command?
Many thanks
Chris
I’ve never used “quit” pretty much at the same time as sending an order, so I’m only wondering here, rather than talking from experience: could it be that “quit” is instantaneous in such a way that the “exitshort at market” order is cancelled at the same time as exiting the strategy before it has any chance of being executed? Might be worth trying to delay your “quit” instruction by at least one candle after sending the exitshort order, just to see if it gives more time for the exitshort order to happen…
To delay by one candle, many ways to do it, but for example something like this should allow for the experiment:
adding at the beginning of the code :
quitting=0
and modifying the end with:
IF SHORTONMARKET THEN
IF c4 THEN
EXITSHORT AT MARKET
quitting = 1
ENDIF
ENDIF
if quitting[1]=1 then
quit
endif
Thank you Noobywan – good point about the quit command. I’ll just remove it for now and see if it makes a difference, then will try your code suggestion. I’ll update tomorrow.
Best regards