-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement max amount * Update version * Add max_amount in init
- Loading branch information
1 parent
6adc359
commit 57b69d0
Showing
4 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__author__ = "AlpacaJapan Co., Ltd." | ||
__version__ = "0.2.0" | ||
__release__ = "0.2.0" | ||
__version__ = "0.2.1" | ||
__release__ = "0.2.1" | ||
__license__ = "MIT" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import pandas as pd | ||
from typing import List | ||
|
||
from backlight.trades.trades import Trades | ||
|
||
|
||
def limit_max_amount(trades: Trades, max_amount: int) -> Trades: | ||
"""Limit trade by max amount. | ||
Args: | ||
trades: Trades | ||
max_amount: Max amount in absolute value | ||
Result: | ||
Trades | ||
""" | ||
assert max_amount > 0.0 | ||
|
||
current_amount = 0.0 | ||
deleted_ids = [] # type: List[int] | ||
for index, row in trades.iterrows(): | ||
|
||
if row["_id"] in deleted_ids: | ||
continue | ||
|
||
next_amount = current_amount + row["amount"] | ||
if abs(next_amount) > max_amount: | ||
deleted_ids.append(row["_id"]) | ||
continue | ||
|
||
current_amount = next_amount | ||
|
||
return trades[~trades["_id"].isin(deleted_ids)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
from backlight.strategies import filter as module | ||
import pytest | ||
import pandas as pd | ||
import numpy as np | ||
|
||
import backlight | ||
from backlight.strategies.amount_based import simple_entry_and_exit | ||
|
||
|
||
@pytest.fixture | ||
def signal(): | ||
symbol = "usdjpy" | ||
periods = 22 | ||
df = pd.DataFrame( | ||
index=pd.date_range(start="2018-06-06", freq="1min", periods=periods), | ||
data=[ | ||
[1, 0, 0], | ||
[0, 0, 1], | ||
[0, 0, 0], | ||
[1, 0, 0], | ||
[1, 0, 0], | ||
[0, 0, 1], | ||
[0, 0, 1], | ||
[0, 0, 0], | ||
[0, 0, 0], | ||
[1, 0, 0], | ||
[1, 0, 0], | ||
[1, 0, 0], | ||
[0, 0, 1], | ||
[0, 0, 1], | ||
[0, 0, 1], | ||
[0, 0, 0], | ||
[0, 0, 0], | ||
[0, 0, 0], | ||
[1, 0, 0], | ||
[1, 0, 0], | ||
[1, 0, 0], | ||
[1, 0, 0], | ||
], | ||
columns=["up", "neutral", "down"], | ||
) | ||
signal = backlight.signal.from_dataframe(df, symbol) | ||
return signal | ||
|
||
|
||
@pytest.fixture | ||
def market(): | ||
symbol = "usdjpy" | ||
periods = 22 | ||
df = pd.DataFrame( | ||
index=pd.date_range(start="2018-06-06", freq="1min", periods=periods), | ||
data=np.arange(periods)[:, None], | ||
columns=["mid"], | ||
) | ||
market = backlight.datasource.from_dataframe(df, symbol) | ||
return market | ||
|
||
|
||
@pytest.fixture | ||
def trades(market, signal): | ||
max_holding_time = pd.Timedelta("3min") | ||
trades = simple_entry_and_exit(market, signal, max_holding_time) | ||
""" | ||
expected = pd.DataFrame( | ||
index=market.index, | ||
data=[ | ||
[True, 1.0], # 1.0 | ||
[True, -1.0], # 0.0 | ||
[False, 0.0], # 0.0 | ||
[True, 0.0], # 0.0 | ||
[True, 2.0], # 2.0 | ||
[True, -1.0], # 1.0 | ||
[True, -2.0], # -1.0 | ||
[True, -1.0], # -2.0 | ||
[True, 1.0], # -1.0 | ||
[True, 2.0], # 1.0 | ||
[True, 1.0], # 2.0 | ||
[True, 1.0], # 3.0 | ||
[True, -2.0], # 1.0 | ||
[True, -2.0], # -1.0 | ||
[True, -2.0], # -3.0 | ||
[True, 1.0], # -2.0 | ||
[True, 1.0], # -1.0 | ||
[True, 1.0], # 0.0 | ||
[True, 1.0], # 1.0 | ||
[True, 1.0], # 2.0 | ||
[True, 1.0], # 3.0 | ||
[True, -3.0], # 0.0 | ||
], | ||
columns=["exist", "amount"], | ||
) | ||
""" | ||
return trades | ||
|
||
|
||
def test_limit_max_amount(market, trades): | ||
max_amount = 2.0 | ||
limited = module.limit_max_amount(trades, max_amount) | ||
expected = pd.DataFrame( | ||
index=market.index, | ||
data=[ | ||
[True, 1.0], # 1.0 | ||
[True, -1.0], # 0.0 | ||
[False, 0.0], # 0.0 | ||
[True, 0.0], # 0.0 | ||
[True, 2.0], # 2.0 | ||
[True, -1.0], # 1.0 | ||
[True, -2.0], # -1.0 | ||
[True, -1.0], # -2.0 | ||
[True, 1.0], # -1.0 | ||
[True, 2.0], # 1.0 | ||
[True, 1.0], # 2.0 | ||
[False, 0.0], # 2.0 | ||
[True, -2.0], # 0.0 | ||
[True, -2.0], # -2.0 | ||
[False, 0.0], # -2.0 | ||
[True, 1.0], # -1.0 | ||
[True, 1.0], # 0.0 | ||
[False, 0.0], # 0.0 | ||
[True, 1.0], # 1.0 | ||
[True, 1.0], # 2.0 | ||
[False, 0.0], # 2.0 | ||
[True, -2.0], # 0.0 | ||
], | ||
columns=["exist", "amount"], | ||
) | ||
assert (limited.amount == expected.amount[expected.exist]).all() |