Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3326f6c
Create SKILL.md
Tsukimarf Apr 12, 2026
400744f
Create database.json
Tsukimarf Apr 12, 2026
26f126f
Create engine.py
Tsukimarf Apr 12, 2026
252ae07
Create client.js
Tsukimarf Apr 12, 2026
5f69871
Update SKILL.md
Tsukimarf Apr 17, 2026
5510330
Update client.js
Tsukimarf Apr 17, 2026
ccf7281
Create 5-tge-state-design-body-english.md
Tsukimarf Apr 17, 2026
39176e1
Create pirc_allocation_design2.json
Tsukimarf Apr 17, 2026
63fa957
Update pirc_allocation_design2.json
Tsukimarf Apr 17, 2026
32eae96
Create pirc_allocation_design2.js
Tsukimarf Apr 17, 2026
970c9fe
Update pirc_allocation_design2.js
Tsukimarf Apr 17, 2026
75d0287
Update pirc_allocation_design2.js
Tsukimarf Apr 17, 2026
0e4709a
Merge pull request #2 from Tsukimarf/Tsukimarf-patch-2
Tsukimarf Apr 17, 2026
86862b2
Revert "Create pirc_allocation_design2.js"
Tsukimarf Apr 17, 2026
454e4d1
Merge pull request #3 from Tsukimarf/revert-2-Tsukimarf-patch-2
Tsukimarf Apr 18, 2026
506726d
Consolidate TGE design details into a single document
Tsukimarf Sep 8, 2026
fb12969
Implement TGE state model with MongoDB MQL
Tsukimarf Sep 8, 2026
487add8
Add database schema and seed data for TGE state
Tsukimarf Sep 8, 2026
5a9eac1
Update tge-state.sql
Tsukimarf Sep 8, 2026
4d37b8c
Update tge-state.mql.js
Tsukimarf Sep 8, 2026
5ba8935
Update tge-state.mql.js
Tsukimarf Sep 8, 2026
082fb57
Update tge-state.sql
Tsukimarf Sep 8, 2026
6f7ba1f
Add Allocation Period Design documentation
Tsukimarf Sep 8, 2026
96c40a3
Create allocation design JSON for PiRC Section 4
Tsukimarf Sep 8, 2026
9a8819e
Create allocation-state.mql for database schema and logic
Tsukimarf Sep 8, 2026
ac66d9e
Rename allocation-state.mql to allocation-state.mql.js
Tsukimarf Sep 8, 2026
714f3e3
Rename allocation-state.mql.js to allocation-state.sql
Tsukimarf Sep 8, 2026
a7e93bb
Add MongoDB script for Allocation Period model
Tsukimarf Sep 8, 2026
a28663a
Add bodyfile-composition.hpp for candle analysis
Tsukimarf Sep 8, 2026
f6faa0c
Update 4-allocation-bodyfile.md
Tsukimarf Sep 8, 2026
df35408
Update 4-allocation-bodyfile.md
Tsukimarf Sep 8, 2026
0711bc5
Add bodyfail_composition.py for trading analysis
Tsukimarf Sep 8, 2026
cd2c4b7
Add BodyFailComposition_v2 trading module
Tsukimarf Sep 8, 2026
69ea89a
Create JSON configuration for bodyfail module
Tsukimarf Sep 8, 2026
42efa7e
Create bodyfail database schema and seed data
Tsukimarf Sep 8, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions PiRC1/4-allocation/.JSON
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"module": {
"name": "bodyfail_composition",
"version": "2.0.0",
"description": "Candlestick body/wick composition decomposition and body-failure signal detection for Pi-Nexsus algorithmic trading",
"languages": ["sql", "mql5", "python", "cpp", "json"],
"predecessor": "BodyFail Database v1 (bodyfail_events, bodyfail_labels, bodyfail_stats)"
},

"classification_thresholds": {
"doji_max_body_ratio": 0.10,
"marubozu_min_body_ratio": 0.85,
"small_wick_max_ratio": 0.10,
"long_wick_min_ratio": 0.60,
"small_body_max_ratio": 0.30
},

