Skip to content

Commit

Permalink
Code verification and cleanup.
Browse files Browse the repository at this point in the history
Proof of cencept testing and safety protocol analysis conducted on PCTtables...

Changes to be committed:
	modified:   Base/Library/CONDccxt.py
	modified:   Base/Library/JRRoanda.py
	modified:   Base/Library/JackrabbitRelay.py
	modified:   Base/OANDA-PlaceOrder
	new file:   Extras/CodeProofs/enforceFIFO
  • Loading branch information
rapmd73 committed May 23, 2024
1 parent eaae942 commit 477e5c7
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 12 deletions.
1 change: 0 additions & 1 deletion Base/Library/CONDccxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ def OrderProcessor(Orphan):
else:
# Give OliverTwist a response
relay.JRLog.Write(f"{id}: Order failed with {relay.GetFailedReason(result)}",stdOut=False)
relay.JRLog.Write(f"{id} -> {cid}: {result}",stdOut=False)
return 'Waiting'
else:
# Strike did not happen
Expand Down
18 changes: 9 additions & 9 deletions Base/Library/JRRoanda.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,21 +337,21 @@ def PlaceOrder(self,**kwargs):
if 'orderCreateTransaction' in self.Results:
if 'orderCancelTransaction' in self.Results:
if Quiet!=True:
self.Log.Write("|- Order CANCELLED: "+self.Results['orderCancelTransaction']['reason'])
self.Log.Write("|- Order failed with: CANCELLED "+self.Results['orderCancelTransaction']['reason'])
else:
if Quiet!=True:
self.Log.Write("|- Order Confirmation ID: "+self.Results['orderCreateTransaction']['id'])
elif 'longOrderCreateTransaction' in self.Results:
if 'orderCancelTransaction' in self.Results:
if Quiet!=True:
self.Log.Write("|- Order CANCELLED: "+self.Results['orderCancelTransaction']['reason'])
self.Log.Write("|- Order failed with: CANCELLED "+self.Results['orderCancelTransaction']['reason'])
else:
if Quiet!=True:
self.Log.Write("|- Order Confirmation ID: "+self.Results['longOrderCreateTransaction']['id'])
elif 'shortOrderCreateTransaction' in self.Results:
if 'orderCancelTransaction' in self.Results:
if Quiet!=True:
self.Log.Write("|- Order CANCELLED: "+self.Results['orderCancelTransaction']['reason'])
self.Log.Write("|- Order failed with: CANCELLED "+self.Results['orderCancelTransaction']['reason'])
else:
if Quiet!=True:
self.Log.Write("|- Order Confirmation ID: "+self.Results['shortOrderCreateTransaction']['id'])
Expand All @@ -376,21 +376,21 @@ def PlaceOrder(self,**kwargs):
if 'orderCreateTransaction' in self.Results:
if 'orderCancelTransaction' in self.Results:
if Quiet!=True:
self.Log.Write("|- Order CANCELLED: "+self.Results['orderCancelTransaction']['reason'])
self.Log.Write("|- Order failed with: CANCELLED "+self.Results['orderCancelTransaction']['reason'])
else:
if Quiet!=True:
self.Log.Write("|- Order Confirmation ID: "+self.Results['orderCreateTransaction']['id'])
elif 'longOrderCreateTransaction' in self.Results:
if 'orderCancelTransaction' in self.Results:
if Quiet!=True:
self.Log.Write("|- Order CANCELLED: "+self.Results['orderCancelTransaction']['reason'])
self.Log.Write("|- Order failed with: CANCELLED "+self.Results['orderCancelTransaction']['reason'])
else:
if Quiet!=True:
self.Log.Write("|- Order Confirmation ID: "+self.Results['longOrderCreateTransaction']['id'])
elif 'shortOrderCreateTransaction' in self.Results:
if 'orderCancelTransaction' in self.Results:
if Quiet!=True:
self.Log.Write("|- Order CANCELLED: "+self.Results['orderCancelTransaction']['reason'])
self.Log.Write("|- Order failed with: CANCELLED "+self.Results['orderCancelTransaction']['reason'])
else:
if Quiet!=True:
self.Log.Write("|- Order Confirmation ID: "+self.Results['shortOrderCreateTransaction']['id'])
Expand All @@ -411,21 +411,21 @@ def PlaceOrder(self,**kwargs):
if 'orderCreateTransaction' in self.Results:
if 'orderCancelTransaction' in self.Results:
if Quiet!=True:
self.Log.Write("|- Order CANCELLED: "+self.Results['orderCancelTransaction']['reason'])
self.Log.Write("|- Order failed with: CANCELLED "+self.Results['orderCancelTransaction']['reason'])
else:
if Quiet!=True:
self.Log.Write("|- Order Confirmation ID: "+self.Results['orderCreateTransaction']['id'])
elif 'longOrderCreateTransaction' in self.Results:
if 'orderCancelTransaction' in self.Results:
if Quiet!=True:
self.Log.Write("|- Order CANCELLED: "+self.Results['orderCancelTransaction']['reason'])
self.Log.Write("|- Order failed with: CANCELLED "+self.Results['orderCancelTransaction']['reason'])
else:
if Quiet!=True:
self.Log.Write("|- Order Confirmation ID: "+self.Results['longOrderCreateTransaction']['id'])
elif 'shortOrderCreateTransaction' in self.Results:
if 'orderCancelTransaction' in self.Results:
if Quiet!=True:
self.Log.Write("|- Order CANCELLED: "+self.Results['orderCancelTransaction']['reason'])
self.Log.Write("|- Order failed with: CANCELLED "+self.Results['orderCancelTransaction']['reason'])
else:
if Quiet!=True:
self.Log.Write("|- Order Confirmation ID: "+self.Results['shortOrderCreateTransaction']['id'])
Expand Down
4 changes: 3 additions & 1 deletion Base/Library/JackrabbitRelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,11 @@ def GetFailedReason(self,res):
if res==None:
return None

