SimpleMovingAverage
interface cAlgo.API.Indicators
DesktopCLICloud
The simple moving average is an average of price within n previous periods.
Definition
public interface SimpleMovingAverage
Namespace: cAlgo.API.Indicators · Group: Indicators · Implements: MovingAverage, StandardIndicator
Remarks
The simple moving average is the unweighted mean of the previous n price data, where n is the period used for the calculation and price data the price data source, e.g. The closing price.
Examples
Example 1
public partial class SimpleMovingAverageExample : Indicator
{
private SimpleMovingAverage _simpleMovingAverage;
protected override void Initialize()
{
_simpleMovingAverage = Indicators.SimpleMovingAverage(Source, Periods);
}
public override void Calculate(int index)
{
var average = _simpleMovingAverage.Result[index];
double sum = 0;
for (var period = 0; period < Periods; period++)
{
sum += Math.Pow(Source[index - period] - average, 2.0);
}
Result[index] = Math.Sqrt(sum / Periods);
}
}
import clr
clr.AddReference("cAlgo.API")
from cAlgo.API import *
import math
class Test():
def initialize(self):
self.simpleMovingAverage = api.Indicators.SimpleMovingAverage(api.Source, api.Periods)
def calculate(self, index):
average = self.simpleMovingAverage.Result[index]
sumResult = 0
for period in range(api.Periods):
sumResult += pow(api.Source[index - period] - average, 2.0)
api.Result[index] = math.sqrt(sumResult / api.Periods)
{
"Parameters": [
{
"Name": "Source",
"Type": "DataSeries"
},
{
"Name": "Periods",
"Type": "Integer",
"DefaultValue": 14,
"MinValue": 2
}
],
"LineOutputs": [
{
"Name": "Result",
"Color": "Orange"
}
]
}
Example 2
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
// This sample cBot shows how to use the Simple Moving Average indicator
public partial class SimpleMovingAverageSample : Robot
{
private double _volumeInUnits;
private SimpleMovingAverage _fastSimpleMovingAverage;
private SimpleMovingAverage _slowSimpleMovingAverage;
public Position[] BotPositions
{
get
{
return Positions.FindAll(Label);
}
}
protected override void OnStart()
{
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
_fastSimpleMovingAverage = Indicators.SimpleMovingAverage(FastMaSource, FastMaPeriod);
_slowSimpleMovingAverage = Indicators.SimpleMovingAverage(SlowMaSource, SlowMaPeriod);
}
protected override void OnBar()
{
if (_fastSimpleMovingAverage.Result.HasCrossedAbove(_slowSimpleMovingAverage.Result, 0))
{
ClosePositions(TradeType.Sell);
ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
}
else if (_fastSimpleMovingAverage.Result.HasCrossedBelow(_slowSimpleMovingAverage.Result, 0))
{
ClosePositions(TradeType.Buy);
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 SimpleMovingAverageSample():
def on_start(self):
self.volumeInUnits = api.Symbol.QuantityToVolumeInUnits(api.VolumeInLots)
self.fastSimpleMovingAverage = api.Indicators.SimpleMovingAverage(api.FastMaSource, api.FastMaPeriod)
self.slowSimpleMovingAverage = api.Indicators.SimpleMovingAverage(api.SlowMaSource, api.SlowMaPeriod)
def on_bar_closed(self):
if Functions.HasCrossedAbove(self.fastSimpleMovingAverage.Result, self.slowSimpleMovingAverage.Result, 0):
self.close_positions(TradeType.Sell)
api.ExecuteMarketOrder(TradeType.Buy, api.SymbolName, self.volumeInUnits, api.Label, api.StopLossInPips, api.TakeProfitInPips)
elif Functions.HasCrossedBelow(self.fastSimpleMovingAverage.Result, self.slowSimpleMovingAverage.Result, 0):
self.close_positions(TradeType.Buy)
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": "FastMaSource",
"DisplayName": "Source",
"Type": "DataSeries",
"Group": "Fast MA"
},
{
"Name": "FastMaPeriod",
"DisplayName": "Period",
"Type": "Integer",
"DefaultValue": 9,
"Group": "Fast MA"
},
{
"Name": "SlowMaSource",
"DisplayName": "Source",
"Type": "DataSeries",
"Group": "Slow MA"
},
{
"Name": "SlowMaPeriod",
"DisplayName": "Period",
"Type": "Integer",
"DefaultValue": 20,
"Group": "Slow MA"
},
{
"Name": "VolumeInLots",
"DisplayName": "Volume (Lots)",
"Type": "Double",
"DefaultValue": 0.01,
"Group": "Trade"
},
{
"Name": "StopLossInPips",
"DisplayName": "Stop Loss (Pips)",
"Type": "Double",
"DefaultValue": 10,
"Group": "Trade"
},
{
"Name": "TakeProfitInPips",
"DisplayName": "Take Profit (Pips)",
"Type": "Double",
"DefaultValue": 10,
"Group": "Trade"
},
{
"Name": "Label",
"Type": "String",
"DefaultValue": "Sample",
"Group": "Trade"
}
]
}
Inherited members
From MovingAverage: Result
From StandardIndicator: AddToChart, Symbol, TimeFrame, Bars