| stevehopwoodforex.com https://www.stevehopwoodforex.com/phpBB3/ Print view |
|
| Oanda REST API scripts https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4286 |
Page 1 of 3 |
| Author: | mcbusrider [ Sun Jun 07, 2015 12:02 pm ] |
| Post subject: | Oanda REST API scripts |
Latest trading script: http://www.stevehopwoodforex.com/phpBB3 ... 10#p149134 For running more than one script per minute on crontab: http://www.stevehopwoodforex.com/phpBB3 ... 49#p149149 Close All Profitable Trades Script: This post. Place Pending Order Script: http://www.stevehopwoodforex.com/phpBB3 ... 86#p124919 Written in Python, using Oandapy wrapper Scripts for the Oanda REST API REALLY IMPORTANT: IF YOU'RE USING NEW ACCOUNTS WITH OANDA AND WANT TO TRY THESE SCRIPTS OUT, YOU'LL NEED TO ADAPT THEM TO USE THE OANDAPYV20 WRAPPER. ALL MY SCRIPTS WORK WITH THE LEGACY API AND THE OANDAPY WRAPPER MODULE - NOT WITH THE NEWER V20 API. THIS IS BECAUSE ALL MY ACCOUNTS WITH OANDA ARE LEGACY ACCOUNTS, NOT V20 ACCOUNTS. CHEERS! NIGE Hi, everyone, It's great to be a part of this community and I feel, after silently lurking for a few months, that it's time to give something back. I'm a novice coder and this is my first serious attempt at making a trading script work If you don't code yet, or are struggling, I can assure you that it's worth sticking with! Equally, if you are a more experienced coder, please feel free to amend or advise - I'm really enjoying the journey! I hope you find this script (written in Python using the Oandapy wrapper) and the ideas presented here useful. I believe the Oanda REST API to offer great functionality but I haven't found many scripts around which use it. I hope this post may go some way towards changing that situation. I guess I should add the usual stuff about losing money with Forex, etc...USE A PRACTICE ACCOUNT IF YOU'RE IN DOUBT!!! This script has been working ok, but that just means I haven't discovered any issues with the code yet. Again, USE A PRACTICE ACCOUNT IF YOU'RE IN DOUBT!!! Anyhoo... THE BACKGROUND I've been using Oanda for a while now, with no major issues. I like this broker because: 1) You can split your main account into a number of sub-accounts (I believe the limit is 19 sub-accounts - I have 15 at the moment) and you can move money around between the accounts, up to a daily limit of these types of transactions (I've only ever hit this once). This limits your exposure in any one account, which in turn allows you to trade more aggressively, if this kind of thing floats your boat. 2) You also have a choice of currency on these accounts and you can use these sub-accounts to change currency at the current Oanda market rate, if you wish to go down that route. 3) They allow you to trade from 1 unit (0.00001 lot) upwards, in increments of 1 unit, instead of the 1000 unit lower limit (0.01 lot) of pretty much everything else out there. This allows precision sizing of lots and also opens up live trading strategies which would normally be out of reach until you get five grand in the bank. 4) Their in-house fxTrade trading platform works really well, for what it is. They have good versions for iPhone and Android. THE IDEA So, there I was, happily scalping away at my 10 unit lot sizes on my smartphone, growing my account by about a penny a week, when I had a brainwave of automating some of the tasks. I wanted to code a script to close all profitable trades, over all sub-accounts. THE ISSUES However, Empty4 doesn't recognise anything under 1000 units (0.01 lot), so an Expert Advisor or Script on that platform was not going to be fit for purpose, as I was only trading a 1/100th of this lot size. Also, it gets a bit messy if you want to automatically change accounts in Empty4, unless you run multiple instances of the program (with the relevant increase in required processing power). This is ok for one or two accounts, perhaps, but not too good for 15 accounts if you're paying by the gigabyte for a VPS... THE SOLUTION After some digging around, I found this thing called the Oanda REST API. It's like a kind of interface between user and account. You can do all the necessary trading stuff with it: get market prices, open and close pending orders and trades but, most importantly, you can do it all with unit increments and minimum size of 1 unit. There's also a number of programming language "wrappers" linked on the Oanda REST API developer pages. These allow you to use your programming language of choice to code your scripts. I looked through a few of these and Python seemed to offer the best example code, so I installed the wrapper and started amending the script until I got it to work. THE "closeAllProfitableTrades.py" OANDA REST API (PYTHON/OANDAPY) SCRIPT To make this script work, you'll need (at least) the following: 1) a Python interpreter. If you use Linux, this comes inbuilt. I don't know about Mac, but you can find a Windows Python IDE pretty easily. 2) the "Oandapy" wrapper (Google "Oandapy" or get it from the link at the Oanda REST API developer pages). This goes in the same folder as your script and allows the Python interpreter to interface with the Oanda REST API. 3) an Oanda practice account (or live account, if you're feeling more adventurous). 4) an Oanda REST API access token - you get this from the Oanda website, this is your personal token and allows UNLIMITED ACCESS to all your accounts (including deleting all trades) so TAKE CARE WITH IT!!! 5) a Text Editor (or Python IDE) for amending/generating the script with your own account information. I recommend using "Gedit" on Ubuntu or "Notepad ++" on Windows, as these highlight the script/comment text in different colours. 6) Use the text editor to save the attached code 'closeAllProfitableTrades.py' in the same directory as Oandapy.py You'll need to enter your account details and the REST API token in the relevant parts of the script, using the text editor/IDE. If you're using live accounts, you'll also need to change the "environment" variable from "practice" to "live" N.B. there are different Access Tokens for live and practice accounts - make sure you're using the correct one for your account type(s)! WHAT THE SCRIPT DOES (or at least, what the script is designed to do...) :youknow: I've added some notes in the script, which should explain the functionality in depth. However, as a brief overview, it's designed to run once, check the price of all open trades in all sub-accounts and close any that are in profit over a specified amount (the "offset" variable). It works on any lot size from 1 unit (0.00001 lot) up, on all open trades and on all sub accounts with Oanda. It's probably very clunky and no doubt there is a fair bit of redundant code in there, but it works (tested on Lubuntu Linux). AUTOMATING IT: The script can easily be made to run infinitely (using a 'while True:' loop and adding a 'time.sleep(3600)' - for a one hour delay), but I personally prefer to run it as a one-shot script on an automated scheduler (I use Crontab on Lubuntu) as I don't trust my code enough yet to have it run indefinitely! If you're manually trading, this is a real timesaver on closing all your profitable trades. Happy trading! Nige ![]() closeAllProfitableTrades.py Code: Select all |
|
| Author: | TickJob [ Wed Jul 22, 2015 3:10 pm ] |
| Post subject: | Oanda REST API scripts |
Hi mcbusrider, I also like to try Oanda auto trade but I don't how to start. Could you help me to start? The auto trading program basically do the following: 1. submit a series of limit buy or sell orders initially, 2. add some more orders when certain number of initial orders become open orders, 3. check total profit reaching profit target then close all orders 4. repeat 1. Thanks in advanced. |
|
| Author: | mcbusrider [ Wed Jul 22, 2015 8:37 pm ] |
| Post subject: | Oanda REST API scripts |
Hi, TickJob, Thanks for your interest in this; I'll certainly do my best to help you get up and running with using the REST API, but first we need to be on the same page. May I just ask if you have a Python interpreter installed on your operating system? If not, this is your first step. Once you have the interpreter installed, you also need to install Oandapy, which you can find on GitHub or through a Google search. Check out the sample scripts that come with Oandapy and see if you can adapt any of that code for your own stuff. I have a Python script for opening pending orders, which I'll be happy to share with you in a few hours' time, when I get to my computer. I've also made a script which closes all profitable trades which are over a certain pip profit threshold. I run both scripts on a Lubuntu VPS at regular intervals, using Crontab task scheduler. This ensures that if the script crashes at some point during execution, it will start up again at the next scheduled runtime. These two scripts should at least get you up and running with opening/closing trades and it should be pretty easy to modify the logic in these scripts to fit your preferred open/close scenarios. I should be able to post the scripts in about 12 hours or so. Cheers, Nige |
|
| Author: | TickJob [ Wed Jul 22, 2015 11:19 pm ] |
| Post subject: | Oanda REST API scripts |
Thanks mcbusrider. I have just installed Anaconda on my Windows XP, and also start reading up py programming. Your working script of open order and closing order is great starting point for me to learn. Hopefully I can just load and run and see how it work, and then try to change bit by bit to learn. |
|
| Author: | TickJob [ Wed Jul 22, 2015 11:37 pm ] |
| Post subject: | Oanda REST API scripts |
actually I am playing with the fxtrade manually to submit a series of buy and sell as shown in the picture below. quite tediou and tired doing manually. |
|
| Author: | mcbusrider [ Thu Jul 23, 2015 12:02 am ] |
| Post subject: | Oanda REST API scripts |
'placePendingOrders.py' Script Can't sleep - dogs are keeping me up I'm new to all this programming lark, too! It seems like a long, drawn-out, painful process, but completely worth the journey, when you get something to work. Hopefully, this should help you with setting the pending orders. This is the script I use, with comments added. It SHOULD work - no guarantees with anything, as I don't have the requisite skillset to troubleshoot more than basic issues at the moment. Obviously, you'll need to replace all sensitive information with your own details... The script contains a built-in balance checker function - this will only allow trading on accounts with an available balance of more than 25% NAV (change this if you like, it's in the "checkAvailBalance" function (line 80). On Line 94, "NavFree=NAV/4" sets NavFree to one quarter of NAV, or 25% (NAV = Net Asset Value = what your account is worth if everything is closed down and all profits/losses are realised). To change this line to a more accurate percentage calculation, you could use "NavFree=NAV/100*25" I use the "Buy Low...Sell High" principle with these, so I set all my buy orders lower than current market ask price, with all my sell orders higher than current market bid price. If you want to change this part of the code, have a mess around with the values in the "placeBuyOrder" (line 126) and "placeSellOrder" (line 204) functions. So, here's the script. You need to save this with a ".py" extension for it to work. Let me know how you get on! placePendingOrders.py Code: Select all |
|
| Author: | mcbusrider [ Thu Jul 23, 2015 12:24 am ] |
| Post subject: | Oanda REST API scripts |
I like fxTrade on the iPhone and I was using it extensively until I found out about the REST API, which is great, once you get the hang of it. Setting the pending trades is a lot easier with a script I had to put the balance checker in the script as I trusted it too early and had a closeout on one of my accounts. I'd really advise you, if you're putting this on a live account, to only use 1 unit lot size until you get the hang of it. If you don't want to run the script on an automated scheduler, try putting a "while: true" loop around the code, which will keep it running indefinitely, with a "time.sleep(3600)" command in there at the end of the code, which will delay for 3600 seconds (example), or one hour. Then the script will just loop again and start from the top of the script. However, I've noticed that sometimes the API overloads with an error of "too many connections" and throws you out of the script. I much prefer running it on Crontab, as if it breaks out of the script due to an error, it just starts again at the allotted time. If you're looking for a replacement for XP, I'd recommend Lubuntu. It's a bit of a learning curve, but has better functionality than XP and seems more stable for this kind of thing IMO. Also, Python is installed as standard. |
|
| Author: | mcbusrider [ Thu Jul 23, 2015 12:35 am ] |
| Post subject: | Oanda REST API scripts |
If you want to run scripts all the time, a VPS is the way to go - it will run all the time. I have a Windows Server 2008 VPS with InterServer. No problems at all with them, their customer service is impeccable and they're cheap and good - a rare combination nowadays. On the Windows Server, I run Virtualbox with an instance of Lubuntu. Virtualbox is a virtualisation software, which allows you to run an operating system inside an operating system...potentially creating a scenario of Matryoshka doll-contained operating systems, only limited by memory resources, graphic card rendering capability and hard drive space...but I digress... I can access my VPS through Remote Desktop Protocol (RDP) on my laptop or smartphone, so any maintenance or script changes I want to make can be done from either device. If you're having issues syncing time, I use Dimension 4, which is a freeware utility that runs in the background and syncs your internal computer's clock with an internet time server. The good folks at InterServer put me on to that one. I use a Dropbox account to store my scripts, with access from my laptop, my phone, the VPS and Lubuntu. One change on the script updates all instances of the script, so I can work offline from the server and not need to keep on logging into the VPS every time I want to make a change. For editing scripts on the iPhone, I recommend the "CodeAnywhere" app. I noticed that sometimes, the script executable permissions change after an edit and it won't run, so if the script stops working after an edit, you may need to alter the permissions of it (right-click/properties/permissions and make sure the script is set to execute/edit by all users). |
|
| Author: | mcbusrider [ Thu Jul 23, 2015 1:15 am ] |
| Post subject: | Oanda REST API scripts |
Oh, one more thing, before I murder these dogs... Python works on block spacing: you have to have correct block spacing in lines of code. For example: Code: Select all This: Code: Select all If the scripts don't work, try moving the comments across so they are in line with the next line of code, or taking them out completely. It was working before I put the comments in, but I haven't tested it since. |
|
| Author: | mcbusrider [ Thu Jul 23, 2015 2:09 am ] |
| Post subject: | Oanda REST API scripts |
this will mean making a slight amendment to closeAllProfitableTrades.py the logic for this check is: Code: Select all Bear in mind the above code is not valid Python, it's just the process. Most of this code is already in the script, it just needs a tweak here and there. Have a go at doing this, TickJob, see how you get on - that's the best way to learn. If you get stuck, gimme a shout! Best of luck, Nige |
|
| All times are UTC | Page 1 of 3 |
|
Powered by phpBB® Forum Software © phpBB Limited |
|