Stop loss off of high – attempt and resultant weird code?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #88736 quote
    Finning
    Participant
    Veteran

    Hi all,

    I have been trying to make a stop loss to recover a nominal dollar amount – to stop/exit a position if there is a >=$50k drawdown – but only if the position is > $200k profitable – and not have this mechanism activate for dollar profit amounts less than $200k.

    Sounds great already.

    An intra-bar stop would be great, though I have heard that this may not be possible?

    I wrote this code below – to try and do what I wanted above.

    dollars = (close*countofposition*pipsize*pipvalue)-(tradeprice*countofposition*pipsize*pipvalue)
    
    hd = highest[4](dollars) 
    
    if  longonmarket or shortonmarket then
    if  dollars >200000 and ((hd - dollars) < 50000) then
    sell at (tradeprice + (dollars/countofposition/pipsize/pipvalue)) stop
    exitshort at (tradeprice + (dollars/countofposition/pipsize/pipvalue)) stop
    endif
    endif

    Have attached 2 pictures below – “before” and “after” the code above was implemented.

    So it seems that the code is not working intra-bar – but it sells at the START of the bar that I thought it would have sold at somewhere part way through when it would have got down to its drawdown stop target.

    I don’t quite know how it works, but it seems to go back in time/doesn’t work. Be great if it did work…

    Anyways – just looking for a little help to do this dollar-target based stop loss, which only kicks in after a certain profit size, to sell after a certain quantity of drawdown. Intra-bar great, end of bar if nothing else.

    Thanks,

    Finning.

    Before.jpg Before.jpg After.jpg After.jpg
    #88860 quote
    Finning
    Participant
    Veteran
    #88957 quote
    Nicolas
    Keymaster
    Master

    You should GRAPH the value you take to put your pending orders, otherwise you’ll never understand why your orders are not exited correctly (or the way you think they should):

    GRAPH (tradeprice + (dollars/countofposition/pipsize/pipvalue))

    When something go wrong with a code, try first to debug each variable of the program.

    #89011 quote
    Finning
    Participant
    Veteran

    Hi Nicolas,

    yes, you were right. After reading https://www.prorealcode.com/topic/problem-with-stoploss-function/   , the function that you told me to check was a level – and not a distance – which would have caused a problem – even though the function seemed to work.

    A dollar based stop loss is a great way to think about things – however it might be simpler if I just stick to points – and it looks like https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/    does a great job. I’ll probably just use that code it seems to work well.

    I am still perplexed with the funny close outs on my initial code above. I guess I just have to not expect such results in actual trading.

    Just another question – if I did choose to try and work in dollars – and kept the function

    dollars = (close*countofposition*pipsize*pipvalue)-(tradeprice*countofposition*pipsize*pipvalue)

    which graphs to be the same as your function in post #21529

    //floating profit
    floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains

    can I then use a bank of multiple (2+) sequential set stop $trailing codes?

    for example:

    IF  longonmarket and (Dollars >= 8001 and dollars < 9799) THEN
    SET STOP $TRAILING 500
    ENDIF
    
    IF  longonmarket and (Dollars >= 9800 and dollars < 20000) THEN
    SET STOP $TRAILING 5000
    ENDIF
    

    The reason I’m asking is that when I seem to use these multiple stops, I get an error claiming the loss of tick by tick data – seen in the attached picture file – an error that I haven’t seen before.

    The start date of the backtest was the end of 2014 – well after the 2010 introduction of pip data.

    Many thanks,

    Finning.

    Pip-Error.jpg Pip-Error.jpg
    #89209 quote
    Nicolas
    Keymaster
    Master

    This error message is about the fact that there are no ticks data at the date you want your backtest to be made/started. No ticks data means not accurate backtest, especially when using a trailing stop (exit based on prices met intra-bar).

    #89269 quote
    Vonasi
    Moderator
    Master

    I sometimes get this error during tick by tick back tests even though the start date is set correctly. If you keep trying new start dates then eventually you get the back test to run without error. I think there must be some corruption in the tick by tick data that some strategies hit on and others don’t. I’ve tried to work out what date this is but repeatedly back testing with new start dates is a very long process! I seem to recall that last time I had to set a start date in 2012 despite the fact that tick by tick data starts in 2010.

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

Stop loss off of high – attempt and resultant weird code?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Finning @finning Participant
Summary

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

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/16/2019
Status: Active
Attachments: 3 files
Logo Logo
Loading...