New to learning this code and trying to build a strategy that only trades during specific hours. But I cannot seem to get the code quite right, can anyone help me please?
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
Defparam FLATAFTER = 235900
// Conditions to enter long positions
indicator1 = MACDline[12,26,9](close)
indicator2 = ExponentialAverage[9](MACDline[12,26,9](close))
c1 = (indicator1 CROSSES OVER indicator2)
IF (Time >= 175900) AND (Time <= 205900) AND c1 THEN
BUY 1 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
IF (Time >= 235900) THEN
SELL AT MARKET
ENDIF
// Stops and targets
Your code seems right, except that you can delete the lines 14 to the end because you use FLATAFTER instruction that will close immediately all orders on market.
If you plan to trade this strategy on a daily basis, that will not work because trade will never be executed (due to time limitations you made), maybe that’s where is your trouble with this code.
Nicolas thank you for replying. The problem I am having is getting the trade to exit. In backtesting I can see one Buy, but then it does not exit at 2359hrs and therefore no other trades can take place.
On which timeframe do you test it?
Nicolas for whatever reason my code started working without much changes by me. The trades now close and my latest version is:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
Defparam FLATAFTER = 083000
// Conditions to enter long positions
indicator1 = MACDline[12,26,9](close)
indicator2 = ExponentialAverage[9](MACDline[12,26,9](close))
c1 = (indicator1 CROSSES OVER indicator2)
IF (Time >= 054500) AND (Time <= 0715000) AND c1 THEN
BUY 2 PERPOINT AT MARKET
ENDIF
// Conditions to enter short positions
indicator1 = MACDline[12,26,9](close)
indicator2 = ExponentialAverage[9](MACDline[12,26,9](close))
c2 = (indicator1 CROSSES UNDER indicator2)
IF c2 THEN
SELLSHORT 1 PERPOINT AT MARKET
ENDIF
// Stops and targets
SET STOP %LOSS 0.4
Set target PPROFIT 18