Skip to content

Commit

Permalink
Limit max amount (#75)
Browse files Browse the repository at this point in the history
* Implement max amount

* Update version

* Add max_amount in init
  • Loading branch information
keisuke-umezawa authored Feb 4, 2019
1 parent 6adc359 commit 57b69d0
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/backlight/__init__.py
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"
1 change: 1 addition & 0 deletions src/backlight/strategies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
entry_and_exit_at_other_signals,
entry_and_exit_by_expectation,
)
from backlight.strategies.filter import limit_max_amount # noqa
32 changes: 32 additions & 0 deletions src/backlight/strategies/filter.py
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)]
127 changes: 127 additions & 0 deletions tests/strategies/test_filter.py
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()

0 comments on commit 57b69d0

Please sign in to comment.