Just wondering if its possible to have 2 different stop loss strategies in a trading strat for longs and shorts and what is the syntax to acheive this . lets say this set of parameters for longs
SET STOP pLOSS 10
SET TARGET pPROFIT 15
SET STOP pTRAILING 20
and this set of parameters for shorts
SET STOP pLOSS 20
SET TARGET pPROFIT 30
SET STOP pTRAILING 25
Ive had a few shots at it but cant get it working in anyway
You can just set variables say TP and SL within your IF THEN BUY SELL like this:
IF your conditions THEN
BUY 1 Contract AT MARKET
TP = 10
SL = 5
ENDIF
IF your conditions THEN
SELLSHORT 1 Contract AT MARKET
TP = 20
SL = 10
ENDIF
SET TARGET pPROFIT TP
SET STOP pLOSS SL
You can just set variables say TP and SL within your IF THEN BUY SELL like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
IF your conditions THEN
BUY 1 Contract AT MARKET
TP = 10
SL = 5
ENDIF
IF your conditions THEN
SELLSHORT 1 Contract AT MARKET
TP = 20
SL = 10
ENDIF
SET TARGET pPROFIT TP
SET STOP pLOSS SL
|
Awesome thanks for that , would appear to be perfect for the job at hand , I really appreciate the level of knowledge and generosity in these forums
Brisvegas, ProOrder won’t allow you to use both STOP orders as in your lines 2-3, line 2 will be immediately overridden by line 3 (since it comes last) and it will not be reported as an error.
Vonasi‘s code is the correct way to accomplish different SLs and TPs for LONG and SHORT trades.
Despite not being 100% sure I want to point out that your code
IF your conditions THEN
BUY 1 Contract AT MARKET
TP = 10
SL = 5
ENDIF
might modify TP and SL values even when OnMarket, because if your conditions are true the BUY won’t be executed in case you forbade this from happening with DEFPARAM ComulateOrders = FALSE, but all lines will be evaluated and variables set accordingly. So, in case you have some trailing stop code that has previously changed those value, they might be restored to the original ones, thus producing unwanted results.
I suggest that the above code be changed to
IF your conditions AND Not OnMarket THEN
BUY 1 Contract AT MARKET
TP = 10
SL = 5
ENDIF
to make sure that when OnMarket the whole IF…ENDIF block is skipped.
Thanks for spotting that Robertogozzi. I always use AND NOT OnMarket but left is out for simplification as I always assume that everyone else knows what they are doing!
Many strategies can be confused by having variables set every time entry conditions are met even if the actual BUY and SELL does not take place due to CumulateOrders = false.
I do know what im doing but i got to say the resources for actually learning this code are very skinny outside this forum , I am a trader first and then a coder , i have the maths of all this down pat , its just half a line of syntax that slows me down and for the life of me i could find no documentation to assist me , I only ask here as a last resort unlike some , working it out for yourself is the best way if you can , trust me i have tried to work on some syntax for weeks before asking in here ..
LOL would be great if the people who build the website were as talented as the coders here . This should be front and centre and in a drop down menu on every page in this site . I am actually starting a bookmark folder for this place as there is little rhyme or reason to the cataloguing of content 😉 https://www.prorealcode.com/documentation/
Sorry Brisvegas I was not trying to imply that you do not know what you are doing. Sometimes the written word is not the best form of communication. I was actually criticising myself for always assuming too much. As my girlfriend always says Assume makes an ASS out of U and ME.
Six months ago I spent a week of my life trying to fathom out how SET STOP and SET TARGET worked within a code which is why I happily answered your opening post.
LOL would be great if the people who build the website were as talented as the coders here .
Sorry, I do my best…
….and your best is very very good even when you are supposed to be on holiday!
is it possible to write the code like this?
if longonmarket then
set stop ploss 50
set target pprofit 50
endif
is it possible to write the code like this?
|
|
if longonmarket then
set stop ploss 50
set target pprofit 50
endif
|
Yes. The SET instruction just places an order on the market that is in place until the position is closed or until the SET value is set to zero. I just find that having the SET instructions at the end of a code with a variable allows more elegant adjustment of stops and targets.
Thats what i thought Vonasi, thanks for confirming.
LOL would be great if the people who build the website were as talented as the coders here .
Sorry, I do my best…
Sorry Nicolas you do a great job overall no doubt
Hi everybody,
after many trials of placing the different set for loss&profit in long and short positions, I’ve done the following way and it seems to give backtests with less tick/tick values.
what do you think?
IF LONGONMARKET AND CLOSE>=(TRADEPRICE+(TPL*PIPSIZE)) OR CLOSE<=(TRADEPRICE-(SL*PIPSIZE)) THEN
SELL AT MARKET
ENDIF
IF SHORTONMARKET AND CLOSE<=(TRADEPRICE-(TP1*PIPSIZE)) OR CLOSE>=(TRADEPRICE+(SL1*PIPSIZE)) THEN
EXITSHORT AT MARKET
ENDIF