@@ -699,15 +699,16 @@ def new_order(self,
699
699
tp = tp and float (tp )
700
700
701
701
is_long = size > 0
702
+ adjusted_price = self ._adjusted_price (size )
702
703
703
704
if is_long :
704
- if not (sl or - np .inf ) <= (limit or stop or self . last_price ) <= (tp or np .inf ):
705
+ if not (sl or - np .inf ) < (limit or stop or adjusted_price ) < (tp or np .inf ):
705
706
raise ValueError ("Long orders require: SL ({}) < LIMIT ({}) < TP ({})" .format (
706
- sl , limit or stop or self . last_price , tp ))
707
+ sl , limit or stop or adjusted_price , tp ))
707
708
else :
708
- if not (tp or - np .inf ) <= (limit or stop or self . last_price ) <= (sl or np .inf ):
709
+ if not (tp or - np .inf ) < (limit or stop or adjusted_price ) < (sl or np .inf ):
709
710
raise ValueError ("Short orders require: TP ({}) < LIMIT ({}) < SL ({})" .format (
710
- tp , limit or stop or self . last_price , sl ))
711
+ tp , limit or stop or adjusted_price , sl ))
711
712
712
713
order = Order (self , size , limit , stop , sl , tp , trade )
713
714
# Put the new order in the order queue,
@@ -730,11 +731,13 @@ def new_order(self,
730
731
731
732
@property
732
733
def last_price (self ) -> float :
733
- """Return price at the last (current) close.
734
- Used e.g. in `Orders._is_price_ok()` to see if the set price is reasonable.
735
- """
734
+ """ Price at the last (current) close. """
736
735
return self ._data .Close [- 1 ]
737
736
737
+ def _adjusted_price (self , size = None , price = None ) -> float :
738
+ """ Long/short `price`, adjusted for commitions."""
739
+ return (price or self .last_price ) * (1 + copysign (self ._commission , size ))
740
+
738
741
@property
739
742
def equity (self ) -> float :
740
743
return self ._cash + sum (trade .pl for trade in self .trades )
@@ -839,7 +842,7 @@ def _process_orders(self):
839
842
840
843
# Adjust price to include commission (or bid-ask spread).
841
844
# In long positions, the adjusted price is a fraction higher, and vice versa.
842
- adjusted_price = price * ( 1 + copysign ( self ._commission , order .size ) )
845
+ adjusted_price = self ._adjusted_price ( order .size , price )
843
846
844
847
# If order size was specified proportionally,
845
848
# precompute true size in units, accounting for margin and spread/commissions
0 commit comments