Skip to content

Commit

Permalink
**IMPORTANT:** You will need to run UpdatePlaceOrder with the list of…
Browse files Browse the repository at this point in the history
… cryptocurrency exchanges you are

currently using.

Massive code update to support proper position flipping where the *entire* position is *closed* before the
new direction is opened. Added *Flip* to the list of available actions.

Changes to be committed:
	modified:   Base/CCXT-PlaceOrder.future
	modified:   Base/CCXT-PlaceOrder.margin
	modified:   Base/CCXT-PlaceOrder.spot
	modified:   Base/CCXT-PlaceOrder.swap
	modified:   Base/DSR-PlaceOrder
	modified:   Base/Library/JRRccxt.py
	modified:   Base/Library/JRRmimic.py
	modified:   Base/Library/JRRoanda.py
	modified:   Base/Library/JackrabbitRelay.py
	modified:   Base/MIMIC-PlaceOrder
	modified:   Base/OANDA-PlaceOrder
	modified:   Base/PROXY-PlaceOrder
	modified:   Extras/CodeProofs/orderDetails
	modified:   Extras/PlaceManualOrder
	modified:   Extras/TV2Exchange
  • Loading branch information
rapmd73 committed May 13, 2024
1 parent a28ca1c commit ba2498d
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 89 deletions.
49 changes: 34 additions & 15 deletions Base/CCXT-PlaceOrder.future
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def GetPCTvalue(pct,close,bal):
return amount,volume

def GetPCTamount(relay,currency,quote,close):
fn=relay.DataDirectory+'/'+relay.Exchange+'.'+relay.Account+'.PCTtable'
fn=relay.Directories['Data']+'/'+relay.Exchange+'.'+relay.Account+'.PCTtable'
PCTtable=JRRsupport.TimedList("PCTtable",fn,Log=relay.JRLog)
if relay.Order['Action'].lower()=='close':
expire=0
Expand Down Expand Up @@ -153,7 +153,7 @@ def main():

if not "OverrideMaxAssets" in relay.Order:
if "MaxAssets" in relay.Active:
fn=relay.DataDirectory+'/'+relay.Exchange+'.'+relay.Account+'.MaxAssets'
fn=relay.Directories['Data']+'/'+relay.Exchange+'.'+relay.Account+'.MaxAssets'
maxAssetsList=JRRsupport.TimedList("MaxAssets",fn,maxsize=int(relay.Active['MaxAssets']),Log=relay.JRLog)
if relay.Order['Action'].lower()=='close':
expire=0
Expand Down Expand Up @@ -238,7 +238,10 @@ def main():
# Make sure minimum is enforced.
# KuCoin sub accounts do NOT verify or report correctly on this.
if abs(amount)<minimum:
amount=minimum
if amount>0:
amount=minimum
else:
amount=minimum*-1

# Figure limit order pricing, minimum cost is ignored as limit orders can be
# anything.
Expand Down Expand Up @@ -301,16 +304,6 @@ def main():
relay.JRLog.Write("Reduction: "+str(r))
amount-=r

# Determine trade action and carry it out

total=round(amount*price,8)
relay.JRLog.Write("Placing Order")
relay.JRLog.Write(f"|- Asset: {relay.Order['Asset']}")
relay.JRLog.Write(f"|- Action: {relay.Order['Action']}")
relay.JRLog.Write(f"|- Contracts: {amount:.8f}")
relay.JRLog.Write(f"|- Price: {price:.8f}")
relay.JRLog.Write(f"|- Total: {total:.8f}")

# Set leverage

try:
Expand All @@ -329,13 +322,39 @@ def main():
except:
pass

if relay.Order['Action'].lower()=='close':
relay.Order['Action']='sell'
if relay.Order['Action'].lower()=='flip':
pchg=amount-abs(pBalance)
if pBalance>0 and amount<0:
relay.Order['Action']='sell'
amount=abs(pchg)
elif pBalance<0 and amount>0:
relay.Order['Action']='buy'
amount=abs(pchg)
elif pBalance>0 and amount>0:
relay.Order['Action']='buy'
elif pBalance<0 and amount<0:
relay.Order['Action']='sell'
amount=abs(amount)
elif pBalance==0:
if amount<0:
relay.Order['Action']='sell'
else:
relay.Order['Action']='buy'
elif relay.Order['Action']=='long':
relay.Order['Action']='buy'
elif relay.Order['Action']=='short':
relay.Order['Action']='sell'

