ProRealCode - Trading & Coding with ProRealTime™
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
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.
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
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
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.
// 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
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
Timeframe(Daily)
and append this line at the end:
Timeframe(default)
Timeframe(Daily)
// LONG open dates
$myDateL[1] = 20240214
REST of CODE here…
ENDIF
NEXT
ENDIF
Timeframe(default)
// 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
// 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?
Buy on certin dates, not on technical singal
This topic contains 21 replies,
has 4 voices, and was last updated by Laliberte
1 year, 12 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 02/19/2024 |
| Status: | Active |
| Attachments: | 2 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.