ProRealCode - Trading & Coding with ProRealTime™
Hi,
Many thanks in advance for the time you will take to read, understand and hopefully answer this post.
Well I am trying to program something I am often doing while I am manually trading.
The idea is simple :
I join hereafter the beginning of the corresponding piece of code. But I do not know how to manage the consecutive STOP ORDERS and STOP LOSS modifications and TARGET PROFITS as well…
If you could help, that would be great and you will enjoy in return a nice piece of winning trading methodology I think.
BR,
// Définition des paramètres du code
DEFPARAM CumulateOrders = False // Cumul des positions désactivé
// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
DEFPARAM FLATAFTER = 210000
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiée
noEntryBeforeTime = 080000
timeEnterBefore = time >= noEntryBeforeTime
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiée
noEntryAfterTime = 210000
timeEnterAfter = time < noEntryAfterTime
// Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiés
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions pour ouvrir une position acheteuse
c1 = (close CROSSES OVER 13000)
IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
BUY 4 CONTRACT AT MARKET
BUY 2 CONTRACTS AT CLOSE+30 STOP
ENDIF
// Conditions pour ouvrir une position en vente à découvert
c2 = (close CROSSES UNDER 12900)
IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
SELLSHORT 4 CONTRACT AT MARKET
SELLSHORT 2 CONTRACT AT CLOSE-30 STOP
ENDIF
// Stops et objectifs
SET STOP LOSS 50
SET TARGET PROFIT 75
It’s not possible to set a STOP LOSS/TARGET PROFIT at different points, it will always be the same for ALL the contracts you have bought, no matter when and at what price you traded them.
For breakeven and trailing stops you may find useful codes and suggestions here:
https://www.prorealcode.com/blog/learning/breakeven-code-automated-trading-strategy/
https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
Thanks for your answer Robertogozzi.
Maybe the fact that I wrote about “TARGET PROFIT” was a bit misleading in my explanation. Because you should understand from it that all my consecutive orders have the same exits points (but that points are changing according to new orders coming in) ; so I should rather speak about LIMIT/STOP ORDERS (in profit or loss) rather than about TARGET PROFIT/STOP LOSS.
You will find hereafter again my explanation with less misslideading words I hope :
The idea is simple :
I join hereafter the beginning of the corresponding piece of code. But I do not know how to manage the consecutive STOP and LIMIT orders
// Définition des paramètres du code
DEFPARAM CumulateOrders = False // Cumul des positions désactivé
// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
DEFPARAM FLATAFTER = 210000
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiée
noEntryBeforeTime = 080000
timeEnterBefore = time >= noEntryBeforeTime
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiée
noEntryAfterTime = 210000
timeEnterAfter = time < noEntryAfterTime
// Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiés
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions pour ouvrir une position acheteuse
c1 = (close CROSSES OVER 13000)
IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
BUY 4 CONTRACT AT MARKET
BUY 2 CONTRACTS AT CLOSE+30 STOP
ENDIF
// Conditions pour ouvrir une position en vente à découvert
c2 = (close CROSSES UNDER 12900)
IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
SELLSHORT 4 CONTRACT AT MARKET
SELLSHORT 2 CONTRACT AT CLOSE-30 STOP
ENDIF
…
If you could help, that would be great and you will enjoy in return a nice piece of winning trading methodology I think.
BR,
If you want different take profits for 4 contracts
then use 2 systems (same code) instead of 1 (or 4 instead of 1)
and also trailing stop
Thanks Eric.
And/but again to be clear, I DON’T “want” different take profits.
I would like my orders to have the same limit (the initial one) and a protection which evolves while number of orders is increasing.
I’m working on it, will be back within 20-30 minutes.
This what I managed to arrange
// Définition des paramètres du code
DEFPARAM CumulateOrders = False // Cumul des positions désactivé
// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
DEFPARAM FLATAFTER = 210000
ONCE MyStop = 50 * pipsize
ONCE MyProfit = MyStop * 1.5
ONCE MyLimit1 = 30 * pipsize //limit to set SL to breakeven (will only be checked at the CLOSING of the bar)
ONCE MyLimit2 = 50 * pipsize
ONCE Points2Keep = 10 * pipsize //can be ZERO (or even nagative if you want!)
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiée
noEntryBeforeTime = 080000
timeEnterBefore = time >= noEntryBeforeTime
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiée
noEntryAfterTime = 210000
timeEnterAfter = time < noEntryAfterTime
// Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiés
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// set stop Loss & Target to default values when not onmarket
IF Not OnMarket THEN
MyStop = 50 * pipsize
MyProfit = MyStop * 1.5
ENDIF
///////////////////////////////////////////////////////////////////////////
// this section is to make sure each pending order is placed at each new bar, since
// all pending orders expire after one bar!
// Here I use TRADEPRICE because:
// - Close would be different from the one when the trade was entered
// - Tradeprice is more accurate
//
IF LongOnMarket THEN
IF close >= (TRADEPRICE + MyLimit1) THEN
MyStop = TRADEPRICE + Points2Keep //set SL at breakeven + points to keep
ENDIF
IF CountOfPosition = 4 THEN
BUY 2 CONTRACTS AT TRADEPRICE + MyLimit1 STOP //2nd order
ENDIF
IF CountOfPosition = 6 THEN
BUY 1 CONTRACTS AT TRADEPRICE + MyLimit2 STOP //3rd order
ENDIF
SELL AT TRADEPRICE - MyStop STOP //Stop Loss
SELL AT TRADEPRICE + MyProfit LIMIT //Target Profit
ENDIF
IF ShortOnMarket THEN
IF close <= (TRADEPRICE - MyLimit1) THEN
MyStop = TRADEPRICE - Points2Keep //set SL at breakeven + points to keep
ENDIF
IF CountOfPosition = 4 THEN
SELLSHORT 2 CONTRACTS AT TRADEPRICE - MyLimit1 STOP//2nd order
ENDIF
IF CountOfPosition = 6 THEN
SELLSHORT 1 CONTRACTS AT TRADEPRICE - MyLimit2 STOP//3rd order
ENDIF
EXITSHORT AT TRADEPRICE + MyStop STOP //Stop Loss
EXITSHORT AT TRADEPRICE - MyProfit LIMIT //Target Profit
ENDIF
///////////////////////////////////////////////////////////////////////////
// Conditions pour ouvrir une position acheteuse
c1 = (close CROSSES OVER 13000)
IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THEN
BUY 4 CONTRACT AT MARKET //1st order
BUY 2 CONTRACTS AT CLOSE + MyLimit1 STOP //2nd order
BUY 1 CONTRACTS AT CLOSE + MyLimit2 STOP //3rd order
SELL AT CLOSE - MyStop STOP //Stop Loss
SELL AT CLOSE + MyProfit LIMIT //Target Profit
ENDIF
// Conditions pour ouvrir une position en vente à découvert
c2 = (close CROSSES UNDER 12900)
IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THEN
SELLSHORT 4 CONTRACT AT MARKET //1st order
SELLSHORT 2 CONTRACT AT CLOSE - MyLimit1 STOP //2nd order
SELLSHORT 1 CONTRACT AT CLOSE - MyLimit2 STOP //3rd order
EXITSHORT AT close + MyStop STOP //Stop Loss
EXITSHORT AT close - MyProfit LIMIT //Target Profit
ENDIF
I only tested it for syntax, let me know about logic issues.
I use pipsize, despite not necessary when trading DAX, to make code portable so that you can use it for other instruments letting ProOrder to take care of conversions.
Tx a lot Robertogozzi !
I will try it “against” my manual practice and will let you know in case I would see something not matching.
Comulate Order = true
Is it possible to change the code so you can manually enter PRT and change STOP LOSS / TARGET PROFIT as you do when trading manually or is there any other system setting that controls it?
Whenever you manually modify a trade opened by ProOrder, the strategy is immediately quit.
I changed CUMULATEORDERS to TRUE as suggested by Leo and also added a new line after lines 41 and 55 to accomodate for the 7 orders, in case on the following (just opening) bar ALL the three need to be entered.
// Définition des paramètres du code
DEFPARAM CumulateOrders = True // Cumul des positions désactivé
// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
DEFPARAM FLATAFTER = 210000
ONCE MyStop = 50 * pipsize
ONCE MyProfit = MyStop * 1.5
ONCE MyLimit1 = 30 * pipsize //limit to set SL to breakeven (will only be checked at the CLOSING of the bar)
ONCE MyLimit2 = 50 * pipsize //limit to set SL to breakeven (will only be checked at the CLOSING of the bar)
ONCE Points2Keep = 10 * pipsize //can be ZERO (or even nagative if you want!)
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiée
noEntryBeforeTime = 080000
timeEnterBefore = time >= noEntryBeforeTime
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiée
noEntryAfterTime = 210000
timeEnterAfter = time < noEntryAfterTime
// Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiés
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// set stop Loss & Target to default values when not onmarket
IF Not OnMarket THEN
MyStop = 50 * pipsize
MyProfit = MyStop * 1.5
ENDIF
///////////////////////////////////////////////////////////////////////////
// this section is to make sure each pending order is placed at each new bar, since
// all pending orders expire after one bar!
// Here I use TRADEPRICE because:
// - Close would be different from the one when the trade was entered
// - Tradeprice is more accurate
//
IF LongOnMarket THEN
IF close >= (TRADEPRICE + MyLimit1) THEN
MyStop = TRADEPRICE + Points2Keep //set SL at breakeven + points to keep
ENDIF
IF CountOfPosition = 4 THEN
BUY 2 CONTRACTS AT TRADEPRICE + MyLimit1 STOP //2nd order
BUY 1 CONTRACTS AT TRADEPRICE + MyLimit2 STOP //3rd order
ENDIF
IF CountOfPosition = 6 THEN
BUY 1 CONTRACTS AT TRADEPRICE + MyLimit2 STOP //3rd order
ENDIF
SELL AT TRADEPRICE - MyStop STOP //Stop Loss
SELL AT TRADEPRICE + MyProfit LIMIT //Target Profit
ENDIF
IF ShortOnMarket THEN
IF close <= (TRADEPRICE - MyLimit1) THEN
MyStop = TRADEPRICE - Points2Keep //set SL at breakeven + points to keep
ENDIF
IF CountOfPosition = 4 THEN
SELLSHORT 2 CONTRACTS AT TRADEPRICE - MyLimit1 STOP//2nd order
SELLSHORT 1 CONTRACTS AT TRADEPRICE - MyLimit2 STOP//3rd order
ENDIF
IF CountOfPosition = 6 THEN
SELLSHORT 1 CONTRACTS AT TRADEPRICE - MyLimit2 STOP//3rd order
ENDIF
EXITSHORT AT TRADEPRICE + MyStop STOP //Stop Loss
EXITSHORT AT TRADEPRICE - MyProfit LIMIT //Target Profit
ENDIF
///////////////////////////////////////////////////////////////////////////
// Conditions pour ouvrir une position acheteuse
c1 = (close CROSSES OVER 13000)
IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THEN
BUY 4 CONTRACT AT MARKET //1st order
BUY 2 CONTRACTS AT CLOSE + MyLimit1 STOP //2nd order
BUY 1 CONTRACTS AT CLOSE + MyLimit2 STOP //3rd order
SELL AT CLOSE - MyStop STOP //Stop Loss
SELL AT CLOSE + MyProfit LIMIT //Target Profit
ENDIF
// Conditions pour ouvrir une position en vente à découvert
c2 = (close CROSSES UNDER 12900)
IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THEN
SELLSHORT 4 CONTRACT AT MARKET //1st order
SELLSHORT 2 CONTRACT AT CLOSE - MyLimit1 STOP //2nd order
SELLSHORT 1 CONTRACT AT CLOSE - MyLimit2 STOP //3rd order
EXITSHORT AT close + MyStop STOP //Stop Loss
EXITSHORT AT close - MyProfit LIMIT //Target Profit
ENDIF
How to manage consecutive stop orders in a program ?
This topic contains 14 replies,
has 5 voices, and was last updated by
robertogozzi
7 years, 9 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 05/17/2018 |
| Status: | Active |
| Attachments: | No 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.