PowerLanguage Code for a MultiCharts Trading Strategy

In this section I am going to share a pretty simple rule based trading strategy which you might like to copy and test out in MultiCharts for yourself.

I’ll briefly talk about the rules for the strategy and then show and explain how I have written the code in the PowerLanguage editor in MultiCharts.

I’ll leave the code towards the end so you can copy and paste it into your PowerLanguage editor.

A Trend Breakout Strategy

I have purposely written this strategy so that you can experiment with different markets, time frames and input settings.

Feel free to copy my market, time frame and settings first to make sure you can replicate my results.  But don’t expect the results to be identical as different Forex data feeds have slightly different prices.

Important: 

We look at the highest and lowest closes of the past X number of bars.  Remember that if you want to use more than 50 bars back you will have to increase the “Maximum number of bars study will reference” in the strategy properties.

Read more about the “Maximum number of bars study will reference” here: Backtesting a Trading Strategy

Also, unless you have your instrument configured the same as mine in the QuoteManager, you might have to adjust the decimal place on your stop loss.

Make sure you are using the default session and exchange time zone too.

Read more about the instrument settings here: Data Feed and Broker Connection
And how to set up your chart with default session and exchange timezone here: Creating a Price Chart

Strategy Rules

We are using a price channel looking at the highest and lowest closes over the last X number of bars.

I have chosen to use 15 minute bars but this will work on any intra day time frame for example 5 minute, 60 minute, 4 hour etc.

The code won’t work on the daily or weekly time frame unless you remove the “pause window” (more about this later.)

There are 2 “Inputs” included in the code which you can adjust for yourself once you have the strategy on the chart.

The first is the “LookbackLen” which is the number of bars back we are checking the closing prices.

And the second is the StopLoss which is simply the stop loss in pips.  Note that because of the instrument settings, a value of 200 will not necessarily be 200 pips.  Using the same instrument settings in the QuoteManager as I do, 200 pips is expressed as “2”

Entries

The program looks at the closing price of the current bar and checks it against the previous X bars as determined by the adjustable input value “LookbackLen.”

If the current close is the highest of all the bars then go long next bar at market.  And if it’s the lowest close, go short.

A classic trend following strategy.

This is a stop and reverse strategy which means for the most part it is always in the market.  There’s no dedicated exits.  If long, we exit when we get a signal to go short.  When we get the short signal, we exit the long trade and then go short.  And vice versa.

The only time we will be out of the market is if we get stopped out.  I have a stop loss which is adjustable by using the input “StopLoss.”  Note that the stop loss only works when using a value greater than 0.  If you select 0, no stop loss is used (purposely programmed this way.)

The final part of the code I have included is a time window where trades cannot be taken (the stop loss is still active though.)

I have created a pause period time window between 1655 and 1830 EST where trades cannot be taken to avoid large spreads which can occur in the Forex markets.  This is hard coded but you can adjust if you like.

Using the Code on Daily Charts

If you would like to try the code on daily charts then you will have to alter the pause period time window.

The daily bar time stamps will be the time of the session close.  For most FX data this will be 1659 or 1700.  Hover over the daily bar to see the time stamp.

For the code to work on a daily bar, our pause period must not incorporate the time of the daily bar.  

I suggest changing the part of the code to: “If time < 2400 or time > 0 then begin”

Now there is no pause period at all and the code will work on daily and weekly bars too.  It will also still work just fine on intra day time frames but be aware you might have trades occur during those periods of higher than normal spreads.

I have included notes in the code to help users understand it better.

Strategy Code Written in PowerLanguage

// 1st August 2024 Demo code written by J Goodwin
// This code was written for the 15 minute time frame GBP/JPY Spot FX
// Price channel breakout.  Stop and reverse (always in the market unless stopped out)

Input:		LookbackLen(150),	// Length of the lookback period
		StopLoss(1.2);		// Stop loss of 120 pips from entry

// Pause period.  If breakouts occur during this period we don't take them to avoid large spreads

If time < 1655 or time > 1830 then begin  // 1hr 35 mins where trades aren't taken

	// Long entry

	If close = highest(close, LookbackLen) then buy next bar at market;

	// Short entry

	If close = lowest (close, LookbackLen) then sellshort next bar at market;
	
End;  // End of time window with pause period


// Set a stop loss based on pips
setstopcontract;

// If StopLoss input is set to 0, no stop loss is used.  
If StopLoss > 0 then setstoploss (StopLoss);

Performance Report

Below I have included 6 screenshots of the performance reports for 2 different data sets.  Like previously mentioned, we will always see minor differences in results using different data feeds.  One data feed is from Oanda and the other LMAX Global.

I’ve included these so you can make a comparison if you try and replicate the results.

  • Market: GBP/JPY
  • Time Frame: 15 minute
  • Date range: 01/01/2012 – 01/01/2024
  • Trade Size: 100,000 Fixed Shares / Contracts
  • Sessions: Default
  • Time Zone: Exchange
  • LookbackLen: 150 bars
  • Stop Loss: 120 pips 

If your results are wildly different I expect your settings aren’t the same as mine.  Find out more about the settings in the earlier pages of this guide here: 

Data Feed and Broker Connection

Creating a Price Chart

Oanda Equity Curve Detailed
LMAX Equity Curve Detailed
Oanda Strategy Performance Summary
LMAX Strategy Performance Summary
Oanda Total Trade Analysis
LMAX Total Trade Analysis

Optimizing Inputs

If you haven’t already used the Optimization feature of MultiCharts, then the above strategy is a good one for you to get started with.

For the above strategy you can now test various Lookback Lengths and stop loss values.

You can read more about how to run an optimization in MultiCharts in the section here:  Backtesting a Trading Strategy

Be careful when optimizing though as there is more to creating a robust trading strategy than picking the “best” input values.

MultiCharts User Guide Index:

MultiCharts – Download and Install

MultiCharts – Data Feed and Broker Connection

MultiCharts – Creating a Price Chart

MultiCharts – Backtesting a Trading Strategy

MultiCharts – Automated Trading

MultiCharts – Programming a Trading Strategy