placing an order when predefined levels achieved.

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #169015 quote
    Slowlyslowly
    Participant
    Average

    Hi

    If you look at the attached I need to know how to code this example.

    lets assume CURRENT  price is ABOVE level A and B.

    Price needs to achieve a price less than A but not less than B (a -10 pips) , then place an order to BUY when the 9 EMA crosses 26 EMA .

    If price achieves level B (a -10 pips) then no order is placed for the EMA cross over .

    Is this possible?

    Obviously if you could elaborate  on the reverse example , i.e placing a code when the current price is lower than a fictions level A and B (which are higher levels)  and the code would place a sell order for a EMA cross under.

     

    Many thanks.

    Screenshot-2021-05-07-at-09.07.45.png Screenshot-2021-05-07-at-09.07.45.png
    #169030 quote
    robertogozzi
    Moderator
    Master

    There you go:

    DEFPARAM CumulateOrders = FALSE
    ONCE P        = 4
    ONCE Pips     = 10 * PipSize
    ONCE LongA    = 0
    ONCE ShortA   = 0
    
    //clear data each new day
    IF Day <> Day[1] THEN
       LongA      = 0
       ShortA     = 0
    ENDIF
    
    //set entry conditions, if not previously set
    IF LongA = 0 THEN
       LongA      = lowest[P](low)
       LongB      = LongA - Pips
       EntryLong  = 0
    ENDIF
    IF ShortA = 0 THEN
       ShortA     = highest[P](high)
       ShortB     = ShortA + Pips
       EntryShort = 0
    ENDIF
    
    //clear entry data when on market
    IF OnMarket THEN
       EntryLong  = 0
       EntryShort = 0
       IF LongOnMarket THEN
          LongA   = 0
          LongB   = 0
       ENDIF
       IF ShortOnMarket THEN
          ShortA  = 0
          ShortB  = 0
       ENDIF
    ENDIF
    
    //calculate averages
    Ema9  = average[9,1](close)
    Ema26 = average[26,1](close)
    
    //LONG
    IF close <= LongB THEN
       EntryLong  = 0
    ELSIF close < LongA THEN
       EntryLong  = 1
    ENDIF
    IF EntryLong  AND Not LongOnMarket  AND Ema9 CROSSES OVER  Ema26 THEN
       Buy 1 Contract at Market
    ENDIF
    
    //SHORT
    IF close >= ShortB THEN
       EntryShort = 0
    ELSIF close > ShortA THEN
       EntryShort = 1
    ENDIF
    IF EntryShort AND Not ShortOnMarket AND Ema9 CROSSES UNDER Ema26 THEN
       Sellshort 1 Contract at Market
    ENDIF
    
    // TP & SL
    SET Target pProfit 100
    SET Stop   pLoss   50
    //monitor some data
    //graphonprice LongA  coloured(0,128,0,150)
    //graphonprice LongB  coloured(0,128,0,150)
    //graphonprice ShortA coloured(0,0,0,255)
    //graphonprice ShortB coloured(0,0,0,255)
    #169031 quote
    robertogozzi
    Moderator
    Master

    At lines 15 and 20 you can change the way to calculate the A-B range. I used the lowest and the highest prices within the last P bars.

    #169055 quote
    Slowlyslowly
    Participant
    Average

    Thank you Roberto ,

     

    so just to clarify if i wanted a level broken below for a buy at 2100 i would amend line 15 to :

    Long  A = 2100

    and if I wanted a level broken above for a sell at 3800 i would amend line 20 to :

    Short A = 3800.

     

    so would i still need line 2 P= 4 ? As not sure how that relates…..

     

    and assuming a long trade ie trigger level is 2100 (market is dropping and looking for reversal) ( but not 10 pips lower ) and entry is a 9/26 cross over , if the 926 cross over happens outside the range of 2100-2090 which is very likely the entry will still work  as 2100 was achieved 2090 was not (which invalidates the trade) – it then retraces to more than 2100 to cross over 926 (ie reversal happened) for example 2120 and then enters the trade as the 926 crosses over for a buy ?

    The code will cope with this ?

     

     

    Many thanks for clarification.

    #169056 quote
    robertogozzi
    Moderator
    Master

    You don’t need line 2 anymore as it was only used by HIGHEST and LOWEST.

    As to your question “and assuming a long trade ie trigger level is 2100 (market is dropping and looking for reversal) ( but not 10 pips lower ) and entry is a 9/26 cross over , if the 926 cross over happens outside the range of 2100-2090 which is very likely the entry will still work as 2100 was achieved 2090 was not (which invalidates the trade) – it then retraces to more than 2100 to cross over 926 (ie reversal happened) for example 2120 and then enters the trade as the 926 crosses over for a buy ?“, it already entered a LONG trade, do you want to accumulate another position if the same 2100 level is crossed over again?

    #169057 quote
    Slowlyslowly
    Participant
    Average

    Hi roberto

     

    No did not want to enter again – was just clarifying that once it hits the initial trigger of 2100 it places an order for a 926 cross over , and even if this 926 does not happen for a while it will maintain that order until its complete or becomes invalid if subsequently price  crosses line B (+-10 depending on direction ) ?

    #169073 quote
    robertogozzi
    Moderator
    Master

    Yes, that’s exactly what it’s doing now.

    It is either triggered or invalidated.

    #169254 quote
    Slowlyslowly
    Participant
    Average

    Hi Roberto

     

    Code looks fantastic but a problem …..

    I ran the code this morning on the FTSE – so i amended line 20 to read SHORT A = 7154 removed the line 2 P reference and just set LONG A = 0

    I loaded the code up at 0900 am and it triggered on the 926 cross under (2 minutes) but it should not have as SHORT A was never reached at 7154.

    It had previously hit 7154 earlier but even if this is the error and it noted the level was hit before the code was activated it should have been an invalid trade as it breached 10 pips higher at 7164 ?

     

    Am i missing something ?

     

    attached screen shot and actual code used on the FTSE this morning on 2 minutes timeframe .

    Screenshot-2021-05-10-at-10.47.01.png Screenshot-2021-05-10-at-10.47.01.png 00originalcoderoberto.itf
    #169260 quote
    robertogozzi
    Moderator
    Master

    Ok, I modified it so that it starts ONLY when you run it, ignoring whatever happened before:

    DEFPARAM CumulateOrders = FALSE
    DEFPARAM PreLoadBars    = 0
    //ONCE P        = 4
    ONCE Pips     = 10 * PipSize
    ONCE LongA    = 0
    ONCE ShortA   = 0
    ONCE MyTime   = Time
    //clear data each new day
    IF Day <> Day[1] THEN
    LongA      = 0
    ShortA     = 0
    ENDIF
    IF BarIndex >= 26 AND (Time >= MyTime) then 
    //set entry conditions, if not previously set
    IF LongA = 0 THEN
    LongA      = 0//lowest[P](low)
    LongB      = LongA - Pips
    EntryLong  = 0
    ENDIF
    IF ShortA = 0 THEN
    ShortA     = 7154//highest[P](high)
    ShortB     = ShortA + Pips
    EntryShort = 0
    ENDIF
     
    //clear entry data when on market
    IF OnMarket THEN
    EntryLong  = 0
    EntryShort = 0
    IF LongOnMarket THEN
    LongA   = 0
    LongB   = 0
    ENDIF
    IF ShortOnMarket THEN
    ShortA  = 0
    ShortB  = 0
    ENDIF
    ENDIF
     
    //calculate averages
    Ema9  = average[9,1](close)
    Ema26 = average[26,1](close)
    //LONG
    IF close <= LongB THEN
    EntryLong  = 0
    ELSIF close < LongA THEN
    EntryLong  = 1
    ENDIF
    IF EntryLong  AND Not LongOnMarket  AND Ema9 CROSSES OVER  Ema26 THEN
    Buy 1 Contract at Market
    ENDIF
     
    //SHORT
    IF close >= ShortB THEN
    EntryShort = 0
    ELSIF close > ShortA THEN
    EntryShort = 1
    ENDIF
    IF EntryShort AND Not ShortOnMarket AND Ema9 CROSSES UNDER Ema26 THEN
    Sellshort 1 Contract at Market
    ENDIF
     
    // TP & SL
    SET Target pProfit 100
    SET Stop   pLoss   50
    ENDIF
    //monitor some data
    //graphonprice LongA  coloured(0,128,0,150)
    //graphonprice LongB  coloured(0,128,0,150)
    //graphonprice ShortA coloured(0,0,0,255)
    //graphonprice ShortB coloured(0,0,0,255)
    
    // Stops and targets : Enter your protection stops and profit targets here
    00originalcoderobertoV2.itf
    #169361 quote
    Slowlyslowly
    Participant
    Average

    Hi Roberto

     

    great seems to have fixed that , but code seems to be ignoring line 4 ONCE PIPS = 5 x PIPSIZE, and line 35 LONGB = LONG A – PIPS

    so long A is trigger point to look for a 926 cross over on 2 minutes , but if LONG B is hit no trade should action for the day .

    In the attached example it entered at the 926 cross over great ! but it ignored the fact LINE B was breached by a lot , and as I read the code line B was 5 pips below line A as soon as this is hit no 926 should be looked for and trade is invalid for the day ?

    attached is a back test but it performed exactly the same in real time yesterday ?

     

    attached code which has a flag statement added but that’s it. what am I missing ?

    Thanks for your help.

    Screenshot-2021-05-11-at-08.01.59.png Screenshot-2021-05-11-at-08.01.59.png 007007007BUY.itf
    #169422 quote
    robertogozzi
    Moderator
    Master

    It’s not ignoring any lines.

    It enters AFTER a crossover has occurred at the closing of the candle (and prior to that, price had sit in the range a-B).

    #169432 quote
    Slowlyslowly
    Participant
    Average

    Hi Roberto

    maybe I didn’t explain it correctly .

    level A triggers looking for an EMA cross over .

    if BEFORE this cross over occurs it breaches level B Then the trade is invalid and no trade must occur .

    you can clearly see from the screen shot that code is placed live then level A is hit  then level B is breached before an EMA cross over so no trade should happen .

    You are correct the cross over is in the range a-b but that should be irrelevant – the cross over CAN occur even above level A if price retraces after hitting level A as long as level B is not breached before the ema cross over.

    The key is AFTER the code is activated – the first action is level A must be hit – this activates looking for the ema cross over .

    if level b is breached BEFORE the ema cross over the trade is cancelled for the day . this is very important as it can retrace which is what it did in the screen shot – but the trade is no longer valid .

    you will see from the screen shot – level B was breached after level A was hit and before the ema cross over  therefore it should not trade .

    In the screenshot it did subsequently retrace and crossed over on the ema and traded but it should not as level B was breached before the ema cross over so it must cancel the trade / deactivate the code . Once level B is breached it should stop looking for a trade or any ema cross over .

    it’s probably easier to look at the possible options

    …………………………………………………………..,,
    The possible option are ONLY  2 .

    price is falling so this is the sequence of events in order :

    OPTION 1

    1.level A is hit

    2.it starts to look for ema cross over

    3.ema cross over happens a trade is placed – this cross over can be at ANY level below A OR above A but NOT  below B ( note the cross over can happen at ANY level >B and <or > A AFTER level A is activated FIRST triggering it looking for this ema cross over – which is step 1 above   )

    (B)

    1. level A hit

    2.it starts to look for ema cross over

    3.no ema cross over happens

    4.level B hit so trade/code cancelled . It no longer looks for any cross over or any trade the breach of level B basically turns the code off .

    obviously the reverse for a sell.

    does that help explain it better by looking at the only 2 possible sequences of events ?

    #169452 quote
    robertogozzi
    Moderator
    Master

    I could find the glitch.

    I had forgotten to disable trading for the day on a levelB breach. Now as breach occurs, trading will be disabled (for the chosen direction) for the day and willbe resumed at the start of the new day (when IntraDayBarIndex = 0).

    I also added comments where you can replace CLOSE with LOW (for Long trades) or HIGH (for Short trades), if you want to use, as price trigger before the crossover, just a “touch” of the price, rather than the closing price.

    Slowlyslowly thanked this post
    007007007BUY-2.itf
    #169455 quote
    robertogozzi
    Moderator
    Master

    You may have seen an email about a post that I almost immediately deleted because it was not a working correction.

    #169462 quote
    Slowlyslowly
    Participant
    Average

    Many many thanks Roberto , I will test it today.

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

placing an order when predefined levels achieved.


ProScreener: Market Scanners & Detection

New Reply
Author
Summary

This topic contains 19 replies,
has 2 voices, and was last updated by Slowlyslowly
4 years, 10 months ago.

Topic Details
Forum: ProScreener: Market Scanners & Detection
Language: English
Started: 05/07/2021
Status: Active
Attachments: 11 files
Logo Logo
Loading...