forked from iamvazu/trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrading-bot.py
102 lines (89 loc) · 2.93 KB
/
trading-bot.py
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
import time
import sys, getopt
import datetime
from poloniex import poloniex
def main(argv):
period = 10
pair = "BTC_XML"
prices = []
currentMovingAverage = 0;
lengthOfMA = 0
startTime = False
endTime = False
historicalData = False
tradePlaced = False
typeOfTrade = False
dataDate = ""
orderNumber = ""
try:
opts, args = getopt.getopt(argv,"hp:c:n:s:e:",["period=","currency=","points="])
except getopt.GetoptError:
print 'trading-bot.py -p <period length> -c <currency pair> -n <period of moving average>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'trading-bot.py -p <period length> -c <currency pair> -n <period of moving average>'
sys.exit()
elif opt in ("-p", "--period"):
if (int(arg) in [300,900,1800,7200,14400,86400]):
period = arg
else:
print 'Poloniex requires periods in 300,900,1800,7200,14400, or 86400 second increments'
sys.exit(2)
elif opt in ("-c", "--currency"):
pair = arg
elif opt in ("-n", "--points"):
lengthOfMA = int(arg)
elif opt in ("-s"):
startTime = arg
elif opt in ("-e"):
endTime = arg
conn = poloniex('key goes here','key goes here')
if (startTime):
historicalData = conn.api_query("returnChartData",{"currencyPair":pair,"start":startTime,"end":endTime,"period":period})
while True:
if (startTime and historicalData):
nextDataPoint = historicalData.pop(0)
lastPairPrice = nextDataPoint['weightedAverage']
dataDate = datetime.datetime.fromtimestamp(int(nextDataPoint['date'])).strftime('%Y-%m-%d %H:%M:%S')
elif(startTime and not historicalData):
exit()
else:
currentValues = conn.api_query("returnTicker")
lastPairPrice = currentValues[pair]["last"]
dataDate = datetime.datetime.now()
if (len(prices) > 0):
currentMovingAverage = sum(prices) / float(len(prices))
previousPrice = prices[-1]
if (not tradePlaced):
if ( (lastPairPrice > currentMovingAverage) and (lastPairPrice < previousPrice) ):
print "SELL ORDER"
orderNumber = conn.sell(pair,lastPairPrice,.01)
tradePlaced = True
typeOfTrade = "short"
elif ( (lastPairPrice < currentMovingAverage) and (lastPairPrice > previousPrice) ):
print "BUY ORDER"
orderNumber = conn.buy(pair,lastPairPrice,.01)
tradePlaced = True
typeOfTrade = "long"
elif (typeOfTrade == "short"):
if ( lastPairPrice < currentMovingAverage ):
print "EXIT TRADE"
conn.cancel(pair,orderNumber)
tradePlaced = False
typeOfTrade = False
elif (typeOfTrade == "long"):
if ( lastPairPrice > currentMovingAverage ):
print "EXIT TRADE"
conn.cancel(pair,orderNumber)
tradePlaced = False
typeOfTrade = False
else:
previousPrice = 0
print "%s Period: %ss %s: %s Moving Average: %s" % (dataDate,period,pair,lastPairPrice,currentMovingAverage)
prices.append(float(lastPairPrice))
prices = prices[-lengthOfMA:]
if (not startTime):
time.sleep(int(period))
if __name__ == "__main__":
main(sys.argv[1:])