Increase position based on equity drawdown

Viewing 15 posts - 1 through 15 (of 22 total)
  • Author
    Posts
  • #121300 quote
    swedshare
    Participant
    Senior

    Hi,

    This is probably an easy question for any one who nows coding in PRT. I would like some help to create a code that increases/cumulate a position if the equity drops x% below the strategy’s maxequity. I thought it would be something like the code below that I found but it clearly doesn’t work:

    maxequity = max(equity,maxequity)
    Drawdown = 15 // % drawdown from max equity needed to increase position
    
    if longonmarket and equity < maxequity * (1 - (Drawdown/100)) then
    buy 1 contract at market
    endif
    
    if shortonmarket and equity < maxequity * (1 - (Drawdown/100)) then
    buy 1 contract at market
    endif

    Any idea on how to fix it? Is it the definition of maxequity (line 1) that is wrong?

    #121433 quote
    swedshare
    Participant
    Senior

    …Or maybe the above is not logical. Instead, what would be the code for increasing a position if current position is down x %?

    #121434 quote
    Vonasi
    Moderator
    Master

    Perhaps something like this – not tested.

    capital = 10000 //starting capital
    
    if longonmarket then 
    equity = capital + strategyprofit + ((close - positionprice) * countofposition)
    endif
    
    if shortonmarket then 
    equity = capital + strategyprofit + ((positionprice - close) * abs(countofposition))
    endif
    
    maxequity = max(equity,maxequity)
    
    Drawdown = 15 // % drawdown from max equity needed to increase position
    
    if longonmarket and equity < maxequity * (1 - (Drawdown/100)) then
    buy 1 contract at market
    endif
    
    if shortonmarket and equity < maxequity * (1 - (Drawdown/100)) then
    buy 1 contract at market
    endif
    #121446 quote
    swedshare
    Participant
    Senior

    Thank you @Vonasi , it worked! 🙂

    It was also a minor disaster since it accumulated like crazy… do you know if there is a command to limit the increased order to only once per trade? Something like “max cumulateorders = 1”? I tried using the “countofposition” command but that only works if the strategy is static on the number of contracts to buy or sellshort and I prefer to use re-invest with increasing positions. I looked in the PRT manual but it didn’t have any example on limiting the number of cumulate orders.

    #121447 quote
    Vonasi
    Moderator
    Master
    if longonmarket and equity < maxequity * (1 - (Drawdown/100)) and countofposition < 2 then
    buy 1 contract at market
    endif
    
    
    if shortonmarket and equity < maxequity * (1 - (Drawdown/100)) and countofposition > -2 then
    buy 1 contract at market
    endif

     

    swedshare thanked this post
    #126741 quote
    swedshare
    Participant
    Senior

    I would like to do a twist on the above code and instead create a trigger to increase a position with x contracts if the current position is down x %. I could really use some help since I failed with my own poor skills…

    #126746 quote
    Vonasi
    Moderator
    Master

    Not sure that I fully understand what you are trying to do but this is what you have described. Not tested.

    percentage = 5
    
    if not longonmarket and (your long entry conditions) then
    buy 1 contract at market
    buy percentage contracts at close * (1-(percentage/100)) limit
    endif
    
    if longonmarket then
    buy percentage contracts at positionprice * (1-(percentage/100)) limit
    endif
    
    if not shortonmarket and (your short entry conditions) then
    sellshort 1 contract at market
    sellshort percentage contracts at close * (1+(percentage/100)) limit
    endif
    
    if shortonmarket then
    sellshort percentage contracts at positionprice * (1+(percentage/100)) limit
    endif
    #126750 quote
    swedshare
    Participant
    Senior

    We’re getting closer. We can focus on only longonmarket and ignore short. I would like the code to trigger a buy of x contracts at market if the current position has declined x % since buy.

    Let’s say I have 0.2 contracts on DJI and if this position is down 0.5% in value I want to buy an additional 0.2 contracts. I would also like the maximum position to be 0.4 contracts.

    #126751 quote
    swedshare
    Participant
    Senior

    A picture says more than a thousand words, right. See attached. When the MAE is down 15 euro (which is roughly 0,3%) I want to buy 0.2 contracts. Is that possible?

    example.jpg example.jpg
    #126753 quote
    Vonasi
    Moderator
    Master

    So you don’t want to buy x contracts at a drop of x%. You want to buy y contracts at a drop of x%.

    percentage = 0.5
    positionsize = 0.2
    maxposition = 0.4
    
    if not onmarket and (your long entry conditions) then
    buy positionsize contracts at market
    buy positionsize contracts at close * (1-(percentage/100)) limit
    endif
    
    if onmarket and countofposition < maxposition then
    buy positionsize contracts at positionprice * (1-(percentage/100)) limit
    endif
    #126754 quote
    Vonasi
    Moderator
    Master

    The above code places a pending order on the market at the same time that it opens the first trade so that if the market drops 0.5% in that first candle then a second position will open. This initial order however has to be calculated on the closing price of the previous candle and so if there is a gap down then it will not be 0.5% below your first buy price. After the first candle it is exactly 0.5% below because we now know what price we bought at.

    #126755 quote
    Vonasi
    Moderator
    Master

    You also have to consider that that code uses the POSITIONPRICE and not TRADEPRICE in the calculations so if you increase the maximum position size so that it opens more individual trades than two then the 0.5% is calculated off the average price you purchased at and not the last price you purchased at. Just change POSITIONPRICE to TRADEPRICE to match whichever you prefer.

    #126756 quote
    swedshare
    Participant
    Senior

    It didn’t have any effect. No additional buy is being made. But it usually takes way more than one candle before the market has dropped to the trigger level. Does the limit order cancel itself after one candle?

    IF not longonmarket and c1def and c11m and c21m and c41m and c2def and c1def and c160m and not BadTime then
    Buy positionsize contracts at market
    buy positionsize contracts at close * (1-(percentage/100)) limit
    endif
     
    if onmarket and countofposition < maxposition then
    buy positionsize contracts at tradeprice * (1-(percentage/100)) limit
    endif
    
    percentage = 0.3
    positionsize = 0.2
    maxposition = 0.4
    #126770 quote
    Vonasi
    Moderator
    Master

    I just tested this version on DJI daily and it worked just fine.

    percentage = 0.5
    positionsize = 1
    maxposition = 2
    
    if not onmarket and close < open then
    buy positionsize contracts at market
    buy positionsize contracts at close * (1-(percentage/100)) limit
    endif
    
    if onmarket and countofposition < maxposition then
    buy positionsize contracts at positionprice * (1-(percentage/100)) limit
    endif
    
    set target %profit 1

    It did nothing though with a position size of 0.2.

    Why have you moved the variable setting to the bottom of the code? They must be at the start of the code or they will not be known for the first bar on the chart.

    Screenshot_8.png Screenshot_8.png
    #126775 quote
    swedshare
    Participant
    Senior

    I get no extra trade when I add the code. Changed the positionsize to 1 and max to 2. See attached for ITF if you want to give it a go. DJI/wall street cash (1 eur) with 5sec timeframe. I do appreciate your support 🙂

    Test-DOW-5s.itf
Viewing 15 posts - 1 through 15 (of 22 total)
  • You must be logged in to reply to this topic.

Increase position based on equity drawdown


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
swedshare @swedshare Participant
Summary

This topic contains 21 replies,
has 3 voices, and was last updated by Vonasi
5 years, 10 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/06/2020
Status: Active
Attachments: 3 files
Logo Logo
Loading...