Is it possible to buy at open?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #86011 quote
    Harrys
    Participant
    Junior

    Im wondering if there is a way to buy/sell exactly at the open on a lower timeframe strategy.

    The following code works at backtest but not when running demo.

     

    
    /-------------------------------------------------------------------------
    // Main code : buyopen Dow 5min
    //-------------------------------------------------------------------------
    DEFPARAM Cumulateorders = False
    DEFPARAM flatafter  = 160000
    
    
    
    
    
    TIMEFRAME(5 minutes, default)
    
    
    
    buytime = currenttime > 152600 and currenttime < 153400
    
    
    size = 1
    
    
    
    
    If buytime THEN
    Buy size contracts at market
    ENDIF
    #86014 quote
    Vonasi
    Moderator
    Master

    On a 5 minute chart if you want to buy at 1530 then just write:

    if time = 153000 then 
    buy 1 contract at market
    endif
    

    Does it buy in demo at a different time if so maybe it is your chart time settings?

    Harrys thanked this post
    #86038 quote
    Harrys
    Participant
    Junior

    Thank you for your answer!

    I want to execute an order at exactly the open. But i want one parameter to be where the open is. For example over or below ma200.

    The code below works in backtest but i fear that it will execute first 1 candle after the open. Guess il have to see tomorrow.

     

     

    DEFPARAM PRELOADBARS = 1000
    DEFPARAM Cumulateorders = False
    DEFPARAM flatafter  = 160000
    timeframe(daily)
    
    ma200=average[200]
    
    
    TIMEFRAME(5 minutes, default)
    
    
    if time = 153000 and open[0]>ma200 then
    buy 1 contract at market
    endif
    #86039 quote
    Vonasi
    Moderator
    Master

    All decisions are made by any code at the close of a candle. So on your 5 minute chart the candle that opens at 1525 and closes at 1530 is the one you want to use. That candle has a TIME = 153000 and an OPENTIME = 152500.

    So if you use OPEN[0] < AVERAGE[200] then it will be the opening price at 1525 that is used for the comparison. You cannot use the opening price of the next candle as all decisions are made at the close of a candle. This is only an issue in markets where there is a big gap between the close of the 1525 candle and the open of the 1530 candle.

    I hope this makes things clearer!

    Note: Also if you are not using MTF then you do not need to put the TIMEFRAME command in your code. just launch it on 5 min time frame and it will make decisions based on this.

    Harrys and Nicolas thanked this post
    #86059 quote
    Harrys
    Participant
    Junior

    So tried the code on the dax open now and it didnt execute, still works on backtests. Im using custom timezone UTC +01:00 with custom trading hours 09:00:00-17:30:00

    im guessing that i have to change the custom time to 08:55:00 for it to work.  since the open[0]>ma200 is outside the timezones

    DEFPARAM PRELOADBARS = 1000
    DEFPARAM Cumulateorders = False
    DEFPARAM flatafter  = 173000
    timeframe(daily)
    
    ma200=average[200]
    
    
    TIMEFRAME(5 minutes, default)
    
    
    if time = 090000 and open[0]>ma200 then
    buy 1 contract at market
    endif
    
    
    if time = 090000 and open[0]<ma200 then
    sellshort 1 contract at market
    endif
    
    set stop ploss 50
    set target pprofit 50
    #86062 quote
    Vonasi
    Moderator
    Master

    I don’t use any custom hours as it just adds an extra level of confusion in my mind so I just use my memory to know when markets open and close on a UTC0000 chart so I have to guess that you are right about needing a 0855 candle as if you have set the chart to display from 0900 onwards then you have eliminated your decision candle which opens at 0855 and closes at 0900.

    Harrys thanked this post
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Is it possible to buy at open?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Harrys @harrys Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by Vonasi
7 years, 3 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/29/2018
Status: Active
Attachments: No files
Logo Logo
Loading...