Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

P-Signal Strategy Long Only Strategy – Amibroker AFL Code

2 min read

This tutorial provides an overview of the P-Signal reversal strategy, a long only strategy and quantitative trading approach that utilizes statistical parameters and error functions to generate probabilistic trading signals. This strategy, like the pivot reversal strategy, aims to capture market reversal points by tracking the extreme value distributions of a series of K-lines. We cover the principles of the strategy, analyze its advantages and risks, suggest optimization directions, and provide an implementation in the Amibroker AFL code

Overview

The P-Signal reversal strategy is designed around the P-signal indicator, which integrates statistical parameters like moving averages and standard deviations. These are mapped into a range of -1 to 1 through the Gaussian error function to create a quantified judgment indicator. The strategy takes short positions when the P-signal moves from positive to negative and long positions when it moves from negative to positive.

Strategy Principles

  • Cardinality: Controls the sample size of the statistical parameters.
  • ΔErf: Adjusts the dead band of the error function, reducing trading frequency.
  • Observation Time: Determines the start time of the strategy’s operation.

Advantage Analysis

The P-signal strategy’s strength lies in its foundation on probability distributions, allowing it to effectively identify market characteristic points and capture reversal opportunities. Its parameterized design enhances adaptability and flexibility, making it suitable for various market conditions.

OpenAlgo: one API for 35+ Indian brokers

FREE AND OPEN SOURCE

OpenAlgo: one API for 35+ Indian brokers

Self-hosted algo trading platform. Send orders from Python, AmiBroker, TradingView, Excel or a no-code flow through one consistent API.

  • Unified REST and WebSocket API
  • Works with AmiBroker, TradingView, ChartInk and Excel
  • Sandbox mode to test before going live
Get OpenAlgo →by Marketcalls

Risk Analysis

The strategy’s main risk stems from its reliance on the parameters of probability distribution, making it susceptible to misjudgments caused by abnormal data. Strategies to mitigate these risks include increasing the sample size and adjusting the ΔErf range to control trading frequency.

Optimization Directions

Optimizations can include:

  • Incorporating additional indicators for filtering signals.
  • Validating signals across multiple timeframes.
  • Implementing stop-loss strategies.
  • Fine-tuning parameters for improved profitability.
  • Employing machine learning for dynamic parameter adjustment.

Long Only Strategy Amibroker AFL Code

The provided AFL code implements the P-Signal strategy in Amibroker. It outlines the setup of chart options, defines the P-Signal calculation function, and includes logic for generating buy and sell signals based on the indicator’s behavior. Additionally, it plots the trading signals on the chart for visual analysis.


_SECTION_BEGIN("P-Signal Strategy");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));


SetPositionSize(1*RoundLotSize,spsShares);

// Parameters and const of P-Signal.
nPoints = Optimize("Number of Bars", 9, 4, 100, 1);


function fPSignal(ser, integer) {
    nStDev = StDev(ser, integer);
    nSma = MA(ser, integer);
    return IIf(nStDev > 0, Erf(nSma/nStDev/sqrt(2)), sign(nSma)*1.0);
}

// Strategy logic.
ohlc4 = (O + H + L + C)/4;
nIntr = nPoints - 1;
nPSignal = MA(fPSignal(Ref(ohlc4, 0) - Ref(ohlc4, -1), nIntr), nIntr);
ndPSignal = sign(nPSignal - Ref(nPSignal, -1));

Buy = nPSignal < 0 AND ndPSignal > 0;
Sell = nPSignal > 0 AND ndPSignal < 0;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Buy = ref(Buy,-1);
Sell = ref(Sell,-1)

// Plotting.
//Plot(nPSignal, "nPSignal", colorBlue, styleLine);


//Plot the trading signals

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);


_SECTION_END();

_SECTION_BEGIN("Candlestick Charts with Date & Time Axis");

//Enable the Date & Time Axis
SetChartOptions(0, chartShowArrows | chartShowDates);

//Plotting Candlestick charts
Plot(Close,"Candle",colorDefault,styleCandle);


_SECTION_END();

The P-Signal reversal strategy is a quantitative approach that uses statistical analysis to find market reversal signals. Its probabilistic basis and flexible parameters make it a good starting point for algorithmic trading. Adding multi-indicator filters and proper risk management can improve its stability across different market conditions.

Marketcalls Elite Traders Club

MEMBERSHIP

Marketcalls Elite Traders Club

Every Marketcalls course and live mentorship in one membership: 127 courses and 2,500+ hours of learning.

  • Market Profile, Orderflow, AFL, Pine Script and Python
  • Live mentorship and TradEdge trading room
  • Premium tools and the Elite Slack community

Learn by watching: free videos every week

Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.

Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

4 Replies to “P-Signal Strategy Long Only Strategy – Amibroker AFL Code”

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More