Struggelig a bit with my trading algo. It seems that the orders cant finish before the next onBars() is called, and the quantities become a mess. Im using the enterLongLimit() to enter a trade, which calls onEnterOk() when it finishes - but im using limitOrder to exit parts of a position based on some technical indicator, and that doesnt seem to call onExitOk().
def onExitOk(self, position):
print("Exit ok", position.getExitOrder().getExecutionInfo().getDateTime())
def onEnterOk(self, position):
print("Enter ok", position.getEntryOrder().getExecutionInfo())
def _closePosition(self, price, qty, reason, date):
print("Closing position with price", price, "and closing qty", qty)
brk = self.getBroker()
shares = brk.getShares(self.instrument) * qty
print("Cash now before sell: ", brk.getCash(self.instrument))
self.info("Sell BTC %s at %s because %s on %s " % (shares, price, reason, date))
self.position = self.limitOrder(self.instrument, price, shares*-1)
print("Cash now after sell: ", brk.getCash(self.instrument))
Execution:
Closing position with price 746.3 and closing qty 0.5
Cash now before sell: 17.423283999999967
Cash now after sell: 17.423283999999967
The cash before and after the limitOrder is the same, so i have to wait for the event to come in. Ideas?
I had a similar problem and then after some digging I found that onEnter and onExit events are only triggered when a position is opened using one of the enterLong or enterShort methods. Trades executed with stop, limit or stop-limit orders do not trigger onEnter or onExit events which is a real shame.
This what the comments in the source code say, ie.:
def onEnterOk(self, position):
"""Override (optional) to get notified when the order submitted to enter a position was filled. The default implementation is empty.
:param position: A position returned by any of the enterLongXXX or enterShortXXX methods.
:type position: :class:`pyalgotrade.strategy.position.Position`.
"""
pass
See the strategy class code.
Your limit order should still work as expected even though you do not get the notification.
Related
I am trying to define a function that divides Amount of money by Number of days.
So far the user can submit values, but I don't know how to make the result display on my Streamlit web app.
I copied part of my code below.
P.S. I am also a complete beginner in Python.
Thanks for any help
#HOW OFTEN EAT OUT
st.write("2. How often do you eat out?")
form04 = st.form(key='form04')
days = form04.text_input('Please enter average number of days')
submit04 = form04.form_submit_button('Submit')
#HOW MUCH INCOME
if submit04:
st.write('3. What is your monthly income?')
form05 = st.form(key='form05')
income = form05.text_input('Please enter monthly income')
submit05 = form05.form_submit_button('Submit')
if submit05:
def idealbudget(days, income):
budget=float(income)/float(days)
return float(budget)
st.write('Result is', budget)
In this code snippet, you define a function but you never actually call it:
if submit05:
def idealbudget(days, income):
budget=float(income)/float(days)
return float(budget)
st.write('Result is', budget)
Additionally, your st.write call is tabbed incorrectly, it should be at the same level as the def statement. A working solution probably looks like the following (untested):
if submit05:
def idealbudget(days, income):
budget=float(income)/float(days)
return float(budget)
st.write('Result is', idealbudget(days, income))
My problem looks weird.
Exchange 2010 SP1. Code looks for conflicting record in Room Calendar and suposed to delete it.
Code looks like:
#DELEGATE also doesn't work
roomAccount = Account(primary_smtp_address=room_mailbox, config=config,
autodiscover=False, access_type=IMPERSONATION)
items = roomAccount.calendar.filter(start__gt=now_with_past)
for item in items:
if (start_dt_ews != start_dt_remote or item.subject != remote_record_subject or duration != remote_record_duration):
has_conflicts = detect_conflicts(roomAccount, item)
if has_conflicts:
process_conflict(item, 'update_conflict')
remove_meeting_data(item, item.organizer)
continue
def detect_conflicts(roomAccount, item):
try:
has_conflicts = False
if item.conflicting_meeting_count > 0:
if item.start_dt != item.start:
has_conflicts = True
return has_conflicts
except Exception as e:
trace_back = traceback.format_exc()
log_str = "Error in process_service " + str(e) + " " + str(trace_back)
Logger.error(log_str)
return False
def process_conflict(item, category):
if item.recurrence:
conflict_notify_and_delete(item, category, True)
else:
conflict_notify_and_delete(item, category, False)
Logger.error(item.subject + " meeting conflict error.")
def conflict_notify_and_delete(item, category, serial):
send_email(item.organizer, category, (item.subject))
try:
if serial:
item.delete(affected_task_occurrences=ALL_OCCURRENCIES)
item.save()
else:
# doesn't work
item.delete(send_meeting_cancellations=SEND_TO_NONE,
affected_task_occurrences=ALL_OCCURRENCIES)
# or also doesn't work
item.delete()
# or raise trash folder absense error.
item.move_to_trash()
# or raise trash folder absense error again.
item.soft_delete()
#abrakadabra atempts with rescheduling and subsequent deletion raise
# "Set action is invalid for property. (field: FieldURI(field_uri='calendar:StartTimeZone'))" error
item.start = UTC_NOW() + timedelta(days=6000)
item.save(update_fields=['start'])
item.delete()
The strangest fact about all this - any of delete() processing simply silent - no errors, no exceptions, everything looks like to be just fine while actually nothing is deleted of modified.. Second strangest fact - sometimes but not every time i'm trying to item.save() after item.delete() , it raise " AttributeError("Folder must be supplied when in save-only mode")", but item may be deleted at once. And may be not :((
This weird things happen only in part of code that process conflicting calendar items. Notconflicting items processing is fine - deleting and modifiying are ok.
Does anybody has any idea, what is going on and how to finally delete conflicting record from Room calendar without canceling a meeting from organizers's calendar - we do not want user to loose his item and information inside it? I've tried to google EWS setiings that can cause such weirdness but with no luck :(
I have the following select statement:
SELECT [TR_DATE]
,[TR_TIME]
,RIGHT('000000' + CONVERT(varchar,TR_TIME), 6)
,TIMEFROMPARTS(SUBSTRING(RIGHT('000000' + CONVERT(varchar,TR_TIME), 6),1,2), SUBSTRING(RIGHT('000000' + CONVERT(varchar,TR_TIME), 6),3,2), SUBSTRING(RIGHT('000000' + CONVERT(varchar,TR_TIME), 6),5,2),0,0) as trtime
,[ET_TYPENO]
,[ET_NAME]
FROM [DB400].[dbo].[TRANSACTIONREPORT]
where DEPT_NAME = 'GIJIMA AST HOLDINGS (PTY) LTD'
/*and LOC_NAME = 'Front Door and Gate'*/
and TR_DATE between '20200120' and '20200130'
order by TR_DATE, MST_LASTNAME, MST_FIRSTNAME, TR_TIME
which returns the following data:
I would like to use this to calculate the time the was spent in the building.
Note that ET_TYPENO = 1 equates to "Allowed Normal In" and ET_TYPENO = 2 to "Allowed Normal Out"
I have the following expression inside an ssrs report
=iif(Fields!ET_TYPENO.Value=2,
DateDiff(DateInterval.Hour, previous(Fields!trtime.Value),Fields!trtime.Value),
"")
But it resolves to the following #Error.
UPDATE
Expected Result
Calculate the time difference between the "Allowed Nornmal In" event and the "Allowed Normal Out" event.
Take line 2 and 3 for example. "Allowed Nornmal In" occured at 07:23:19 and "Allowed Normal Out" occured at 08:55:48. I need it to return the time difference between these two times. I.E. 1:32:29.
The trtime needs to be converted to a DATE type with the CDATE() function to use the DATEDIFF function.
=IIF(Fields!ET_TYPENO.Value = "OUT",
DateDiff(DateInterval.Hour, CDATE(previous(Fields!trtime.Value)), CDATE(Fields!trtime.Value)),
NOTHING)
Also, shouldn't Fields!ET_TYPENO.Value = 1 ?
Taking the suggestion from Honnover Fist's answer that the parameter of the DateDiff need to be Dates instead of Times. (The Documentation does say that a Time will work). I have changed my query to a DATETIMEFROMPARTS() instead of a TIMEFROMPARTS().
Then I changed the condition to the below code so that the hours would be displayed more accurately.
=IIF(Fields!ET_TYPENO.Value = 2,
DateDiff(DateInterval.Minute, previous(Fields!TR_DATETIME.Value), Fields!TR_DATETIME.Value) / 60,
NOTHING)
I want to calculate test statistics for a fb prophet forecast in a function because I want to average the test stats over different forecasts and cutoff points after using the fb-prophet cross_validation to get df_cv. I created a function that I apply to the dataframe after grouping by the cutoff points, in order to receive a measure per cutoff point. Then I calculate the mean over all these values.
The problem is that my function returns not only the value I am looking for but also a 0 as well as an information of the dtype. I can still do calculations with the returned value but when I want to plot etc. later it is very inconvenient. How can I strip these unnecessary values from the output?
def compute_avg_stats(df_cv,perf_measure):
measures = {'mse':mean_squared_error,'mae':mean_absolute_error,'mape':mean_absolute_percentage_error,'rmse':mean_squared_error}
performance_stats = {}
if perf_measure == 'rmse':
measure = np.sqrt(measures[perf_measure](y_true=df_cv['y'],y_pred=df_cv['yhat']))
else:
measure = measures[perf_measure](y_true=df_cv['yu'],y_pred=df_cv['yhat'])
return measure
df_cv.groupby('cutoff').apply(compute_avg_stats,perf_measure='rmse').to_frame().mean()
I think .mean() returns a Series. Try with .mean()[0]
I'm new to python and have been trying like hell for the past few hours to figure out how to get this to work properly...
It's very simple code I'm sure, but I'm just not getting it.
It should be pretty self-explanatory below in the code, but basically I'm asking a user to input the date of an event as an 'int' and if it's not a number, then ask them to try again... UNLESS it's a "?"
while True:
date = None
street = str(input('Name of street?: ').title())
city = str(input("In what city?: ").title())
while True:
try:
year = int(input("Date of event? (or '?'): "))
if date == "?":
break
except Exception:
print("That's not a date, try again!")
continue
break
It seems that it's not even getting to see IF because it gets caught by the 'except' before it can.
If you're going to display help or something when a '?' is input, then just call the function to display the help where you have the break currently.
if date == "?":
display_help()
continue
Then, split reading the input and processing it into two steps.
in = input("Date of event? (or '?'): ")
if in == "?":
display_help()
continue
year = int(in)
Also, you ask for a date but then assume that a year is entered, I'd be more explicit in your promt.
"Please enter the year of the event, ex: 1998"
or whatever form you actually want it in.
Trying using a valueError exception. Also I think in your post you mentioned you wanted to enter a date as integer, so I replaced year with the date. If you wanted the year to be an integer you can replace the variable date with year. If you wanted to the user to enter a year, day and month then this program needs to be redesigned a bit.
date = None
street = str(input('Name of street?: ').title())
city = str(input("In what city?: ").title())
while True:
date = input("Date of event? (or '?'): ")
if date == "?":
break
else:
try:
date = int(date)
except ValueError:
print("That's not a date, try again!")
continue
break