LogoLogo
Back to HaasOnline.comSwitch to Trade Platform
3.x
3.x
  • Welcome
  • Getting Started
    • Using Local API Server
    • Authentication
    • Response
      • Error Codes
  • HaasScript
    • Using HaasScript
      • HaasScript Facts
      • Charting
      • Order Handling
      • Interval
      • Input Fields
      • Positions Handling
        • Fee correction
      • Position Information
      • Memory Management
      • Optimizations
      • Signal Handling
      • Trading
    • Script Editor
      • Syntax
      • Parameters
      • Interaction
    • Visual Editor
      • Blocks
      • Parameters
      • Flow Control
      • Interaction
    • Custom Commands
    • Tutorials
      • Trade Bot Guide
        • Creating A Trade Bot
          • Visual Editor Guide
          • Script Editor Guide
          • Custom Containers
        • Customizing Indicators
        • Customizing Safeties
        • Customizing Insurances
        • Creating Easy Indicators
      • Unmanaged Trading Guide
        • Executing Orders
        • Managing Orders
        • Managing Positions
        • Managing Wallet
      • Script Editor
        • Classes
        • MadHatter BBands
        • Percentage Price Change
      • Visual Editor
        • Importing Scripts
        • SmoothRSI
        • Scalper Bot
    • Commands
      • Array Helpers
      • Charting
      • Constants
      • Custom Commands Helpers
      • Easy Indicators
      • Easy Insurances
      • Easy Safeties
      • Equations
      • Flow Control
      • Input Fields
      • Input Settings
      • Mathematical
      • Memory Helpers
      • Miscellaneous
      • Order Handling
      • Order Information
      • Position Information
      • Position Prices
      • Price Data
      • Price Market Information
      • Profit Information
      • Settings
      • Signal Helpers
      • String Helpers
      • Technical Analysis
      • Technical Analysis Helpers
      • Time Information
      • Trade Actions (Managed)
      • Trade Actions (Unmanaged)
      • Trade Bot
      • Trade Market Information
      • Wallet
  • API Endpoints
    • Software API
    • Market Data API
    • Account Data API
    • Trade Data API
    • Advanced Order API
    • Trade Bot API
    • Custom Trade Bot API
    • ENUMS
    • Data Objects
  • Examples
    • Script Bots (C#)
      • Scalper Trade Bot
      • Flash Crash Trade Bot
    • Script Indicators (C#)
      • Indicator Script
      • Technical Analysis Library
    • Pshai Scripts (C#)
      • BBands Ext
      • BBands Ext v2
      • Chaikin A/D Line
      • Calibrator
      • Pshai's RVI
    • Scripted Driver
  • Other Resources
    • YouTube
    • Guides & Tutorials
    • Questions & Answers
    • Community Projects
  • Need Help?
    • Ask on Discord
    • Submit Support Ticket
Powered by GitBook
On this page

Was this helpful?

  1. Examples
  2. Script Indicators (C#)

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
            };
    }
}
PreviousScript Indicators (C#)NextTechnical Analysis Library

Last updated 6 years ago

Was this helpful?