How to use _greater_than method in pandera

Best Python code snippet using pandera_python

currency_indicator_backtest.py

Source:currency_indicator_backtest.py Github

copy

Full Screen

1import pandas as pd2import numpy as np3import sys4import matplotlib.pyplot as plt5oandapyV20.endpoints.accounts.AccountInstruments6sys.path.insert(1, '/Users/user/Desktop/diff')7from libraries.oanda import get_candles_bid_close8"""9Backtesting currencies and Indicators and Look for major trends:10 11 1. Call candles for over multiple years12 2. Calculate Currencies and Indicator.13 3. go through Data and Graph.14 4. Report Findings.15 5. Save in convenient fashion for reuse - answerting different questions.16 6. Save pulled candles onto disk so do not need to call Oanda every time.17Questions:18 19 What is volume in candle?20"""21# Candle Parameters22_from = '2019-06-01T00:00:00Z'23_to = '2020-01-01T00:00:00Z'24granularity = 'D'25# Pairs26pairs_index = ['AUD_CAD', 'AUD_CHF', 'AUD_HKD', 'AUD_JPY', 'AUD_NZD',27 'AUD_USD', 'CAD_CHF', 'CAD_HKD', 'CAD_JPY', 'CHF_HKD',28 'CHF_JPY', 'EUR_AUD', 'EUR_CAD', 'EUR_CHF', 'EUR_GBP',29 'EUR_HKD', 'EUR_JPY', 'EUR_NZD', 'EUR_USD', 'GBP_AUD',30 'GBP_CAD', 'GBP_CHF', 'GBP_HKD', 'GBP_JPY', 'GBP_NZD',31 'GBP_USD', 'HKD_JPY', 'NZD_CAD', 'NZD_CHF', 'NZD_HKD',32 'NZD_JPY', 'NZD_USD', 'USD_CAD', 'USD_CHF', 'USD_HKD',33 'USD_JPY']34# Currencies35currencies_index = list(set('_'.join(pairs_index).split('_')))36currencies_index.sort()37# Call Candles and assemble in to appropriate Dataframe38##############################################################################39if True:40 pairs = get_candles_bid_close(pairs_index[0], granularity, _from, _to)41 pairs = pairs.drop('volume', axis=1)42 pairs = pairs.set_index('timestamp', drop=True)43 for pair in pairs_index[1:]:44 df = get_candles_bid_close(pair, granularity, _from, _to)45 df = df.set_index('timestamp', drop=True)46 df = df.drop('volume', axis=1)47 pairs = pairs.join(df, how='inner')48 # Fill Forward missing prices, drop top row if any nans remain49 pairs = pairs.fillna(method='ffill').dropna()50# Calculate Currencies and Indicator for each timestamp51##############################################################################52if True:53 def conversion_dict(pairs, currencies):54 '''55 Calculate currencies based on pairs56 Return Currency dictionary based on full set of instrument prices57 Input: pairs: dictionary: currenct value of each pair 58 currencies: dictionary: values can be zero59 '''60 for currency in currencies.keys():61 # If pair contains currency, add it to the subset, making sure that62 # It is added with the currency as the denominator63 pair_subset = []64 for pair in list(pairs.keys()):65 if currency == pair.split('_')[0]:66 pair_subset.append(1 / pairs[pair])67 if currency == pair.split('_')[1]:68 pair_subset.append( pairs[pair])69 currencies[currency] = 1 / (np.array(pair_subset).sum() + 1 )70 return currencies 71 72 currency_dict = dict(zip(currencies_index, [0] * len(currencies_index)))73 currencies = pd.DataFrame(columns = currencies_index)74 for index, row in pairs.iterrows():75 p = row.to_dict()76 currencies.loc[index] = conversion_dict(p, currency_dict)77 78 79 # Rcalculate Pairs based on currency prices80 calculated = pairs.copy()81 for column in calculated:82 left = column.split('_')[0]83 right = column.split('_')[1]84 calculated[column] = currencies[left] / currencies[right]85 86 # Calculate Difference Indicator 87 differences = calculated - pairs88 89 90# Difference test 191##############################################################################92if False:93 '''94 for a currency:95 Bin amount differences were positive 96 for each bin:97 how many times did it happen?98 what was the percent of time that the next days price was higher?99 100 '''101 102 # Only choose those days where the difference was greater than zero103 104 # This is wrong.105 106 '''107 curr = 'EUR_USD'108 greater_than_0_index = differences[curr].values > 0109 diff_greater = differences[curr].values[greater_than_0_index]110 pair_greater = pairs[curr].values[greater_than_0_index]111 bins = pd.cut(diff_greater, 50, labels=False)112 113 114 _bins = []115 _count = []116 _greater_than = []117 for i in range(bins.max() + 1):118 _bins.append(i)119 _count.append((bins == i).sum())120 # greater_than = (pairs[curr].values[bins == i] < \121 # pairs[curr].values[np.roll(bins == i, 1)]).mean()122 greater_than = (pair_greater[bins == i] < \123 pair_greater[np.roll(bins == i, 1)]).mean()124 _greater_than.append(greater_than)125 126 plt.scatter(_count, _greater_than)127 '''128 129 130 131 132 # WY DOES E_USD RETURN ON 300 RESUTLS ?? ? ? 133 # ANYWAY - TIRED NOW.134 135 136 137 curr = 'EUR_USD'138 bins = pd.cut(differences[curr], 50, labels=False)139 greater_than_0_index = calculated[curr] > pairs[curr]140 diff_greater = differences[curr].values[greater_than_0_index]141 pair_greater = pairs[curr].values[greater_than_0_index]142 # Simple question - if it was higher, was the next ( what about the last)143 _greater = pairs[curr][greater_than_0_index].values < pairs[curr][np.roll(greater_than_0_index, 1)].values144 print(_greater.mean())145 146 # The answer is no - what about if we bin the sizes147 148 bins = pd.cut(differences[curr][greater_than_0_index].values, 50, labels=False)149 150 151 152 _bins = []153 _count = []154 _greater_than = []155 for i in range(bins.max() + 1):156 _bins.append(i)157 _count.append((bins == i).sum())158 # greater_than = (pairs[curr].values[bins == i] < \159 # pairs[curr].values[np.roll(bins == i, 1)]).mean()160 greater_than = (pair_greater[bins == i] < \161 pair_greater[np.roll(bins == i, 1)]).mean()162 _greater_than.append(greater_than)163 164 plt.scatter(_count, _greater_than)165 ...

