Long Rule: Actions

Now that we have defined the Long Rule with its conditions, it is time to associate the trading actions triggered by this rule. This is a rule that changes the trading scenario based on our strategy. So, at the moment this rule is met by its conditions, we need to clean any orders open from the opposite scenario (Short). For this purpose, the first action that we associate to this rule is the Close Sell orders.

 

This action does not require any settings but in case you need to specify any you can use the Trading SM reference to see what can be customized in each action.

After closing all Sell orders, we want to open a Buy order. The action that will do this is Buy. In this action, we need to specify the trading volume for the new order. So, we need to first create a parameter that will hold the trading volume.

 

Note that we have marked the parameter as external. That way, we can modify the trading volume from the Metatrader terminal every time we run the Expert Advisor.

Finally, we can associate the Buy action to the Long Rule.

Create Long Rule

Now that we have all the Long conditions created, we can use the Rules Creator to create the Long rule.

 

Rules are made up of conditions. When we need that all the conditions meet at the same time in order to perform some trading action, then we use the “all met” rule type. Instead, when we just need that at least one condition meets, then we use an “at least one met” rule type. You can modify the rule type from a choice box at the top of Rules Creator.

 

In the case of this strategy we need that all the conditions meet at the same time.

Long Rule: Conditions

Long Condition 1

Because we only want to trade when we can confirm that the Fast MA finally closes above the Slow MA, we need to ensure that a new candle just opens and see if in the candle that just closes the Fast MA is above the Slow MA. Having that in mind, we need a way to add a condition for our Long rule that will determine whether a new candle just opens.

In Trading SM a condition is a comparison between two values. If you click the New Condition button you can see in the upper board how the Conditions Creator looks like.

 

Two squares separated by a choice box appear inside the upper board. If you click the choice box you can see all the possible comparisons. Basically, you set a value in the left square that you want to compare with the value you set in the right square. In order to introduce the values in the corresponding squares, we need to first create those values.

In Trading SM one way to create values is by making questions. The answer to a question is a value. We can find all the available questions on the left side panels: Indicators, Timeseries and Misc.

 

As you can see, we can make a lot of questions in Trading SM. We will use the answers to these questions to make trade decisions. For the purpose of this condition we will use the Is New Bar question. The answer to this question can be true or false and it will be true every time a new bar opens.

The condition we are pursuing looks like this: true equal Is New Bar

In the right square of the condition we will need to set the answer to the question Is New Bar. And in the left square the true value. In the choice box we will select the equal comparison. Meaning that if the answer to Is New Bar is equal to true, then true is equal to true and so, the condition is met. Otherwise, if the answer to the question is false, then false is not equal to true and so, the condition is not met. The condition would be met only once per bar (at the open).

In order to set the true value in the left square we need to create a parameter that will hold this value. So, the first thing we are going to do is to create a parameter named True that will hold the value true. That can be done in the Parameters Creator:

 

Don’t forget to add the parameter by clicking the Add button as we do in the above video. As we create parameters, they are added to the list of parameters that can be seen at the Parameters pane on the right.

After that, we will come back to the Conditions Creator and drag and drop the parameter just created to the left square:

 

We select the equal comparison and on the right square we will drag and drop the Is New Bar question. The Is New Bar question is available at Misc panel:

 

Don’t forget to add the condition.

The buttons available at the top of Trading SM are creator buttons.

Every time you click one of those buttons, the corresponding creator shows up in the upper board. Each creator can use values or questions from different side panels. In order to help you, Trading SM will print in blue the borders of the panels from where you can use values and questions at each creator.

We have already created the first condition. In the next chapter we keep creating the remaining conditions.

 

Long Condition 2

We do not want to open a new Buy order when there is one already open. For that purpose we need a condition that will check whether there are more buy orders open in the terminal.

There is also a question available in Trading SM that will answer us how many orders of a specific type (like buy or sell) are open at any time in the terminal. So, the comparison of this condition would be something like this: 0 equal Total Orders Open (type Buy)

The first thing we need to do is create a parameter that will hold the value 0:

 

Once this parameter is created we can create our condition. The question Total Orders Open answers us know how many orders are open by type. Let’s create the condition:

 

Long Condition 3

In the previous conditions, we have been dropping questions directly to any square of the Conditions Creator but we can also use a variable to hold the answer of some question and drop the variable instead. This approach helps us in avoiding to introduce all the settings every time we need to make the same question. We will create this condition using this approach.

This condition compares the Fast MA value in bar 2 with the Slow MA value in bar 2. From Trading SM, we can get values from all the indicators available in Metatrader. In this case we need the values of a Moving Average indicator. For this condition, we need the value of a 25 period Moving Average in bar 2 and the value of a 100 period Moving Average in bar 2. Because these values are also required in Short Condition 3, we can create variables and later, use the same variables in Short Condition 3. Let’s see how we can create a variable that will hold the value of a Moving Average.

In Trading SM, we have available several questions in the Indicators panel. In this case we use the Moving Average question.

 

Note that all the fields in the Moving Average question match the fields you see when you attach the Moving Average indicator in Metatrader. This applies for each indicator question. The only new field to any indicator is the Bar field because you need to specify from which bar number you want the value of the indicator.

Now that we have the corresponding variables, we can create the Long Condition 3.

 

Long Condition 4

