-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample.py
54 lines (41 loc) · 1.44 KB
/
sample.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
"""Barebones implementation of a Cryptle strategy without using optional Cryptle
subsystems.
The sample is tested with a historical dataset in the main() function by manual
wiring of data source and a mock exchange object. For more a more detailed look
at backtest and deployment in Cryptle, refer to the topic guides in
documentation.
"""
import os
import sys
import logging
import cryptle.logging
from cryptle.strategy import SingleAssetStrategy
from cryptle.exchange import Paper
class BTCStrat(SingleAssetStrategy):
def setTrade(self):
self.buy = 0
self.sell = 0
def onTrade(self, price, timestamp, volume, action):
if price < 200 and not self.hasBalance():
self.buy += 1
self.marketBuy(1)
if price > 400 and self.hasBalance():
self.sell += 1
self.marketSell(1)
def main():
starting_capital = 10000
exchange = Paper(starting_capital)
strat = MillionDollarStrat(exchange, 'bch')
strat.portfolio.cash = starting_capital
strat.setTrade()
# get sample data from the unittest package
from test import utils
trades = utils.get_sample_trades()
for trade in trades:
price, ts, vol, action = trade
exchange.updatePrice('bchusd', float(price))
strat.pushTrade(float(price), int(ts), float(vol), int(action))
if __name__ == '__main__':
logger = logging.getLogger('cryptle')
logger.setLevel(logging.DEBUG)
main()