Current Price in different timeframes

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #206710 quote
    fonsie
    Participant
    New

    Hi,

    First off i want to explain that i’m new to programming and to PRT so excuse me in advance. I’ve tried to find a solution now for a couple of days but have a hard time.

    I want to buy the close if the index is up more than X percent and above MA200 the same day and then sell the open the day after.

    The problem i’ve encountered is getting the current price before close and compare it from the previous days close. I came across the function timeframe and was thinking that using that in 5 min and update the price before close and then compare it with yesterdays price.

    Anyone have a good solution to this and what timeframe as default would you use for this solution?

    Sounds simple but i guess it’s not, thanks in advance.

    #206711 quote
    robertogozzi
    Moderator
    Master

    So, what do you want X% to be calculated on?

    #206712 quote
    fonsie
    Participant
    New

    I want X% to be calculated on yesterday’s close, just like they do with stocks.

    For example yesterday dax closed at 14070 and current price is 14255 and it is up 1.33%. So at today’s close if Dax is still up X% I want to buy to sell immediately at open tomorrow.

    #206713 quote
    fonsie
    Participant
    New
    //Current price in 5min timeframe
    timeframe(5 minutes, updateonclose)
    CurrentPrice = close
    
    timeframe(default)
    
    IF TIME = 210000 THEN
    buyconditiontime = 1
    ELSE
    buyconditiontime = 0
    ENDIF
    
    MA1 = Average[200](open)
    MA2 = open > MA1
    
    PercentageDiff = (CurrentPrice > close*1.015)
    
    IF PercentageDiff THEN
    dailypercentage = 1
    ELSE
    dailypercentage = 0
    ENDIF
    
    IF NOT LongOnMarket AND buyconditiontime AND MA2 AND dailypercentage THEN
    BUY 1 CONTRACTS AT MARKET
    ENDIF
    
    IF TIME = 090000 THEN
    sellconditiontime = 1
    ELSE
    sellconditiontime = 0
    ENDIF
    
    // Conditions to exit long positions
    If LongOnMarket AND sellconditiontime THEN
    SELL AT MARKET
    ENDIF

    This was what i thought would be correct but it’s not since i cant use timeframe 5 min then go back to daily candles.

    #206716 quote
    robertogozzi
    Moderator
    Master

    You can refer to daily constants Dopen(0), Dhigh(0), Dlow(0) and Dclose(0) to acces today’s daily data. Use (1) to refer to yesterday’s daily data, etc…

    #206718 quote
    JS
    Participant
    Senior

    Hi @fonsie

    You can use something like that…

    xPercent= 1.33
    
    xDifference = (DClose(0) - DClose(1))/DClose(1) *  100
    
    If NOT OnMarket and xDifference >= xPercent then
    Buy 1 contract at Market
    EndIf
    
    If NOT OnMarket and xDifference <= xPercent then
    SellShort 1 contract at Market
    EndIf
    #206719 quote
    fonsie
    Participant
    New
    Thanks for the support and tips, i’ll give it a try! Much appreciated!!
    #206722 quote
    fonsie
    Participant
    New
    // Conditions
    // Buy at 21:00 if index is above MA200 and up 1% same day.
    // Index must be above MA200 for positive trend
    MA1 = Average[200](close)
    MA2 = close > MA1
    
    xPercent= 1.33
    xDifference = (DClose(0) - DClose(1))/DClose(1) *  100
    
    // Time condition for Buy at 21:00
    IF TIME = 210000 THEN
    buyconditiontime = 1
    ELSE
    buyconditiontime = 0
    ENDIF
    
    // Buy 1 contract if the above indicators are true
    IF NOT LongOnMarket AND buyconditiontime AND MA2 AND xDifference >= xPercent THEN
    BUY 1 CONTRACTS AT MARKET
    ENDIF
    
    
    // Time condition for Sell at 9:00 the day after
    IF TIME = 090000 THEN
    sellconditiontime = 1
    ELSE
    sellconditiontime = 0
    ENDIF
    
    // Conditions to exit long positions
    If LongOnMarket AND sellconditiontime THEN
    SELL AT MARKET
    ENDIF
    
    It works but the results are not that good 🙂 Also i’m running it in 1hour timeframe but when backtesting i only get 30 total trades. Would like to expand the backtesting period, is it possible to do the same execution but in a daily timeframe?
    #206726 quote
    JS
    Participant
    Senior

    Hi @fonsie

     

    You can use the code in a daily time frame only then you can no longer use the times (Time)…

    The times (Time) are only triggered when you use a time frame of one hour…

    #206729 quote
    robertogozzi
    Moderator
    Master
    Actually you might use multiple time frames, so you can wait for some conditions to be met on a Daily TF and other ones on a smaller TF.
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

Current Price in different timeframes


General Trading: Market Analysis & Manual Trading

New Reply
Author
author-avatar
fonsie @fonsie Participant
Summary

This topic contains 9 replies,
has 3 voices, and was last updated by robertogozzi
3 years, 1 month ago.

Topic Details
Forum: General Trading: Market Analysis & Manual Trading
Language: English
Started: 01/03/2023
Status: Active
Attachments: No files
Logo Logo
Loading...