-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oanda5
executable file
·128 lines (92 loc) · 3.25 KB
/
oanda5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# system imports
import sys
sys.path.append('.')
import os
import math
import datetime
import time
# OANDA library functions
from oandaLib import *
# Update volume on disk
def WriteVolume(pVol):
FileHandle=open('wg.txt','w')
FileHandle.write(f"{pVol}\n")
FileHandle.close()
# Append to a file
def AppendFile(fn,data):
cf=open(fn,'a')
cf.write(data)
cf.close()
###
### Main code base. Place order on exchange
###
Blank=" "
# Load credentials from file
broker=oanda("credentials.txt")
markets=broker.Markets
# Select the pair
pair='EUR/USD'
# Get ticker
ticker=broker.GetTicker(pair)
BuyPrice=ticker['Ask']
SellPrice=ticker['Bid']
# Set the initial position size
Units=100
# make sure our glass if full if we don't have anything.
if os.path.exists('wg.txt'):
fh=open('wg.txt','r')
PositionVolume=float(fh.readline().strip())
fh.close()
else:
order=broker.PlaceOrder(pair=pair, orderType='Market', action='Buy', amount=Units, price=BuyPrice)
PositionVolume=Units*float(order['orderFillTransaction']['price'])
WriteVolume(PositionVolume)
done=False
while not done:
# Orderbook ticker values are continously updated.
ticker=broker.GetTicker(pair)
BuyPrice=ticker['Ask']
SellPrice=ticker['Bid']
Spread=SellPrice-BuyPrice
# Get units own and calculate mid price.
NewVol=broker.GetPositionsUnits(pair)
MidPrice=((BuyPrice+SellPrice)/2)
MidVol=NewVol*MidPrice
# OANDA requires a minimum of 1 unit, Threshold is 1 unit plus 3 times the
# spread. You pay spread on buy AND sell. 3 times spread gives a little more
# profit, but is mostly used for brokers that allow partial units to ensure
# profit.
Threshold=(1+(Spread*3))
# These are mostly for display and demonstration of the algorithm.
# Threshold to make a buy
bt=PositionVolume-Threshold
# Threshold to make a sell
st=PositionVolume+Threshold
CurTime=(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
# Sell only when new volume is above the current positional volume by at
# least the desired threshold.
if (NewVol*SellPrice)>(PositionVolume+Threshold):
SellSize=int((NewVol*SellPrice)-PositionVolume)
if SellSize>0:
order=broker.PlaceOrder(pair=pair, orderType='Market', action='Sell', amount=SellSize, price=SellPrice)
Balance=broker.GetBalance()
SellVol=SellSize*SellPrice
t=f"{CurTime} Sell {SellPrice:8.4f} {SellVol:8.4f} {SellSize:8.4f} {Balance:12.4f}"
print(t)
AppendFile('oanda5.log',t+'\n')
# Buy
elif (NewVol*BuyPrice)<(PositionVolume-Threshold):
BuySize=int(PositionVolume-(NewVol*BuyPrice))
if BuySize>0:
order=broker.PlaceOrder(pair=pair, orderType='Market', action='Buy', amount=BuySize, price=BuyPrice)
Balance=broker.GetBalance()
BuyVol=BuyPrice*BuySize
t=f"{CurTime} Buy {BuyPrice:8.4f} {BuyVol:8.4f} {BuySize:8.4f} {Balance:12.4f}"
print(t)
AppendFile('oanda5.log',t+'\n')
# Simple diagnostics and flutter...
Balance=broker.GetBalance()
print(f"{CurTime} {Blank:4} {bt:8.4f} {MidVol:8.4f} {st:8.4f} {Balance:12.4f}")
time.sleep(5)