Hi all,
I want to add another stop loss to my code below but I’m not sure how to do it. I already have a a fixed stop loss based on price (as you can see below). I was hoping you fine people could help to add another stop loss which would only be allowed to work at the END of an interval (e.g 1hour) for a different stop loss price.
So I would end up with :
- stop loss which can exit at anytime throughout the trade for a fixed price e.g 120 points.
- stop loss which can only exit the trade at the end of a time interval if the price hits e.g 70.
Many thank,
James
The code below is just used as an example !
ValidTime = (TIME > 070000) AND (TIME <120000)
once TradeTime = 1
IF ValidTime THEN
TradeTime = 1
ELSE
TradeTime = 0
ENDIF
// Conditions to enter long positions
indicator1 = Average[55](close)
c1 = (close[1] CROSSES OVER indicator1[1])
IF c1 and tradetime and close [0] > close [1] THEN
BUY 1 PERPOINT AT MARKET
ENDIF
// Conditions to enter short positions
indicator2 = Average[45](close)
c2 = (close[1] CROSSES UNDER indicator2[1])
IF c2 and tradetime and close [0] < close [1] THEN
SELLSHORT 1 PERPOINT AT MARKET
ENDIF
// Stops and targets
SET STOP pLOSS 100
SET TARGET pPROFIT 100
If you want to exit an order exactly on the first candlestick Close of the hour, you can test it like this:
if hour<>hour[1] then
sell at market
exitshort at market
endif
You can of course add any other condition with AND
(.. AND if Close>=70)
Thanks Nicolas for the reply. Sorry I didn’t explain myself better originally.
I only want the trade to exit if the price has gone higher or lower than e.g 40 points, at the end of ANY interval be it the 1st or the 12 or 24th. It would only close when an interval has finished and the price is higher than what I stated.
I hope that makes better sense as currently it will exit after the first interval close.
Hopefully you can help my frustrations in this matter.
Many thanks,
James
Since the code is only read one time at Close, any instruction that close an order is already doing it “at the end of any interval”.
If you want to close an order that is higher than 40 points than your order open price, you can do it as follow:
if close-tradeprice(1)>40*pointsize then
exitshort at market
endif
Of course this code will run at the end of each candlestick on the timeframe the strategy is currently running on.
So is tradeprice(1) the price of the currently open trade?
Or should it be tradeprice(0)?
So is tradeprice(1) the price of the currently open trade?
I should have said …
So is tradeprice(1) the price at execution of the currently open trade?
tradeprice(0) returns nothing at all. It must be tradeprice or tradeprice(1).
A massive thanks again. Much appreciated ! Only.
I’m having differing back test results. As you can see. The chart on the left has only a basic stop loss based on price. The chart on the right has the basic price stop loss and the added code for exiting the position at the end of an interval. As you can see in the results the chart with the added code has a lot of 0 bar trades when back tested with the added stop loss. I’m unsure why. Any help would be greatly appreciated. The code I’m playing around with is on a 1 hour time frame on the Dow.
Thanks again
Hi all,
please can you let me know if the coding is correct in the highlighted area in the attachment to bring short and long trades out. Please don’t take any notice of the coding as this is just an example.
I seem to be having some anomalies from the results above and am wondering if this part of the code is wrong.
thanks,
james