"bodyfail_trigger": {
"min_trigger_body_ratio": 0.70,
"min_retracement_ratio": 0.50,
"confidence_formula": "min(1.0, body_ratio * 0.6 + 0.4)"
},

"body_type_rules": [
{ "type": "flat", "condition": "range_size == 0" },
{ "type": "doji", "condition": "body_ratio < doji_max_body_ratio" },
{ "type": "marubozu", "condition": "body_ratio > marubozu_min_body_ratio" },
{ "type": "hammer", "condition": "direction == bullish AND lower_wick_ratio > long_wick_min_ratio AND body_ratio < small_body_max_ratio AND upper_wick_ratio < small_wick_max_ratio" },
{ "type": "hanging_man", "condition": "direction == bearish AND lower_wick_ratio > long_wick_min_ratio AND body_ratio < small_body_max_ratio AND upper_wick_ratio < small_wick_max_ratio" },
{ "type": "shooting_star", "condition": "direction == bullish AND upper_wick_ratio > long_wick_min_ratio AND body_ratio < small_body_max_ratio AND lower_wick_ratio < small_wick_max_ratio" },
{ "type": "inverted_hammer", "condition": "direction == bearish AND upper_wick_ratio > long_wick_min_ratio AND body_ratio < small_body_max_ratio AND lower_wick_ratio < small_wick_max_ratio" },
{ "type": "spinning_top", "condition": "body_ratio < small_body_max_ratio (fallback)" },
{ "type": "normal", "condition": "default" }
],

"record_schema": {
"bodyfail_composition": {
"symbol": "string",
"timeframe": "string (M1|M5|M15|H1|H4|D1)",
"candle_time": "ISO-8601 timestamp",
"open": "number", "high": "number", "low": "number", "close": "number", "volume": "number",
"range_size": "number", "body_size": "number", "body_ratio": "number 0..1",
"upper_wick": "number", "upper_wick_ratio": "number 0..1",
"lower_wick": "number", "lower_wick_ratio": "number 0..1",
"direction": "bullish|bearish|flat",
"body_type": "doji|marubozu|hammer|hanging_man|shooting_star|inverted_hammer|spinning_top|normal|flat",
"is_bodyfail_trigger": "boolean"
},
"bodyfail_event": {
"symbol": "string", "timeframe": "string",
"trigger_time": "ISO-8601 timestamp", "confirm_time": "ISO-8601 timestamp",
"direction_failed": "bullish|bearish",
"trigger_body_ratio": "number 0..1",
"retracement_ratio": "number",
"confidence": "number 0..1",
"status": "pending|confirmed|invalidated"
}
},

