-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathexample.py
More file actions
63 lines (56 loc) · 2.27 KB
/
example.py
File metadata and controls
63 lines (56 loc) · 2.27 KB
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
import asyncio
import tulipy # Can be any TA library.
import octobot_script as obs
async def example():
async def initialize(ctx):
# Compute entries only once per backtest.
closes = await obs.Close(ctx, max_history=True)
times = await obs.Time(ctx, max_history=True, use_close_time=True)
rsi_v = tulipy.rsi(closes, period=ctx.tentacle.trading_config["period"])
delta = len(closes) - len(rsi_v)
# Populate entries with timestamps of candles where RSI is
# bellow the "rsi_value_buy_threshold" configuration.
run_data["entries"] = {
times[index + delta]
for index, rsi_val in enumerate(rsi_v)
if rsi_val < ctx.tentacle.trading_config["rsi_value_buy_threshold"]
}
await obs.plot_indicator(ctx, "RSI", times[delta:], rsi_v, run_data["entries"])
async def strategy(ctx):
# Called at each candle.
# Uses pre-computed entries times to enter positions when relevant.
# Also, instantly set take profits and stop losses.
# Position exits could also be set separately.
if obs.current_live_time(ctx) in run_data["entries"]:
await obs.market(
ctx,
"buy",
amount="10%",
stop_loss_offset="-15%",
take_profit_offset="25%",
)
# Configuration that will be passed to each run.
# It will be accessible under "ctx.tentacle.trading_config".
config = {
"period": 10,
"rsi_value_buy_threshold": 28,
}
# Read and cache candle data to make subsequent backtesting runs faster.
data = await obs.get_data(
"BTC/USDT", "1d", start_timestamp=1505606400, social_services=[]
)
run_data = {
"entries": None,
}
# Run a backtest using the above data, strategy and configuration.
res = await obs.run(
data, config, initialize_func=initialize, strategy_func=strategy
)
print(res.describe())
# Generate and open report including indicators plots
await obs.generate_and_show_report(res)
# Stop data to release local databases.
await data.stop()
# Call the execution of the script inside "asyncio.run" as
# OctoBot-Script runs using the python asyncio framework.
asyncio.run(example())