LinearRegressionRSquared
interface cAlgo.API.Indicators
DesktopCLICloud
The Linear Regression R Squared is used to confirm the strength of the market trend.
Definition
public interface LinearRegressionRSquared
Namespace: cAlgo.API.Indicators · Group: Indicators · Implements: StandardIndicator
Remarks
A higher value of R-Squared means that the stronger the trend.
Examples
Example 1
private LinearRegressionRSquared rSquared;
protected override void OnStart()
{
// initialize rSquared indicator
rSquared = Indicators.LinearRegressionRSquared(Bars.ClosePrices, 20);
}
protected override void OnTick()
{
Print("{0}", rSquared.Result.LastValue);
}
import clr
clr.AddReference("cAlgo.API")
# Import cAlgo API types
from cAlgo.API import *
# Import trading wrapper functions
from robot_wrapper import *
class Test():
def on_start(self):
# Initialize rSquared indicator
self.rSquared = api.Indicators.LinearRegressionRSquared(api.Bars.ClosePrices, 20)
def on_tick(self):
api.Print(f"{self.rSquared.Result.LastValue}")
Example 2
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
// This sample cBot shows how to use the Linear Regression R Squared indicator
public partial class LinearRegressionRSquaredSample : Robot
{
private double _volumeInUnits;
private LinearRegressionRSquared _linearRegressionRSquared;
private SimpleMovingAverage _simpleMovingAverage;
private ExponentialMovingAverage _exponentialMovingAverage;
public Position[] BotPositions
{
get
{
return Positions.FindAll(Label);
}
}
protected override void OnStart()
{
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
_linearRegressionRSquared = Indicators.LinearRegressionRSquared(Bars.ClosePrices, 20);
_simpleMovingAverage = Indicators.SimpleMovingAverage(_linearRegressionRSquared.Result, 10);
_exponentialMovingAverage = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 20);
}
protected override void OnBar()
{
if (Bars.ClosePrices.Last(1) > _exponentialMovingAverage.Result.Last(1) && Bars.ClosePrices.Last(2) <= _exponentialMovingAverage.Result.Last(2))
{
ClosePositions(TradeType.Sell);
if (_linearRegressionRSquared.Result.Last(1) > _simpleMovingAverage.Result.Last(1))
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
}
}
else if (Bars.ClosePrices.Last(1) < _exponentialMovingAverage.Result.Last(1) && Bars.ClosePrices.Last(2) >= _exponentialMovingAverage.Result.Last(2))
{
ClosePositions(TradeType.Buy);
if (_linearRegressionRSquared.Result.Last(1) > _simpleMovingAverage.Result.Last(1))
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
}
}
}
private void ClosePositions(TradeType tradeType)
{
foreach (var position in BotPositions)
{
if (position.TradeType != tradeType) continue;
ClosePosition(position);
}
}
}
}
import clr
clr.AddReference("cAlgo.API")
# Import cAlgo API types
from cAlgo.API import *
# Import trading wrapper functions
from robot_wrapper import *
class LinearRegressionRSquaredSample():
def on_start(self):
self.volumeInUnits = api.Symbol.QuantityToVolumeInUnits(api.VolumeInLots)
self.linearRegressionRSquared = api.Indicators.LinearRegressionRSquared(api.Bars.ClosePrices, 20)
self.simpleMovingAverage = api.Indicators.SimpleMovingAverage(self.linearRegressionRSquared.Result, 10)
self.exponentialMovingAverage = api.Indicators.ExponentialMovingAverage(api.Bars.ClosePrices, 20)
def on_bar_closed(self):
if api.Bars.ClosePrices.Last(0) > self.exponentialMovingAverage.Result.Last(0) and api.Bars.ClosePrices.Last(1) <= self.exponentialMovingAverage.Result.Last(1):
self.close_positions(TradeType.Sell)
if self.linearRegressionRSquared.Result.Last(0) > self.simpleMovingAverage.Result.Last(0):
api.ExecuteMarketOrder(TradeType.Buy, api.SymbolName, self.volumeInUnits, api.Label, api.StopLossInPips, api.TakeProfitInPips)
elif api.Bars.ClosePrices.Last(0) < self.exponentialMovingAverage.Result.Last(0) and api.Bars.ClosePrices.Last(1) >= self.exponentialMovingAverage.Result.Last(1):
self.close_positions(TradeType.Buy)
if self.linearRegressionRSquared.Result.Last(0) > self.simpleMovingAverage.Result.Last(0):
api.ExecuteMarketOrder(TradeType.Sell, api.SymbolName, self.volumeInUnits, api.Label, api.StopLossInPips, api.TakeProfitInPips)
def get_bot_positions(self):
return api.Positions.FindAll(api.Label)
def close_positions(self, tradeType):
for position in self.get_bot_positions():
if position.TradeType != tradeType:
continue
api.ClosePosition(position)
{
"AccessRights": "None",
"TimeZone": "UTC",
"Parameters": [
{
"Name": "VolumeInLots",
"DisplayName": "Volume (Lots)",
"Type": "Double",
"DefaultValue": 0.01
},
{
"Name": "StopLossInPips",
"DisplayName": "Stop Loss (Pips)",
"Type": "Double",
"DefaultValue": 10
},
{
"Name": "TakeProfitInPips",
"DisplayName": "Take Profit (Pips)",
"Type": "Double",
"DefaultValue": 10
},
{
"Name": "Label",
"Type": "String",
"DefaultValue": "Sample"
}
]
}
Properties
| Name | Description |
|---|---|
ResultDesktopCLICloudIndicatorDataSeries | The Result Series of the Linear Regression R Squared indicator. |
Inherited members
From StandardIndicator: AddToChart, Symbol, TimeFrame, Bars