"seed_data": {
"compositions": [
{ "symbol": "PIUSD", "timeframe": "M15", "candle_time": "2026-09-08T00:00:00Z", "open": 0.6500, "high": 0.6620, "low": 0.6495, "close": 0.6610, "volume": 15320, "range_size": 0.0125, "body_size": 0.0110, "body_ratio": 0.88000, "upper_wick": 0.0010, "upper_wick_ratio": 0.08000, "lower_wick": 0.0005, "lower_wick_ratio": 0.04000, "direction": "bullish", "body_type": "marubozu", "is_bodyfail_trigger": true },
{ "symbol": "PIUSD", "timeframe": "M15", "candle_time": "2026-09-08T00:15:00Z", "open": 0.6610, "high": 0.6615, "low": 0.6540, "close": 0.6555, "volume": 9870, "range_size": 0.0075, "body_size": 0.0055, "body_ratio": 0.73333, "upper_wick": 0.0005, "upper_wick_ratio": 0.06667, "lower_wick": 0.0015, "lower_wick_ratio": 0.20000, "direction": "bearish", "body_type": "normal", "is_bodyfail_trigger": false },
{ "symbol": "PIUSD", "timeframe": "M15", "candle_time": "2026-09-08T00:30:00Z", "open": 0.6555, "high": 0.6560, "low": 0.6510, "close": 0.6552, "volume": 7200, "range_size": 0.0050, "body_size": 0.0003, "body_ratio": 0.06000, "upper_wick": 0.0005, "upper_wick_ratio": 0.10000, "lower_wick": 0.0042, "lower_wick_ratio": 0.84000, "direction": "bearish", "body_type": "doji", "is_bodyfail_trigger": false },
{ "symbol": "PIUSD", "timeframe": "M15", "candle_time": "2026-09-08T01:00:00Z", "open": 0.6558, "high": 0.6562, "low": 0.6470, "close": 0.6555, "volume": 11200, "range_size": 0.0092, "body_size": 0.0003, "body_ratio": 0.03261, "upper_wick": 0.0004, "upper_wick_ratio": 0.04348, "lower_wick": 0.0085, "lower_wick_ratio": 0.92391, "direction": "bearish", "body_type": "hammer", "is_bodyfail_trigger": true },
{ "symbol": "PIUSD", "timeframe": "M15", "candle_time": "2026-09-08T01:15:00Z", "open": 0.6555, "high": 0.6640, "low": 0.6550, "close": 0.6635, "volume": 16400, "range_size": 0.0090, "body_size": 0.0080, "body_ratio": 0.88889, "upper_wick": 0.0005, "upper_wick_ratio": 0.05556, "lower_wick": 0.0005, "lower_wick_ratio": 0.05556, "direction": "bullish", "body_type": "marubozu", "is_bodyfail_trigger": true },
{ "symbol": "BTCUSD", "timeframe": "H1", "candle_time": "2026-09-08T00:00:00Z", "open": 58210.0, "high": 58890.0, "low": 58150.0, "close": 58840.0, "volume": 421.5, "range_size": 740.0, "body_size": 630.0, "body_ratio": 0.85135, "upper_wick": 50.0, "upper_wick_ratio": 0.06757, "lower_wick": 60.0, "lower_wick_ratio": 0.08108, "direction": "bullish", "body_type": "marubozu", "is_bodyfail_trigger": true },
{ "symbol": "BTCUSD", "timeframe": "H1", "candle_time": "2026-09-08T01:00:00Z", "open": 58840.0, "high": 58910.0, "low": 58020.0, "close": 58260.0, "volume": 388.2, "range_size": 890.0, "body_size": 580.0, "body_ratio": 0.65169, "upper_wick": 70.0, "upper_wick_ratio": 0.07865, "lower_wick": 240.0, "lower_wick_ratio": 0.26966, "direction": "bearish", "body_type": "normal", "is_bodyfail_trigger": true }
],
"events": [
{ "symbol": "PIUSD", "timeframe": "M15", "trigger_time": "2026-09-08T00:00:00Z", "confirm_time": "2026-09-08T00:15:00Z", "direction_failed": "bullish", "trigger_body_ratio": 0.88000, "retracement_ratio": 0.50, "confidence": 0.928, "status": "confirmed" },
{ "symbol": "BTCUSD", "timeframe": "H1", "trigger_time": "2026-09-08T00:00:00Z", "confirm_time": "2026-09-08T01:00:00Z", "direction_failed": "bullish", "trigger_body_ratio": 0.85135, "retracement_ratio": 0.92, "confidence": 0.911, "status": "confirmed" }
]
}
}
207 changes: 207 additions & 0 deletions PiRC1/4-allocation/.mq5
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
//+------------------------------------------------------------------+
//| BodyFailComposition_v2.mq5 |
//| Pi-Nexsus / PiRC algorithmic trading module |
//| Candle body/wick decomposition + body-failure signal detection |
//| v2: adds full "composition" breakdown (body_ratio, wick ratios, |
//| body_type classification) on top of BodyFail v1 events. |
//+------------------------------------------------------------------+
#property copyright "Tsukimarf / Pi-Nexsus"
#property version "2.00"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3

