Hi,
I’m quite new here, but I have the following questions. I’m creating a code, but I can not figure out how to create the following piece of code:
- Every trading day I would like to know what the highest market price is between the previous day 21.00 and the current day 04.00 hours. And I would like to know what the lowest market price is between the previous day 21.00 and the current day 04.00 hours.
- Trading hours are from 06.30 hours until 21.00 hours
- I only want to trade when the 1-hour candle closes above the highest market price mentioned before or when the 1-hour candle closes under the lowest market price mentioned before.
- When the 1-hour candle closes above the highest market price, we take a long trade with a stop loss of 230 pips and a profit stop of 800 pips
- When the 1-hour candle closes below the lowest market price, we take a short trade with a stop loss of 230 pips and a profit stop of 800 pips
- We will only trade once a day. So after the first trade, the system should stop.
Thanks guys!!!
WingParticipant
Veteran
First question:
Once myhigh=0
Once mylow=0
Once tradedtoday=0
If time=040000 then
tradedtoday=0
MyHigh=Highest[7](high)
MyLow=Lowest[7](low)
Endif
Second question: Use “defparam flatafter/flatbefore”. Eg. “Defparam flatafter 210000”. Must be the first lines in your code.
Third question:
If close>myhigh and tradedtoday=0 then
Buy 1 lot at market
tradedtoday=1
Endif
Then set your SL/TP. Does that help you on the way?
Thanks a lot, Wing. I created the following code, but it is not working. Would you be able to look into it, maybe you have a solution for me? Thanks in advance…
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the "FLATBEFORE" time.
DEFPARAM FLATBEFORE = 063000
// Cancel all pending orders and close all positions at the "FLATAFTER" time
DEFPARAM FLATAFTER = 210100
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 1 OR OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// check on highest and lowest price
Once myhigh=0
Once mylow=0
Once tradedtoday=0
If time=040000 then
tradedtoday=0
MyHigh=Highest[7](high)
MyLow=Lowest[7](low)
Endif
// buy when above highest high
If close>myhigh and tradedtoday=0 AND not daysForbiddenEntry THEN
Buy 1 lot at market
tradedtoday=1
Endif
// sell when below lowest low
If close>mylow and tradedtoday=0 AND not daysForbiddenEntry THEN
Buy 1 lot at market
tradedtoday=1
Endif
// Stops and targets
SET STOP pLOSS 230
SET TARGET pPROFIT 800
Hi PJ, Wing is offline, so thought I’d jump in (to keep you going 🙂 ) … is this maybe the problem?
// sell when below lowest low
If close>mylow and tradedtoday=0 AND not daysForbiddenEntry THEN
Buy 1 lot at market
tradedtoday=1
Endif
From your ‘non-code strategy’ below, it looks like it should be …
- When the 1-hour candle closes below the lowest market price, we take a short trade with a stop loss of 230 pips and a profit stop of 800 pips
// sellshort when below lowest low
If close<mylow and tradedtoday=0 AND not daysForbiddenEntry THEN
SellShort 1 lot at market
tradedtoday=1
Endif
Maybe “Sellshort” instead of buy in line 30?
Edit: You were faster Grahal 🙂
Thank you guys. Changed it, but still no results 🙁
Close cannot be superior to myHigh or inferior to myLow, you must referred to their values one bar ago, otherwise Close is myHigh or myLow and testing a value superior or inferior to itself has no sense.
// sell when below lowest low
If close>mylow[1] and tradedtoday=0 AND not daysForbiddenEntry THEN
Buy 1 lot at market
tradedtoday=1
Endif
// sellshort when below lowest low
If close<mylow[1] and tradedtoday=0 AND not daysForbiddenEntry THEN
SellShort 1 lot at market
tradedtoday=1
Endif
Andyswede we were both correct, but not quite correct enough! 🙂
Hi guys,
Thanks again, but I still dont have any results. Something is wrong. I did some manual backtesting and there should be 17 trades in june 2017. You have any clues left? Thanks again for helping me out, I really appreciate it.
Hi PJ
Please add your code at latest up to date version on here then I will run it on my platform,
GraHal
Thanks GraHal,
This is the code I use right now.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the "FLATBEFORE" time.
DEFPARAM FLATBEFORE = 063000
// Cancel all pending orders and close all positions at the "FLATAFTER" time
DEFPARAM FLATAFTER = 210000
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 1 OR OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// check on highest and lowest price
Once myhigh=0
Once mylow=0
Once tradedtoday=0
If time=040000 then
tradedtoday=0
MyHigh=Highest[7](high)
MyLow=Lowest[7](low)
Endif
// buy when above highest high
If close>myhigh[1] and tradedtoday=0 AND not daysForbiddenEntry THEN
Buy 1 lot at market
tradedtoday=1
Endif
// sellshort when below lowest low
If close<mylow[1] and tradedtoday=0 AND not daysForbiddenEntry THEN
sellshort 1 lot at market
tradedtoday=1
Endif
// Stops and targets
SET STOP pLOSS 230
SET TARGET pPROFIT 800
You say … . I did some manual backtesting and there should be 17 trades in june 2017 …. what does manual backtest mean, how did you do it??
Aren’t you restricting potential with time= 040000 ??
Results attached since 1 Apr 13 (100,000 bars?) on DAX at 1 hour TF with time = 040000 lower curve. With time = 090000 upper curve … rest of the code same as yours above.
I did the backtesting myself using graphs and no software.
Attached you will find the results with a 250 pips profit stop and a 230 pips stoploss
Your code above shows 800 profit stop, but your manual results above are based on 250 profit stop?
So is there still a problem or are you good to continue now?
If there is still a problem, is it that you want PRT backtest results to match your manual backtest results? Have you got same parameters in each scenario, including spread etc?