This condition requires some operation. We need to perform the following operation: Fast MA in 1 – Slow MA in 1. Later, we need to compare the result with a parameter (5 pips). So, the first thing we need to do is to create two variables: Fast MA 1 and Slow MA 1. Second, we create an operation to perform: Fast MA 1 – Slow MA 1. Third, we create the 5 pips parameter. Finally, we create the condition.

Let’s create Fast MA 1 and Slow MA 1.

 

Let’s create the operation.

 

Let’s create the distance parameter.

 

Let’s create the condition.

Long Rule

As we mentioned in the previous chapter, rules are made up of conditions. In this chapter, we create the conditions involved in the Long rule. Finally, we create the rule itself and the trading actions that this rule triggers when all the conditions that define the rule met.

 

1. Long

Conditions

  • Long Condition 1: Is new bar equals true (A new bar just opens).
  • Long Condition 2: Total buy orders open equals 0 (we only want one order open at a time).
  • Long Condition 3: Fast MA in 2 less than Slow MA in 2
  • Long Condtion 4: Fast MA in 1 minus Slow MA in 1 is greater than 5 pips.

Actions

  • Long Action 1: Close Sell orders (if there is any).
  • Long Action 2: Open a Buy order.

Working with pips

In Forex markets is very common to use values in pips. For most Forex pairs the value of 1 pip is 0.0001 but for others it is 0.01 or even 0.1.

Expert Advisors usually have external parameters that require values in pips. We can also create external parameters in pips in Trading SM. Let’s see how to accomplish this in the Distance parameter used in the MA Crossovers Expert Advisor of the Getting Started tutorial.

First of all, we add a new external parameter that will hold the value of 1 pip. A Metatrader user will need to modfify this value taking into account the symbol in which the EA operates: 0.0001, 0.01 or 0.1.

 

Now that we have a Pip Value parameter in Trading SM, we create an operation that converts pip values to real values.

 

Now, in the original Distance parameter we may set 5 instead of 0.0005. Also, we make it external allowing a Metatrader user to customize the Expert Advisor.

Finally, we change on each condition the Distance parameter and select the Real Distance instead.

 

Trading SM trading Actions use real values instead of pips. This is good because we can use Trading SM to create Expert Advisors for any other market than Forex.

We save the robot and compile again. From now on, we have to provide the Distance value in pips in Metatrader.

Introduction: MA Crossovers

Trading SM is very powerful because you can start creating trading robots very fast. In this Getting Started tutorial we see how simple it is to create an Expert Advisor.

Prior to start creating robots in Trading SM, we need a trading strategy in mind. In this tutorial we use a trading strategy that relies on Moving Average crossovers. Trading signals in this type of strategies come from crossovers between two Moving Averages: a Moving Average with shorter period (25) called Fast MA and a Moving Average with longer period (100) called Slow MA.

We want to open a Buy position when a new candle just opens and if in the previous candle (the one that just closes) the Fast MA closed above the Slow MA after being below. Also, we want to see a consistent cross and for that purpose we include a condition that checks the distance between Moving Averages in the candle that just closes (the first candle where Fast MA closes above Slow MA). The distance needs to be greater than some specific value (5 pips). We want to open a Sell position when the opposite conditions met. The chart below shows this Buy scenario.

 

From Trading SM perspective, each bar has a number. The bar that is being formed (the one that hasn’t already closed) is 0. The previous consecutive bars are 1, 2 and so on. Look at this in the above chart. Whenever you need to specify in Trading SM from which bar you need some specific value you need to bear this in mind.

Trading robots require some trading logic. In the end, every trading robot performs some trading actions based on some trading rules. So, the first thing we need to do is to describe the rule that will trigger a buy trading action and the rule that will trigger a sell trading action.

In the following lines, we see a simple description of this strategy indicating every trading rule that we use in this trading robot and the corresponding trading actions that each trading rule triggers. Each rule is made up of several trading conditions.

Trading Rules

1. Long

Conditions

  • Long Condition 1: Is new bar equals true (A new bar just opens).
  • Long Condition 2: Total buy orders open equals 0 (we only want one order open at a time).
  • Long Condition 3: Fast MA in 2 less than Slow MA in 2
  • Long Condtion 4: Fast MA in 1 minus Slow MA in 1 is greater than 5 pips.

Actions

  • Long Action 1: Close Sell orders (if there is any).
  • Long Action 2: Open a Buy order.

2. Short

Conditions

  • Short Condition 1: Is new bar is true (A new bar just opens).
  • Short Condition 2: Total sell orders open is 0 (we only want one order open at a time).
  • Short Condition 3: Fast MA in 2 greater than Slow MA in 2
  • Short Condtion 4: Slow MA in 1 minus Fast MA in 1 is greater than 5 pips.

Actions

  • Short Action 1: Close Buy orders (if there is any).
  • Short Action 2: Open a Sell order.

 

In the next chapter we’ll see how to create the Long rule with its conditions. So, if you haven’t already, open Trading SM and go to File -> New.

Getting Started

Here, we learn how fast is to create trading robots with Trading SM. We see the typical structure of a trading strategy.

With Trading SM we divide every trading strategy into different trading rules that trigger trading actions like buy and sell.  Every trading rule is made up of conditions of the type some fast Moving Average is above some slow Moving Average. When the conditions that make up a Rule are met, the trading Actions associated with that Rule are triggered.

The main idea is that Rules are made up of Conditions to trigger trading Actions.