Full Screen

Full Screen

whole_currnecy_universe_test.py

Source:whole_currnecy_universe_test.py Github

copy

Full Screen

1import pandas as pd2import numpy as np3import sys4import matplotlib.pyplot as plt5sys.path.insert(1, '/Users/user/Desktop/diff')6from libraries.oanda import get_candles_bid_close7"""8Backtesting currencies and Indicators and Look for major trends:9 10 1. Call candles for over multiple years11 2. Calculate Currencies and Indicator.12 3. go through Data and Graph.13 4. Report Findings.14 5. Save in convenient fashion for reuse - answerting different questions.15 6. Save pulled candles onto disk so do not need to call Oanda every time.16Questions:17 18 What is volume in candle?19"""20# Candle Parameters21_from = '2019-06-01T00:00:00Z'22_to = '2020-01-01T00:00:00Z'23granularity = 'D'24# Pairs25pairs_index = ['AUD_CAD', 'AUD_CHF', 'AUD_HKD', 'AUD_JPY', 'AUD_NZD',26 'AUD_USD', 'CAD_CHF', 'CAD_HKD', 'CAD_JPY', 'CHF_HKD',27 'CHF_JPY', 'EUR_AUD', 'EUR_CAD', 'EUR_CHF', 'EUR_GBP',28 'EUR_HKD', 'EUR_JPY', 'EUR_NZD', 'EUR_USD', 'GBP_AUD',29 'GBP_CAD', 'GBP_CHF', 'GBP_HKD', 'GBP_JPY', 'GBP_NZD',30 'GBP_USD', 'HKD_JPY', 'NZD_CAD', 'NZD_CHF', 'NZD_HKD',31 'NZD_JPY', 'NZD_USD', 'USD_CAD', 'USD_CHF', 'USD_HKD',32 'USD_JPY']33# Currencies34currencies_index = list(set('_'.join(pairs_index).split('_')))35currencies_index.sort()36# Call Candles and assemble in to appropriate Dataframe37##############################################################################38if True:39 pairs = get_candles_bid_close(pairs_index[0], granularity, _from, _to)40 pairs = pairs.drop('volume', axis=1)41 pairs = pairs.set_index('timestamp', drop=True)42 for pair in pairs_index[1:]:43 df = get_candles_bid_close(pair, granularity, _from, _to)44 df = df.set_index('timestamp', drop=True)45 df = df.drop('volume', axis=1)46 pairs = pairs.join(df, how='inner')47 # Fill Forward missing prices, drop top row if any nans remain48 pairs = pairs.fillna(method='ffill').dropna()49# Calculate Currencies and Indicator for each timestamp50##############################################################################51if True:52 def conversion_dict(pairs, currencies):53 '''54 Calculate currencies based on pairs55 Return Currency dictionary based on full set of instrument prices56 Input: pairs: dictionary: currenct value of each pair 57 currencies: dictionary: values can be zero58 '''59 for currency in currencies.keys():60 # If pair contains currency, add it to the subset, making sure that61 # It is added with the currency as the denominator62 pair_subset = []63 for pair in list(pairs.keys()):64 if currency == pair.split('_')[0]:65 pair_subset.append(1 / pairs[pair])66 if currency == pair.split('_')[1]:67 pair_subset.append( pairs[pair])68 currencies[currency] = 1 / (np.array(pair_subset).sum() + 1 )69 return currencies 70 71 currency_dict = dict(zip(currencies_index, [0] * len(currencies_index)))72 currencies = pd.DataFrame(columns = currencies_index)73 for index, row in pairs.iterrows():74 p = row.to_dict()75 currencies.loc[index] = conversion_dict(p, currency_dict)76 77 78 # Rcalculate Pairs based on currency prices79 calculated = pairs.copy()80 for column in calculated:81 left = column.split('_')[0]82 right = column.split('_')[1]83 calculated[column] = currencies[left] / currencies[right]84 85 # Calculate Difference Indicator 86 differences = calculated - pairs87 88 89# Difference test 190##############################################################################91if False:92 '''93 for a currency:94 Bin amount differences were positive 95 for each bin:96 how many times did it happen?97 what was the percent of time that the next days price was higher?98 99 '''100 101 # Only choose those days where the difference was greater than zero102 103 # This is wrong.104 105 '''106 curr = 'EUR_USD'107 greater_than_0_index = differences[curr].values > 0108 diff_greater = differences[curr].values[greater_than_0_index]109 pair_greater = pairs[curr].values[greater_than_0_index]110 bins = pd.cut(diff_greater, 50, labels=False)111 112 113 _bins = []114 _count = []115 _greater_than = []116 for i in range(bins.max() + 1):117 _bins.append(i)118 _count.append((bins == i).sum())119 # greater_than = (pairs[curr].values[bins == i] < \120 # pairs[curr].values[np.roll(bins == i, 1)]).mean()121 greater_than = (pair_greater[bins == i] < \122 pair_greater[np.roll(bins == i, 1)]).mean()123 _greater_than.append(greater_than)124 125 plt.scatter(_count, _greater_than)126 '''127 128 129 130 131 # WY DOES E_USD RETURN ON 300 RESUTLS ?? ? ? 132 # ANYWAY - TIRED NOW.133 134 135 136 curr = 'EUR_USD'137 bins = pd.cut(differences[curr], 50, labels=False)138 greater_than_0_index = calculated[curr] > pairs[curr]139 diff_greater = differences[curr].values[greater_than_0_index]140 pair_greater = pairs[curr].values[greater_than_0_index]141 # Simple question - if it was higher, was the next ( what about the last)142 _greater = pairs[curr][greater_than_0_index].values < pairs[curr][np.roll(greater_than_0_index, 1)].values143 print(_greater.mean())144 145 # The answer is no - what about if we bin the sizes146 147 bins = pd.cut(differences[curr][greater_than_0_index].values, 50, labels=False)148 149 150 151 _bins = []152 _count = []153 _greater_than = []154 for i in range(bins.max() + 1):155 _bins.append(i)156 _count.append((bins == i).sum())157 # greater_than = (pairs[curr].values[bins == i] < \158 # pairs[curr].values[np.roll(bins == i, 1)]).mean()159 greater_than = (pair_greater[bins == i] < \160 pair_greater[np.roll(bins == i, 1)]).mean()161 _greater_than.append(greater_than)162 163 plt.scatter(_count, _greater_than)164 ...

