Coding required – Trade on specific day only via parameter

Forums ProRealTime English forum ProOrder support Coding required – Trade on specific day only via parameter

Viewing 15 posts - 1 through 15 (of 25 total)
  • #242897

    Good Morning,

    Can anyone help please with a piece of code.

    I have various parameters in some moving averages code and I would like to add a parameter which allows me to select which days, or all days, to run back test/trade on? I have very basic coding knowledge so a parameter with option of 0 or 1 to select or not would be ideal.

    can anyone help with that piece of code please ?

    thanks in advance

     

    #242909

    Do you want to run the backtest (i.e. trading set to on) on all or selected days, or do you want to have your averages either calculated normally or on selected days?

     

    #242933

    thanks for coming back.

    the averages are currently set for all week and that’s fine as triggers for buy and sell can only occur during a single days activity (EMA crosses)  so that’s fine as is.

    during my back testing i noticed that some days of the week performed far better than others so i want to be able to select certain days only for further back testing before implementation.

    long winded answer, apologies, so this parameter im asking for is a way to select either one day, multiple days or all days for execution.

    ideally something like all days of the week listed and i can put a 1 or 0 to indicate execute on that day if the strategy produces a trade – or something similar

    hope that makes a bit more sense.

    thanks in advance

    #242934

    This snippet allows you to select each day (0=disabled, 1=enabled as from the RED rectangle in the attached pic). The GREEN rectangle highlights the general trading times. The CYAN rectangle shows the areas that you can change at your convenience; leaving the default variables startT and endT the general times (as said earlier) will be used, but you can change them at will, for instance you can set the Tuesday line as “td2 = 1 AND DayRef = 2 AND TimeRef >= 150000 AND TimeRef <= endT“, or “td2 = 1 AND DayRef = 2 AND TimeRef >= 110000 AND TimeRef <= 140000“:

    then use the variable tdCond as a boolean variable (0, 1) to be added to your entry conditions to set when trading is enabled or disabled.

    Setting the general times from 000000 to 240000 will grant trading is enabled 24 hours.

     

    1 user thanked author for this post.
    #242936

    td stands for Time and Day, as you can select both trading days and trading times.

     

    1 user thanked author for this post.
    #242948

    many thanks for coming back so quickly, and for the code – much appreciated.

    will test and let you know.

    thanks again

    #242953

    thanks again for the code, works perfectly.

    however, could I please ask for one addition if you have time. the code allows you to select times to open a trade but I would like to add some code, for each individual day that forces a trade to close. example, if I’m trading a European index I want the trade to close at 4.30pm. or alternatively, i may only want to trade in the morning and close all trades on that day at 1pm for example.

    hope that makes sense

    thanks in advance

    #242966

    I modified my code to support only a CLOSING time, which can be different each day, if you need it:

    use tcCond as a condition (or as an additional condition to others of yours, if any)  to close a trade.

    I havent tested it.

     

    #243036

    many thanks for the code – much appreciated

    will test in next few days and let you know

    thanks again

    #243077

    hi Roberto,

    unfortunately the code does not work as intended.

    as an fyi – i currently use DefParam for flat before and flat after which works well for trading hours for the whole strategy but the idea is that i have all 5 days in one piece of code with potentially different open times and closing times and also slightly different inputs for about 7/8 parameters that i use to trigger a trade.

    the day code you sent has worked perfectly to isolate a day – thank you. i have back-tested each individual day and therefore have slightly different inputs for the parameters for each day so i just need at some point to combine all 5 individual days into 1 long piece of code. i currently have them as 5 different pieces of code.

    are you aware of a relatively simplistic way to combine? ie, mondays code at the top with its individual parameters and then move down to Tuesday etc – assuming the key is how you separate the code and move onto the next day when appropriate?

    hope that makes sense and apologies to keep coming back – but appreciate your help

    regards

     

    #243091

    I thinkit’s not possible, but I am not sure I did understand exactly what you need.

    If you post an example of how you are managing this case, I might try to arrange something different.

     

    #243129

    Apologies Roberto if I’m not being clear as I appreciate you trying to help.

    so I have a moving averages strategy that utilises various parameters and I have realised during back-testing that to get the best out of the strategy I need to have different numbers (such as stop loss, profit targets etc) for each day and that significantly enhances returns. you kindly shared the code for me to test individual days and I now have 5 pieces of code/strategies – 1 for each day.

    however, ideally, I want to combine all 5 together to have 1 strategy but utilising different parameters for each day. the issue I have is that I don’t have code to make this happen.

    the logic for the EMA’s once the day is confirmed does not change and that code works. so I need some code that says on a Monday look at Mondays parameters, and on Tuesday look at Tuesdays parameters etc

    does that make sense?

    regards

     

    #243134

    You can use arrays to accomplish that:

    1 user thanked author for this post.
    #243182

    Hi Roberto,

    that’s amazing – thank you so much for your help. my coding skills are very very basic so i will need some time to work with this and confirm it works or not.

    but thanks again – much appreciated.

    1 user thanked author for this post.
    #243190

    hello,

    I use the following way, both in backtest and then in live, to select the better days and hours of the week:

    1. at the beginning I write:    DD = (dayofweek=1 or dayofweek=2 or dayofweek=3 or dayofweek=4 or dayofweek=5)
    2.  in the buy/sell conditions I add:      and DD and (hour=>H1 and hour<=H2)
    3.   as variables I write        H1   and  H2
    4.   then I run the backtest.  Once I’ve decided all variables, then I run the backtest by filtering the day (in this rxample below only day 1) and do the same for the other days      DD = (dayofweek=1)   // or dayofweek=2 or dayofweek=3 or dayofweek=4 or dayofweek=5)
    5.   it’s a simple way to filter hours first and then days.
    6.   I consider only the days from Mondat to Friday, avoiding the weekend
    1 user thanked author for this post.
Viewing 15 posts - 1 through 15 (of 25 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login