DAS Trader Pro – automatic checks for study trends

Until version 5.8.2.5 of DAS Trader Pro, retrieving a historical value of a study was possible as described in the previous post

https://www.guardiantrading.com/das-trader-pro-reading-study-values ,

msgbox(getwindowobj(NAME).getstudyval("MYEMA11","bybarnum",-3));

which resulted in a text string

that required a string-to-number conversion afterward.

Since every newer version above 5.8.2.5, we can use a direct retrieval as a number by adding a fourth parameter to the getstudyval() function.

The fourth parameter

the fourth parameter is “number”, which does the conversion to a number straightforwardly without the need to add the special code

The usage is as following example:

msgbox(getwindowobj(NAME).getstudyval(“MYEMA11″,”bybarnum”,-3,”number”));

A real life example

Let’s say that our strategy is a trend continuation strategy, which needs to be confirmed by a moving average sloping up for a long position to confirm the uptrend.

If we want to determine if a moving average is sloping up or down, for our eyes it is an easy task – look and see. But to determine it in a script, it is a bit of a different story. We need to ask the chart if the value now is higher or lower than the value X candles back.

For this real-life example, let’s take 3 measurements for a moving average.

  1. now
  2. 5 candles back
  3. 10 candles back

If the value of now is higher than the value 5 candles back and the value 5 candles back is higher than the value 10 candles back, we can say that the moving average is sloping up.

Graphically it looks like this

Let’s prepare the chart.

First, we add the button for the functionality. This is purely for display purposes. Usually the code to determine the slope will be ran in a chart update script or in a hotkey that will do the determination for us.

Then we need to name the moving average. In this case, it is the green moving average study that needs to be named. The default names for all studies are the same, so it has to be a unique name for the chart window. MY50SMA is the name in our example.

The code for the newly created button goes as below:

Note: At the time of writing, the function had a bug, and the resulting value was still a string-hence the conversion with the round function.

$valNOW=getwindowobj(NAME).getstudyval(“MY50SMA”,”bybarnum”,0,”number”);
$valNOW=ROUND($valNOW,4);
$val5=getwindowobj(NAME).getstudyval(“MY50SMA”,”bybarnum”,-5,”number”);
$val5=ROUND($val5,4);
$val10=getwindowobj(NAME).getstudyval(“MY50SMA”,”bybarnum”,-10,”number”);
$val10=ROUND($val10,4);

if($valnow>$val5)
{
if($val5>$val10)
{
msgbox(“The green 50SMA is sloping UP”);
}
}
if($valnow<$val5)
{
if($val5<$val10)
{
msgbox(“The green 50SMA is sloping DOWN”);
}
}

To make it part of a hotkey to go long, we determine the slope and display the message if trying to go long on a down-sloping MA instead of allowing the entry.

$valNOW=getwindowobj(NAME).getstudyval(“MY50SMA”,”bybarnum”,0,”number”);
$valNOW=ROUND($valNOW,4);
$val5=getwindowobj(NAME).getstudyval(“MY50SMA”,”bybarnum”,-5,”number”);
$val5=ROUND($val5,4);
$val10=getwindowobj(NAME).getstudyval(“MY50SMA”,”bybarnum”,-10,”number”);
$val10=ROUND($val10,4);

if($valnow>$val5)
{
if($val5>$val10)
{
// the entry hotkey goes here
$MONTAGE=GetWindowObj(“MONTAGE1”);
$MONTAGE.CXL ALLSYMB;
$buyprice=$MONTAGE.Ask;
$risk=GetAccountObj($MONTAGE.ACCOUNT).openEQ/800;
$mystop=$MONTAGE.price;
setvar(“STOP_”+SYMB,$MYSTOP);
$pricetostop=$buyprice-$mystop;
$target=3*$pricetostop+Ask;
$amount=$risk/$pricetostop;
$MONTAGE.Share=$amount;
$MONTAGE.ROUTE=”LIMIT”;
$MONTAGE.Price=Round($buyprice,2);
$MONTAGE.TIF=DAY+;
$MONTAGE.BUY;
$MONTAGE.TriggerOrder=RT:STOP STOPTYPE:RANGEMKT LowPrice:$mystop HighPrice:$target ACT:SELL QTY:POS TIF:DAY+;

}
}
if($valnow<$val5)
{
if($val5<$val10)
{
// the denial message goes here
msgbox(“No long entries on a down trend!”);
}
}

This will check if the condition of “The 50 moving average needs to slope up for long positions.”

And just like that, we can do automatic checks for any readable study trends.