# Determine trade action and carry it out

total=round(amount*price,8)
relay.JRLog.Write("Placing Order")
relay.JRLog.Write(f"|- Asset: {relay.Order['Asset']}")
relay.JRLog.Write(f"|- Action: {relay.Order['Action']}")
relay.JRLog.Write(f"|- Contracts: {amount:.8f}")
relay.JRLog.Write(f"|- Price: {price:.8f}")
relay.JRLog.Write(f"|- Total: {total:.8f}")

if amount>0:
lNote=None
if 'LedgerNote' in relay.Order:
Expand Down
49 changes: 34 additions & 15 deletions Base/CCXT-PlaceOrder.margin
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def GetPCTvalue(pct,close,bal):
return amount,volume

def GetPCTamount(relay,currency,quote,close):
fn=relay.DataDirectory+'/'+relay.Exchange+'.'+relay.Account+'.PCTtable'
fn=relay.Directories['Data']+'/'+relay.Exchange+'.'+relay.Account+'.PCTtable'
PCTtable=JRRsupport.TimedList("PCTtable",fn,Log=relay.JRLog)
if relay.Order['Action'].lower()=='close':
expire=0
Expand Down Expand Up @@ -153,7 +153,7 @@ def main():

if not "OverrideMaxAssets" in relay.Order:
if "MaxAssets" in relay.Active:
fn=relay.DataDirectory+'/'+relay.Exchange+'.'+relay.Account+'.MaxAssets'
fn=relay.Directories['Data']+'/'+relay.Exchange+'.'+relay.Account+'.MaxAssets'
maxAssetsList=JRRsupport.TimedList("MaxAssets",fn,maxsize=int(relay.Active['MaxAssets']),Log=relay.JRLog)
if relay.Order['Action'].lower()=='close':
expire=0
Expand Down Expand Up @@ -238,7 +238,10 @@ def main():
# Make sure minimum is enforced.
# KuCoin sub accounts do NOT verify or report correctly on this.
if abs(amount)<minimum:
amount=minimum
if amount>0:
amount=minimum
else:
amount=minimum*-1

# Figure limit order pricing, minimum cost is ignored as limit orders can be
# anything.
Expand Down Expand Up @@ -299,16 +302,6 @@ def main():
relay.JRLog.Write("Reduction: "+str(r))
amount-=r

# Determine trade action and carry it out

total=round(amount*price,8)
relay.JRLog.Write("Placing Order")
relay.JRLog.Write(f"|- Asset: {relay.Order['Asset']}")
relay.JRLog.Write(f"|- Action: {relay.Order['Action']}")
relay.JRLog.Write(f"|- Amount: {amount:.8f}")
relay.JRLog.Write(f"|- Price: {price:.8f}")
relay.JRLog.Write(f"|- Total: {total:.8f}")

# Set leverage

try:
Expand All @@ -327,13 +320,39 @@ def main():
except:
pass

if relay.Order['Action'].lower()=='close':
relay.Order['Action']='sell'
if relay.Order['Action'].lower()=='flip':
pchg=amount-abs(pBalance)
if pBalance>0 and amount<0:
relay.Order['Action']='sell'
amount=abs(pchg)
elif pBalance<0 and amount>0:
relay.Order['Action']='buy'
amount=abs(pchg)
elif pBalance>0 and amount>0:
relay.Order['Action']='buy'
elif pBalance<0 and amount<0:
relay.Order['Action']='sell'
amount=abs(amount)
elif pBalance==0:
if amount<0:
relay.Order['Action']='sell'
else:
relay.Order['Action']='buy'
elif relay.Order['Action']=='long':
relay.Order['Action']='buy'
elif relay.Order['Action']=='short':
relay.Order['Action']='sell'

# Determine trade action and carry it out

total=round(amount*price,8)
relay.JRLog.Write("Placing Order")
relay.JRLog.Write(f"|- Asset: {relay.Order['Asset']}")
relay.JRLog.Write(f"|- Action: {relay.Order['Action']}")
relay.JRLog.Write(f"|- Amount: {amount:.8f}")
relay.JRLog.Write(f"|- Price: {price:.8f}")
relay.JRLog.Write(f"|- Total: {total:.8f}")

if amount>0:
lNote=None
if 'LedgerNote' in relay.Order:
Expand Down
4 changes: 2 additions & 2 deletions Base/CCXT-PlaceOrder.spot
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def GetPCTvalue(pct,close,bal):
return amount,volume

