Indicator Script
// The reference files (found in Haasbot installation folder):
// - TradeServer.ScriptingDriver.dll
// - TradeServer.Interfaces.dll
// - TA-Lib-Core.dll
using TradeServer.ScriptingDriver.DataObjects;
using TradeServer.ScriptingDriver.Interfaces;
using TicTacTec.TA.Library;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System;
public class ExampleScript : IIndicatorScript
{
//Data to be shown in the indicator-chart:
private double buyPrice;
private double emaShort;
private double emaLong;
//Parameters:
private int emaShortLength = 7;
private int emaLongLength = 30;
public string Name
{
get{ return string.Format("Script: EMA {0}-{1}", emaShortLength, emaLongLength); }
}
public int TicksBack
{
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){
// Something went wrong
emaShort = buyPrice;
emaLong = buyPrice;
return IndicatorResult.Stay;
}else{
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
if(emaShort > emaLong)
return IndicatorResult.Buy;
else if(emaLong > emaShort)
return IndicatorResult.Sell;
else
return IndicatorResult.Stay;
}
}else{
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>()
{
buyPrice,
emaShort,
emaLong
};
}
}
Last updated