| stevehopwoodforex.com https://www.stevehopwoodforex.com/phpBB3/ Print view |
|
| Oanda REST API scripts https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4286 |
Page 2 of 3 |
| Author: | TickJob [ Fri Jul 24, 2015 2:48 am ] |
| Post subject: | Oanda REST API scripts |
Hi mcbustider, Thank you very much to provide me so much useful information to learn. I will try them out. |
|
| Author: | mcbusrider [ Fri Jul 24, 2015 3:42 am ] |
| Post subject: | Oanda REST API scripts |
You're most welcome - enjoy the journey! :hi: |
|
| Author: | QuantHH [ Sun Nov 06, 2016 5:18 pm ] |
| Post subject: | Oanda REST API scripts |
Thanks for the hint script and the description. Is there a way to close all profitable trades, when the value of the trade is a certain value, independently of the number of pips or the spread of the currency pair. For example I may close all profitable trades for the USD/GBP pair which value is equal or greater than 1,0 $USD. ( pip value may be smaller than 1$) Regards QHH |
|
| Author: | mcbusrider [ Sun Nov 06, 2016 6:59 pm ] |
| Post subject: | Oanda REST API scripts |
Hi, QHH, Thanks for your interest in the scripts and I hope you can find some use for them. Since I wrote these, Oanda have started a new API, called Oanda V20. If you have opened an account with them recently, you'll need the new OandapyV20 wrapper. All my Oanda accounts are legacy accounts, which can still be accessed by using the old Oandapy module. I've not been using the Oanda APIs of late, as I've been concentrating on learning how to program EAs on Empty4. However, before I stopped using the Python scripts, I wrote the following script. I set this to run every minute using Crontab on my Ubuntu VPS. Basically, the functionality to close profitable trades can be found in the tradeMaster(side) function. You are looking to loop through the open trades of each pair and compare the price of each open trade (tradePrice) against the current market (Bid[0] or Ask[0]) price (newPrice). The script uses the priceDifference variable to check if the tradePrice is further than 2 pips in profit away from market value. If it is, it will set stop loss to 2 pips and trailing stop to 5 pips. No reason why you couldn't adapt that functionality to close all profitable trades straight away. If you run the script in a terminal, you should be able to work out what's going on, as I make console logs throughout the script. Use the BREAKPOINT() functions in the script to create a pause, which waits for you to press enter to continue. Tested on Python 2.7 Hope this is some use for you! Cheers, Nige Code: Select all Your Oanda legacy account number, expressed as an Int (number with no quote marks around it!) masterLotSizeIncreaseThreshold This increases the lot size by 1 unit every x units of account currency NAV. Similar to Steve's "sizePerDollop" variable. For example, if your account is in USD and your NAV is $200 with a masterLotSizeIncreaseThreshold of 10, the lotSize for each trade the script places will be 20: lotSize=NAV/masterLotSizeIncreaseThreshold Or $200 NAV/$10 masterLotSizeIncreaseThreshold=20 units per trade I calculate NAV like this: NAV = balance+UPL (UPL= Unrealized Profit Loss) tradeDelta Minimum distance in pips between trades. The script checks all open trades in each pair and will not open another trade if the Ask[0]/Bid [0] prices are less than this value in pips away from any trades in this pair. For example: a GBP_JPY trade is open at 128.5 tradeDelta is 10 No trades will be opened by the script between 128.4 and 128.6 (+- 10 pips either side of open trade) oilDelta/oilLotSizeIncreaseThreshold Allows you to set different values if you are trading BCO_USD or WTICO_USD. You could easily expand this functionailty for other commodities, eg Copper, Silver etc. If you want to save the text generated by the program to a log file, uncomment out line 53. You'll need to change the directory and filename variables to fit your own system. |
|
| Author: | mcbusrider [ Sun Nov 06, 2016 7:34 pm ] |
| Post subject: | Oanda REST API scripts |
The latest script adds the spread into the equation when it works out how far from Bid[0] or Ask[0] price to place new trades. With regards to adding a basket closure or working out how much each trade or a series of trades was profiting, I just worked on closing each profitable trade, so I never added basket trading functionality. It's entirely feasible to add it, but not something I've done personally. I was trading such small trades (<20 units each trade) that it seemed pointless adding up the profits, I just set the script to modify any trade that was more than 2 points in the green by setting a 2 pip stop loss with a 5 pip trailing stop. This let the winners run. I just kept the losers open to see what would happen. Trading such small trade sizes lets you keep trades open for as long as you like without too much drama on the drawdown side of things - the interest was a bit of a killer after a while but it was still making a fair amount of money most days. If things started to get too crazy on drawdown, I'd just soak up some of the profits by manually closing the worst losing trades. The trading went OK this year; I used some of the account balance to sort a load of dental work out I'll probably look to get back to using the Oanda REST API's at some point in the future, but right now I'm enjoying learning MQL4. Despite the unreliability and quirks of the platform, pretty much every broker lets you use Empty4, so it seems the logical progression to target that program. It's a shame that Empty4 has a 1,000 unit (0.01 lot) lower limit for trades. Oanda lets you go down to 1 unit through REST API, but not through Empty4. I just want to make sure that I'm not solely dependant on one broker. |
|
| Author: | QuantHH [ Sun Nov 06, 2016 10:07 pm ] |
| Post subject: | Oanda REST API scripts |
Hello Thanks once more for the information and the script, is not exactly what I am looking to do, but it provides me hints and a clear view of how it could be done. I am doing many small trades in short times, so I am interested in closing all the profitable trades which are having a profit > 0.01 $ , and checking that often, in intervals. The strategy for generating the orders is still tested, but as you said v20 it is now implemented in Oanda API and it offers hedging, so I am just having 50 % of the positions in one direction and 50 % in the other direction, and closing them, when they are profitable, in short intervals, and repeat the operation as much as possible during a trading day. |
|
| Author: | mcbusrider [ Mon Nov 07, 2016 7:13 am ] |
| Post subject: | Oanda REST API scripts |
If you want to run the script more than once a minute, you could use something like this: Code: Select all If you make a few of these scripts with varying delay times, importing the same script, then run them all every minute on a crontab, you can effectively run your main trading script more than once per minute. Cron only allows you to run scripts once per minute maximum, so this is a workaround. Don't have too many scripts running, each instance of the script opens a new connection to the API and if you have too many open, the server will refuse any new connections. TOO MANY SCRIPTS OPEN WILL CAUSE ALL SORTS OF SHIT with your computer, including memory leaks and crashes. This will also probably happen when you are asleep and blissfully unaware of the carnage happening on your trading VPS, until you try to log in the next day and get the horrible sinking feeling that SOMETHING IS NOT QUITE RIGHT WITH YOUR INSTALLATION ANYMORE. My script took about 4 seconds to run - this was checking about 500 open trades. I had six scripts a minute running (one every ten seconds) which worked out fine. Check the values of the startTime() and endTime() functions in the terminal for an idea of how long the script takes to run. You should see these printed to the console just before the script finishes executing, so you don't need to scroll up through all the printout. Good luck! |
|
| Author: | mcbusrider [ Mon Nov 07, 2016 7:22 am ] |
| Post subject: | Oanda REST API scripts |
Sorry I can't help you out much on using the V20 endpoints, there's a module on GitHub called OandapyV20 but I never messed around with it (I still have legacy accounts). I read the documentation on the Oanda developer portal but I was a bit disappointed with it; all the info on the V20 pages seemed to link to old REST scripts. If you manage to get something working on V20, please share your findings, I'm sure someone on here will find it helpful. It'll probably take a bit of hacking to replace the legacy endpoints in my scripts with the new V20 endpoints, but most of the basic functionality of the script will work the same. |
|
| Author: | mcbusrider [ Mon Nov 07, 2016 8:19 am ] |
| Post subject: | Oanda REST API scripts |
If you're hedging, check out Dottybot for some great ideas on how to offset hedge trade closures. http://www.stevehopwoodforex.com/phpBB3 ... =93&t=4797 The manual describes the methods in great detail, so I won't do so here. |
|
| Author: | mcbusrider [ Tue Nov 08, 2016 7:05 am ] |
| Post subject: | Oanda REST API scripts |
To close all profitable trades only when profit> $1 over all trades (leave losers open), I would loop through all open trades. The loop would be used to calculate the Unrealized P&L of each open trade in that pair (or over multiple pairs, if you are taking that into account). Add the calculated p&l for each open trade to a master Profit variable (Google is your friend if you want to know how to calculate P&L on forex trades) Anything that shows a profit: append a list with the trade ID of that profitable trade. When the loop has completed, check if masterProfit> $1 If masterProfit>$1, loop through and close each trade in the trade ID list of profitable trades. |
|
| All times are UTC | Page 2 of 3 |
|
Powered by phpBB® Forum Software © phpBB Limited |
|