Hello.
I don’t know coding to write the code for this idea. I will appreciate help on this one please.
The idea I have.
As you know when the US market opens the volumes are high and moves fast in any direction.
Have code that will open 2 x short OR 2 x long positions depending on the price direction.
The positions opens on price change at a set time(market open time) Or opens positions at a candle count after market open.
With the time set or candle count one can start trades on any market and on any time frame even on a 10sec time frame.
Can apply this strategy on any market on any time frame. The market and time frame can then be any of your choice.
If this can work this can work in Bull and Bear market.
On market open and the price goes higher that what the price was at open. Open 2 x Long positions.
LONG:
1.) Buy 1 contract – set profit for $20 or 20 Points (20 = example/variable)
Close this trade automatically when set profit is reached.
Set Trailing stop at $10 or 10 points (10 = example/variable)
2.) Buy 1 contract with $10/10 points trailing stop so this one can run (10 points = example)
SHORT:
The same applies as the above if the price moves lower on market open.
1.) Short 1 contract – set profit for $20 or 20 Points (20 = example/variable)
Close this trade when profit set is reached.
Set Trailing stop at $10 or 10 points (10 = example/variable)
2.) Short 1 contract with $10/10 points trailing stop so this one can run (10 points = example)
Thank you.
This is what chatgtp made of it.
// Strategy Parameters
TimeMarketOpen = 093000 // Market open time in HHMMSS format (e.g., 09:30:00)
TP1 = 20 // Take Profit for the first position in points
TS1 = 10 // Trailing Stop for the first position in points
TS2 = 10 // Trailing Stop for the second position in points
StartTime = TimeMarketOpen
CandleCountAfterOpen = 0 // Set to 0 to trade immediately at open
// Initialize
ONCE MarketOpenPrice = 0
// Determine the market open price or wait for the candle count
IF (Time = StartTime AND CurrentMinute = CandleCountAfterOpen) THEN
MarketOpenPrice = Close
ENDIF
// Determine the direction and enter positions
IF (MarketOpenPrice > 0) THEN
IF Close > MarketOpenPrice THEN
// Price is higher than market open, go long
BUY 1 CONTRACT AT MARKET
SET TARGET pPROFIT TP1
SET STOP TRAILING TS1
BUY 1 CONTRACT AT MARKET
SET STOP TRAILING TS2
ELSIF Close < MarketOpenPrice THEN
// Price is lower than market open, go short
SELLSHORT 1 CONTRACT AT MARKET
SET TARGET pPROFIT TP1
SET STOP TRAILING TS1
SELLSHORT 1 CONTRACT AT MARKET
SET STOP TRAILING TS2
ENDIF
ENDIF
And now I would like to see the backtest.
Thank you for this.
May be I’m missing something but the code does not open any positions. I changed the TimeMarketOpen, TP & TS values but still no results.
Used the code like one would test any strategy. My PRT is working and have no issues there.
line 14
MarketOpenPrice = Close
line 19 and line 27
MarketOpenPrice cannot be different from close
fifi743
Thank you for the input.
Your comment is the same as in the original code. What do you suggest?
line 14 – MarketOpenPrice = Close
What do you mean? I don’t understand (because I can’t code)
line 19 and line 27
MarketOpenPrice cannot be different from close
Thank you
line 13
IF (Time = StartTime ) THEN
line 14
MarketOpenPrice = Close
Ah.! Now it takes positions.
Thank you.
But it keeps on opening positions. I want it just to take positions and then stop.
Example: When the market opens it should take positions buy/sell then exit the code/market.
by adding at the top of the code
defparam cumulateorders =false
It still opens trades at any time.
Does not start at set time and end when set target profit is reached.
Thanks you.
Try this version:
DEFPARAM CumulateOrders = FALSE
// Strategy Parameters
TimeMarketOpen = 093000 // Market open time in HHMMSS format (e.g., 09:30:00)
TP1 = 20 // Take Profit for the first position in points
TS1 = 10 // Trailing Stop for the first position in points
TS2 = 10 // Trailing Stop for the second position in points
StartTime = TimeMarketOpen
CandleCountAfterOpen = 0 // Set to 0 to trade immediately at open
// Initialize
MarketOpenPrice = 0
// Determine the market open price or wait for the candle count
IF (Time = StartTime) then//AND CurrentMinute = CandleCountAfterOpen) THEN
MarketOpenPrice = open
ENDIF
// Determine the direction and enter positions
IF (MarketOpenPrice > 0) THEN
IF Close > MarketOpenPrice THEN
// Price is higher than market open, go long
BUY 2 CONTRACT AT MARKET
SET TARGET pPROFIT TP1
//SET STOP TRAILING TS1
//BUY 1 CONTRACT AT MARKET
SET STOP TRAILING TS2
ELSIF Close < MarketOpenPrice THEN
// Price is lower than market open, go short
SELLSHORT 2 CONTRACT AT MARKET
//SET TARGET pPROFIT TP1
//SET STOP TRAILING TS1
//SELLSHORT 1 CONTRACT AT MARKET
//SET STOP TRAILING TS2
ENDIF
ENDIF
the last SET TARGET and SET STOP always override the previous ones. It’s not possible to set two different STOPs or TARGETs.
This one works. Thank you Robert.
Profit not as much as I thought it might be but I’ll keep on playing with the TS and TP and see which combination gives best results.
Question:
See att image. What are the differences between the highlighted amounts?
JSParticipant
Senior
The difference is simply because of the “Starting Capital” with which the “backtest” starts… (you can set up starting capital)
Starting capital=100 (your equity curve start at 100)
Profit=158,80
Equity(curve): 258.80