Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.

Commit c39a692

Browse files
committed
adding simple strat #35
1 parent 688ff57 commit c39a692

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from ..strategy import ticks, \
2+
TradingStrategy
3+
from ..structs import MarketData, TradeRequest, TradeResponse
4+
from ..enums import Side, TradeResult, OrderType
5+
from ..logging import STRAT as slog, ERROR as elog
6+
7+
8+
class TestStrategy(TradingStrategy):
9+
def __init__(self) -> None:
10+
super(TestStrategy, self).__init__()
11+
self.active = False
12+
self.prev_state = ''
13+
self.state = ''
14+
15+
self.bought = 0.0
16+
self.bought_qty = 0.0
17+
self.profits = 0.0
18+
19+
def onBuy(self, res: TradeResponse) -> None:
20+
if not res.status == TradeResult.FILLED:
21+
slog.critical('order failure: %s' % res)
22+
return
23+
24+
self.bought = res.volume*res.price
25+
self.bought_qty = res.volume
26+
slog.critical('d->g:bought %.2f @ %.2f for %.2f' % (res.volume, res.price, self.bought))
27+
28+
def onSell(self, res: TradeResponse) -> None:
29+
if not res.status == TradeResult.FILLED:
30+
slog.info('order failure: %s' % res)
31+
return
32+
33+
sold = res.volume*res.price
34+
profit = sold - self.bought
35+
self.profits += profit
36+
slog.critical('g->d:sold %.2f @ %.2f for %.2f - %.2f - %.2f' % (res.volume, res.price, sold, profit, self.profits))
37+
self.bought = 0.0
38+
self.bought_qty = 0.0
39+
40+
@ticks
41+
def onTrade(self, data: MarketData) -> bool:
42+
if not self.active:
43+
req = TradeRequest(side=Side.BUY,
44+
# buy between .2 and 1 BTC
45+
volume=max(min(1.0, data.volume), .2),
46+
currency=data.currency,
47+
order_type=OrderType.MARKET,
48+
price=data.price)
49+
slog.critical("requesting buy : %s", req)
50+
self.requestBuy(self.onBuy, req)
51+
return True
52+
else:
53+
req = TradeRequest(side=Side.SELL,
54+
volume=self.bought_qty,
55+
currency=data.currency,
56+
order_type=OrderType.MARKET,
57+
price=data.price)
58+
slog.critical("requesting sell : %s", req)
59+
self.requestSell(self.onSell, req)
60+
return True
61+
62+
return False
63+
64+
def onError(self, e) -> None:
65+
elog.critical(e)
66+
67+
def onAnalyze(self, _) -> None:
68+
pass
69+
70+
def onChange(self, data: MarketData) -> None:
71+
pass
72+
73+
def onContinue(self, data: MarketData) -> None:
74+
pass
75+
76+
def onDone(self, data: MarketData) -> None:
77+
pass
78+
79+
def onHalt(self, data: MarketData) -> None:
80+
pass
81+
82+
def onOpen(self, data: MarketData) -> None:
83+
pass
84+
85+
def onReceived(self, data: MarketData) -> None:
86+
pass
87+
88+
def slippage(self, resp: TradeResponse) -> TradeResponse:
89+
return resp
90+
91+
def transactionCost(self, resp: TradeResponse) -> TradeResponse:
92+
return resp

0 commit comments

Comments
 (0)