druby is right, change those lines as recommended. This change will still keep your SL changing every candle.
If you want to set your SL only once, I recommend adding these lines at the beginning of your code (in between lines 1 and 2):
IF LongOnMarket AND Not LongOnMarket[1] THEN
SET STOP LOSS StopLoss1
ENDIF
IF ShortOnMarket AND Not ShortOnMarket[1] THEN
SET STOP LOSS StopLoss
ENDIF
this will set your SL according to the values retained when the pending order was set (the prior candle).
If you want to set your SL according to the values set when the order was triggered (the currently closed candle), then add those lines in between lines 52 and 53, instead.
Hi druby and robertogozzi,
I have tried adding as robberto mentioned but did not change anything and still the same.
Once the long or the short entry is triggered, SL or TP must not change. should stay the same but it should not go till the next trade. is it possible to cancel everything maybe just before at 19.00. means will exit the whole trade with a loss or a profit as SL or the TP not hit. and everything to be calculated fresh as a new trade from 1900-0100 next day? seems like this one goes on until the SL or the TP hit? Please correct me if I’m wrong.
I have added the same example EURUSD – May 14th still entered and exit at the same time.
DEFPARAM CumulateOrders = False
IF LongOnMarket AND Not LongOnMarket[1] THEN
SET STOP LOSS StopLoss1
ENDIF
IF ShortOnMarket AND Not ShortOnMarket[1] THEN
SET STOP LOSS StopLoss
ENDIF
starttime = 191500
endtime = 011500
trigTime = 100000 // added!
//------------------------------------------------------ open data window
if time = starttime and startFlag = 0 then
startFlag = 1
startBar = barindex
hh = 0
ll = 0
endFlag = 0
trigFlag = 0
endif
//------------------------------------------------------ update hh/ll while in window open
if startFlag = 1 then
if high > hh then
hh = high
endif
if low < ll or ll = 0 then
ll = low
endif
endif
//------------------------------------------------------ close data window
if time = endTime and startFlag = 1 and endFlag = 0 then
endFlag = 1
endBar = barindex
startFlag = 0
elsif startFlag = 1 then
endbar = barindex
endif
//------------------------------------------------------ when window closed
if endFlag = 1 then
dif = hh-ll
fib38 = hh
fib0 = ll
endif
//------------------------------------------------------ when trigger time reached
if time >= trigTime and startFlag = 0 and endFlag = 1 then // added! +2 lines
trigFlag = 1
endif
EntryPrice = (fib38-fib0)*2.5+fib0
EntryPrice1 = (fib0-fib38)*1.5+fib0
StopLoss = (fib38-fib0)*3.0050+fib38
StopLoss1 = (fib0-fib38)*3.0050+fib0
takeprofit = (fib38-fib0)/2+fib0 //changed
takeprofit1 = (fib0-fib38)/2+fib0 //changed!
// Conditions to enter Short positions
IF NOT ShortOnMarket AND startFlag = 0 and endFlag = 1 and trigFlag = 0 then // changed!
SEllSHORT 1 CONTRACTS AT EntryPrice LIMIT
ENDIF
IF OnMarket THEN
SET STOP LOSS StopLoss
Endif
Set Target pProfit takeprofit
// Conditions to enter long positions
//IF NOT LongOnMarket AND >tradingtime< and >value2< THEN //????? was this line in error ---
If NOT LongOnMarket AND startFlag = 0 and endFlag = 1 and trigFlag = 0 then // changed and added!
BUY 1 CONTRACTS AT EntryPrice1 LIMIT
ENDIF
IF OnMarket THEN
SET STOP LOSS StopLoss1
ENDIF
Set Target pProfit takeprofit1
I really appreciate all of your attention, thank you once again.
JSParticipant
Senior
Hi,
These levels are calculated as a percentage of the original range (Diff=HH-LL) …
Is this what you’re looking for?
The difference in notation is because you are probably on a “spread betting account”…
Hi JS,
Your Pic example shows exactly what should happen. should this change to calculate values instead of percentages if it doesn’t work on spread betting? 🙁
JSParticipant
Senior
It will make no difference…
JSParticipant
Senior
Hi,
(Intra) Day starts at 00:00
First action at 01:00: determine the range (HH-LL) over the last six hours…
(Six hours corresponds to the time between yesterday 19:00 and today 01:00)
You only set a new range when there is no position open, this to prevent the SL and TP from changing while a position is open…
If OpenTime=010000 and NOT OnMarket then
HH=Highest[24](High)
LL=Lowest[24](Low)
diff=HH-LL
Level0=HH
Level100=LL
EndIf
Second action runs from 01:00 to 10:00
Positions can be opened in this time slot, but only if there is no open position yet…
If Time>010000 and Time<100000 and NOT OnMarket then
If Low<Level0-(Diff*Mult) then
Buy 1 contract at Market
EndIf
If Close>Level100+(Diff*Mult) then
SellShort 1 contract at Market
EndIf
Set Stop Loss SL*Diff
Set Target Profit TP*Diff
EndIf
That’s it…
DefParam CumulateOrders=False
If OpenTime=010000 and NOT OnMarket then
HH=Highest[24](High)
LL=Lowest[24](Low)
Diff=HH-LL
Level0=HH
Level100=LL
EndIf
If Time>010000 and Time<100000 and NOT OnMarket then
If Low<Level0-(Diff*Mult) then
Buy 1 contract at Market
EndIf
If Close>Level100+(Diff*Mult) then
SellShort 1 contract at Market
EndIf
Set Stop Loss SL*Diff
Set Target Profit TP*Diff
EndIf
//GraphOnPrice HH as "HH"
//GraphOnPrice LL as "LL"
GraphOnPrice Level0 as "Level0"
GraphOnPrice Level100 as "Level100"
Graph Diff as "Diff"
GraphOnPrice (Level0-(Diff*Mult)) as "EntryLong" coloured("Green")
GraphOnPrice (Level100+(Diff*Mult)) as "EntryShort" coloured("Green")
GraphOnPrice (Level0-(Diff*Mult))-(SL*Diff) as "SLLong" coloured("Red")
GraphOnPrice (Level100+(Diff*Mult))+(SL*Diff) as "SLShort" coloured("Red")
GraphOnPrice (Level0-(Diff*Mult))+(TP*Diff) as "TPLong" coloured("Blue")
GraphOnPrice (Level100+(Diff*Mult))-(TP*Diff) as "TPShort" coloured("Blue")
JSParticipant
Senior
If you want to see the same values as on my screenshot, use:
DefParam CumulateOrders=False
Mult=2.5
SL=1.5
TP=2
If OpenTime=010000 and NOT OnMarket then
…
Hi JS,
It works nicely… only one or two entries are in different values! thanks very much I’m happy at least to develop this strategy to this level!
I noticed something on EURUSD 17MAY – SL hit at 9.30am and then at 9.45am another long trade gets triggered. is this because it’s before 10am? how to cancel that? same thing has happen on the 17MAR! Please see attached example.
How to avoid this please? Please see attached example
DefParam CumulateOrders=False
Mult=2.5
SL=1.5
TP=2
If OpenTime=010000 and NOT OnMarket then
HH=Highest[24](High)
LL=Lowest[24](Low)
Diff=HH-LL
Level0=HH
Level100=LL
EndIf
If Time>010000 and Time<100000 and NOT OnMarket then
If Low<Level0-(Diff*Mult) then
Buy 1 contract at Market
EndIf
If Close>Level100+(Diff*Mult) then
SellShort 1 contract at Market
EndIf
Set Stop Loss SL*Diff
Set Target Profit TP*Diff
EndIf
//GraphOnPrice HH as "HH"
//GraphOnPrice LL as "LL"
GraphOnPrice Level0 as "Level0"
GraphOnPrice Level100 as "Level100"
Graph Diff as "Diff"
GraphOnPrice (Level0-(Diff*Mult)) as "EntryLong" coloured("Green")
GraphOnPrice (Level100+(Diff*Mult)) as "EntryShort" coloured("Green")
GraphOnPrice (Level0-(Diff*Mult))-(SL*Diff) as "SLLong" coloured("Red")
GraphOnPrice (Level100+(Diff*Mult))+(SL*Diff) as "SLShort" coloured("Red")
GraphOnPrice (Level0-(Diff*Mult))+(TP*Diff) as "TPLong" coloured("Blue")
GraphOnPrice (Level100+(Diff*Mult))-(TP*Diff) as "TPShort" coloured("Blue")
Thanks again to everyone developing this strategy. Even though the original strategy is about Central Banks Dealers Range at 19.00-1.00 ! it gets very interesting when you change the time ranges in different markets.
JSParticipant
Senior
Hi @crolakstrading
That’s right, between Time>010000 and Time<100000, the system is free to open and close positions… I have changed the code so that only 1 trade is opened per day…
A few points to consider…
In line 14 “If Low<…” you can use the “Close” or the “Low”
In line 18 “If Close>…” you can use the “Close” or the “High”
Good luck with your system…
DefParam CumulateOrders=False
Mult=2.5
SL=1.5
TP=2
MaxTrades=1 //One Trade a Day
If IntraDayBarIndex=0 then
LongTally=0
ShortTally=0
EndIf
If OpenTime=010000 and NOT OnMarket then
HH=Highest[24](High)
LL=Lowest[24](Low)
Diff=HH-LL
Level0=HH
Level100=LL
EndIf
If Time>010000 and Time<100000 and NOT OnMarket then
If Low<Level0-(Diff*Mult) and LongTally<MaxTrades then
Buy 1 contract at Market
LongTally=LongTally+1
EndIf
If Close>Level100+(Diff*Mult) and ShortTally<MaxTrades then
SellShort 1 contract at Market
ShortTally=ShortTally+1
EndIf
Set Stop Loss SL*Diff
Set Target Profit TP*Diff
EndIf
//GraphOnPrice HH as "HH"
//GraphOnPrice LL as "LL"
GraphOnPrice Level0 as "Level0"
GraphOnPrice Level100 as "Level100"
//Graph Diff as "Diff"
GraphOnPrice (Level0-(Diff*Mult)) as "EntryLong" coloured("Green")
GraphOnPrice (Level100+(Diff*Mult)) as "EntryShort" coloured("Green")
GraphOnPrice (Level0-(Diff*Mult))-(SL*Diff) as "SLLong" coloured("Red")
GraphOnPrice (Level100+(Diff*Mult))+(SL*Diff) as "SLShort" coloured("Red")
GraphOnPrice (Level0-(Diff*Mult))+(TP*Diff) as "TPLong" coloured("Blue")
GraphOnPrice (Level100+(Diff*Mult))-(TP*Diff) as "TPShort" coloured("Blue")
Hi
@JS,
Thanks very much again.. I have been checking this it works great I think. I changed the line 28 If Close> to If High>
Some entries both short or long are a bit different. example EURUSD 17MAy it triggers 2 pips above. so instead of hitting TP it goes to SL. This might be a stupid question but is this because of the spread? please see below pic.
Thank yo
DefParam CumulateOrders=False
Mult=2.5
SL=1.5
TP=2
MaxTrades=1 //One Trade a Day
If IntraDayBarIndex=0 then
LongTally=0
ShortTally=0
EndIf
If OpenTime=010000 and NOT OnMarket then
HH=Highest[24](High)
LL=Lowest[24](Low)
Diff=HH-LL
Level0=HH
Level100=LL
EndIf
If Time>010000 and Time<100000 and NOT OnMarket then
If Low<Level0-(Diff*Mult) and LongTally<MaxTrades then
Buy 1 contract at Market
LongTally=LongTally+1
EndIf
If high>Level100+(Diff*Mult) and ShortTally<MaxTrades then
SellShort 1 contract at Market
ShortTally=ShortTally+1
EndIf
Set Stop Loss SL*Diff
Set Target Profit TP*Diff
EndIf
//GraphOnPrice HH as "HH"
//GraphOnPrice LL as "LL"
GraphOnPrice Level0 as "Level0"
GraphOnPrice Level100 as "Level100"
//Graph Diff as "Diff"
GraphOnPrice (Level0-(Diff*Mult)) as "EntryLong" coloured("Green")
GraphOnPrice (Level100+(Diff*Mult)) as "EntryShort" coloured("Green")
GraphOnPrice (Level0-(Diff*Mult))-(SL*Diff) as "SLLong" coloured("Red")
GraphOnPrice (Level100+(Diff*Mult))+(SL*Diff) as "SLShort" coloured("Red")
GraphOnPrice (Level0-(Diff*Mult))+(TP*Diff) as "TPLong" coloured("Blue")
GraphOnPrice (Level100+(Diff*Mult))-(TP*Diff) as "TPShort" coloured("Blue")
u in advance.
JSParticipant
Senior
Hi @crolakstrading
On May 17, the “EntryLong” is 1.085480, the “Low” (you have to look at the bar before the entry) is 1.08544 so a long position will be opened on the “Open” of the next bar (1.08557) …
So, it’s exactly right and has nothing to do with the (unused) spread… 🙂
@JS thank you so much! will test this more! 🙂
JSParticipant
Senior
Hi @crolakstrading
Currently, the orders that are used in your system are “market orders”…
These orders will never open exactly on the drawn lines because they open on the “Open” of the next bar…
If you do want to have an exact execution on the lines, you will have to use a different type of order, for example “limit orders”…
HI
@JS,
yeah the lines show exact execution but entries are little bit different. how can we change it to “limit orders”? to get exact entry as the line? this will make the 17th loss in to a profit.
If Time>010000 and Time<100000 and NOT ONMARKET then
If Low<Level0-(Diff*Mult) and LongTally<MaxTrades then
Buy 1 contract AT close LIMIT
LongTally=LongTally+1
EndIf
If high>Level100+(Diff*Mult) and ShortTally<MaxTrades then
SellShort 1 contract AT close LIMIT
ShortTally=ShortTally+1
EndIf
Set Stop Loss SL*Diff
Set Target Profit TP*Diff
EndIf
tried but clearly i’m doing it wrong!!
JSParticipant
Senior
Hi @crolakstrading
Here is the system based on “Limit” orders instead of “Market” orders…
TimeFrame(15 minutes)
Mult=2.5
SL=1.5
TP=2
MaxDayTrades=1
If IntraDayBarIndex=0 then
DayTrades=0
EndIf
If OpenTime=010000 and NOT OnMarket then
HH=Highest[24](High)
LL=Lowest[24](Low)
Diff=HH-LL
Level0=HH
Level100=LL
Set Stop Price 0
Set Target Price 0
EndIf
If Time>010000 and Time<100000 and NOT OnMarket and DayTrades=0 then
Buy 1 contract at (Level0-(Diff*Mult)) Limit
SellShort 1 contract at (Level100+(Diff*Mult)) Limit
EndIf
If LongOnMarket then
Set Stop Price (Level0-(Diff*Mult))-(SL*Diff)
Set Target Price (Level0-(Diff*Mult))+(TP*Diff)
DayTrades=1
EndIf
If ShortOnMarket then
Set Stop Price (Level100+(Diff*Mult))+(SL*Diff)
Set Target Price (Level100+(Diff*Mult))-(TP*Diff)
DayTrades=1
EndIf
GraphOnPrice Level0 as "Level0"
GraphOnPrice Level100 as "Level100"
GraphOnPrice (Level0-(Diff*Mult)) as "EntryLong" coloured("Green")
GraphOnPrice (Level100+(Diff*Mult)) as "EntryShort" coloured("Green")
GraphOnPrice (Level0-(Diff*Mult))-(SL*Diff) as "SLLong" coloured("Red")
GraphOnPrice (Level100+(Diff*Mult))+(SL*Diff) as "SLShort" coloured("Red")
GraphOnPrice (Level0-(Diff*Mult))+(TP*Diff) as "TPLong" coloured("Blue")
GraphOnPrice (Level100+(Diff*Mult))-(TP*Diff) as "TPShort" coloured("Blue")