-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemacrossover.txt
52 lines (38 loc) · 1.48 KB
/
emacrossover.txt
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
def initialize(state):
state.number_offset_trades = 0;
@schedule(interval="1h", symbol="BTCUSDT")
def handler(state, data):
'''
1) Compute indicators from data
'''
ema_long = data.ema(40).last
ema_short = data.ema(20).last
# on erronous data return early (indicators are of NoneType)
if ema_long is None:
return
current_price = data.close_last
'''
2) Fetch portfolio
'''
portfolio = query_portfolio()
balance_quoted = portfolio.excess_liquidity_quoted
# we invest only 95% of available liquidity
buy_value = float(balance_quoted) * 0.95
'''
3) Fetch position for symbol
'''
position = query_open_position_by_symbol(data.symbol,include_dust=False)
has_position = position is not None
'''
4) Resolve buy or sell signals
'''
if ema_short > ema_long and not has_position:
print("-------")
print("Buy Signal: creating market order for {}".format(data.symbol))
print("Buy value: ", buy_value, " at current market price: ", data.close_last)
order_market_value(symbol=data.symbol, value=buy_value)
elif ema_short < ema_long and has_position:
print("-------")
logmsg = "Sell Signal: closing {} position with exposure {} at current market price {}"
print(logmsg.format(data.symbol,float(position.exposure),data.close_last))
close_position(data.symbol)