def GetPCTamount(relay,currency,quote,close):
fn=relay.DataDirectory+'/'+relay.Exchange+'.'+relay.Account+'.PCTtable'
fn=relay.Directories['Data']+'/'+relay.Exchange+'.'+relay.Account+'.PCTtable'
PCTtable=JRRsupport.TimedList("PCTtable",fn,Log=relay.JRLog)
if relay.Order['Action'].lower()=='close':
expire=0
Expand Down Expand Up @@ -163,7 +163,7 @@ def main():

if not "OverrideMaxAssets" in relay.Order:
if "MaxAssets" in relay.Active:
fn=relay.DataDirectory+'/'+relay.Exchange+'.'+relay.Account+'.MaxAssets'
fn=relay.Directories['Data']+'/'+relay.Exchange+'.'+relay.Account+'.MaxAssets'
maxAssetsList=JRRsupport.TimedList("MaxAssets",fn,maxsize=int(relay.Active['MaxAssets']),Log=relay.JRLog)
if relay.Order['Action'].lower()=='close':
expire=0
Expand Down
49 changes: 34 additions & 15 deletions Base/CCXT-PlaceOrder.swap
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def GetPCTvalue(pct,close,bal):
return amount,volume

def GetPCTamount(relay,currency,quote,close):
fn=relay.DataDirectory+'/'+relay.Exchange+'.'+relay.Account+'.PCTtable'
fn=relay.Directories['Data']+'/'+relay.Exchange+'.'+relay.Account+'.PCTtable'
PCTtable=JRRsupport.TimedList("PCTtable",fn,Log=relay.JRLog)
if relay.Order['Action'].lower()=='close':
expire=0
Expand Down Expand Up @@ -153,7 +153,7 @@ def main():

if not "OverrideMaxAssets" in relay.Order:
if "MaxAssets" in relay.Active:
fn=relay.DataDirectory+'/'+relay.Exchange+'.'+relay.Account+'.MaxAssets'
fn=relay.Directories['Data']+'/'+relay.Exchange+'.'+relay.Account+'.MaxAssets'
maxAssetsList=JRRsupport.TimedList("MaxAssets",fn,maxsize=int(relay.Active['MaxAssets']),Log=relay.JRLog)
if relay.Order['Action'].lower()=='close':
expire=0
Expand Down Expand Up @@ -238,7 +238,10 @@ def main():
# Make sure minimum is enforced.
# KuCoin sub accounts do NOT verify or report correctly on this.
if abs(amount)<minimum:
amount=minimum
if amount>0:
amount=minimum
else:
amount=minimum*-1

# Figure limit order pricing, minimum cost is ignored as limit orders can be
# anything.
Expand Down Expand Up @@ -301,16 +304,6 @@ def main():
relay.JRLog.Write("Reduction: "+str(r))
amount-=r

# Determine trade action and carry it out

total=round(amount*price,8)
relay.JRLog.Write("Placing Order")
relay.JRLog.Write(f"|- Asset: {relay.Order['Asset']}")
relay.JRLog.Write(f"|- Action: {relay.Order['Action']}")
relay.JRLog.Write(f"|- Contracts: {amount:.8f}")
relay.JRLog.Write(f"|- Price: {price:.8f}")
relay.JRLog.Write(f"|- Total: {total:.8f}")

# Set leverage

try:
Expand All @@ -329,13 +322,39 @@ def main():
except:
pass

if relay.Order['Action'].lower()=='close':
relay.Order['Action']='sell'
if relay.Order['Action'].lower()=='flip':
pchg=amount-abs(pBalance)
if pBalance>0 and amount<0:
relay.Order['Action']='sell'
amount=abs(pchg)
elif pBalance<0 and amount>0:
relay.Order['Action']='buy'
amount=abs(pchg)
elif pBalance>0 and amount>0:
relay.Order['Action']='buy'
elif pBalance<0 and amount<0:
relay.Order['Action']='sell'
amount=abs(amount)
elif pBalance==0:
if amount<0:
relay.Order['Action']='sell'
else:
relay.Order['Action']='buy'
elif relay.Order['Action']=='long':
relay.Order['Action']='buy'
elif relay.Order['Action']=='short':
relay.Order['Action']='sell'

# Determine trade action and carry it out

