There are two dictionaries with different keys name. I want to check if the 'pair'
in trades_to_make
is as instrument
in open_position
, and units
in trades_to_make
and long.units
or short.units
in open_position
are in same direction.
if ('pair'.values() in trades_to_make == 'instrument'.values() in open_position) :
if (('units'.values in trades_to_make and 'long.units' in open_position) >0) or (('units'.values in trades_to_make and 'short.units' in open_position) <0):
#drop the false element in `trades-to_make`
keep_dict = {}
#the drop element in a new dict
drop_dict = {}
The desired output is
keep_dict = [{'pair': 'USD_JPY',
'units': 4655,
'take_profit': 156.232,
'stop_loss': 155.428}]
drop_dict = [{'pair': 'USD_SGD',
'units': -465483,
'take_profit': 1.26701,
'stop_loss': 1.27486}]
USD_JPY
has the units
and long.units
are both more than 0, so it is in the keep_dict.
USD_SGD
has the units
less than 0 and long.units
more than 0. Since it is not both more than 0 or both less than 0, it is in drop_dict
trades_to_make = [{'pair': 'USD_JPY',
'units': 4655,
'take_profit': 156.232,
'stop_loss': 155.428},
{'pair': 'USD_SGD',
'units': -465483,
'take_profit': 1.26701,
'stop_loss': 1.27486}]
open_position = [{'instrument': 'USD_JPY',
'pl': '0.0000',
'resettablePL': '0.0000',
'financing': '0.0000',
'commission': '0.0000',
'dividendAdjustment': '0.0000',
'guaranteedExecutionFees': '0.0000',
'unrealizedPL': '0.0006',
'marginUsed': '0.0280',
'long.units': '1',
'long.averagePrice': '115.515',
'long.pl': '0.0000',
'long.resettablePL': '0.0000',
'long.financing': '0.0000',
'long.dividendAdjustment': '0.0000',
'long.guaranteedExecutionFees': '0.0000',
'long.tradeIDs': ['9585'],
'long.unrealizedPL': '0.0006',
'short.units': '0',
'short.pl': '0.0000',
'short.resettablePL': '0.0000',
'short.financing': '0.0000',
'short.dividendAdjustment': '0.0000',
'short.guaranteedExecutionFees': '0.0000',
'short.unrealizedPL': '0.0000',
'short.averagePrice': nan,
'short.tradeIDs': nan},
{'instrument': 'AUD_USD',
'pl': '-308.6247',
'resettablePL': '-308.6247',
'financing': '-132.6313',
'commission': '0.0000',
'dividendAdjustment': '0.0000',
'guaranteedExecutionFees': '0.0000',
'unrealizedPL': '1222.7585',
'marginUsed': '8526.2600',
'long.units': '426313',
'long.averagePrice': '0.71254',
'long.pl': '-1800.7434',
'long.resettablePL': '-1800.7434',
'long.financing': '-73.8533',
'long.dividendAdjustment': '0.0000',
'long.guaranteedExecutionFees': '0.0000',
'long.tradeIDs': ['12314'],
'long.unrealizedPL': '1222.7585',
'short.units': '0',
'short.pl': '1492.1187',
'short.resettablePL': '1492.1187',
'short.financing': '-58.7780',
'short.dividendAdjustment': '0.0000',
'short.guaranteedExecutionFees': '0.0000',
'short.unrealizedPL': '0.0000',
'short.averagePrice': nan,
'short.tradeIDs': nan},
{'instrument': 'USD_SGD',
'pl': '4111.9010',
'resettablePL': '4111.9010',
'financing': '-76.9777',
'commission': '0.0000',
'dividendAdjustment': '0.0000',
'guaranteedExecutionFees': '0.0000',
'unrealizedPL': '2.1516',
'marginUsed': '55.9734',
'long.units': '2000',
'long.averagePrice': '1.34485',
'long.pl': '2150.1331',
'long.resettablePL': '2150.1331',
'long.financing': '-53.5704',
'long.dividendAdjustment': '0.0000',
'long.guaranteedExecutionFees': '0.0000',
'long.tradeIDs': ['9219', '9229'],
'long.unrealizedPL': '2.1516',
'short.units': '0',
'short.pl': '1961.7679',
'short.resettablePL': '1961.7679',
'short.financing': '-23.4073',
'short.dividendAdjustment': '0.0000',
'short.guaranteedExecutionFees': '0.0000',
'short.unrealizedPL': '0.0000',
'short.averagePrice': nan,
'short.tradeIDs': nan},
{'instrument': 'USD_CAD',
'pl': '-7048.5238',
'resettablePL': '-7048.5238',
'financing': '-18.2784',
'commission': '0.0000',
'dividendAdjustment': '0.0000',
'guaranteedExecutionFees': '0.0000',
'unrealizedPL': '1101.4851',
'marginUsed': '11931.0968',
'long.units': '0',
'long.averagePrice': nan,
'long.pl': '-3089.4037',
'long.resettablePL': '-3089.4037',
'long.financing': '0.0000',
'long.dividendAdjustment': '0.0000',
'long.guaranteedExecutionFees': '0.0000',
'long.tradeIDs': nan,
'long.unrealizedPL': '0.0000',
'short.units': '-426313',
'short.pl': '-3959.1201',
'short.resettablePL': '-3959.1201',
'short.financing': '-18.2784',
'short.dividendAdjustment': '0.0000',
'short.guaranteedExecutionFees': '0.0000',
'short.unrealizedPL': '1101.4851',
'short.averagePrice': '1.27418',
'short.tradeIDs': ['12318']},
{'instrument': 'EUR_GBP',
'pl': '-3066.4302',
'resettablePL': '-3066.4302',
'financing': '-302.2728',
'commission': '0.0000',
'dividendAdjustment': '0.0000',
'guaranteedExecutionFees': '0.0000',
'unrealizedPL': '-137.8529',
'marginUsed': '13512.9337',
'long.units': '425564',
'long.averagePrice': '0.83718',
'long.pl': '-2519.9944',
'long.resettablePL': '-2519.9944',
'long.financing': '-158.5779',
'long.dividendAdjustment': '0.0000',
'long.guaranteedExecutionFees': '0.0000',
'long.tradeIDs': ['12278'],
'long.unrealizedPL': '-137.8529',
'short.units': '0',
'short.pl': '-546.4358',
'short.resettablePL': '-546.4358',
'short.financing': '-143.6949',
'short.dividendAdjustment': '0.0000',
'short.guaranteedExecutionFees': '0.0000',
'short.unrealizedPL': '0.0000',
'short.averagePrice': nan,
'short.tradeIDs': nan}]
CodePudding user response:
According to your description, this code should do the trick:
keep_dicts = list()
drop_dicts = list()
for trade in trades_to_make:
for position in open_position:
if trade['pair'] == position['instrument']:
long_unit_float = float(position['long.units'])
if trade['units'] > 0 and long_unit_float > 0:
keep_dicts.append(trade)
elif trade['units'] < 0 and long_unit_float < 0:
drop_dicts.append(trade)
print(keep_dicts)
print(drop_dicts)
Output:
[{'pair': 'USD_JPY', 'units': 4655, 'take_profit': 156.232, 'stop_loss': 155.428}]
[]
There is however an inconsistency in the data, if I look at your expected output. The only other entry with USD_SGD
has a long.unit
value of 2000
which is greater than 0
. So it should not appear in the drop_dict
according to your parameters.
But hopefully this code will get you in the right direction.