-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.py
51 lines (38 loc) · 1.37 KB
/
example.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
import logging
import numpy as np
import pandas as pd
from mikasa import BT, DataSeries, SMAIndicator
import ccxt
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
exchange = ccxt.binance()
# create strategy for backtesting
class SMABT(BT):
def prepare_data(self):
self.dataseries.add_indicators([SMAIndicator(period=100)])
# set up how to process each bar
def process_bar(self):
d = self.dataseries
if np.isnan(d[0].sma):
return
if d[-1].close < d[-1].sma:
if d[0].close > d[0].sma:
self.buy(d[0].close, 0.005)
if d[-1].close > d[-1].sma:
if d[0].close < d[0].sma:
self.sell(d[0].close)
if __name__ == "__main__":
# upload and map data from CSV
ohlcv = exchange.fetch_ohlcv("BTC/USDT", timeframe="1d", limit=1000)
df = pd.DataFrame(ohlcv, columns=["datetime", "open", "high", "low", "close", "volume"])
df["datetime"] = pd.to_datetime(df["datetime"], unit="ms")
# create DataSeries instance
ds = DataSeries(df)
# create instance of BT and set params
bt = SMABT(ds, balance=1000.0)
# run backtesting
bt.run()
logger.info(f"Profit: ${bt.get_profit():.2f}")
logger.info(f"ROI: ${bt.get_roi()*100:.2f}%")
# plot data and indicators
bt.plot()