Skip to content

Commit 52c713b

Browse files
committed
Bug fixes in order failed responses.
Bug fixed in Oanda percentage based unit sizes when PCT table overrides (rare situation) are used. Changes to be committed: modified: Base/Library/CONDccxt.py modified: Base/Library/CONDoanda.py modified: Base/Library/JRRccxt.py modified: Base/Library/JRRoanda.py modified: Base/OANDA-PlaceOrder
1 parent 477e5c7 commit 52c713b

File tree

5 files changed

+65
-49
lines changed

5 files changed

+65
-49
lines changed

Base/Library/CONDccxt.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,4 @@ def OrderProcessor(Orphan):
210210
# Something went wrong
211211
relay.JRLog.Write(f"{Orphan['Key']}: Code Error - {sys.exc_info()[-1].tb_lineno}/{str(e)}",stdOut=False)
212212
return 'Waiting'
213+

Base/Library/CONDoanda.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,11 @@ def OrderProcessor(Orphan):
306306

307307
# Order must be closed as it succedded
308308
newOrder['ID']=oid
309-
relay.WriteLedger(Order=newOrder,Response=result)
309+
relay.WriteLedger(Order=newOrder,Response=None)
310310
return 'Delete'
311311
else:
312312
# Give OliverTwist a response
313313
relay.JRLog.Write(f"{id} -> {cid}: Order failed with {relay.GetFailedReason(result)}",stdOut=False)
314-
relay.JRLog.Write(f"{id} -> {cid}: {result}",stdOut=False)
315314
return 'Waiting'
316315
else:
317316
# Strike did not happen

Base/Library/JRRccxt.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,11 @@ def WriteLedger(self,**kwargs):
730730
IsLog=kwargs.get('Log')
731731
LedgerDirectory=kwargs.get('LedgerDirectory')
732732

733+
if type(Order) is str:
734+
Order=json.loads(Order)
735+
if type(Response) is str:
736+
Response=json.loads(Response)
737+
733738
if Response!=None:
734739
Response.pop('Identity',None)
735740
id=Response['id']
@@ -798,3 +803,5 @@ def FindLedgerID(self,**kwargs):
798803

799804
fh.close()
800805
return data['Detail']
806+
807+

Base/Library/JRRoanda.py

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -584,48 +584,57 @@ def WriteLedger(self,**kwargs):
584584
IsLog=kwargs.get('Log')
585585
LedgerDirectory=kwargs.get('LedgerDirectory')
586586

