// The reference files (found in Haasbot installation folder):
// - TradeServer.ScriptingDriver.dll
// - TradeServer.Interfaces.dll
using TradeServer.ScriptingDriver.DataObjects;
using TradeServer.ScriptingDriver.Interfaces;
using TicTacTec.TA.Library;
using System.Collections.Generic;
public class ExampleScript : IIndicatorScript
//Data to be shown in the indicator-chart:
private int emaShortLength = 7;
private int emaLongLength = 30;
get{ return string.Format("Script: EMA {0}-{1}", emaShortLength, emaLongLength); }
get { return Core.EmaLookback(emaLongLength) + 1; }
public List<ScriptParameter> GetParameters()
return new List<ScriptParameter>(){
new ScriptParameter(){Name = "EMA short length",
Type = ScriptParameterType.Integer,
Value = emaShortLength.ToString(),
Info = "The length of the shorter/faster EMA."},
new ScriptParameter(){Name = "EMA long length", Type = ScriptParameterType.Integer, Value = emaLongLength.ToString()}
public void SetParameters(Dictionary<string, object> parameters)
emaShortLength = Convert.ToInt32(parameters["EMA short length"]);
emaLongLength = Convert.ToInt32(parameters["EMA long length"]);
public IndicatorResult GetResult(IndicatorContext context)
PriceInstrument instrument = context.PriceInstrument;
int dataCount = instrument.Close.Length;
int beginIndexShort, outNBElementsShort;
int beginIndexLong, outNBElementsLong;
double[] emaShortValues = new double[dataCount];
double[] emaLongValues = new double[dataCount];
var emaShortReturnCode = Core.Ema(0, dataCount - 1, instrument.Close, emaShortLength, out beginIndexShort, out outNBElementsShort, emaShortValues);
var emaLongReturnCode = Core.Ema(0, dataCount - 1, instrument.Close, emaLongLength, out beginIndexLong, out outNBElementsLong, emaLongValues);
if (emaShortReturnCode == Core.RetCode.Success && emaLongReturnCode == Core.RetCode.Success){
buyPrice = instrument.Close[dataCount - 1]; //Take current (last) value
if(outNBElementsShort == 0 || outNBElementsLong == 0){
return IndicatorResult.Stay;
emaShort = emaShortValues[outNBElementsShort - 1]; //Take current EMA (last one of the valid values)
emaLong = emaLongValues[outNBElementsLong - 1]; //Take current EMA (last one of the valid values)
context.Logger.LogToFile("EMA Short: " + emaShort + "\tEMA Long:" + emaLong); //Log to file
//Determine indicator signal
return IndicatorResult.Buy;
else if(emaLong > emaShort)
return IndicatorResult.Sell;
return IndicatorResult.Stay;
context.Logger.LogToFile("EMA calculation failed."); //Log to file
return IndicatorResult.Stay;
public List<ChartRecord> GetChartLines()
return new List<ChartRecord>()
new ChartRecord(ChartIndex = 1, Name = "Buy Price", HexLineColor = "#00FF00"),
new DataSerie(Name = "EMA Short", ChartIndex = 1, HexLineColor = "#FF0000"),
new DataSerie(Name = "EMA Long", ChartIndex = 1, HexLineColor = "#FFFF00")
public List<double> GetChartData()
return new List<double>()