Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 67 additions & 68 deletions README.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions examples/backtest_example/run_backtest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from datetime import datetime
import logging.config
from datetime import datetime, timedelta

Expand Down Expand Up @@ -124,6 +125,7 @@ def apply_strategy(self, context: Context, market_data):
trade = context.get_trade(order_id=order.id)
context.add_stop_loss(
trade=trade,
trade_risk_type="trailing",
percentage=5,
sell_percentage=50
)
Expand Down
37 changes: 37 additions & 0 deletions investing_algorithm_framework/domain/models/trade/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ def __init__(
status,
net_gain=0,
last_reported_price=None,
last_reported_price_datetime=None,
high_water_mark=None,
high_water_mark_datetime=None,
updated_at=None,
stop_losses=None,
take_profits=None,
Expand All @@ -76,12 +78,47 @@ def __init__(
self.remaining = remaining
self.net_gain = net_gain
self.last_reported_price = last_reported_price
self.last_reported_price_datetime = last_reported_price_datetime
self.high_water_mark = high_water_mark
self.high_water_mark_datetime = high_water_mark_datetime
self.status = status
self.updated_at = updated_at
self.stop_losses = stop_losses
self.take_profits = take_profits

def update(self, data):

if "status" in data:
self.status = TradeStatus.from_value(data["status"])

if TradeStatus.CLOSED.equals(self.status):

# Set all stop losses to inactive
if self.stop_losses is not None:
for stop_loss in self.stop_losses:
stop_loss.active = False

# set all take profits to inactive
if self.take_profits is not None:
for take_profit in self.take_profits:
take_profit.active = False

if "last_reported_price" in data:
self.last_reported_price = data["last_reported_price"]

if self.high_water_mark is None:
self.high_water_mark = data["last_reported_price"]
self.high_water_mark_datetime = \
data["last_reported_price_datetime"]
else:

if data["last_reported_price"] > self.high_water_mark:
self.high_water_mark = data["last_reported_price"]
self.high_water_mark_datetime = \
data["last_reported_price_datetime"]

return super().update(data)

@property
def closed_prices(self):
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,26 @@ def __init__(
total_amount_trade: float,
sell_percentage: float = 100,
active: bool = True,
sell_prices: str = None
sell_prices: str = None,
sell_price_dates: str = None,
high_water_mark_date: str = None,
):
self.trade_id = trade_id
self.trade_risk_type = trade_risk_type
self.percentage = percentage
self.sell_percentage = sell_percentage
self.high_water_mark = open_price
self.high_water_mark_date = high_water_mark_date
self.open_price = open_price
self.stop_loss_price = self.high_water_mark * \
(1 - (self.percentage / 100))
self.sell_amount = total_amount_trade * (self.sell_percentage / 100)
self.sold_amount = 0
self.active = active
self.sell_prices = sell_prices
self.sell_price_dates = sell_price_dates

def update_with_last_reported_price(self, current_price: float):
def update_with_last_reported_price(self, current_price: float, date):
"""
Function to update the take profit price based on the last
reported price.
Expand All @@ -88,13 +92,16 @@ def update_with_last_reported_price(self, current_price: float):

if TradeRiskType.FIXED.equals(self.trade_risk_type):
# Check if the current price is less than the high water mark
if current_price > self.high_water_mark:
self.high_water_mark = current_price
return
else:
# Check if the current price is less than the stop loss price
if current_price <= self.stop_loss_price:
return
elif current_price > self.high_water_mark:
self.high_water_mark = current_price
self.high_water_mark_date = date
self.stop_loss_price = self.high_water_mark * \
(1 - (self.percentage / 100))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,26 @@ def __init__(
total_amount_trade: float,
sell_percentage: float = 100,
active: bool = True,
sell_prices: str = None
sell_prices: str = None,
sell_price_dates: str = None,
high_water_mark_date: str = None,
):
self.trade_id = trade_id
self.trade_risk_type = trade_risk_type
self.percentage = percentage
self.sell_percentage = sell_percentage
self.high_water_mark = None
self.high_water_mark_date = high_water_mark_date
self.open_price = open_price
self.take_profit_price = open_price * \
(1 + (self.percentage / 100))
self.sell_amount = total_amount_trade * (self.sell_percentage / 100)
self.sold_amount = 0
self.active = active
self.sell_prices = sell_prices
self.sell_price_dates = sell_price_dates

def update_with_last_reported_price(self, current_price: float):
def update_with_last_reported_price(self, current_price: float, date):
"""
Function to update the take profit price based on
the last reported price.
Expand All @@ -85,15 +89,28 @@ def update_with_last_reported_price(self, current_price: float):

# Do nothing for fixed take profit
if TradeRiskType.FIXED.equals(self.trade_risk_type):

if self.high_water_mark is not None:
if current_price > self.high_water_mark:
self.high_water_mark = current_price
self.high_water_mark_date = date
else:
if current_price >= self.take_profit_price:
self.high_water_mark = current_price
self.high_water_mark_date = date
return

return
else:

if self.high_water_mark is None:

if current_price >= self.take_profit_price:
self.high_water_mark = current_price
self.high_water_mark_date = date
new_take_profit_price = self.high_water_mark * \
(1 - (self.percentage / 100))

if self.take_profit_price <= new_take_profit_price:
self.take_profit_price = new_take_profit_price

Expand All @@ -106,6 +123,7 @@ def update_with_last_reported_price(self, current_price: float):
# Increase the high water mark and take profit price
elif current_price > self.high_water_mark:
self.high_water_mark = current_price
self.high_water_mark_date = date
new_take_profit_price = self.high_water_mark * \
(1 - (self.percentage / 100))

Expand Down
Loading
Loading