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.
So, what do you want X% to be calculated on?
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.
//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.
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…
JSParticipant
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
Thanks for the support and tips, i’ll give it a try!
Much appreciated!!
// 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?
JSParticipant
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…
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.