srch='failed with:'

result=None
try:
if res.find('PlaceOrder failed with:')>-1:
if res.find(srch)>-1:
s=res.find('with:')+6
for e in range(s,len(res)):
if res[e]=='\n':
Expand Down
7 changes: 6 additions & 1 deletion Base/OANDA-PlaceOrder
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ def GetPCTamount(relay,close):
else:
expire=(3650*86400)

# OverridePCTtable should probably be the default state for OliverTwist, since each trade is
# independant. For now though, I am going to not make this default as a safety protocol to keep trades
# linear and controlled. User can override this, but that really is the point. The user MUST take action
# to override the saety layers.

if "OverridePCTtable" not in relay.Active and "OverridePCTtable" not in relay.Order:
bal=relay.GetBalance()
mr=float(relay.Markets[relay.Asset]['marginRate'])
Expand Down Expand Up @@ -263,8 +268,8 @@ def main():
relay.JRLog.Write(f'|- Minimum Amount: {minimum:.8f}')
relay.JRLog.Write(f'|- Minimum Cost: {mincost:.8f}')
else:
price=ticker['Ask']
if '%' in relay.Order['Units']:
price=(ticker['Ask']+ticker['Bid'])/2
amount=int(GetPCTamount(relay,price))
else:
amount=int(relay.Order['Units'].split('.')[0])
Expand Down
67 changes: 67 additions & 0 deletions Extras/CodeProofs/enforceFIFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Jackrabbit Relay
# 2021 Copyright © Robert APM Darin
# All rights reserved unconditionally.

import sys
sys.path.append('/home/GitHub/JackrabbitRelay/Base/Library')
import os
import json

import JackrabbitRelay as JRR

# Find the lowest lot size used in the list of open trades. This is a good way to make
# the entire process dynamic and fluid as lot size can be varied by any metric,
# including but not limited to, the amount of margin used.

def LowestLotSize(relay,asset,units,step):
lowestSize=abs(units)
lotSize=abs(step)

openTrades=relay.GetOpenTrades(symbol=asset)

usedSize=[]
for trade in openTrades:
t=abs(int(trade['currentUnits']))
if t not in usedSize:
usedSize.append(t)
t=abs(int(trade['initialUnits']))
if t not in usedSize:
usedSize.append(t)
usedSize=sorted(usedSize)

print(usedSize)

nextSize = lowestSize # Start with the lowestSize

while nextSize in usedSize:
nextSize+=lotSize

# Shorts are negative so check direction
if units<0:
sign=-1
else:
sign=1

print(f'EnforceFIFO: next unit size is {nextSize*sign}')
return nextSize*sign

###
### Main code base. Place order on exchange
###

relay=JRR.JackrabbitRelay()
if relay.GetArgsLen() > 3:
exchangeName=relay.GetExchange()
account=relay.GetAccount()
asset=relay.GetAsset()
units=int(relay.GetArgs(4))
else:
print("An exchange, (sub)account, and an asset must be provided.")
sys.exit(1)

ls=LowestLotSize(relay,asset,units,1)

print(ls)

0 comments on commit 477e5c7

Please sign in to comment.