The Tale of the Five Guardians and the Perceptron Oracle

In the bustling metropolis of Zorropolis, where digital assets danced in the ever-changing winds of the market, a young trader named Ava sought the secret to consistent fortune. Many had tried and failed, lost in the noise of random market movements. But Ava was different. She believed in logic, precision, and—most importantly—machine learning.

Ava had heard whispers of the ancient Perceptron Oracle, a mystical force known for its unparalleled predictions. But to summon the Oracle, one needed to gather the wisdom of the Five Guardians of the Market. Each guardian held a vital piece of the market puzzle, and together, they could unlock the Oracle’s true potential.

The Guardians:

The Saucer Sage – A sharp-eyed seer who noticed when trends gently shifted gears, like a saucer smoothly turning.
The Zero Line Nomad – A wanderer who lived on the line between profit and loss, signaling when it was time to cross to the winning side.
The Valley Whisperer – A quiet guide who knew when the market had sunk to its lowest point, whispering, “Now is the time to rise.”
The Divergence Shaman – An enigmatic figure who could spot when prices and momentum drifted apart, revealing hidden opportunities.
The Trend Warden – A steadfast guardian who ensured that Ava only traded when the trend and momentum agreed, warning against false hopes.
Ava ventured through the digital plains, seeking each guardian and gathering their signals—like ancient runes carved into her MLsignals tablet. Each guardian offered a signal, but Ava knew that not all signals were created equal. She had to decide which wisdom mattered most.

But how?

She had heard tales of failed traders who tried to guess these weights manually, only to be consumed by the market’s volatility.

Ava needed the Oracle.

At the heart of Zorropolis, she placed the signals into the hands of the Perceptron Oracle, whispering, “Guide me.”

The Oracle, an ancient machine learning entity, processed the signals, calculating the perfect balance—assigning dynamic weights to each guardian's wisdom. With each passing trade, the Oracle learned, adapting to the ever-shifting market winds.

And so, Ava’s strategy was born:

Signals from the Five Guardians combined into a single weighted force.
The Perceptron Oracle dynamically adjusted these weights, ensuring Ava’s trades were always aligned with the market’s pulse.
Ava’s digital ship sailed smoothly through the market’s waves, entering trades when the Oracle foresaw profit, and retreating when danger loomed.
In the end, Ava didn’t just find fortune—she found balance, with a strategy as elegant as a puzzle perfectly solved.
(please keep in mind, that the sharing of the code is to inspire your mind, and find ideas to build probabilistic state machines which will turn your dreams into reality, But be aware, that the code will not produce for you millions. :-)

Code
var MLsignals[7];  // Additional signal for threshold learning

#define condition1 MLsignals[0]
#define condition2 MLsignals[1]
#define condition3 MLsignals[2]
#define condition4 MLsignals[3]
#define condition5 MLsignals[4]
#define threshold MLsignals[5]  // Dynamic threshold
#define finalOutput MLsignals[6]  // Final Perceptron output

var MLp[5];  // Individual Perceptron outputs

function run() 
{
    set(PARAMETERS|RULES);  // Enable parameter optimization, training, and rule generation

    StartDate = 20220101;          // Start date for training
    EndDate = 20250101;            // End date for training
    NumWFOCycles = 10;             // Walk-Forward Optimization cycles
    BarPeriod = 5;                // Bar timeframe in minutes
    LookBack = 200;                // Lookback period for indicators
    Capital = 1000;                // Initial capital

    vars MedianPrice = series((priceHigh() + priceLow()) / 2);
    vars AO = series(SMA(MedianPrice, 5) - SMA(MedianPrice, 34));  // Awesome Oscillator
    vars RSI = series(RSI(series(priceClose()), 14));              // RSI Indicator

    while (asset(loop(
        "EUR/USD", "GBP/USD", "USD/JPY", "USD/CHF", "USD/CAD", "AUD/USD", "NZD/USD",
        "EUR/GBP", "EUR/JPY", "EUR/CHF", "GBP/JPY", "GBP/CHF", "AUD/JPY", "AUD/CHF", "GBP/CHF", "NZD/CAD",
        "NZD/JPY", "NZD/CHF", "CAD/JPY", "CAD/CHF", "CHF/JPY", "EUR/AUD", "EUR/NZD",
        "EUR/CAD", "GBP/AUD", "GBP/NZD", "GBP/CAD", "AUD/NZD")))
    {
        // Define machine learning input conditions
        condition1 = ifelse(AO[2] < AO[1] && AO[1] < AO[0], 1, 0);
        condition2 = ifelse(crossOver(AO, 0), 1, 0);
        condition3 = ifelse(valley(AO), 1, 0);
        condition4 = ifelse(priceClose(1) > priceClose(2) && AO[1] < AO[2], 1, 0);
        condition5 = ifelse(AO[0] > 0 && RSI[0] > 50, 1, 0);

        // Train individual Perceptrons for each condition
        MLp[0] = adviseLong(PERCEPTRON+RETURNS, 0, &condition1, 1);
        MLp[1] = adviseLong(PERCEPTRON+RETURNS, 0, &condition2, 1);
        MLp[2] = adviseLong(PERCEPTRON+RETURNS, 0, &condition3, 1);
        MLp[3] = adviseLong(PERCEPTRON+RETURNS, 0, &condition4, 1);
        MLp[4] = adviseLong(PERCEPTRON+RETURNS, 0, &condition5, 1);

        // Train Perceptron to find the optimal threshold dynamically
        threshold = adviseLong(PERCEPTRON+RETURNS, 0, MLp, 5);  

        // Final Perceptron for the trading decision
        finalOutput = adviseLong(PERCEPTRON+RETURNS, 0, MLp, 5);

        // Trading logic with dynamically learned threshold
        if (finalOutput > threshold) {
            enterLong();
        } else if (finalOutput < -threshold) {
            enterShort();
        }
    }

    // Plot indicators and results
    plot("AO", AO, NEW, BLUE);
    plot("RSI", RSI, NEW, RED);
    plot("Threshold", threshold, NEW, GREEN);
    plot("FinalOutput", finalOutput, NEW, BLACK);
}

Last edited by TipmyPip; 02/20/25 23:45.