Buy on certin dates, not on technical singal

Viewing 15 posts - 1 through 15 (of 22 total)
  • Author
    Posts
  • #228395 quote
    Laliberte
    Participant
    New

    Hi,
    I’m a cycle analyst and want to try my predicting of change in market trends by using PRTs backtest function and possibly try to automate trading of my models going forward as well.

    Is it possible to set certain dates when the LONG/SHORT order is opened? Instead of using a technical indicator as trigger (or several).

    Any my general strategy for the Open/close of position would be:
    Example have a scheduled trend change tomorrow (currently hold no positions). I want to LONG so I buy 1 lot at market close. In a few days time I have another trend change date and I want to sell my LONG at close. And open up a SHORT at the same time. and this goes on and on continuously. LONG positions turned into SHORT positions at the close of my selected pivot dates.

    SL would be set to 2-3%.

    Also, is the process of entering/selecting dates for the trades easy? I have maybe 50 dates on a year, sometimes less, can I upload or do I manually select them in the software?

    Any and all help is much appreciated. I’m new to this software of endless possibilities. 🙂

    Thanks so much.

    Regards

    #228414 quote
    robertogozzi
    Moderator
    Master

    This is an example to open and close Long and Short trades according to set entry and exit dates:

    // LONG open dates
    $myDateL[1]  = 20220401
    $myDateL[2]  = 20220715
    $myDateL[3]  = 20221122
    $myDateL[4]  = 20230202
    // LONG exit dates
    $myExitL[1]  = 20220420
    $myExitL[2]  = 20220730
    $myExitL[3]  = 20221202
    $myExitL[4]  = 20230209
    LastElementL = 4
    // SHORT open dates
    $myDateS[1]  = 20220507
    $myDateS[2]  = 20221115
    $myDateS[3]  = 20221219
    $myDateS[4]  = 20230629
    // SHORT exit dates
    $myExitS[1]  = 20220524
    $myExitS[2]  = 20221123
    $myExitS[3]  = 20221229
    $myExitS[4]  = 20230713
    LastElementS = 4
    //
    IF Not OnMarket THEN
       FOR i = 1 TO max(LastElementL,LastElementS)
          //LONG  trades
          IF i <= LastElementL THEN
             IF Date = $myDateL[i] THEN
                BUY 1 CONTRACT AT MARKET
                break
             ENDIF
          ENDIF
          // SHORT trades
          IF i <= LastElementS THEN
             IF Date = $myDateS[i] THEN
                SELLSHORT 1 CONTRACT AT MARKET
                break
             ENDIF
          ENDIF
       NEXT
    ENDIF
    //
    // exit LONG  trades
    IF LongOnMarket  THEN
       FOR i = 1 TO LastElementL
          IF Date = $myExitL[i] THEN
             SELL AT MARKET
             break
          ENDIF
       NEXT
    ENDIF
    // exit SHORT trades
    IF ShortOnMarket THEN
       FOR i = 1 TO LastElementS
          IF Date = $myExitS[i] THEN
             EXITSHORT AT MARKET
             break
          ENDIF
       NEXT
    ENDIF

    it can be adapted to your needs, but I need some examples to better understand what you want to achieve.

    #228421 quote
    Laliberte
    Participant
    New

    Thanks for your fantastic input Roberto, much appreciated.

    Let me give you a real example for the month of February of this year.

    For the purpose of this example I start with zero trades, no position.

    In my cycle work I continuously generate a date sequence which is basically a High-Low-High-Low-High pattern going forward.

    Now in Feb the dates are:
    20240201 High
    20240214 Low
    20240219 High
    20240223 Low
    20240228 High
    and so on…

    Now, coming 1 Feb I want to go short (-1 lot = -1 pos) at market close price since the shceduled pivot is a HIGH in market. SL 2%
    14 Feb: close my short (+1 lot = 0 pos) at market close and go long market, so buy 1 lot (+1 lot = +1 pos) at the same time as my previous short is closed, at market close. SL 2%.
    19 Feb: sell long (-1 lot = 0 pos) at market close and go short (-1 lot = -1 pos) at market close. SL 2%.
    23 Feb: close short (+1 lot = 0 pos) at market close and go long (+1 lot = +1 pos) at market close.
    28 Feb: close long (-1 lot = 0 pos) at market close and go short (-1 lot = -1 pos)

    And so it continues.

    Other extras I can think of:
    + Always close the previous position/direction before going into new position/direction (long/short)
    + Maybe build in possibility to have toggle option on trailing, lets say start with fixed Stop Loss at 2-3% (preferably adjustable manually) but when/if the market moves in right direction and positive trade, option to change it to a trailing Stop of xx-%.

    + Now to the final adjustment I need to figure out how to solve as the time goes by.
    At points in time there can all of a sudden appear an extra pivot in the sequence, one extra trading date, and as a result the polarity of the remaining pivots during the month/year/sequence all change their polarity. IE high becomes a LOW and I have to change polarity of all trades onward.

    Here is an example of extra pivot being introduced in February and the “new” sequence would look like this:

    20240201 High
    20240214 Low
    20240219 High
    20240221 NEW/EXTRA pivot which becomes LOW (from prior sequence)
    20240223 Originally Low but now changes into HIGH
    20240228 Originally High but now changes into LOW.

    The H-L-H-L-H-L needs to be intact all the time.

    So I’m thinking that instead of changing everything in the code would it be possible to assign a numerical value to each $myDateL, $myExitL, $myDateS and $myExitS.
    For example $myDateL = 1 and $myExitL = 2, $myDateS = 3 and $myExitS = 4.
    And if I see that this extra pivot have happened in the market I simply change the numbers above (in input fields) so the Long  becomes Short and vice versa.
    Or if you can think of other solution that is much more elegant. 🙂

    Again thanks Roberto.

    Regards
    Fredrik

    #228493 quote
    Laliberte
    Participant
    New

    Roberto,
    Forgot to add. If you need me to clarify any of the process/cycle just let me know. I’m so grateful for all your input and help here.

    Grazie mille! 🙂

    Regards
    Fredrik

    #228523 quote
    robertogozzi
    Moderator
    Master

    Please post my original code with the updated dates you want.

    Dates already have a value, the one within brackets used to identify each elelement of the array.

    Then please rephrase the changes you want to make.

    #228531 quote
    Laliberte
    Participant
    New
    // LONG open dates
    $myDateL[1]  = 20240214
    $myDateL[2]  = 20240223
    $myDateL[3]  = 20240306
    $myDateL[4]  = 20240315
    // LONG exit dates
    $myExitL[1]  = 20240219
    $myExitL[2]  = 20240228
    $myExitL[3]  = 20240313
    $myExitL[4]  = 20240321
    LastElementL = 4
    // SHORT open dates
    $myDateS[1]  = 20240201
    $myDateS[2]  = 20240219
    $myDateS[3]  = 20240228
    $myDateS[4]  = 20240313
    // SHORT exit dates
    $myExitS[1]  = 20240214
    $myExitS[2]  = 20240223
    $myExitS[3]  = 20240306
    $myExitS[4]  = 20240315
    LastElementS = 4
    //
    IF Not OnMarket THEN
       FOR i = 1 TO max(LastElementL,LastElementS)
          //LONG  trades
          IF i <= LastElementL THEN
             IF Date = $myDateL[i] THEN
                BUY 1 CONTRACT AT MARKET
                break
             ENDIF
          ENDIF
          // SHORT trades
          IF i <= LastElementS THEN
             IF Date = $myDateS[i] THEN
                SELLSHORT 1 CONTRACT AT MARKET
                break
             ENDIF
          ENDIF
       NEXT
    ENDIF
    //
    // exit LONG  trades
    IF LongOnMarket  THEN
       FOR i = 1 TO LastElementL
          IF Date = $myExitL[i] THEN
             SELL AT MARKET
             break
          ENDIF
       NEXT
    ENDIF
    // exit SHORT trades
    IF ShortOnMarket THEN
       FOR i = 1 TO LastElementS
          IF Date = $myExitS[i] THEN
             EXITSHORT AT MARKET
             break
          ENDIF
       NEXT
    ENDIF

    Roberto,

    Ok, I have changed to the correct dates based on the cycle logic I gave in the example.

    + Can you please add on all Open Long and Short trades, a fixed Stop Loss of 2% ?

    When are the trades executed, exactly at market close? And that is defined by my current selection of market, right?

    Also. this is just a big plus if it’s possible, you tell me:
    As I described earlier there can be an extra pivot coming into play without me knowing it in advance, called inversion, but I will notice quickly. This extra pivot changes the whole polarity going forward. The sequence must always be High-Low-High-Low and so on, and in code language BUY-SELL//SELLSHORT-EXITSHORT//BUY-SELL//SELLSHORT-EXITSHORT//BUY-SELL and so on.

    But if inversion happen the polarity of forward sequence change and I need to adjust all forward dates. IE instead of shorting next date in que I want to Long it and so on. Is there a simple way to change all entered dates so they become the opposite?

    I don’t know PRT code so please correct me here, but could this be done by altering:
    Line 29 from BUY to SELLSHORT
    Line 36 from SELLSHORT to BUT
    Line 47 from SELL AT MARKET to EXITSHORT AT MARKET
    Line 56 from EXITSHORT AT MARKET to SELL AT MARKET

    What do you think? Would this change the sequence without moving dates around?
    But this is only in case I need to change the whole sequence going forward.

    Thanks for you valuable input and help here Roberto.

    Regards
    Fredrik

    #228549 quote
    robertogozzi
    Moderator
    Master
    Your questions:

    Can you please add on all Open Long and Short trades, a fixed Stop Loss of 2% ? done (see my code below)

    When are the trades executed, exactly at market close? And that is defined by my current selection of market, right? correct (there might be some slippage, though)

    Also. this is just a big plus if it’s possible, you tell me: As I described earlier there can be an extra pivot coming into play without me knowing it in advance, called inversion, but I will notice quickly. This extra pivot changes the whole polarity going forward. The sequence must always be High-Low-High-Low and so on, and in code language BUY-SELL//SELLSHORT-EXITSHORT//BUY-SELL//SELLSHORT-EXITSHORT//BUY-SELL and so on.

    But if inversion happen the polarity of forward sequence change and I need to adjust all forward dates. IE instead of shorting next date in que I want to Long it and so on. Is there a simple way to change all entered dates so they become the opposite?

    I don’t know PRT code so please correct me here, but could this be done by altering: Line 29 from BUY to SELLSHORT Line 36 from SELLSHORT to BUT Line 47 from SELL AT MARKET to EXITSHORT AT MARKET Line 56 from EXITSHORT AT MARKET to SELL AT MARKET

    What do you think? Would this change the sequence without moving dates around? Perfect, thats exacly what you need to do.

    Updated code with a 2% SL (you can change that percentage whenever you need to).
    // LONG open dates
    $myDateL[1]  = 20240214
    $myDateL[2]  = 20240223
    $myDateL[3]  = 20240306
    $myDateL[4]  = 20240315
    // LONG exit dates
    $myExitL[1]  = 20240219
    $myExitL[2]  = 20240228
    $myExitL[3]  = 20240313
    $myExitL[4]  = 20240321
    LastElementL = 4
    // SHORT open dates
    $myDateS[1]  = 20240201
    $myDateS[2]  = 20240219
    $myDateS[3]  = 20240228
    $myDateS[4]  = 20240313
    // SHORT exit dates
    $myExitS[1]  = 20240214
    $myExitS[2]  = 20240223
    $myExitS[3]  = 20240306
    $myExitS[4]  = 20240315
    LastElementS = 4
    //
    SLpercent    = 2             //2%  stop loss
    //
    IF Not OnMarket THEN
       FOR i = 1 TO max(LastElementL,LastElementS)
          //LONG  trades
          IF i <= LastElementL THEN
             IF Date = $myDateL[i] THEN
                BUY 1 CONTRACT AT MARKET
                SET STOP %LOSS SLpercent
                break
             ENDIF
          ENDIF
          // SHORT trades
          IF i <= LastElementS THEN
             IF Date = $myDateS[i] THEN
                SELLSHORT 1 CONTRACT AT MARKET
                SET STOP %LOSS SLpercent
                break
             ENDIF
          ENDIF
       NEXT
    ENDIF
    //
    // exit LONG  trades
    IF LongOnMarket  THEN
       FOR i = 1 TO LastElementL
          IF Date = $myExitL[i] THEN
             SELL AT MARKET
             break
          ENDIF
       NEXT
    ENDIF
    // exit SHORT trades
    IF ShortOnMarket THEN
       FOR i = 1 TO LastElementS
          IF Date = $myExitS[i] THEN
             EXITSHORT AT MARKET
             break
          ENDIF
       NEXT
    ENDIF
    #228551 quote
    Laliberte
    Participant
    New
    Thanks for your reply!!!! Will enter dates from a period last year and modify so I can backtest it and see performance. Again thank you!! I need to show DAILY chart in selected market in PRT to get the backtest to work right or can I look at 15min chart and this will still run on Daily time frame? Regards Fredrik
    #228553 quote
    robertogozzi
    Moderator
    Master
    You can use it also on a 15-minute TF, but you need to add this line at the very beginning:
    Timeframe(Daily)
    and append this line at the end:
    Timeframe(default)
    #228557 quote
    PeterSt
    Participant
    Master
    Trying to follow this in itself nice topic … From of sentence 1 I have been confident that what Fredrik wants, can’t work. But please correct me where I am wrong : Fredrik wants to inject newly emerged “pivots”. That is fine in itself. But he won’t be able to do that without stopping the system and change the code (change the arrays). Fredrik, if that is fine with you, then all OK and Roberto’s code will (or may) work. N.b.: My idea of an initial response would have been : Yes, possible, if you first make an Indicator for it after all. Thus, if your code would be able to determine those “dates”, then all would work fine. But this is not really the gist of your topic plus it would be the same everybody else is trying to get right. 🙂 Regards, Peter
    #228559 quote
    Laliberte
    Participant
    New
    Is this correct understood?
    Timeframe(Daily)
    // LONG open dates
    $myDateL[1]  = 20240214
    REST of CODE here…
          ENDIF
       NEXT
    ENDIF
    Timeframe(default)
    #228568 quote
    Laliberte
    Participant
    New
    Roberto, I threw in a few dates from last year to run a backtest and test code. For some reason it looks like the system only open the LONG trades and not the short ones. I’ve been over the code my self a few times to try to spot the error but I cannot see it. So I kindly ask for your help or if anyone else can spot it? No modification of your original code except the dates.
    // LONG open dates
    $myDateL[1]  = 20230316
    $myDateL[2]  = 20230328
    $myDateL[3]  = 20230411
    $myDateL[4]  = 20230424
    // LONG exit dates
    $myExitL[1]  = 20230321
    $myExitL[2]  = 20230403
    $myExitL[3]  = 20230417
    $myExitL[4]  = 20230428
    LastElementL = 4
    // SHORT open dates
    $myDateS[1]  = 20230321
    $myDateS[2]  = 20230403
    $myDateS[3]  = 20230417
    $myDateS[4]  = 20230428
    // SHORT exit dates
    $myExitS[1]  = 20230328
    $myExitS[2]  = 20230411
    $myExitS[3]  = 20230424
    $myExitS[4]  = 20230508
    LastElementS = 4
    //
    SLpercent    = 2             //2%  stop loss
    //
    IF Not OnMarket THEN
       FOR i = 1 TO max(LastElementL,LastElementS)
          //LONG  trades
          IF i <= LastElementL THEN
             IF Date = $myDateL[i] THEN
                BUY 1 CONTRACT AT MARKET
                SET STOP %LOSS SLpercent
                break
             ENDIF
          ENDIF
          // SHORT trades
          IF i <= LastElementS THEN
             IF Date = $myDateS[i] THEN
                SELLSHORT 1 CONTRACT AT MARKET
                SET STOP %LOSS SLpercent
                break
             ENDIF
          ENDIF
       NEXT
    ENDIF
    //
    // exit LONG  trades
    IF LongOnMarket  THEN
       FOR i = 1 TO LastElementL
          IF Date = $myExitL[i] THEN
             SELL AT MARKET
             break
          ENDIF
       NEXT
    ENDIF
    // exit SHORT trades
    IF ShortOnMarket THEN
       FOR i = 1 TO LastElementS
          IF Date = $myExitS[i] THEN
             EXITSHORT AT MARKET
             break
          ENDIF
       NEXT
    ENDIF
    4-trades-help.jpg 4-trades-help.jpg
    #228570 quote
    Laliberte
    Participant
    New
    Was just thinking about one possibility. Could it be that the code see that I already have a position open (Long +1) and avoid to open a second one (short -1)? Or that my myDateL gets canceled by $myDateS and the $myExitL/$myExitS never gets run? Can I space out the Long and Short opening by 1 min to avoid this is this is the case? Any quick fix in the code? I’m not familiar with the logic so cannot tell the order of things.
    #228572 quote
    Laliberte
    Participant
    New
    Hi, I’ve read some more on the topic and I think I understand the solution now with MTF breakdown of the time of execution. I don’t need to complicate things As I LONG/SHORT on fixed dates so I can keep them running on two separate algos. One with LONG and one with SHORT. Was trying to create a separate code for LONG and a separate version for short but get error. Here is my LONG code version and get error on line 15:
    
    // LONG open dates
    $myDateL[1]  = 20230316
    $myDateL[2]  = 20230328
    $myDateL[3]  = 20230411
    $myDateL[4]  = 20230424
    // LONG exit dates
    $myExitL[1]  = 20230321
    $myExitL[2]  = 20230403
    $myExitL[3]  = 20230417
    $myExitL[4]  = 20230428
    LastElementL = 4
    SLpercent    = 2             //2%  stop loss
    //
    IF Not OnMarket THEN
       FOR i = 1 TO max(LastElementL)
          //LONG  trades
          IF i <= LastElementL THEN
             IF Date = $myDateL[i] THEN
                BUY 1 CONTRACT AT MARKET
                SET STOP %LOSS SLpercent
                break
             ENDIF
          ENDIF
    // exit LONG  trades
    IF LongOnMarket  THEN
       FOR i = 1 TO LastElementL
          IF Date = $myExitL[i] THEN
             SELL AT MARKET
             break
          ENDIF
       NEXT
    ENDIF
    Can you spot the error?
    #228575 quote
    PeterSt
    Participant
    Master
    The Max function requires two parameters – not one. Probably you don’t want to use Max at all there ? just LastElementL.
    robertogozzi thanked this post
Viewing 15 posts - 1 through 15 (of 22 total)
  • You must be logged in to reply to this topic.

Buy on certin dates, not on technical singal


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Laliberte @laliberte Participant
Summary

This topic contains 21 replies,
has 4 voices, and was last updated by Laliberte
1 year, 12 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/19/2024
Status: Active
Attachments: 2 files
Logo Logo
Loading...