Stop Loss Problem

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #141038 quote
    mizatt
    Participant
    Junior

    Good morning,

    I have recently put Nicolas’s clever stop loss code :

    SET STOP LOSS abs(close – (lowest[3](low) * pipsize))

    into my code and it works beautifully.

    My problem is that when in a trade, I want to limit the maximum $ loss of any single trade to $100 and I don’t immediately see how to do this whilst retaining the recent swing high/low stop loss code above.

    I checked the forum and found reference to the following post:

    https://www.prorealcode.com/blog/learning/max-profit-loss-day-trading-strategy/

    BUT I can’t get it to work with the swing high/low code.  It’s like the system just ignores my ‘max loss’ code resulting in many losses far in excess of $100 on the backtest.

    See below for what I currently have (without my buy/sell conditions which just add clutter).  Any help much appreciated.

    Regards

    Mark

    
    MaxLoss=100 //Max loss allowed(in $)
     
    // first time we launch the code, the trading is allowed
    once TradeAllowed=1
     
    // reset the current state of the strateygprofit each new day
    If intradaybarindex=0 then
    MyProfit=STRATEGYPROFIT
    TradeAllowed=1
    endif
     
    // test if the strategyprofit of the day is currently above the daily profit allowed of below the daily loss allowed
    If StrategyProfit<=MyProfit-MaxLoss then
    TradeAllowed=0
    endif
    
    // if max $loss of 100 on long/short trade then following code should be executed
    IF LongOnMarket AND TradeAllowed=0 then 
    Sell PositionSize contracts at market 
    endif
    
    If ShortOnMarket AND TradeAllowed=0 then 
    Exitshort PositionSize contracts at market 
    endif
    
    // BUY order code
    IF NOT OnMarket AND buyCondition and TradeAllowed THEN
    BUY PositionSize CONTRACTS AT MARKET
    SET STOP LOSS abs(close - (lowest[3](low) - 1 * pipsize))
    set target $profit 150
    ENDIF
    
    // SELL order code
    IF NOT OnMarket AND sellCondition and TradeAllowed THEN
    SELLSHORT PositionSize CONTRACTS AT MARKET
    SET STOP LOSS abs(close - (highest[3](high) + 1 * pipsize))
    set target $profit 150
    ENDIF
    
    
    #141046 quote
    robertogozzi
    Moderator
    Master

    Actually your line SET STOP LOSS abs(close – (lowest[3](low) * pipsize)) is wrong, LOSS does not require pips, but a difference between prices.

    You could use PLOSS, instead, but in this case it should be “/ pipsize” and the closing parentheses are not in the correct place. It should be one of the following:

    SET STOP  LOSS abs(close – lowest[3](low))
    SET STOP pLOSS abs(close – lowest[3](low)) / pipsize

    But in your case you can easily use:

    SET STOP $LOSS 100
    Nicolas thanked this post
    #141072 quote
    Nicolas
    Keymaster
    Master

    @mizatt

    Where did you find the code snippet you talked about? Would be good to change it as it is not correct 🙂 If it is my fault, please accept my apologize! 😉

    #141110 quote
    mizatt
    Participant
    Junior

    Hi Roberto,

    Thanks for clarifying that the ‘pipsize’ term can simply be removed.

    I know how to use the $LOSS  function on its own but do you have any idea how to COMBINE the

    abs(closelowest[3](low))    AND   $LOSS (max $100)

    Depending on the entry candle of my trade, if the trade moves against me then the loss can be anywhere from $50-$150 (typically).

    I would like the abs(closelowest[3](low)) code to be used as my primary stop loss wherever possible EXCEPT when the loss becomes a value >$100 which is when my secondary $LOSS code executes to get me out.

    Is this easily possible to do ?

    Regards

    Mark

    #141111 quote
    mizatt
    Participant
    Junior

    @Nicolas

    It looks like I found it here:

    https://www.prorealcode.com/topic/stop-loss-at-last-swing-low/

    but it looks like (with hindsight) it was actually a post that Roberto was moderating.

    Apologies for the mix up.

    Regards

    Mark

    #141122 quote
    robertogozzi
    Moderator
    Master

    It was NOT an error, you simply copied it wrong!

    #141130 quote
    mizatt
    Participant
    Junior

    It was not copied wrong Roberto, did you check the link with your post?

    I think you’re missing the point… your code works perfectly for finding the swing low/high!  (either with the word ‘pipsize’ included or without it).

    My remaining issue however is trying to find a solution for combining the swing high/low code and a max $loss as I explained above.  Can anyone help with this issue?

    #141132 quote
    mizatt
    Participant
    Junior

    @Nicolas

    As my coding issue remains unaddressed, can I pay you via Paypal for your expertise in getting this sorted quickly?

    If so, please send me an invoice or contact me at XXXXXXXXXX

    Many thanks

    Mark

    #141133 quote
    robertogozzi
    Moderator
    Master

    One of the rules you should have read clearly states:

    • Do not include personal informationsuch as email addresses or telephone numbers in your posts.

    Abide by it, please.

    I removed your email address.

    #141134 quote
    robertogozzi
    Moderator
    Master

    As for your code the line is correct, but your text is wrong, that’s what I remarked.

    You can calculate the value by using both PIPSIZE and PIPVALUE (not tested):

    Sl = abs(close – lowest[3](low)) / PipSize * PipValue
    Set Stop $loss min(100,Sl)
    mizatt thanked this post
    #141135 quote
    mizatt
    Participant
    Junior

    Ok, many thanks for helping with this code.  Now I think I see it was just a misunderstanding.

    I’ll give it a try and fingers crossed.

    Much appreciated Roberto.

    #141136 quote
    robertogozzi
    Moderator
    Master

    As a moderator I am supposed not to help, this can be done by anyone on this forum (myself included), but to remark what’s not correct in a post pointing out what the correct form is.

    You, as a member of this community, are supposed to abide by the rules.

    Thank you 🙂

    #141137 quote
    mizatt
    Participant
    Junior

    It works Roberto!  Great work!

    Thanks again and sorry about displaying personal email address.  Will remember for future.

    Mark

    #141138 quote
    mizatt
    Participant
    Junior

    I’m sorry if my requests have created a bit of tension between us.  Obviously my goal is simply to get my code strategy sorted and if I can call upon either you or Nicolas’s expertise then it is of great help.

    I am happy to pay for your services as I realize it takes a lot of effort and skill to acquire this knowledge which you give back freely to the community.  That is greatly appreciated and I’m sure I speak for all newbies when I say that.

    Regards

    Mark

    robertogozzi and Nicolas thanked this post
    #141139 quote
    mizatt
    Participant
    Junior

    Hi Roberto,

    I think I’m nearly there with the code BUT came across a perplexing problem a bit later.

    When I apply the stop loss code on the DJI index (Wall St) in IG, it works perfectly as per your suggestion when applied to a PositionSize of £1/point.

    When I increase PositionSize to 2 (or greater), the system no longer exits trades in the correct place.

     

    P1 in pic below is the correct exit (the last swing high)

    P2 in pic below is where the exit occurs when PositionSize is increased from ‘1’ to ‘2’

    Note that when I increased the PositionSize to ‘2’, I also increased the stop loss and profit target e.g.

    Set Stop $loss min(200,S1)    //as opposed to Set Stop $loss min(100,S1)
    Set target $profit 260     //as opposed to Set target $profit 130

    but it still exits incorrectly.  Any idea what is going wrong when the PositionSize is increased?

    Regards

    Mark

    PositionSize = 1 
    
    // BUY order code 
    IF NOT OnMarket AND buyCondition THEN 
    BUY PositionSize CONTRACTS AT MARKET 
    Sl = abs(close – lowest[3](low)-1) / PipSize * PipValue 
    Set Stop $loss min(100,Sl) 
    set target $profit 130 
    ENDIF 
    
    // SELL order code 
    IF NOT OnMarket AND sellCondition THEN 
    SELLSHORT PositionSize CONTRACTS AT MARKET 
    Sl = abs(close - highest[3](high) + 1)/ PipSize * PipValue 
    Set Stop $loss min(100,Sl) 
    set target $profit 130
    ENDIF
    P1.jpg P1.jpg P2.jpg P2.jpg
Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.

Stop Loss Problem


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
mizatt @mizatt Participant
Summary

This topic contains 17 replies,
has 3 voices, and was last updated by Nicolas
5 years, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/07/2020
Status: Active
Attachments: 2 files
Logo Logo
Loading...