Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. 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. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

P-Signal Strategy Long Only Strategy – Amibroker AFL Code

2 min read

This tutorial provides an overview of the P-Signal reversal strategy, a quantitative trading strategy that utilizes statistical parameters and error functions to generate probabilistic trading signals. This strategy aims to capture market reversal points by tracking the extreme value distributions of a series of K-lines. we explore 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.

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.

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 offers a quantitative approach to trading, leveraging statistical analysis for market reversal signals. Its probabilistic basis and flexible parameterization make it a robust framework for algorithmic trading. Through continuous optimization and integration of multi-indicator validation and risk management techniques, the strategy can be enhanced to achieve higher stability and profitability in various market conditions.

Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. 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. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Interactive Brokers – Smart Order Chart Trading Module using…

The IB Controller is an interface designed to facilitate automatic trading with AmiBroker and Interactive Brokers (IB) TWS (Trader Workstation). It serves as a...
Rajandran R
8 min read

Introducing OpenAlgo V1.0: The Ultimate Open-Source Algorithmic Trading Framework…

OpenAlgo V1.0 is an open-source algorithmic trading platform to automate your trading strategies using Amibroker, Tradingview, Metatrader, Python etc. Designed with the modern trader...
Rajandran R
2 min read

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

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