Hi,
I”ve spent some considerable time trying to set ONCE a profit and Take profit as a multiple of say an ATR(14) but without success. The code runs without error but doesn’t do anything. Can you help please?
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
DEFPARAM FLATBEFORE = 070000 // Flat before this time
DEFPARAM FLATAFTER = 205000 // Flat after this time
//use ATR14 to set targets and stops
//Set Stop and Targets
//reset stop and target
IF NOT ONMARKET THEN
St= 0
Tp= 0
// set Stop and target
//ONCE St = ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
//ONCE Tp = ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
//prices to enter trades
BuyPrice = High[0]+2*PointSize
SellPrice = Low[0]-2*PointSize
// Conditions to enter long positions
indicator1 = Stochastic[14,5](close)
c1 = (indicator1 < 25)
indicator2 = Average[20](close)
indicator3 = Average[50](close)
c2 = (indicator2 > indicator3)
IF c1 AND c2 THEN
BUY 1 PERPOINT AT BuyPrice STOP
ENDIF
// set stop
IF LONGONMARKET THEN
St = BuyPrice - ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
Tp = BuyPrice + ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
ENDIF
// Conditions to exit long positions
indicator4 = Stochastic[14,5](close)
c3 = (indicator4 CROSSES UNDER 75)
IF c3 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator5 = Stochastic[14,5](close)
c4 = (indicator5 > 75)
indicator6 = Average[50](close)
indicator7 = Average[20](close)
c5 = (indicator6 > indicator7)
IF c4 AND c5 THEN
SELLSHORT 1 PERPOINT AT SellPrice STOP
ENDIF
// set stop
IF SHORTONMARKET THEN
St = SellPrice + ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
Tp = SellPrice - ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
ENDIF
// Conditions to exit short positions
indicator8 = Stochastic[14,5](close)
c6 = (indicator8 CROSSES OVER 25)
IF c6 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets
// STOP AND TARGET
SET STOP LOSS St
SET TARGET PPROFIT Tp
ENDIF
You should compute them like this, without making any conversion to POINTS and set them as decimal values, not price ones:
//stoploss and takeprofit for long positions
St = ROUND((AverageTrueRange[14](CLOSE))*2)
Tp = ROUND((AverageTrueRange[14](CLOSE))*2)
and use these instructions to set them:
SET STOP LOSS St
SET TARGET PROFIT Tp
Without the “P” which is made to set POINTS/PIPS stoploss and takeprofit
If you are using the strategy on EURUSD for example, you should expect St and Tp to be : 0.0016 , and that’s the kind of value that LOSS and PROFIT need.
Thanks for looking at this Nicolas, however, neither the stops nor take profits are working on the DAX. e.g. on the tester a buy entry at 9:30, yesterday, was made at 9930.3. By 11:50 this was in profit of 72.5 points it was finally closed at 20:50 at 9943.8. Not sure which ATR the program will use but the entry bar was 15.30219 and the previous ATR 16.4528.
Not sure what actually closed the trade as the stochastic crossed under 75 four times before closing the trade.
I enclose my revised code for reference would appreciate your consideration.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
DEFPARAM FLATBEFORE = 070000 // Flat before this time
DEFPARAM FLATAFTER = 205000 // Flat after this time
//use ATR14 to set targets and stops
//Set Stop and Targets
//reset stop and target
IF NOT ONMARKET THEN
St= 0
Tp= 0
//prices to enter trades
BuyPrice = High[0]+2*PointSize
SellPrice = Low[0]-2*PointSize
// Conditions to enter long positions
indicator1 = Stochastic[14,5](close)
c1 = (indicator1 < 25)
indicator2 = Average[20](close)
indicator3 = Average[50](close)
c2 = (indicator2 > indicator3)
IF c1 AND c2 THEN
BUY 1 PERPOINT AT BuyPrice STOP
ENDIF
// set stop
IF LONGONMARKET THEN
St = ROUND((AverageTrueRange[14](CLOSE))*2)
Tp = ROUND((AverageTrueRange[14](CLOSE))*2)
ENDIF
// Conditions to exit long positions
indicator4 = Stochastic[14,5](close)
c3 = (indicator4 CROSSES UNDER 75)
IF c3 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator5 = Stochastic[14,5](close)
c4 = (indicator5 > 75)
indicator6 = Average[50](close)
indicator7 = Average[20](close)
c5 = (indicator6 > indicator7)
IF c4 AND c5 THEN
SELLSHORT 1 PERPOINT AT SellPrice STOP
ENDIF
// set stop
IF SHORTONMARKET THEN
St = ROUND((AverageTrueRange[14](CLOSE))*2)
Tp = ROUND((AverageTrueRange[14](CLOSE))*2)
ENDIF
// Conditions to exit short positions
indicator8 = Stochastic[14,5](close)
c6 = (indicator8 CROSSES OVER 25)
IF c6 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets
// STOP AND TARGET
SET STOP LOSS St
SET TARGET PROFIT Tp
ENDIF
On DAX, please try using PLOSS and PPROFIT instead.
Hi Nicolas,
Same result I’m afraid. Any other suggestions please?
John
Lines 69 and 70 should be at the end of code, not above your last endif.
Hi Nicolas, been away for a couple of days but when I make this change I get the message
Syntax error line 72 character 1
So any other ideas please?
John
I have not tested your code, but I think that it should end like this:
ENDIF
SET STOP LOSS St
SET TARGET PROFIT Tp
Make sure that any IF are correctly ended with an ENDIF.
Thanks Nicolas found an if without an endif
Hi everybody,
I hope you will appreciate this TV script : https://www.tradingview.com/script/ofwFdl2A-stoch-supertrd-atr-200ma/
It features:
– ATR StopLoss using the price + an ATR multiplier
-ATR TakeProfit using the price + the same ATR mult * 2 so it creates a 1:2 Risk/Reward
– a trade breakeaven stop
The sl/tp are fixed once the trade is launched and do not look to update along the trade.
All these gives a better maxDD, specially the breakeven feature.
Cheers 🙂
I reckon you should open a new / separate Topic and post the full script off Trading View and see if some kind soul might convert to PRT for you / us? 🙂
Hi GraHal 🙂
I’ll try to remember to do so within the week but first I will give it a tr by my self maybe I can do it 😀
Have a nice week