Buy or Sell at set time and price change on any market.

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #236742 quote
    Andre Vorster
    Participant
    Senior

    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.

    #236760 quote
    SnorreDK
    Participant
    Junior

    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
    
    #236761 quote
    phoentzs
    Participant
    Master

    And now I would like to see the backtest.

    #236772 quote
    Andre Vorster
    Participant
    Senior

    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.

    #236779 quote
    fifi743
    Participant
    Master

    line 14

     MarketOpenPrice = Close

    line 19 and line 27

    MarketOpenPrice cannot be different from close

    #236780 quote
    Andre Vorster
    Participant
    Senior

    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

    #236781 quote
    fifi743
    Participant
    Master

    line 14

     MarketOpenPrice =open

    #236783 quote
    Andre Vorster
    Participant
    Senior

    Still no trades opened.

    #236784 quote
    fifi743
    Participant
    Master

    line 13

    IF (Time = StartTime ) THEN
    line 14
    MarketOpenPrice = Close
    #236785 quote
    Andre Vorster
    Participant
    Senior

    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.

    #236791 quote
    fifi743
    Participant
    Master

    by adding at the top of the code

    defparam cumulateorders =false

    #236946 quote
    Andre Vorster
    Participant
    Senior

    It still opens trades at any time.
    Does not start at set time and end when set target profit is reached.

    Thanks you.

    #236948 quote
    robertogozzi
    Moderator
    Master

    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.

    #236957 quote
    Andre Vorster
    Participant
    Senior

    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?

    Trade-at-market-open.png Trade-at-market-open.png
    #236967 quote
    JS
    Participant
    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

Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.

Buy or Sell at set time and price change on any market.


General Trading: Market Analysis & Manual Trading

New Reply
Author
Summary

This topic contains 17 replies,
has 7 voices, and was last updated by Andre Vorster
1 year, 6 months ago.

Topic Details
Forum: General Trading: Market Analysis & Manual Trading
Language: English
Started: 08/27/2024
Status: Active
Attachments: 1 files
Logo Logo
Loading...