File size: 2,283 Bytes
c99df4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | import sys
import os
import unittest
import numpy as np
import pandas as pd
# Add src to path
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from src.core.market_profile import MarketProfile
class TestMarketProfile(unittest.TestCase):
def setUp(self):
self.mp = MarketProfile(unit_size=1.0) # Simple unit size
def test_gap_fill(self):
prices = np.array([100.0, 105.0])
timestamps = np.array([0, 100], dtype=np.int64)
filled = self.mp.fill_gaps(prices, timestamps)
# Expected: 100, 101, 102, 103, 104, 105
expected = np.array([100.0, 101.0, 102.0, 103.0, 104.0, 105.0])
np.testing.assert_array_equal(filled, expected)
def test_profile_calculation(self):
# Create a skewed profile
# 100: 10 counts
# 101: 20 counts (POC)
# 102: 5 counts
# Construct dataframe
data = {
'bid': np.concatenate([
np.full(10, 100.0),
np.full(20, 101.0),
np.full(5, 102.0)
]),
'datetime': np.zeros(35, dtype='datetime64[ns]') # Timestamps don't matter much for counts
}
df = pd.DataFrame(data)
# Since gap filling needs consecutive diffs, and here we have flat regions,
# gap filling on [100, 100] produces just [100, 100].
# But `fill_gaps` logic: diff=0 -> count=0 -> total=0?
# My implementation: counts = abs(diff) ... append 1 for last point.
# If diff=0, count=0. Total = 0 + 1 = 1.
# It handles flat lines correctly (just repeats the point).
self.mp.update(df)
vah, val, poc = self.mp.get_vah_val_poc()
self.assertEqual(poc, 101.0)
# Total vol = 35. 70% = 24.5.
# POC volume = 20.
# Neighbors: 100 (10), 102 (5).
# 100 is larger. So it should expand to 100.
# Current vol = 20 + 10 = 30 > 24.5. Stop.
# So Value Area = [100, 101].
# VAL = 100, VAH = 101.
self.assertEqual(val, 100.0)
self.assertEqual(vah, 101.0)
if __name__ == '__main__':
unittest.main()
|