Hi, I was wondering if someone could help me with writing how to enter a position on a fib extension.
I am trying to code for price breaking out of a range, and then reaching a high. I have gotten that far, but once price has reached that high, I want to set an order to enter the market at the 62.8% fib retracement of that high.
I was wondering if anyone else has coded fib retracements before?
There is a nice example of Fibonacci extension calculation from a recent high here in this stock screener: Fibonacci 61.8% retracement screener
Hi Nicolas, thanks for the reply!
I was looking at that when I was writing my code, So far I have this:
IF Close > hh AND MakeTrade = 1 THEN
IF TurnDown THEN
fib = (highest[16](high) - ll) * 0.382
Entry = ll + fib
ENDIF
ENDIF
Where MakeTrade refers to the time of day, ll is the lowest low of the period I want to measure from and hh is the breakout.
What I’m struggling is setting the order for entry, I thought that by defining Entry as the fib + ll and placing an order there would work but my code completely ignores it?
Maybe because if your Close is superior to the current hh, your Close IS THE NEW hh. So you should refer to a previous hh to test if your Close has actually breach this level.
Hmm I don’t think that is the case, I want to set a between 5pm and 7am the next day, so when I code that I do it by saying
IF Time = 070000 THEN
hh = highest[n](high)
ll = lowest[n](low)
ENDIF
When n is the number of bars going back to 5 pm the previous day. Then once that is recorded for that day, I want to check for a breakout between two times later the next day. if price breaks out between these times, wait for a turning point (as in a highest high) and then use that to work out the fib retracement from the previous low.
I know it sounds really complicated to type, but any help would be really appreciated!
I know its quite complicated to type, but any help would be really appreciated!
Please post the whole code, as I could read it in complete and better understand where is the problem. Thanks.
Ok sure, here it is:
DEFPARAM CumulateOrders = False
DEFPARAM FlatAfter = 214500
StartTime = 070000
FinalTime = 100000
// Set Breakout box for between 5pm and 7am
If Time = 070000 THEN
hh = highest[n](high)
ll = lowest[n](low)
ENDIF
//Only make trade between 7am and 10am
If Time >= StartTime AND Time <= FinalTime THEN
MakeTrade = 1
ELSE
MakeTrade = 0
ENDIF
IF MakeTrade = 1 AND highest[y](high) > hh THEN
fib = (highest[y](high) - ll) * 0.382
Entry = ll + fib
ENDIF
IF MakeTrade = 1 AND highest[y](high) > hh THEN
SELLSHORT 1 SHARE AT Entry LIMIT
ENDIF
SET TARGET pPROFIT 50
SET STOP pLOSS 50
Its still a very rough version but its what I have so far, n = number of bars from 5pm to 7am, and y = number of bars from 7 to 10am
It would find the significant turning point (or the closest thing to that) between 7 and 10 and calculate the fib from there. Ultimately I want to code it to work both ways, but for now while I’m still getting to grip with coding I’m just doing the break to the upside.
I was also wondering, since it should only technically make one trade a day if conditions are met, do I have to tell it to restart every day? or will it automatically start doing that at 5pm anyway?
Thanks 🙂
Hi Nicolas, do you have any ideas about how I can tackle this? Or is it a lost cause haha
Hi KJ
Did you get any further with this one please? Maybe it slipped through a crack in Nicolas busy schedule? He might catch sight of it again now.
I manual trade using Fib levels and so I’ve run your code on Demo since 11 Dec and results look very promising – see attached. I made a few very minor changes … I’ll put them on here tomorrow.
Thanks
GraHal
Here’s my few changes, main change is Buy (Sell in original code??) in Line 25.
Results attached for DAX 5M @ £1 per point.
(Why cant I see ‘Insert PRT Code’ again Mods?)
DEFPARAM CumulateOrders = False
DEFPARAM FlatAfter = 214500
StartTime = 065000
FinalTime = 113000
// Set Breakout box for between 5pm and 7am
If Time = 065000 THEN
hh = highest[80](high)
ll = lowest[80](low)
ENDIF
//Only make trade between 7am and 10am
If Time >= StartTime AND Time <= FinalTime THEN
MakeTrade = 1
ELSE
MakeTrade = 0
ENDIF
IF MakeTrade = 1 AND highest[20](high) > hh THEN
fib = (highest[20](high) - ll) * 0.382
Entry = ll + fib
ENDIF
IF MakeTrade = 1 AND highest[20](high) > hh AND close > ExponentialAverage[15](close) THEN
Buy 1 SHARE AT Entry LIMIT
ENDIF
SET TARGET pPROFIT 150
SET STOP pLOSS 240
Hi Grahal,
Maybe Nicolas was installing some upgrades on the website resulting on the “insert PRT code” function temporarily not available, no worry I’ll edit your post with PRT code style
The text editor of forums have 2 tabs, you’ll find them on its upper right side, choose “Visual” one to get the rich text capabilities and the “insert PRT code” button.
About this topic, I’m sorry, because I’m involved in many ones, this one came out of my head.. Thanks GraHal about the help you give to kg6450. Let him answer to your code fix and see if you were correct about the BUY order instead of the SELLSHORT one.
Results seems decent, did you test any further in the past data?
I tried both tabs, even Visual Tab did not have insert PRT Code available; also same for me a day or two back. No big deal for me, but I didn’t want you / mods thinking I’m causing you work 🙂
I’ve got qty 4 PRT Platforms open (I’m rationalising my 200 Demo Algos! :)) if I can’t see PRT code button again, I’ll close platforms one by one to see if it makes any difference. Maybe this is why I’ve had a few other odd ‘platform scenarios’ this week!?
The results above are over 100,000 units @ 5M as this is the most history I get with IG.
Cheers
Graham
If only we could ‘edit out our crap’ 🙂 … I’m mixing up PRT Platform snags and PRC Website snags now … apologies, disregard the 2nd paragraph above please!! I must slow down my brain & fingers, I must slow down my ………. ! haha
GraHal
I’m glad to have you on board GraHal, always diverting on a Saturday evening 🙂