Hi there,
I’m stuck with below idea. Example is being made with the CAC40.
In order to decide whether I want to go long, short or both at the same time with the same system, I would like to create a proxy of the current trend.
The idea is this; at the open of each day, so at 9am, I calculate the ongoing trend (up or down) by applying below formula.
It only needs to be calculated ONCE so I figure that if I code it like this:
Once Myhour = Time = 090000 then
If (((Open - Dclose(1))/Dclose(1))*100) > X
buy 1 contract at market
etc.
Elsif (((Open - Dclose(1))/Dclose(1))*100) < -X
Sellshort 1 contract at market
etc.
endif
The only problem (if above code is correct) is that I would like to define the close of the previous day at a specific hour, in this example at 173000 (= Dclose(1)) but don’t know how to code it!
Could anyone offer a suggestion? (Above idea is not meant to be used as an indicator!)
I know that there is the possibility of working with a customclose by changing the hours in the chartsettings but then, you’re limited to trading only within that timeframe which is not the idea I had in mind.
Thanks for the replies!
It should read:
IF Time = 090000 then
n = (((Open - Dclose(1))/Dclose(1))*100)
ENDIF
If n > X
buy 1 contract at market
etc.
Elsif n < -X
Sellshort 1 contract at market
etc.
endif
You don’t need to use ONCE in your code. ONCE is simply an instruction that means that anything after it is not done or read again after the very first bar the code is set running on.
You can save a value at any time simply with the following:
if time = 173000 then
dayclose = close
endif
Then just use dayclose instead of Dclose(1) in your calculations.
Obviously you have to run your strategy only on timeframes that have a 173000 candle.
@Vonasi:
if time = 173000 then
dayclose = close
endif
Won’t there be a problem with these lines as time = 173000 is the time of the close of the previous day? And since today time = 173000 hasn’t happened yet when I use it in my formula at 9am, it would return “Idunnowhat”…
Also, the formula (((Open – Dclose(1))/Dclose(1))*100) only needs to calculated one time at 9am; the result of the formula will tell my system whether to go only long or short during that day. There are going to be multiple openings and closings of long or short positions during the day. That’s why I used a ONCE function…
I think you are confusing yourself! Once a variable value is set then it remains at that value until it is changed. So my code simply stores the close at 1730 everyday and the value remains fixed until the next 1730 comes along. So at 0900 the value is the close at the previous 1730.
Add my code to Roberto’s code and add a little extra IF THEN to ensure we have had at least one 1730 close and that’s it.
if time = 173000 then
dayclose = close
endif
if dayclose <> 0 then
IF Time = 090000 then
n = (((Open - dayclose)/dayclose)*100)
ENDIF
If n > X
buy 1 contract at market
etc.
Elsif n < -X
Sellshort 1 contract at market
etc.
endif
endif
Okay, thanks a lot to both of you, it’s much appreiciated!
Is this code valid? What i am trying to do is store value of TAKEPROFIT=1 once after trade is entered price moves above supertrend. This is because, if trade is entered trade is closed before price moves above supertrend completely as the exit is based on supertrend itself.
Because this code is for partial exit, I cannot return TREND>0 and trail the stop with supertrend.
Not sure how to make this work, any help would be great
if not onmarket then
takeprofit=0
endif
If onmarket and close[1]>supertrend then
once takeprofit=1
ENDIF
If close crosses under supertrend then
Sell at market
endif
I don’t understand what you are trying to do but you are definitely not using ONCE as it is intended. As said before in this topic:
ONCE is simply an instruction that means that anything after it is not done or read again after the very first bar the code is set running on.
You cannot ever be ONMARKET on the very first bar a strategy sees so your ONCE will never ever be read on that first bar.
Ok thanks for clarification.
What I am trying to achieve here is the ability to exit using say when price closes below 1HR supertrend. The entry can be before the 1HR supertrend turns bullish. Because the exit is based on close under supertrend, the exit condition is triggered in the process of price moving above supertrend after entry well before intended exit.
The solution could be to allow the price to close above supertrend first and when price closes under supertrend after rally, exit the trade.
I have encountered this issue in every indicator making it difficult to use them esp. when entry is before the indicator turned bullish.