#property indicator_label1 "BodyFailTrigger"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrOrangeRed
#property indicator_width1 2

#property indicator_label2 "BodyRatio"
#property indicator_type2 DRAW_NONE

#property indicator_label3 "BodyType"
#property indicator_type3 DRAW_NONE

//--- input parameters (mirrors classification_thresholds in the JSON config)
input double InpDojiMaxBodyRatio = 0.10;
input double InpMarubozuMinBodyRatio = 0.85;
input double InpSmallWickMaxRatio = 0.10;
input double InpLongWickMinRatio = 0.60;
input double InpSmallBodyMaxRatio = 0.30;
input double InpMinTriggerBodyRatio = 0.70;
input double InpMinRetracementRatio = 0.50;
input bool InpExportCSV = true;
input string InpExportFileName = "bodyfail_compositions_v2.csv";

//--- buffers
double BufTriggerArrow[];
double BufBodyRatio[];
double BufBodyType[]; // numeric code, see BodyTypeCode()

//--- body type numeric codes (kept in sync with SQL CHECK / JSON enum)
#define BT_DOJI 0
#define BT_MARUBOZU 1
#define BT_HAMMER 2
#define BT_HANGING_MAN 3
#define BT_SHOOTING_STAR 4
#define BT_INVERTED_HAMMER 5
#define BT_SPINNING_TOP 6
#define BT_NORMAL 7
#define BT_FLAT 8

int fileHandle = INVALID_HANDLE;

//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0, BufTriggerArrow, INDICATOR_DATA);
SetIndexBuffer(1, BufBodyRatio, INDICATOR_CALCULATIONS);
SetIndexBuffer(2, BufBodyType, INDICATOR_CALCULATIONS);

PlotIndexSetInteger(0, PLOT_ARROW, 174); // up/down triangle glyph
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);

if(InpExportCSV)
{
fileHandle = FileOpen(InpExportFileName, FILE_WRITE | FILE_CSV | FILE_ANSI, ',');
if(fileHandle != INVALID_HANDLE)
{
FileWrite(fileHandle,
"symbol","timeframe","candle_time","open","high","low","close","volume",
"range_size","body_size","body_ratio","upper_wick","upper_wick_ratio",
"lower_wick","lower_wick_ratio","direction","body_type","is_bodyfail_trigger");
}
else
Print("BodyFailComposition_v2: failed to open export file, error ", GetLastError());
}

return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(fileHandle != INVALID_HANDLE)
FileClose(fileHandle);
}

//+------------------------------------------------------------------+
//| Classify body type from ratios — mirrors body_type_rules in JSON |
//+------------------------------------------------------------------+
int BodyTypeCode(double bodyRatio, double upperWickRatio, double lowerWickRatio,
bool isBullish, double rangeSize)
{
if(rangeSize <= 0.0)
return BT_FLAT;
if(bodyRatio < InpDojiMaxBodyRatio)
return BT_DOJI;
if(bodyRatio > InpMarubozuMinBodyRatio)
return BT_MARUBOZU;
if(isBullish && lowerWickRatio > InpLongWickMinRatio &&
bodyRatio < InpSmallBodyMaxRatio && upperWickRatio < InpSmallWickMaxRatio)
return BT_HAMMER;
if(!isBullish && lowerWickRatio > InpLongWickMinRatio &&
bodyRatio < InpSmallBodyMaxRatio && upperWickRatio < InpSmallWickMaxRatio)
return BT_HANGING_MAN;
if(isBullish && upperWickRatio > InpLongWickMinRatio &&
bodyRatio < InpSmallBodyMaxRatio && lowerWickRatio < InpSmallWickMaxRatio)
return BT_SHOOTING_STAR;
if(!isBullish && upperWickRatio > InpLongWickMinRatio &&
bodyRatio < InpSmallBodyMaxRatio && lowerWickRatio < InpSmallWickMaxRatio)
return BT_INVERTED_HAMMER;
if(bodyRatio < InpSmallBodyMaxRatio)
return BT_SPINNING_TOP;
return BT_NORMAL;
}

