EasyLanguage Code Examples
This page contains complete code examples for three trading strategies implemented in EasyLanguage for MultiCharts.
Moving Average Crossover Strategy
// Simple Moving Average Crossover Strategy
// This strategy generates buy signals when a fast moving average crosses above a slow moving average
// and sell signals when the fast moving average crosses below the slow moving average.
[IntrabarOrderGeneration = false]
// Input parameters that can be adjusted by the user
Inputs:
FastLength(9), // Length of the fast moving average
SlowLength(20), // Length of the slow moving average
StopLoss(500), // Stop loss amount in points
ProfitTarget(1000); // Profit target amount in points
// Variables to store the moving average values
Variables:
FastMA(0), // Fast moving average value
SlowMA(0), // Slow moving average value
FastMAprev(0), // Previous bar's fast moving average
SlowMArev(0); // Previous bar's slow moving average
// Calculate the moving averages
FastMA = Average(Close, FastLength);
SlowMA = Average(Close, SlowLength);
FastMArev = FastMA[1];
SlowMArev = SlowMA[1];
// Plot the moving averages on the chart
Plot1(FastMA, "Fast MA", Blue);
Plot2(SlowMA, "Slow MA", Red);
// Entry conditions
// Buy when fast MA crosses above slow MA
If FastMA > SlowMA AND FastMArev <= SlowMArev Then
Buy("MA Crossover") Next Bar at Market;
// Sell when fast MA crosses below slow MA
If FastMA < SlowMA AND FastMArev >= SlowMArev Then
Sell("MA Crossover") Next Bar at Market;
// Exit conditions
// Set stop loss and profit target for long positions
If MarketPosition = 1 Then Begin
SetStopLoss(StopLoss);
SetProfitTarget(ProfitTarget);
End;
// Set stop loss and profit target for short positions
If MarketPosition = -1 Then Begin
SetStopLoss(StopLoss);
SetProfitTarget(ProfitTarget);
End;
RSI Strategy
// RSI Strategy
// This strategy uses the Relative Strength Index (RSI) to generate buy and sell signals
// Buy when RSI crosses above oversold level, sell when RSI crosses below overbought level
[IntrabarOrderGeneration = false]
// Input parameters that can be adjusted by the user
Inputs:
RSILength(14), // Length for RSI calculation
OverSold(30), // Oversold threshold
OverBought(70), // Overbought threshold
StopLoss(500), // Stop loss amount in points
ProfitTarget(1000); // Profit target amount in points
// Variables to store values
Variables:
RSIValue(0), // Current RSI value
RSIValuePrev(0); // Previous bar's RSI value
// Calculate RSI
RSIValue = RSI(Close, RSILength);
RSIValuePrev = RSIValue[1];
// Plot RSI on the chart
Plot1(RSIValue, "RSI", Blue);
Plot2(OverBought, "Overbought", Red);
Plot3(OverSold, "Oversold", Green);
// Entry conditions
// Buy when RSI crosses above oversold level
If RSIValue > OverSold AND RSIValuePrev <= OverSold Then
Buy("RSI Oversold") Next Bar at Market;
// Sell when RSI crosses below overbought level
If RSIValue < OverBought AND RSIValuePrev >= OverBought Then
Sell("RSI Overbought") Next Bar at Market;
// Exit conditions
// Set stop loss and profit target for long positions
If MarketPosition = 1 Then Begin
SetStopLoss(StopLoss);
SetProfitTarget(ProfitTarget);
End;
// Set stop loss and profit target for short positions
If MarketPosition = -1 Then Begin
SetStopLoss(StopLoss);
SetProfitTarget(ProfitTarget);
End;
Breakout Strategy
// Breakout Strategy
// This strategy generates buy signals when price breaks above a resistance level
// and sell signals when price breaks below a support level
[IntrabarOrderGeneration = false]
// Input parameters that can be adjusted by the user
Inputs:
Length(20), // Lookback period for support/resistance levels
StopLoss(500), // Stop loss amount in points
ProfitTarget(1000); // Profit target amount in points
// Variables to store values
Variables:
HighestVal(0), // Highest high over lookback period
LowestVal(0); // Lowest low over lookback period
// Calculate support and resistance levels
HighestVal = Highest(High[1], Length);
LowestVal = Lowest(Low[1], Length);
// Plot support and resistance levels on the chart
Plot1(HighestVal, "Resistance", Red);
Plot2(LowestVal, "Support", Green);
// Entry conditions
// Buy when price breaks above resistance level
If Close > HighestVal Then
Buy("Breakout") Next Bar at Market;
// Sell when price breaks below support level
If Close < LowestVal Then
Sell("Breakdown") Next Bar at Market;
// Exit conditions
// Set stop loss and profit target for long positions
If MarketPosition = 1 Then Begin
SetStopLoss(StopLoss);
SetProfitTarget(ProfitTarget);
End;
// Set stop loss and profit target for short positions
If MarketPosition = -1 Then Begin
SetStopLoss(StopLoss);
SetProfitTarget(ProfitTarget);
End;