Stopping second trade if first completes quickly

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #96535 quote
    Crocogotter
    Participant
    New

    Most of my strategies are for Wall Street, which can move very quickly. This means that sometimes a trade is triggered and completed quickly, sometimes within the time period I allow for a trade to trigger. I can’t seem to find a way to stop a second (or sometimes third) trade from setting up.

     

    Eg After a hammer, I might allow 3 candles for a trade to trigger. Sometimes the trade will trigger and reach target within the first candle, but the conditions eg a hammer within the last 3 candles is still the case, so a second trade is now set up.

     

    I’ve tried using ” Not Onmarket” but can’t seem to get it working how I’d like it to ie a maximum of one trade per set up.

     

    I’m sure there is a very simple way of doing this, but I can’t seem to work it out, so any help would be much appreciated.

     

    Many thanks in advance.

    #96536 quote
    robertogozzi
    Moderator
    Master

    When the pattern is detected set a variable, say FLAG=1.

    Add it to your conditions to enter a trade.

    Clear that variable when 3, or whatever number you prefer, bars have elapsed or when you are OnMarket.

    Should your trade enter and end within the same bar without updating the OnMarket status, you need to check if STRATEGYPROFIT is any different from STRATEGYPROFIT[1],  if it is then a trade was entered and exited so you can clear the above mentioned variable.

    Crocogotter thanked this post
    #96539 quote
    Crocogotter
    Participant
    New

    Many thanks for your prompt reply.

    I have already tried doing something along these lines, but without success so far. I’ll persevere, and maybe add the code here if I still can’t get it to work.

    #96540 quote
    Crocogotter
    Participant
    New

    Sorry, perhaps I should add, I have tried using “Not Onmarket”, but the problem isn’t that it enters a second trade at the same time, but simply once I’m not longer “OnMarket” (ie when the first trade is finished), then a second trade may be entered because I’m no longer in a trade.

    I don’t know how to use being “OnMarket” to keep the Flag at 0 when I’m no longer “OnMarket”, as I’m just using if-then-endif conditions to set up the trades.

    #96541 quote
    robertogozzi
    Moderator
    Master

    Post your code, please.

    #96548 quote
    Crocogotter
    Participant
    New
    //hammer 3min WS v2.02 Code
    
    DEFPARAM FlatAfter = 205000
    
    DEFPARAM cumulateorders = false
    
    
    
    result = 0
    
    
    //Red Hammer signal
    
    if not shortonmarket then
    if time > 143530 then
    if low<low[1] then
    if close<open then
    if open-close>high-open then
    if close-low>2*(high-close) then
    if close-low>14.9 then
    if low - .1 < lowest[5](low) then
    result = -1
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    
    
    //Green Hammer signal
    if not shortonmarket then
    if time > 143530 then
    if low<low[1] then
    if close>open then
    if close-open>high-close then
    if open-low>2*(high-open) then
    if open-low>14.9 then
    if low - .1 < lowest[5](low) then
    result = -1
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    
    
    
    
    
    //Green star signal
    if not longonmarket then
    if time > 143530 then
    if high>high[1] then
    if close>open then
    if close-open>open-low then
    if high-close>2*(close-low) then
    if high-close>14.9 then
    if high + .1 > highest[5](high) then
    result = 1
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    
    
    
    
    
    //Red star signal
    if not longonmarket then
    if time > 143530 then
    if high>high[1] then
    if close<open then
    if open-close>close-low then
    if high-open>2*(open-low) then
    if high-open>14.9 then
    if high + .1 > highest[5](high) then
    result = 1
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    
    
    
    
    
    
    // Definition of the validity length of the order
    
    once NbBarLimit = 6
    
    // Conditions to enter long positions
    
    if result= -1 then
    MyStopBuy = 17+ close
    MyIndex = Barindex
    endif
    
    if Barindex >= MyIndex + NbBarLimit then
    MyStopBuy = 0
    endif
    
    IF NOT LongOnMarket and barindex-tradeindex >-1 AND MyStopBuy > 0  THEN
    BUY 1 CONTRACTS AT MyStopBuy Stop
    ENDIF
    
    // Conditions to enter short positions
    
    if result = 1 then
    MyStopSell = close - 17
    MyIndex = Barindex
    endif
    
    if Barindex >= MyIndex + NbBarLimit then
    MyStopSell = 0
    endif
    
    
    IF NOT ShortOnMarket and barindex-tradeindex >-1 AND MyStopSell > 0 THEN
    SELLSHORT 1 CONTRACTS AT MyStopSell Stop
    ENDIF
    
    
    
    // Stops and targets : Enter your protection stops and profit targets here
    
    Set Stop pLoss 53
    
    if time < 193000 then
    Set Target pProfit 25
    else
    set Target pProfit 15
    endif
    

    I appreciate it’s not very elegant; it was originally just for an indicator but has converted into automated trading quite well, apart from this issue with second trades.

    #96570 quote
    GraHal
    Participant
    Master

    The 2nd trade Issue could be due to below as it is always going to be true?

    Line 118 and Line 134

    barindex-tradeindex >-1
    #96571 quote
    robertogozzi
    Moderator
    Master

    You already have two flags: MyStopBuy and MyStopSell.

    To reset them when OnMarket, add these lines at line 6:

    If OnMarket THEN
       MyStopBuy  = 0
       MyStopSell = 0
    Else
       MyProfit = STRATEGYPROFIT
    Endif

    The line activated by ELSE has the purpose of helping you detect a trade that has been triggered and closed within the same bar.

    Below the above snippet of code add this one to clear the two variables in case a trade opened and closed within the same bar:

    If not OnMarket and Not OnMarket[1] then
       If MyProfit <> STRATEGYPROFIT Then
          MyStopBuy  = 0
          MyStopSell = 0
          MyProfit   = STRATEGYPROFIT
       Endif
    Endif

    You check that you are not OnMarket now and were not the previous bar, yet STRATEGYPROFIT has changed.

    Silvia Brugnolaro thanked this post
    #96630 quote
    Crocogotter
    Participant
    New

    Many thanks for the help.

     

    I’ve removed that condition from lines 118 and 134, and have added in the 2 snippets of code, but the amended code will still trigger a second trade if the original trade opens and closes in the same candle.

    #96641 quote
    robertogozzi
    Moderator
    Master

    You shouldn’t have removed those two conditions, otherwise the snippets of code are useless.

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

Stopping second trade if first completes quickly


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 9 replies,
has 3 voices, and was last updated by robertogozzi
6 years, 10 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/15/2019
Status: Active
Attachments: No files
Logo Logo
Loading...