string BodyTypeName(int code)
{
switch(code)
{
case BT_DOJI: return "doji";
case BT_MARUBOZU: return "marubozu";
case BT_HAMMER: return "hammer";
case BT_HANGING_MAN: return "hanging_man";
case BT_SHOOTING_STAR: return "shooting_star";
case BT_INVERTED_HAMMER: return "inverted_hammer";
case BT_SPINNING_TOP: return "spinning_top";
case BT_FLAT: return "flat";
default: return "normal";
}
}

//+------------------------------------------------------------------+
//| Main calculation |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int start = (prev_calculated > 1) ? prev_calculated - 1 : 1;

for(int i = start; i < rates_total; i++)
{
double rangeSize = high[i] - low[i];
double bodySize = MathAbs(close[i] - open[i]);
bool isBullish = (close[i] >= open[i]);

double bodyRatio = (rangeSize > 0) ? bodySize / rangeSize : 0.0;
double upperWick = high[i] - MathMax(open[i], close[i]);
double lowerWick = MathMin(open[i], close[i]) - low[i];
double upperWickRatio = (rangeSize > 0) ? upperWick / rangeSize : 0.0;
double lowerWickRatio = (rangeSize > 0) ? lowerWick / rangeSize : 0.0;

int bodyType = BodyTypeCode(bodyRatio, upperWickRatio, lowerWickRatio, isBullish, rangeSize);

BufBodyRatio[i] = bodyRatio;
BufBodyType[i] = (double)bodyType;

//--- body-fail trigger: strong body candle (marubozu-strength) whose
// direction is later reversed beyond InpMinRetracementRatio.
bool isTrigger = (bodyRatio >= InpMinTriggerBodyRatio);
BufTriggerArrow[i] = isTrigger ? (isBullish ? low[i] - 5 * _Point : high[i] + 5 * _Point)
: EMPTY_VALUE;

//--- confirm reversal against the previous trigger, one bar later
if(i > 0 && BufTriggerArrow[i - 1] != EMPTY_VALUE)
{
double prevBodySize = MathAbs(close[i - 1] - open[i - 1]);
bool prevBullish = (close[i - 1] >= open[i - 1]);
double retracement = (prevBodySize > 0) ? MathAbs(close[i] - close[i - 1]) / prevBodySize : 0.0;
bool reversed = (prevBullish && close[i] < close[i - 1]) ||
(!prevBullish && close[i] > close[i - 1]);

if(reversed && retracement >= InpMinRetracementRatio)
{
double confidence = MathMin(1.0, BufBodyRatio[i - 1] * 0.6 + 0.4);
PrintFormat("BodyFail event: %s trigger=%s confirm=%s dir_failed=%s retr=%.4f conf=%.4f",
_Symbol, TimeToString(time[i - 1]), TimeToString(time[i]),
prevBullish ? "bullish" : "bearish", retracement, confidence);
}
}

if(InpExportCSV && fileHandle != INVALID_HANDLE)
{
FileWrite(fileHandle,
_Symbol, EnumToString((ENUM_TIMEFRAMES)Period()), TimeToString(time[i], TIME_DATE | TIME_SECONDS),
DoubleToString(open[i], _Digits), DoubleToString(high[i], _Digits),
DoubleToString(low[i], _Digits), DoubleToString(close[i], _Digits),
(long)tick_volume[i],
DoubleToString(rangeSize, _Digits), DoubleToString(bodySize, _Digits),
DoubleToString(bodyRatio, 5), DoubleToString(upperWick, _Digits),
DoubleToString(upperWickRatio, 5), DoubleToString(lowerWick, _Digits),
DoubleToString(lowerWickRatio, 5), isBullish ? "bullish" : "bearish",
BodyTypeName(bodyType), isTrigger ? "true" : "false");
}
}

return(rates_total);
}
//+------------------------------------------------------------------+
Loading