ProRealCode - Trading & Coding with ProRealTime™
// 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.
// 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?
Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.
Buy on certin dates, not on technical singal
This topic contains 21 replies,
has 4 voices, and was last updated by Laliberte
2 years, 8 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.