Hi,
I’m trying to get the below strategy to work but its closing on the wrong timeframe.
Sorry, but I can’t see an ‘insert PRT code’ button on the toolbar on this browser.
For example the backtest shows trades opening when the 1minute MA50 is crossed when want I’m hoping for is for a trade to open within a minute or so of the 1hr MA50 being crossed. The same issue is happening on trade exits. I’m hoping someone can easily spot an obvious error I’ve made! Thanks
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
//limits strategy to 1 trade per day
Once TradeON = 1
If IntraDayBarIndex = 0 then
TradeON = 1
Endif
If OnMarket or StrategyProfit <> StrategyProfit[1] Then
TradeON = 0
Endif
///indicators
TIMEFRAME(1hours)
SD1upper = Average[20](close)+1*std[20](close)
SD2upper = Average[20](close)+2*std[20](close)
SD3upper = Average[20](close)+3*std[20](close)
SD1lower = Average[20](close)-1*std[20](close)
SD2lower = Average[20](close)-2*std[20](close)
SD3lower = Average[20](close)-3*std[20](close)
Av20offset = Average[20](close)*0.9995//(offset so if price gets nearly there it will trigger)
Av50offset = Average[50](close)*0.9995
Av200offset = Average[200](close)*0.9995
Av20 = Average[20](close) //(offset so if price gets nearly there it will trigger)
Av50 = Average[50](close)
Av200 = Average[200](close)
/// UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW
SELLCONDITION=Av50offset
BUYCONDITION=SD3lower
//risk reward ratio
RRRatio=5
/// UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE
TIMEFRAME(default, updateonclose)
// Conditions to enter long positions
IF high crosses over SELLCONDITION and Not OnMarket THEN //and TradeON
SELLSHORT 0.52 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
IF low < BUYCONDITION THEN
EXITSHORT AT MARKET
ENDIF
SET STOP PLOSS ((SELLCONDITION-BUYCONDITION)/RRRatio)
Hi there,
// Conditions to enter long positions
IF high crosses over SELLCONDITION and Not OnMarket THEN //and TradeON
SELLSHORT 0.52 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
IF low < BUYCONDITION THEN
EXITSHORT AT MARKET
ENDIF
- Buying Long goes by Buy … at Market – Selling long goes by Sell … at Market.
- Even if you’d have that right, the two commands as how you put them, may cancel out each other because both conditions may be true at the same time and there is no prevention against that; for that, the second condition can contain an And OnMarket.
JSParticipant
Senior
Hi @Jaanboy321
I tried your code and the positions are opened and closed according to the correct time frame…
within a minute or so of the 1hr MA50 being crossed.
At Line 14, instead of …
TIMEFRAME(1hours)
Try
TIMEFRAME(1hour, updateonclose)
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
//limits strategy to 1 trade per day
Once TradeON = 1
If IntraDayBarIndex = 0 then
TradeON = 1
Endif
If OnMarket or StrategyProfit <> StrategyProfit[1] Then
TradeON = 0
Endif
///indicators
TIMEFRAME(1hour) //TIMEFRAME(1hour, updateonclose)
Donchmid = DonchianChannelCenter[50]
SD1upper = Average[20](close)+1*std[20](close)
SD2upper = Average[20](close)+2*std[20](close)
SD3upper = Average[20](close)+3*std[20](close)
SD4upper = Average[20](close)+4*std[20](close)
SD1lower = Average[20](close)-1*std[20](close)
SD2lower = Average[20](close)-2*std[20](close)
SD3lower = Average[20](close)-3*std[20](close)
SD4lower = Average[20](close)-4*std[20](close)
Av20offset = Average[20](close)*1.00005 //(offset so if price gets nearly there it will trigger)
Av50offset = Average[50](close)*1.00005
Av200offset = Average[200](close)*1.00005
Av20 = Average[20](close) //(offset so if price gets nearly there it will trigger)
Av50 = Average[50](close)
Av200 = Average[200](close)
/// UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW
BUYCONDITION=SD3lower
SELLCONDITION=Av20
TIMEFRAME(DEFAULT)
//risk reward ratio
RRRatio=5
/// UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE
// Conditions to enter long positions
IF low crosses under BUYCONDITION and TradeON and Not OnMarket THEN ///and TradeON
BUY 0.52 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
IF high crosses over SELLCONDITION THEN
SELL AT MARKET
ENDIF
SET STOP PLOSS ((SELLCONDITION-BUYCONDITION)/RRRatio)
Thanks everyone, much appreciated. Currently utilising this version to go long USDJPY (setting closing conditions according to the market) . Its not perfect but seems to do what I need and will experiment with your suggestions as required.