Coding open on Tick

Viewing 15 posts - 1 through 15 (of 29 total)
  • Author
    Posts
  • #242737 quote
    Trader Sab
    Participant
    New

    Hallo all

    I like to code a strategy that take the 8.50 to 9.00 candles as a range and then executes a long above the range high or a short below the range low. I how can I give the code the to execute at the break of the range high or low? I have been trying but with no luck. Thank you for your help, really appreciate it!

    I got this code but no trades are executed

    // Definition of code parameters
    DEFPARAM FLATBEFORE = 090000
    DEFPARAM FLATAFTER = 120000

    DEFPARAM CumulateOrders = False // Cumulating positions deactivated

    //Long Conditions
    IF Time = 085500 THEN
    SignalHigh = max(high, high[1])
    SignalLow = min(low, low[1])
    ENDIF

    //Define the breatout Conditions
    LongCondition = (open > SignalHigh and time >= 090000)
    ShortCondition = (open < SignalLow and time >= 090000)

    TradeLong = 0
    TradeShort = 0

    IF Time = 090000 and open= SignalHigh THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF

    //Set Stop-Loss
    SET STOP TRAILING 20

    #242738 quote
    GraHal
    Participant
    Master

    Change to below and you should get some trades?

    IF LongCondition THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    Trader Sab thanked this post
    #242746 quote
    Trader Sab
    Participant
    New

    Good morning

    Thank you for your reply. Unfortunately if I code this way, the trade will only be opened at 090500, instead at 090000. it will not open a position at the range high.

    My idea is that the system opens a long position at the range high +3 pips, or a short position at the range low +3 pips. And I like the system only to have 1 go at either side, meaning once long and once short.

    On Jan 17th the trade was only executed at 091000, which in this case would not have had a great influence on the price, however

    On Jan 16th the trade was executed at 090500 as a buy entry despite the fact that it should have been a short position from 21375. I don’t understand what is wrong in the code that the system opens a long position despite the fact that we are below the Range low.

    How can I program that trades are only openend at the RangeLow or RangeHigh +3 pips?

    I truly appreciate your assistance, as I am at a loss.

    Thank you

    PRT-Code-9-AM-Break.jpg PRT-Code-9-AM-Break.jpg PRT-Code.jpg PRT-Code.jpg
    #242749 quote
    GraHal
    Participant
    Master

    the trade will only be opened at 090500, instead at 090000. it will not open a position at the range high.

    Close needs time to get to / past  the range high.

    I have read your last post 3 times and there seems to be too many restrictions on what you expect price to do and when?

    Price cannot be made to do what we want at exactly the time we want.

    Please state as brief as possible what Algo2 in my next post is not doing that you would like it to do?

    #242750 quote
    GraHal
    Participant
    Master
    // Definition of code parameters
    DEFPARAM FLATBEFORE = 090000
    DEFPARAM FLATAFTER = 120000
    
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    //Long Conditions
    IF Time = 085500 THEN
    SignalHigh = High
    SignalLow = Low
    ENDIF
    
    //Define the breatout Conditions
    LongCondition = (Close > SignalHigh+3*pipsize and time >= 090000)
    ShortCondition = (Close < SignalLow-3*pipsize and time >= 090000)
    
    IF LongCondition THEN
    BUY 1 CONTRACT AT MARKET
    SET STOP pTRAILING 80*pipsize //20*pipsize
    ENDIF
    
    If ShortCondition THEN
    SellShort 1 Contract at Market
    SET STOP pTRAILING 90*pipsize //20*pipsize
    ENDIF
    
    JS and Iván González thanked this post
    Algo2.png Algo2.png
    #242777 quote
    JS
    Participant
    Senior

    I think what is meant here is to first determine the range and then open the position exactly (title:Open on Tick) on the “RangeMax” and “RangeLow”…

    When the range should consist of the first two five-minute bars (08:50-08:55 and 08:55-09:00)…

    DefParam CumulateOrders=False
    DEFPARAM FLATBEFORE = 090000
    DEFPARAM FLATAFTER = 120000
    
    If OpenTime=085000 then
    RangeHigh=High
    RangeLow=Low
    EndIf
    
    If OpenTime=>085000 and OpenTime<090000 then
    RangeHigh=max(RangeHigh,High)//Highest of Range
    RangeLow=min(RangeLow,Low)//Lowest of Range
    EndIf
    
    If OpenTime=>085500 then
    Buy 1 contract at RangeHigh+3*pipsize Stop
    SellShort 1 contract at RangeLow-3*pipsize Stop
    EndIf
    
    GraphOnPrice RangeHigh as "RangeHigh"
    GraphOnPrice RangeLow as "RangeLow"

    GraHal, Trader Sab and Iván González thanked this post
    Scherm­afbeelding-2025-01-18-om-17.12.09.png Scherm­afbeelding-2025-01-18-om-17.12.09.png
    #242787 quote
    Trader Sab
    Participant
    New
    Thank you so much for your help. I let the code run on a 2 min chart and got the much better results. How can I code that at a set level for example SignalHigh a buy order is set and at the SignalLow the Stop Loss?   Just for my understanding. PRT can not open a position on a 5 min candle at the beginning of the candle, right after the signal candle has completed? I let it run on a 2 min chart and got the much better results, however on Jan 15th it opens a long trade below the range high and it opened the trade right on 090000.   I think I am missing a very basic concept here and appreciate your insights.
    Espresso-PRT-Code-180125.jpg Espresso-PRT-Code-180125.jpg
    #242801 quote
    GraHal
    Participant
    Master
    Jan 15th it opens a long trade below the range high and it opened the trade right on 090000.
    Line 9 in your screenshot above show Time = 085580 (typo?) did you want 085500?
    can not open a position on a 5 min candle at the beginning of the candle, right after the signal candle has completed?
    No technical reason why not, but Close (on signal candle) needs to be > High (of signal candle) +3 points (Line 15). If your code was Close = High then maybe, occasionally, a position would be opened at the beginning of the candle, right after the signal candle has completed?
    Trader Sab thanked this post
    #242806 quote
    GraHal
    Participant
    Master
    did you want 085500?
    If you are running on 2 min Chart Timeframe, then you likely want 085400 or 085600?
    #242831 quote
    Trader Sab
    Participant
    New
      Jan 15th it opens a long trade below the range high and it opened the trade right on 090000. Line 9 in your screenshot above show Time = 085580 (typo?) did you want 085500? Code for Line 9 should be the code for the signal candle. I only let it run on a lower timeframe as I was trying to get the code to execute at 090000 on the correct level but I seem unable to achieve atm   can not open a position on a 5 min candle at the beginning of the candle, right after the signal candle has completed? No technical reason why not, but Close (on signal candle) needs to be > High (of signal candle) +3 points (Line 15). If your code was Close = High then maybe, occasionally, a position would be opened at the beginning of the candle, right after the signal candle has completed? Lets take a 5 min chart, Range H from 085000 to 090000 is 21130 and the L 21119. I would like the code to open a long position at 21133 and a short position at 21116 and not just somewhere random.   I appreciate you taken the time. Thank you.
    #242835 quote
    Trader Sab
    Participant
    New
    I think what is meant here is to first determine the range and then open the position exactly (title:Open on Tick) on the “RangeMax” and “RangeLow”… When the range should consist of the first two five-minute bars (08:50-08:55 and 08:55-09:00)…
    Thank you a lot for your help!
    Is it correct that If I want the code to do something, it is always the close of the candle not the open?
    In order to eliminate the number of trades the system takes, how can I code that it waits at least 3 candles before it opens another new position?
    I added as STOP LOSS, which works well, however I should add a if in profit that the stop gets trailed or is that not possible?
    IF LongOnMarket THEN SET STOP LOSS RangeHigh ENDIF IF ShortOnMarket THEN SET STOP LOSS RangeLow ENDIF
    Appreciate your assistance. Thank you.
    #242839 quote
    GraHal
    Participant
    Master
    085580 (typo?) did you want 085500?
    Time format is HHMMSS so there is no 085580 … 085559 then at the Close of the 59th second, time becomes 085600. Correct your Time, then check your results for any changes.
    Trader Sab thanked this post
    #242840 quote
    GraHal
    Participant
    Master
    Lets take a 5 min chart, Range H from 085000 to 090000 is 21130 and the L 21119. I would like the code to open a long position at 21133 and a short position at 21116 and not just somewhere random.
    So if Price goes past 21133 before a trade can be opened, then you don’t want to open a trade. If you are using a 5 min chart then, by the close of the 090500 candle, price may be 21140. To get what you want, you need to use the code provided by JS using Stop Orders.
    Trader Sab thanked this post
    #242842 quote
    JS
    Participant
    Senior

    It is correct that the code is executed at the close of the candlestick (Close). Therefore, when a certain condition is triggered during the execution and this condition initiates a position (at market), the position will be opened at the “Open” of the next bar.

    This is one way to wait for a period after a position is opened…

    Once BarsToWait = 3

    If BarIndex – TradeIndex(1) > BarsToWait then

    If you want to use a “Trailing Stop Loss,” this is, in my opinion, the most used code https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/

    Trader Sab thanked this post
    #242862 quote
    Trader Sab
    Participant
    New
    I actually want the code to wait after having been stopped out for 3 bars before opening a new trade, as I hope with this to reduce the number of stop out trades.   Appreciate your support very much!
Viewing 15 posts - 1 through 15 (of 29 total)
  • You must be logged in to reply to this topic.

Coding open on Tick


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Trader Sab @trader-sab Participant
Summary

This topic contains 28 replies,
has 4 voices, and was last updated by druby
1 year ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/17/2025
Status: Active
Attachments: 9 files
Logo Logo
Loading...