Help with limiting strategy to one trade per day

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #139465 quote
    mizatt
    Participant
    Junior

    Morning,

    I’m very new to PRT coding (though not the markets in general) and have been trying to work on a 15-minute trend reversal strategy for the US 500.

    I’ve pulled several examples of very useful code (mostly from Nicolas) but tonight I’ve become stuck trying to limit the system to just taking one trade per day.  I’ve found multiple examples of how to fix the problem in the forums but when I try to institute each and every one of these (which are all similar), the strategy still occasionally takes 2 trades per day when I backtest (via IG) over the past 6 months and I can’t work out what I’m doing wrong.

    Any help would be much appreciated.

    Here is the code I’ve cobbled together so far (in the attachments).

    Mark

    * Accidentally uploaded the first pic twice – apologies as it didn’t seem to work initially

    PRT2.jpg PRT2.jpg PRT1.jpg PRT1.jpg PRT1-1.jpg PRT1-1.jpg
    #139473 quote
    robertogozzi
    Moderator
    Master

    There you go:

    Once TradeON = 1
    If IntraDayBarIndex = 0 then
       TradeON = 1
    Endif
    If OnMarket or StrategyProfit <> StrategyProfit[1] Then
       TradeON = 0
    Endif
    If MyConditions and TradeON and Not OnMarket Then
       BUY/SELLSHORT.......
    Endif

    use TradeON as an additional condition to enter a trade.

    mizatt thanked this post
    #139483 quote
    mizatt
    Participant
    Junior

    Many thanks for your swift reply Roberto!

    I’ll give that a whirl later this morning.

    Greatly appreciated.

    Mark

    #139484 quote
    mizatt
    Participant
    Junior

    @robertogozzi

    Hi Roberto,

    I’ve just integrated that nice piece of code you sent me this am but frustratingly the order list indicates the strategy is still occasionally taking 2 trades in the same day (12 June, 1st July and 8th July).  See attached pics with updated code.

    Any idea how to get around this?

    Mark

    PRT3.jpg PRT3.jpg PRT4.jpg PRT4.jpg PRT5.jpg PRT5.jpg
    #139493 quote
    robertogozzi
    Moderator
    Master

    Please paste the code using the “Insert PRT code” button, not pics of it, otherwise I cannot use it.

    Thank you 🙂

    #139496 quote
    robertogozzi
    Moderator
    Master

    Moreover, use “@” only when there are so many users posting and you absolutely need to reference one of them. In this case it’s just the two of us. Because everytime you put that sign in front of a username, that user will receive an email.

    Thank you 🙂

    #139516 quote
    mizatt
    Participant
    Junior

    Hi Roberto,

    Thanks for letting me know how things work and apologies for using ‘@’ .  I hadn’t even realised there was an ‘insert PRT code’ button!

    As requested, please find the code attached.

    Any ideas how to ensure the strategy only takes 1 trade/day?

    Many thanks

    Mark

    //US 500
    //15-min TF
    
    DEFPARAM CumulateOrders=false
    //UK(London) time
    DefParam FlatBefore = 100000
    DefParam FlatAfter = 230000
    
    IF Time >= 100000 AND Time <= 170000 OR Time >= 180000 AND Time <= 203000 THEN
    Tradetime = 1
    else
    Tradetime = 0
    endif
    
    Once TradeON = 1
    If IntraDayBarIndex = 0 then
    TradeON = 1
    Endif
    If OnMarket or StrategyProfit <> StrategyProfit[1] Then
    TradeON = 0
    Endif
    
    PositionSize = 50
    
    CorrectVol = high-low[1]>5 AND high-low[2]>5 and high-low[3]>5  //volatility of preceding 3 candles is sufficient for trade to occur
    
    sto = stochastic[8,3]
    
    c1 = close[1]<open[1] and close>open
    c2 = close>open[1] AND close >= (high-low)*0.60 + low
    c3 = lowest[3](low)<lowest[50](low)[1] or lowest[3](low)<lowest[50](low)[2] or lowest[3](low)<lowest[50](low)[3]
    c4 = summation[3](sto<20)>0
    c5 = close[1]>open[1] and close<open
    c6 = close<open[1] AND close <= (high-low)*0.40 + low
    c7 = highest[3](high)>highest[50](high)[1] or highest[3](high)>highest[50](high)[2] or highest[3](high)>highest[50](high)[3]
    c8 = summation[3](sto>80)>0
    
    buyCondition = c1 and c2 and c3 and c4
    sellCondition = c5 and c6 and c7 and c8
    
    // BUY order
    IF NOT OnMarket AND CorrectVol AND buyCondition AND Tradetime and TradeON THEN
    BUY PositionSize CONTRACTS AT MARKET
    SET STOP LOSS abs(close - (lowest[3](low) - 1 * pipsize))
    set target $profit 600
    
    ENDIF
    
    // SELL order
    IF NOT OnMarket AND CorrectVol AND sellCondition AND Tradetime and TradeON then
    SELLSHORT PositionSize CONTRACTS AT MARKET
    SET STOP LOSS abs(close - (highest[3](high) + 1 * pipsize))
    set target $profit 600
    
    ENDIF
     
    If LongonMarket and (high-low[1]) <3 AND (high-low[2]) < 3 THEN   //if volatility very low intra-day then exit positions
    sell positionSize contracts at market
    Endif
    
    If ShortOnMarket AND (high-low[1]) <3 AND (high-low[2]) < 3 THEN   
    buy positionSize contracts at market
    Endif
    #139518 quote
    robertogozzi
    Moderator
    Master

    It’s because at line 62 you are using BUY to exit a Short trade. No, you’ll have to replace it with EXITSHORT.

    To recap:

    • BUY opens a Long trade
    • SELL exits a Long trade
    • SELLSHORT opens a Short trade
    • EXITSHORT exits a Short trade

    using BUY at line 62 implies a Stop & Reverse, since the Short trade is first closed and new Long one is subsequently opened.

    mizatt thanked this post
    #139519 quote
    mizatt
    Participant
    Junior

    Ahhh silly me.  I knew it had to be be something fundamental I was doing wrong.

    Have now corrected this and it works perfectly.

    Hugely appreciate your help with this Roberto!

    Many thanks again

    Mark

    #143596 quote
    frenqle
    Participant
    Average

    is there a different way to limit to ONE trade a day then: IntraDayBarIndex

    When my position ends before the first bar, it does not work. if my position closes at 0 bars.. it takes a new position.

    #143705 quote
    Finning
    Participant
    Veteran

    Have a look how this code is used

    OTD = Barindex - TradeIndex(1) > IntradayBarIndex  // limits the (opening) trades till  1 per day (OTD One Trade per Day)
    if time >= StartE And time <= StartL and OTD  then //and not onmarket then
    IF conditionsmet then
    buy N shares AT MARKET
    endif

    Taken from this strategy here:

    Optimization moving average crossing strategy with “machine learning”

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

Help with limiting strategy to one trade per day


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
mizatt @mizatt Participant
Summary

This topic contains 10 replies,
has 4 voices, and was last updated by Finning
5 years, 5 months ago.

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