This tutorial will show you an advanced example for indicators, conditions, strategies and give you the ability to communicate between these scripts. This will lead you to more code transparency and reduces your programming time.
Why do we want this?
AgenaTrader provides you the ability to create indicators, conditions, alerts and strategies in c# and use them during trading. Of course, you can start creating an indicator and copy the code afterwards into a condition, a strategy or an alert. Programming by using “copy & paste” is easy but on the other hand there are many disadvantages like lack of testing reasons, no single point for bug fixing and low maintainability.
In many cases we are starting with indicators because indicators are the easiest place to start on script development. You will get a quick indication if your trading idea is working and addionally you can screen your instruments of choice visually and verify if your trading idea will be profitable.
The “ResultValue” object will hold all result data from the “calculate” method. Based on this result data, the next steps will be determined. In a strategy we create long or short orders, in a condition we set the “Occured” object, and so on. In our example, we use our global “ResultValue” object, of course you can use your own class if you need more properties.
We want to encapsulate the main logic into one main method in the indicator. In our case we do this by using the following public method in the indicator.
So it is possible that other scripts just call the “calculate” method of the indicator and get a decision of what to do. In our case the “calculate” method returns an object which holds all important information what has to be done. If we get the “OrderAction.Buy” as an “Entry” result, we need to start a long order in a strategy or we set the condition value to 1.
As we have finished our indicator, we can start working on our condition. Because we already have added our trading concept in the “calculate” method in the indicator, we just need a reference to our indicator and we are almost done.
We need to initalize this variable in our OnStartUp() method:
Now we are ready to use the “calculate” method of the indicator in our OnBarUpdate() method of the condition:
In the code snippet above we see that the return value of the “calculate” method is our “ResultValue” from the beginning of this tutorial. So we just need to evaluate this object.
Of course we are following the same procedure as in our condition. We create a variable of the indicator class, we initalize this variable during the OnStartUp() method and we use the object in our OnBarUpdate() method. Please pay attention while backtesting with the parameter “Orders Handling Mode = Advanced”, in this case we need at least two bars!
If you start the strategy on a chart the TimeFrame is automatically set. If you start this strategy within the “Strategy Escort”, it would be a smart idea to set a default TimeFrame, this will lead to a better usability. We do this by adding the default TimeFrame in the Initialize() method.
We use IsAutomated = true to decide if the strategy will do all work fully automated. In this case the strategy can be used within the “Strategy Escort” and will create entry & exit orders automatically.
In the end of the strategy file there are four methods: DoEnterLong(), DoEnterShort(), DoExitLong() and DoExitShort() In these methods we implement all rules for the creation of orders.
Filenames and Class names
To import all scripts into AgenaTrader without any error we add “_indicator”, “_strategy”, “_condition” or “_alert” to the filename and also to the c# class name. This is important because if you like to use all files in your AgenaTrader the names must be different. It is not possible to have an indicator and condition with the same name, e.g. “SMA_CrossOver”. They must have unique names like “SMA_CrossOver_indicator” and “SMA_CrossOver_condition”!
Color and drawing style
If the user has changed the color or the drawing style of the script (indicator or condition) we need to change the setting during the OnBarUpdate() method.
DisplayName and ToString()
In each script we override the ToString() method and the DisplayName property to provide a readable string in AgenaTrader. So we do see a readable string instead of the class name in AgenaTrader. In parentheses we add and C for Condition, I for Indicator, A for Alert and S for Strategy to ensure that we can distinguish between the scripts (e.g. if we are editing on indicators or conditions in charts).