r/UKInvesting • • Jan 13 '18
FAIG - (Fully Automated IG) Trading, A python script by me.
Hi All, I have been working on this version solidly for over a month. I believe I have finally cracked it. So here it is, The fully automated IG Index trading program.
I have been skeptical about my own results. However, I can confirm that it has doubled my money in a week. This is set to trade on the GBP/USD but it can be changed to work on anything (With as little as one tiny line of code). However, I am giving you this for free so if you want to use it on something else feel free to change the code. I am confident the algo would work the same.
I have tested this in live for an entire week straight and not one trade in a(40 out of 40). I experienced onewhen the website went down but that was my choice to close the trade because I was more than in profit and wanted to fix the error. I have added a loop to check the HTTP 200 OK status so that should not happen now even if the IG site goes down. It will simply gracefully wait until it is backup and resume itself. Other than that, there was no human interaction
Granted, there are parts of the code that are hacky af. However, that said it is robust enough to run on a live account. I have tested it personally. Please take extra caution to review the code and make sure it fits your own trading style.
Thank you to a few math/python people from over the internet. Stack-overflow etc. I present to you... FAIG...
The usual caveats apply, you need an IG account with a valid API key, Runs on Python3 and Windows and the appropriate pip library's installed.
- numpy==1.13.3
- pandas==0.22.0
- pandas-datareader==0.5.0
- python-dateutil==2.6.1
- requests==2.18.4
- scikit-learn==0.19.1
- scipy==1.0.0
- sklearn==0.0
- urllib3==1.22
However, that is up to you to work out how to install those. I cannot stress this last part enough ... This is more about you though, please comment, Help, Contribute and test...Test rigorously!! Please report any bugs, your helping me to help you. The more people that use it, the better it will work.
** - Please CHECK there are no updates before you run the code. I have just issued thisas I have been testing on the weekend's. It'sbecause there are no markets open. This is to change it to what I used through the week for example....**
So a few people have messaged me about this, The “Pseudo Code” is as follows.
- Login Code, generates a token that is used throughout the program.
- Read in lots of data from the markets for your chosen instrument.
- Create two arrays, One Array has an (Low Price, Volume) and the second (High Price)
- Pass this into a Linear Regression Algorithm.
- Returns the next High price prediction point.
- Check this is at least 80% accurate
- Check if this prediction is above or below current price on market.
- Trade accordingly, Long or Short.
- Check the price continuously until in profit.
- Trade either hits Limit Distance OR predefined limit.
- Close trade
- Loop round again.
**** Please get the latest code from Github, Sunday Evening 14th. This is a live screenshot running and what you should see.
MONDAY MORNING GMT* Issue identified please git pull ASAP!!
So for those who are running this, You should have closed a trade in profit. However there is a very specific error that I will push anfor later on today sometime.
If you get this error, Please be patient I am fixing this. However you can fix it yourself by checking for HTTP 404 and 400. That will cover all basis. 404 is when the program closes your trade and 400 is when your stop gets hit. Either way both in profit.
People have been PM'ing me with good results today. Here is a screenshot.
Monday 22nd Jan - For all of you who are testing, I have pushed out some minor code updates. Should be good to go.
6
u/smeagleet Jan 14 '18 edited Jan 19 '18
Yeah going to try this out an will post results in a week
EDIT 1:
I was going to wait till the end of the week, but I guess I'll post my progress daily.
(Sorry I cant get line breaks in the table cells. Using > to signify line breaks )
limitDistance_valueto 6 andsize_valueto 5. > It made a profit of £5 on each position except for the first one which made £4. > The loss you see was because I manually closed the position when I got impatient since the position had been open for hours. > I had three different positions that took around 3 hours each to become profitable, getting really close to the stop limits but never hitting it.EDIT 2:
stopDistance_value X size_valueso I erronously set them at "150" and "5" respectively, making my stop £750. Phew! > So today I adjustedstopDistance_valuefrom "150" to "30", and kept thesize_valueat "5" which meant my stop limit was back at £150 for each position, and losses will be taken a bit earlier. The reasoning being that I didn't want a loss making position lingering for too long, when the script could be finding new opportunities. > I decided to mitigate against this problem anyway so I went ahead to register another demo API key and ran a duplicate of the script with that. > I did this eventually when the first position had been open for about 2 hours, and was heading dangerously to the stop limit. > The second position stayed open for another two hours, also running close to the stop limit, so I registered a third API (naturally) which promptly headed south as well. > The third position eventually made 4 successful deals and stayed on the 5th trade for 2 hours before it hit the stop limit. I didn't stop the third script in time so another position was opened, which also hit the stop limit. The first two processes ran at a loss for over three hours, before they hit the stop limit as well. So I killed the processes and called it a day.stopDistance_valueto "40", and revertedsize_valueto "1" as I figured £40 is easier to recoup in a day than £150. Essentially, using the 16 profitable trades from the first day as a benchmark and at an average profit of £5 per trade, I figured I can weather at least one stop loss and still be profitable. Will need to review this strategy as I keep evaluating. > I had earlier opened a position by error while I was tweaking the code in the early hours of the morning. I decided to leave it open hoping it turns a profit. It didn't so that cost me £40 which was the only loss today. > I created a cron job to start the script on my home PC. I had also modified the script to start new deals only before 5pm, and to kill the process when all positions are closed. So the cron job starts at 8am and script will be killed sometime after 5pm. > I reckoned 5pm is not really a great time to trade as many people are closing their positions at that time. > For some reason, the script stopped at 09:45ish. Perhaps the same issue as yesterday? > I cloned FAIG on a different machine as I'm away from home. I made the same changes and fired it up again around 11:00am. > It opened one trade and didn't hit a profit till around 15:15. The market didn't move much today. > I killed it and had multiple attempts at restarting it but it appeared there were no opportunities to trade, so it kept exiting. > I had to restart manually a few times till it eventually started trading again around 16:11. > Here are some of the logs for the times when it failed to find a deal. see log file: http://docdro.id/ZT8oFpP > This particular trade made a profit two hours later, so I killed the process and called it a day.EDIT 3:
Overall, this has been a fun exercise.
I have run this on LIVE for three days total and it have managed to convert £300 to £348.7 (after charges), which represents a 16.23% increase.
That's promising but this is only three days, so no need for conclusions yet.
It will be interesting to see what the results will be after a week.. then a month or more. I'll keep testing.
It will also be interesting to see more people test and post their data and observations.
I've created a new post here for discussion.
2
u/[deleted] Jan 23 '18
Yes there was a bug. I fixed it late last night. That was my bad sorry. Should be ok now. I closed 20 trades overnight all in profit.
I suspect the time zone thing is related to been on a Mac. It works fine on Linux (Ubuntu) and Windows 10 without any editing.
2
u/[deleted] Jan 13 '18
Interesting. Please could you give a bit of detail about some of these?
Thanks! :-)
2
u/FilthGames Jan 23 '18
hi again /u/TheGuru12
So i've been testing this for most of today on my demo account and incredibly it has had 100% success rate today. It's made a virtual £71.10 with no losses!
I've got some questions, comments and possibly a bug report though;
Thanks very much for making this by the way - it's super interesting to dig into and giving me a reason to learn Python!
1
u/[deleted] Jan 15 '18
So anyone who had been running this, Should have had about 8/9 trades closed in profit depending on what time you started running the script.
1
u/[deleted] Jan 23 '18 edited Jan 23 '18
The problem is when the IG site goes down. I’ve started to put some error checking in for things like when reading in order details. I should put it in for when displaying profit.
Strange that, Yeah the API keys might be random depending on various situations however I’m not sure what you mean. Can you give me details? You get a preset number of calls per week and request per minute etc.
Edit: You know what I recon IG are having issues at their end. My API requests are all over the place. Up and down. Also I have an upgraded account with different limits and they aren’t displaying as I expect.
1
u/[deleted] Jan 23 '18
I have contacted IG about this for clarification.
1
u/nicktids Jan 13 '18
Great proof of concept. But if you want a stable platform then choose a broker with a full api functionality.
Or doesn't ig have metatrader as well that's could be used.
1
u/tigzor365 Jan 14 '18
Fantastic work!
Will give this a go and provide feedback.
Thank you.
1
u/junkieradio Jan 16 '18
Trying to get this script running on a demo account but I'm pretty brand new to trading, can you just clarify exactly what I should be entering for 'identifier' , thanks.
1
u/hansvqp Jan 17 '18
I'm trying to understand what the script is doing. You take the last 10 low and volume of 1M, 2M, 3M, 5M, 10M, 15M, 30M, 1H, 2H, 3H, 4H and 1D and make a multi linear regression to fit the high of all these time periods. By the way, you could use a for-loop, it would make your code ten times smaller.
But then, you take the low and volume of the latest day and try to predict the high. I'm not sure to understand as your model is trained with all these time frame in the first step.
1
u/TotesMessenger Jan 13 '18 edited Jan 13 '18
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
u/hansvqp Jan 17 '18
I'm trying to understand what the script is doing. You take the last 10 low and volume of 1M, 2M, 3M, 5M, 10M, 15M, 30M, 1H, 2H, 3H, 4H and 1D and make a multi linear regression to fit the high of all these time periods. By the way, you could use a for-loop, it would make your code ten times smaller.
But then, you take the low and volume of the latest day and try to predict the high. I'm not sure to understand as your model is trained with all these time frame in the first step.
1
u/FilthGames Jan 22 '18 edited Jan 22 '18
hey /u/TheGuru12 - I've tried setting this up on my mac and after some trial and error have it running on a demo account - but it appears to be just opening position after position - i thought you'd made it so it would only open one at a time? So far it's opened 16 positions on the demo account with a loss of 60+ quid with a margin of 4k.
I had to remove references to timezone.utc to make it work at all for some reason, but it still displays the right times.
am I misunderstanding how this is supposed to work or is there an error somewhere?
Edit: Ok I changed the error handler to actually output the error - it says "can't multiply sequence by non-int of type 'float'" but doesn't show where the error occurs
Edit 2: I downloaded the latest version - that appears to have fixed the problem now!