"EXITSHORT" command not closing a short position

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #19266 quote
    Munkanu
    Participant
    New

    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
    #19300 quote
    GraHal
    Participant
    Master

    Hi Munkanu

    Try below and let us know how you get on.

    IF SHORTONMARKET AND c4 THEN
    EXITSHORT AT MARKET
    QUIT
    ENDIF
    
    

    Cheers
    GraHal

    #19303 quote
    Munkanu
    Participant
    New

    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

    #19304 quote
    JC_Bywan
    Moderator
    Master

    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…

    #19307 quote
    JC_Bywan
    Moderator
    Master

    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
    GraHal thanked this post
    #19314 quote
    Munkanu
    Participant
    New

    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

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

"EXITSHORT" command not closing a short position


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Munkanu @munkanu Participant
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by Munkanu
9 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 12/28/2016
Status: Active
Attachments: No files
Logo Logo
Loading...