total=round(amount*price,8)
relay.JRLog.Write("Placing Order")
relay.JRLog.Write(f"|- Asset: {relay.Order['Asset']}")
relay.JRLog.Write(f"|- Action: {relay.Order['Action']}")
relay.JRLog.Write(f"|- Contracts: {amount:.8f}")
relay.JRLog.Write(f"|- Price: {price:.8f}")
relay.JRLog.Write(f"|- Total: {total:.8f}")

if amount>0:
lNote=None
if 'LedgerNote' in relay.Order:
Expand Down
2 changes: 1 addition & 1 deletion Base/DSR-PlaceOrder
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def main():
# to the PlaceOrder function, since placing the order has to be the last thing
# done.

fn=relay.DataDirectory+'/'+relay.GetExchangeLast()+'.'+relay.GetAccountLast()+'.'+relay.Order['Asset']+'.DSR'
fn=relay.Directories['Data']+'/'+relay.GetExchangeLast()+'.'+relay.GetAccountLast()+'.'+relay.Order['Asset']+'.DSR'
dsrList=JRRsupport.TimedList("DSR",fn,Log=relay.JRLog)
if relay.Order['Action'].lower()=='close':
expire=0
Expand Down
29 changes: 28 additions & 1 deletion Base/Library/JRRccxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ def PlaceOrder(self,**kwargs):
if Quiet!=True:
self.Log.Write("|- Order Confirmation ID: "+order['id'])

#JRRledger.WriteLedger(pair, m, action, amount, price, order, ln)
return order

return None
Expand Down Expand Up @@ -667,6 +666,7 @@ def GetOrderDetails(self,**kwargs):
pass
else:
self.Results=self.API("fetchOrder",**kwargs)

return self.Results

# Create an orphan order and deliver to OliverTwist
Expand Down Expand Up @@ -731,13 +731,16 @@ def WriteLedger(self,**kwargs):
LedgerDirectory=kwargs.get('LedgerDirectory')

if Response!=None:
Response.pop('Identity',None)
id=Response['id']
else:
Order.pop('Identity',None)
id=Order['ID']

# We need the embedded order reference if comming from OliverTwist
if 'Order' in Order:
subOrder=json.loads(Order['Order'])
subOrder.pop('Identity',None)
else:
subOrder=Order

Expand Down Expand Up @@ -771,3 +774,27 @@ def WriteLedger(self,**kwargs):

if type(IsLog)==bool and IsLog==True:
self.Log.Write(f"Ledgered: {subOrder['Exchange']}/{subOrder['Account']}:{id}",stdOut=False)

# Read ledger entry and locate by ID

def FindLedgerID(self,**kwargs):
LedgerDirectory=kwargs.get('LedgerDirectory')
id=kwargs.get('ID')
exchange=kwargs.get('Exchange').lower()
account=kwargs.get('Account')
asset=kwargs.get('Asset').upper().replace(':','').replace('/','').replace('-','')
mkt=kwargs.get('Market').lower() # ie, Spot

data=None
lf=f"{LedgerDirectory}/{exchange}.{mkt}.{account}.{asset}.ledger"
fh=open(lf,'r')
for line in fh:
try:
data=json.loads(line)
if data['ID']==id:
break
except Exception as err:
pass

fh.close()
return data['Detail']
5 changes: 5 additions & 0 deletions Base/Library/JRRmimic.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,8 @@ def MakeConditionalOrder(self,id,Order):

def WriteLedger(self,**kwargs):
pass

# Read ledger entry and locate by ID

def FindLedgerID(self,**kwargs):
pass
8 changes: 8 additions & 0 deletions Base/Library/JRRoanda.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,10 @@ def WriteLedger(self,**kwargs):
LedgerDirectory=kwargs.get('LedgerDirectory')

if 'ID' in Order:
Order.pop('Identity',None)
id=Order['ID']
if Response!=None:
Response.pop('Identity',None)
if 'orderCreateTransaction' in Response:
id=Response['orderCreateTransaction']['id']
elif 'longOrderCreateTransaction' in Response:
Expand Down Expand Up @@ -624,3 +626,9 @@ def WriteLedger(self,**kwargs):

if type(IsLog)==bool and IsLog==True:
self.Log.Write(f"Ledgered: {subOrder['Exchange']}:{id}",stdOut=False)

# Read ledger entry and locate by ID

def FindLedgerID(self,**kwargs):
pass

Loading

0 comments on commit ba2498d

Please sign in to comment.