587-
if 'ID' in Order:
588-
Order.pop('Identity',None)
589-
id=Order['ID']
590-
if Response!=None:
591-
Response.pop('Identity',None)
592-
if 'orderCreateTransaction' in Response:
593-
id=Response['orderCreateTransaction']['id']
594-
elif 'longOrderCreateTransaction' in Response:
595-
id=Response['longOrderCreateTransaction']['id']
596-
elif 'shortOrderCreateTransaction' in Response:
597-
id=Response['shortOrderCreateTransaction']['id']
598-
599-
detail=self.GetOrderDetails(OrderID=id)
600-
601-
ledger={}
602-
ledger['DateTime']=(datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
603-
ledger['ID']=id
604-
ledger['Order']=Order
605-
if Response!=None:
606-
ledger['Response']=Response
607-
ledger['Detail']=detail
608-
609-
# We need the embedded order reference if comming from OliverTwist
610-
if 'Order' in Order:
611-
subOrder=json.loads(Order['Order'])
612-
else:
613-
subOrder=Order
614-
if subOrder['Exchange']!=None and subOrder['Account']!=None and subOrder['Asset']!=None:
615-
fname=subOrder['Exchange']+'.'+subOrder['Account']+'.'+subOrder['Asset']
616-
fname=fname.replace('/','').replace('-','').replace(':','').replace(' ','')
617-
lname=LedgerDirectory+'/'+fname+'.ledger'
618-
619-
# Strip Identity
620-
ledger.pop('Identity',None)
621-
622-
ledgerLock=JRRsupport.Locker(lname)
623-
ledgerLock.Lock()
624-
JRRsupport.AppendFile(lname,json.dumps(ledger)+'\n')
625-
ledgerLock.Unlock()
626-
627-
if type(IsLog)==bool and IsLog==True:
628-
self.Log.Write(f"Ledgered: {subOrder['Exchange']}:{id}",stdOut=False)
587+
if type(Order) is str:
588+
Order=json.loads(Order)
589+
if type(Response) is str:
590+
Response=json.loads(Response)
591+
592+
try:
593+
if 'ID' in Order:
594+
Order.pop('Identity',None)
595+
id=Order['ID']
596+
if Response!=None:
597+
Response.pop('Identity',None)
598+
if 'orderCreateTransaction' in Response:
599+
id=Response['orderCreateTransaction']['id']
600+
elif 'longOrderCreateTransaction' in Response:
601+
id=Response['longOrderCreateTransaction']['id']
602+
elif 'shortOrderCreateTransaction' in Response:
603+
id=Response['shortOrderCreateTransaction']['id']
604+
605+
detail=self.GetOrderDetails(OrderID=id)
606+
607+
ledger={}
608+
ledger['DateTime']=(datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
609+
ledger['ID']=id
610+
ledger['Order']=Order
611+
if Response!=None:
612+
ledger['Response']=Response
613+
ledger['Detail']=detail
614+
615+
# We need the embedded order reference if comming from OliverTwist
616+
if 'Order' in Order:
617+
subOrder=json.loads(Order['Order'])
618+
else:
619+
subOrder=Order
620+
if subOrder['Exchange']!=None and subOrder['Account']!=None and subOrder['Asset']!=None:
621+
fname=subOrder['Exchange']+'.'+subOrder['Account']+'.'+subOrder['Asset']
622+
fname=fname.replace('/','').replace('-','').replace(':','').replace(' ','')
623+
lname=LedgerDirectory+'/'+fname+'.ledger'
624+
625+
# Strip Identity
626+
if 'Identity' in ledger:
627+
ledger.pop('Identity',None)
628+
629+
ledgerLock=JRRsupport.Locker(lname)
630+
ledgerLock.Lock()
631+
JRRsupport.AppendFile(lname,json.dumps(ledger)+'\n')
632+
ledgerLock.Unlock()
633+
634+
if type(IsLog)==bool and IsLog==True:
635+
self.Log.Write(f"Ledgered: {subOrder['Exchange']}:{id}",stdOut=False)
636+
except Exception as err:
637+
self.Log.Write(f"WriteLedger Code Error - {sys.exc_info()[-1].tb_lineno}/{str(err)}",stdOut=False)
629638

630639
# Read ledger entry and locate by ID
631640

Base/OANDA-PlaceOrder

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ def GetPCTamount(relay,close):
4343
else:
4444
expire=(3650*86400)
4545

46+
bal=relay.GetBalance()
47+
mr=float(relay.Markets[relay.Asset]['marginRate'])
48+
4649
# OverridePCTtable should probably be the default state for OliverTwist, since each trade is
4750
# independant. For now though, I am going to not make this default as a safety protocol to keep trades
4851
# linear and controlled. User can override this, but that really is the point. The user MUST take action
4952
# to override the saety layers.
5053

5154
if "OverridePCTtable" not in relay.Active and "OverridePCTtable" not in relay.Order:
52-
bal=relay.GetBalance()
53-
mr=float(relay.Markets[relay.Asset]['marginRate'])
5455
pct,PCTtype=GetPCTtype(relay.Order['Units'])
5556
amount,volume=GetPCTvalue(pct,close,bal,mr)
5657

@@ -69,9 +70,8 @@ def GetPCTamount(relay,close):
6970
if (pct<0 and amount>0) or (pct>0 and amount<0):
7071
amount=amount*-1
7172
else:
72-
bal=relay.GetBalance()
73-
mr=float(relay.Markets[relay.Asset]['marginRate'])
74-
amount=round((((float(relay.Order[currency].replace('%',''))/100)*bal)/close)/mr,8)
73+
pct=float(relay.Order['Units'].replace('%',''))/100
74+
amount=round(((pct*bal)/close)/mr,8)
7575
return amount
7676

7777
# Find the lowest lot size used in the list of open trades. This is a good way to make

0 commit comments

Comments
 (0)