Full Screen

Full Screen

ranker.py

Source:ranker.py Github

copy

Full Screen

1#! /usr/bin/env python2import sys3from functools import cmp_to_key4from itertools import combinations5from pyrsistent import pset, pmap_field, PSet, pset_field, PClass, field6class Ranker(PClass):7 _greater_than = pmap_field(str, PSet)8 _remaining_pairs = pset_field(PSet)9 _all_items = field(initial=pset())10 @classmethod11 def new(cls, items):12 remaining = pset({pset(x) for x in combinations(items, 2)})13 return cls(_remaining_pairs=remaining)14 def is_complete(self):15 return len(self._remaining_pairs) == 016 def add_ranking(self, larger, smaller):17 """ Decide that 'larger' is greater than 'smaller' """18 key = pset([larger, smaller])19 slf = self20 if slf.cmp_items(smaller, larger) > 0:21 raise ValueError(f"{smaller} is already greater than {larger}")22 if key not in slf._remaining_pairs:23 return slf24 # Set larger as being greater than smaller25 slf = slf.set(_remaining_pairs=slf._remaining_pairs.remove(key))26 if larger not in slf._greater_than:27 slf = slf.set(_greater_than=slf._greater_than.set(larger, pset()))28 slf = slf.set(29 _greater_than=slf._greater_than.set(larger, slf._greater_than[larger].add(smaller)),30 _all_items=slf._all_items.add(larger).add(smaller),31 )32 # Set larger as being greater than everything which smaller is greater than33 for item in slf.everying_less_than(larger):34 slf = slf.add_ranking(larger, item)35 return slf36 def sample(self):37 for item in self._remaining_pairs:38 return item39 def everying_less_than(self, item):40 return self._greater_than[item]41 def has_item(self, item):42 return item in self._all_items43 def sorted_items(self, *args, **kwargs):44 return self.sorted(self._all_items, *args, **kwargs)45 def sorted(self, items, *args, **kwargs):46 kwargs["key"] = self.key_func47 return sorted(items, *args, **kwargs)48 @property49 def key_func(self):50 return cmp_to_key(self.cmp_items)51 def cmp_items(self, a, b):52 if b in self._greater_than.get(a, {}):53 return 154 if a in self._greater_than.get(b, {}):55 return -156 return 057class Undo(Exception):58 """ Raised when we want to stop """59def pick_one(a, b):60 print("Which is greater?")61 print(f"1. {a}")62 print(f"2. {b}")63 while True:64 result = input("> ")65 if result == "1":66 return a, b67 if result == "2":68 return b, a69 if result == "u":70 raise Undo()71def main():72 items = set(sys.argv[1:])73 if not items:74 print("Warning: No items specified", file=sys.stderr)75 return76 r = Ranker.new(items)77 rankings = []78 s = r.sample()79 while s:80 print()81 print("List:", ", ".join(r.sorted_items(reverse=True)))82 print()83 try:84 larger, smaller = pick_one(*list(s))85 except KeyboardInterrupt:86 break87 except Undo:88 try:89 r = rankings.pop()90 except IndexError:91 pass92 continue93 rankings.append(r)94 r = r.add_ranking(larger, smaller)95 s = r.sample()96 if not r.is_complete():97 print("Warning: Ranking is incomplete")98 for x in r.sorted_items(reverse=True):99 print(x)100if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful