Hi there, I am trying to code a strategy that involves collecting the highest high price and the lowest low of a particular time range in the morning. After this time, there is another period of 2 hour where if the price closes above this range, a long is opened with a stop loss at the lowest low point; and vice versa if close is below the lowest low.
Additionally, if the trade is stopped out; then an opposing trade is opened in the opposite direction, with the stop set again at the opposing end of the range.
I have tried several different ways of coding the entry but no matter what no trades are opened; or if they are, they are very inconsistent and do not conform to the rules and appear to be random and I cannot figure out which part of the code is incorrect and was hoping someone may be able to suggest what I am doing wrong?
Thank you
// Variables
DEFPARAM CumulateOrders = False // Disable accumulation of positions
targetPoints = 50 // Set global target price to 50 points
tradeStart = 1100 // Set start time for entering trades
tradeEnd = 1300 // Set end time for entering trades
sessionEnd = 1800 // Set end time for closing open positions
rangeStart = 0900 // Define range start time
rangeEnd = 1100 // Define range end time
If IntraDayBarIndex = 0 then
highestPrice = 0
lowestPrice = 999999
endif
// Calculate highest and lowest prices between 00:00 and 07:00
IF time >= rangeStart AND time <= rangeEnd THEN
highestPrice = max(high,highestPrice)
lowestPrice = min(low,lowestPrice)
ENDIF
// Long entry condition
IF time >= tradeStart AND time <= tradeEnd AND close > highestPrice THEN
buy at market
SET STOP stopLossLong Limit
SET TARGET PROFIT targetPoints
ENDIF
// Short entry condition
IF time >= tradeStart AND time <= tradeEnd AND close < lowestPrice THEN
sellshort at market
SET STOP stopLossShort limit
SET TARGET PROFIT targetPoints
ENDIF
// Stop loss and target for long positions
IF LONGONMARKET THEN
sell at stopLossLong stop
sell at targetPoints limit
IF barindex - tradeindex = 1 AND low[1] <= stopLossLong THEN
sellshort at market
stopLossShort = highestPrice
SET TARGET PROFIT targetPoints
ENDIF
ENDIF
// Stop loss and target for short positions
IF SHORTONMARKET THEN
exitshort at stopLossShort stop
exitshort at targetPoints limit
IF barindex - tradeindex = 1 AND high[1] >= stopLossShort THEN
buy at market
stopLossLong = lowestPrice
SET TARGET PROFIT targetPoints
ENDIF
ENDIF
IF time >= sessionEnd THEN
sell at market
exitshort at market
ENDIF
Your times are wrong. 9:00 a.m. is written as 090000.
Hi, thank you for your response, I have corrected all the time but no trades are still opening. Is there anything else wrong with the logic that you can see?
no matter what no trades are opened;
Have you checked Closed Trades in the Detailed Report? You may be opening and closing trades in the same bar and so they do not show under the equity curve?
Also, try replacing Line 25 and Line 32 with Set Stop pLoss x and also comment out Line 36 to the end … just to get it going / taking trades.
You can then uncomment back in to see which part of the code is stopping the strategy working correctly.
Also include below at the end of your code to see when it is True.
GRAPH other variables & conditions to see how the code is / is not working.
A few ideas to try anyway, hope they help?
GRAPH close > highestPrice
@kg6450
Maybe my topic will interest you because they use more or less the same idea with a condition that exceeds the highest and in a certain time interval
You can go directly to the last message even if I haven’t finished yet, it might help you