Problem with SV (when calculating formulas with negative and previous values)

Advanced automation available in Guardian - Chat with others and share files here.
Post Reply
gaz36f
Posts: 16
Joined: Mon Nov 30, 2020 7:52 am

I am attempting to teach myself how to write relatively complex automation rules so that I can automate some functions of my manual trading. To this end, I am writing rules that will calculate certain TA indicators. I know that many believe these indicators are worthless in Sports Trading (and they may be right), but I am using them purely as a way of getting me to understand how I can use BA’s automation function to calculate formulas.

For example, this is what I have done to calculate Average True Range (ATR):

I first need to calculate the True Range (TR) so I setup five stored values:

LatestHi (the maximum value from the range of History List 3 – LTP: 13 seconds to now)
LatestLo (the minimum value from the range of History List 3 – LTP: 13 seconds to now)
HIGHminusLTP (LatestHi – LTP (now))
LOWminusLTP (LatestLo – LTP (now))
HIGHminusLOW (LatestHi – LatestLo)

To get the average I would just divide these last 3 stored values by 3. However, it is possible that one of the values (especially LOWminusLTP) is negative. So, all values must be Absolute (ABS).

As there is no function in BA for ABS, I have created another rule using a signal:

Signal name: lessthanzero (value: 1)
Condition: LOWminusLTP<0

I have then created a new SV (twice):

1st time: LOWminusLTPnew multiplied by 1 (If lessthanzero is not set)
2nd time: LOWminusLTPnew multiplied by -1 (If lessthanzero is set)

I then created an SV (SumofTR):

HIGHminusLTP
LOWminusLTPnew
HIGHminusLOW

And then a final SV (‘TR’) with SumofTR divided by 3.

So, I believe I have overcome the ABS problem using a signal, and additional stored values. Am I right? Is there a better way? I know I can do it in Excel (I already have), but I want to find a way to do everything in BA if possible.

The next problem I am unable to resolve.

To get ATR, the formula is:

ATR = ((ATRprevious*13)+TR)/14

In excel, for the initial ATRprevious value, I would use TR. I would then use the last ATR value in all subsequent calculations. In Excel it is easy. But, as I say, I want to do this using BA Automation. I have tried setting up a benchmark ‘FirstATR’ and creating a HistoryList Value and then creating a subsequent stored value 14 seconds later. But, along the way, my head has become mashed and I cannot work it out.

Can anyone suggest a way how I can create a workaround where a variable (i.e. ATR) is being created using a formula that requires the variables previous value?

I am pretty certain it is possible but after I have really lost my way.

Many thanks,
Gary
sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

For working with the minus values I think you can reduce a step by not having to use a signal

I have a similar rules for calculating Liability, where the SV could be a minus figure. I just use 2 rules, the first where the condition SV is < 0 , the other >= 0. Only one can be true so the true one then sets the SV as required ( * -1, * 1 etc)

Capture.JPG
You do not have the required permissions to view the files attached to this post.
gaz36f
Posts: 16
Joined: Mon Nov 30, 2020 7:52 am

sniffer66 wrote:
Tue Feb 09, 2021 7:13 am
For working with the minus values I think you can reduce a step by not having to use a signal

I have a similar rules for calculating Liability, where the SV could be a minus figure. I just use 2 rules, the first where the condition SV is < 0 , the other >= 0. Only one can be true so the true one then sets the SV as required ( * -1, * 1 etc)


Capture.JPG
Wonderful. Thanks Sniffer66. I am kicking myself that I hadn't thought of that already. Any possible ways of reducing the steps involved is great. Much appreciated. I have a few rules at the moment where I require an Absolute value, so I will make the appropriate tweaks. Thanks again.
sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

No problem Gary :)

I was trying to get my head around your formula issue also, but it's early here and I havent had coffee yet lol

Your previous value that you use in your current, is that static or does it recalculate regularly ? i.e every x cycles or is it set as a new value every time you create the new ?
gaz36f
Posts: 16
Joined: Mon Nov 30, 2020 7:52 am

Hahaha...No worries. It really hurts the head, and I certainly wasn't expecting a solution so quickly/early in the morning :D

The previous variable is calculated every refresh. The problem is that when the rules first opens in the market, there is no previous value. That is why I am using the True Range (TR) value in the very first instance to give an initial ATR. But subsequently I want to use the variable ATRPrevious in all future calculations for that market.

In excel it is easy as the first row has the TR, the second row uses the the previous TR (first row) for the initial ATR calculation. The third and subsequent row uses the previous ATR value.

But I just can't figure out a way to do just a sequence using BA Automation.
sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

So, if I understood that correctly...

Create a Run Once rule using True Range (TR). That sets a signal "ATR Set" and saves the value to an SV - "ATR Previous"

Create a repeating rule that checks if signal "ATR Set" is set and runs if it is. That uses "ATR Previous" to run your calcs and updates SV "ATR Previous" to its latest value. On next run it's using the most recent "ATR Previous" value to re-calc and update

If the calcs are multiple step you may need to do a series of rules, each relying on a signal being set by the previous rule, then resetting the signal once complete, effectively daisy chaining the rules together
gaz36f
Posts: 16
Joined: Mon Nov 30, 2020 7:52 am

I think you have cracked it sniffler66. Let me set it up in the way you have suggested and I'll post back. It will probably be tomorrow now. Really appreciated. Thanks again! Best wishes, Gary
User avatar
goat68
Posts: 2019
Joined: Tue Jun 30, 2020 3:53 pm
Location: Hampshire, UK

gaz36f wrote:
Tue Feb 09, 2021 3:57 am
I am attempting to teach myself how to write relatively complex automation rules so that I can automate some functions of my manual trading. To this end, I am writing rules that will calculate certain TA indicators. I know that many believe these indicators are worthless in Sports Trading (and they may be right), but I am using them purely as a way of getting me to understand how I can use BA’s automation function to calculate formulas.

For example, this is what I have done to calculate Average True Range (ATR):

I first need to calculate the True Range (TR) so I setup five stored values:

LatestHi (the maximum value from the range of History List 3 – LTP: 13 seconds to now)
LatestLo (the minimum value from the range of History List 3 – LTP: 13 seconds to now)
HIGHminusLTP (LatestHi – LTP (now))
LOWminusLTP (LatestLo – LTP (now))
HIGHminusLOW (LatestHi – LatestLo)

To get the average I would just divide these last 3 stored values by 3. However, it is possible that one of the values (especially LOWminusLTP) is negative. So, all values must be Absolute (ABS).

As there is no function in BA for ABS, I have created another rule using a signal:

Signal name: lessthanzero (value: 1)
Condition: LOWminusLTP<0

I have then created a new SV (twice):

1st time: LOWminusLTPnew multiplied by 1 (If lessthanzero is not set)
2nd time: LOWminusLTPnew multiplied by -1 (If lessthanzero is set)

I then created an SV (SumofTR):

HIGHminusLTP
LOWminusLTPnew
HIGHminusLOW

And then a final SV (‘TR’) with SumofTR divided by 3.

So, I believe I have overcome the ABS problem using a signal, and additional stored values. Am I right? Is there a better way? I know I can do it in Excel (I already have), but I want to find a way to do everything in BA if possible.

The next problem I am unable to resolve.

To get ATR, the formula is:

ATR = ((ATRprevious*13)+TR)/14

In excel, for the initial ATRprevious value, I would use TR. I would then use the last ATR value in all subsequent calculations. In Excel it is easy. But, as I say, I want to do this using BA Automation. I have tried setting up a benchmark ‘FirstATR’ and creating a HistoryList Value and then creating a subsequent stored value 14 seconds later. But, along the way, my head has become mashed and I cannot work it out.

Can anyone suggest a way how I can create a workaround where a variable (i.e. ATR) is being created using a formula that requires the variables previous value?

I am pretty certain it is possible but after I have really lost my way.

Many thanks,
Gary
Hi Gary,
An interesting problem. I've not quite followed your logic above, but I think there maybe an issue with the fact your using a "moving" window of LTP from History3. The TR series relies on a series of periods, if you're moving them then I don't think it will truly be a ATR as the previous ATR is not static. Also not sure this is quite right?
HIGHminusLTP (LatestHi – LTP (now))
Doesn't TR need to use CurrentPeriodHigh-PreviousClose(of previous period) ?

The way I would approach this is, assuming say I want to create a 14period ATR of TR, where each period is 10seconds, so ATR is of the pervious 140seconds of TRs. This method is a bit brute force but I think will work accurately:
1) Create a continuous hi/lo SVs which you can reset periodically (rearm every refresh)
2) Create a 10second "timer" signal with a "count" SV incremented each time (rearm after 10seconds)
3) For each timer event up to 14 periods to create initial ATR ("count" < 15):
- Calculate ending period endingTR using: hi, lo, PreviousPeriodLTP
- Add endingTR to sumTR14
- reset hi & lo to LTP
- set PreviousPeriodLTP to LTP
- on 14th period calculate previousATR = sumTR14/14
3) For each timer event after the first 14(as no ATR until 14 periods) ("count">=15) record SV for:
- Calculate ending period endingTR using: hi, lo, PreviousPeriodLTP
- reset hi & lo to LTP
- set PreviousPeriodLTP to LTP
- Calculate newATR = (previousATRx13+endingTR)/14
- set previousATR to newATR
4) Calculate currentATR (rearm every refresh)
- Calculate current period currentTR using: hi, lo, PreviousPeriodLTP
- Calculate currentATR = (previousATRx13+currentTR)/14

I hope that helps
User avatar
goat68
Posts: 2019
Joined: Tue Jun 30, 2020 3:53 pm
Location: Hampshire, UK

gaz36f wrote:
Tue Feb 09, 2021 3:57 am
I am attempting to teach myself how to write relatively complex automation rules so that I can automate some functions of my manual trading. To this end, I am writing rules that will calculate certain TA indicators. I know that many believe these indicators are worthless in Sports Trading (and they may be right), but I am using them purely as a way of getting me to understand how I can use BA’s automation function to calculate formulas.

For example, this is what I have done to calculate Average True Range (ATR):

I first need to calculate the True Range (TR) so I setup five stored values:

LatestHi (the maximum value from the range of History List 3 – LTP: 13 seconds to now)
LatestLo (the minimum value from the range of History List 3 – LTP: 13 seconds to now)
HIGHminusLTP (LatestHi – LTP (now))
LOWminusLTP (LatestLo – LTP (now))
HIGHminusLOW (LatestHi – LatestLo)

To get the average I would just divide these last 3 stored values by 3. However, it is possible that one of the values (especially LOWminusLTP) is negative. So, all values must be Absolute (ABS).

As there is no function in BA for ABS, I have created another rule using a signal:

Signal name: lessthanzero (value: 1)
Condition: LOWminusLTP<0

I have then created a new SV (twice):

1st time: LOWminusLTPnew multiplied by 1 (If lessthanzero is not set)
2nd time: LOWminusLTPnew multiplied by -1 (If lessthanzero is set)

I then created an SV (SumofTR):

HIGHminusLTP
LOWminusLTPnew
HIGHminusLOW

And then a final SV (‘TR’) with SumofTR divided by 3.

So, I believe I have overcome the ABS problem using a signal, and additional stored values. Am I right? Is there a better way? I know I can do it in Excel (I already have), but I want to find a way to do everything in BA if possible.

The next problem I am unable to resolve.

To get ATR, the formula is:

ATR = ((ATRprevious*13)+TR)/14

In excel, for the initial ATRprevious value, I would use TR. I would then use the last ATR value in all subsequent calculations. In Excel it is easy. But, as I say, I want to do this using BA Automation. I have tried setting up a benchmark ‘FirstATR’ and creating a HistoryList Value and then creating a subsequent stored value 14 seconds later. But, along the way, my head has become mashed and I cannot work it out.

Can anyone suggest a way how I can create a workaround where a variable (i.e. ATR) is being created using a formula that requires the variables previous value?

I am pretty certain it is possible but after I have really lost my way.

Many thanks,
Gary
If you want me to code up a standalone baf with what I just mentioned I don't mind doing that, I like coding challenges!! Let me know
gaz36f
Posts: 16
Joined: Mon Nov 30, 2020 7:52 am

Thank you very much goat68. Those steps look very useful indeed. It seems that I have made various errors with the earlier calculations, so I will have to take a much closer look. You are right that TR involves 'CurrentPeriodHigh-PreviousClose', but that is what I thought I had done using LatestHi – LTP (with LatestHi being an SV from an earlier calculation; maximum value from the range of History List 3. After your post, I can see how I have messed this up.

I will go through each of your steps carefully. Thanks for them and thanks for the offer of building a baf file. But as I am doing this to learn more about building automation rules, I want to attempt this myself. Having said that, I will post my draft baf file in this thread and you're more than welcome to critique it! lol

I will start in earnest again tomorrow as it is late here and I'm having a beer.

Thanks, Gary
gaz36f
Posts: 16
Joined: Mon Nov 30, 2020 7:52 am

I have made a total hash of this.

I have understood (I think) sniffer66's point about ABS values and I have managed to remove the signals and incorporate the suggestion into these new set of rules.

I have tried, very hard, to go through each of goat68's steps but, sadly, I have failed abysmally. Whilst I thought I had understood how signals/SV's worked relatively well, I clearly don't. I cannot get my head around:

1. PreviousLTP - is this not the same 'chicken and egg' problem I have with PreviousATR in the ATR equation? I have set it up as a SV with LTP, but I am assuming this is wrong.

2. Resets - I have tried to work out why these values need resetting if they change on each iteration of the 'count'. I know there will be an explanation but I can neither think of one or find one in the forum.

3. Step 4.

These are the rules I have created so far:
Screenshot (116).png
I have attached the baf file. Goat68, would you very kindly take a look and let me know where I am going wrong?

Many thanks, Gary
ATR New.baf
You do not have the required permissions to view the files attached to this post.
User avatar
goat68
Posts: 2019
Joined: Tue Jun 30, 2020 3:53 pm
Location: Hampshire, UK

gaz36f wrote:
Wed Feb 10, 2021 7:57 am
I have made a total hash of this.

I have understood (I think) sniffer66's point about ABS values and I have managed to remove the signals and incorporate the suggestion into these new set of rules.

I have tried, very hard, to go through each of goat68's steps but, sadly, I have failed abysmally. Whilst I thought I had understood how signals/SV's worked relatively well, I clearly don't. I cannot get my head around:

1. PreviousLTP - is this not the same 'chicken and egg' problem I have with PreviousATR in the ATR equation? I have set it up as a SV with LTP, but I am assuming this is wrong.

2. Resets - I have tried to work out why these values need resetting if they change on each iteration of the 'count'. I know there will be an explanation but I can neither think of one or find one in the forum.

3. Step 4.

These are the rules I have created so far:

Screenshot (116).png

I have attached the baf file. Goat68, would you very kindly take a look and let me know where I am going wrong?

Many thanks, Gary

ATR New.baf
sure np, I should have some time this evening to take a look
gaz36f
Posts: 16
Joined: Mon Nov 30, 2020 7:52 am

Thanks Goat68. Much appreciated.
User avatar
goat68
Posts: 2019
Joined: Tue Jun 30, 2020 3:53 pm
Location: Hampshire, UK

gaz36f wrote:
Wed Feb 10, 2021 7:57 am


ATR New.baf
Hi Gary,
I'm just looking at your baf file, some notes:
"hi/lo SVs" rule:
You're setting hi & lo to the High Traded Price, however that is not quite what you want, as that is the High/Low since the market opened...whereas you want the high/low for the current 10second TR period. What you need is to on your 10second timer reset your hi/lo to the current LTP. Then have 2 rules one for hi one for lo, which has a condition if current LTP > hi then update hi SV, and a lo rule if current LTP < lo then update lo. Then at the next 10second timer they get reset to currentLTP, etc etc

timer rule:
This needs to be slightly different,as you need to "signal" to the following rules which will then reset the signal once processed, and also keep count. So have the signal say called "timer_trigger", and in the same rule also increment the "count" as a SV (count = count +1)

For each timer count<15:
So the Conditions for this is now 2 lines: "timer_trigger" set and SV "count"<15
In SV actions:
PreviousPeriodLTP can just be set to Current LTP at the "end" of the SV actions, as this has just triggered at the end of the period, so the closing period price for the current period is the current LTP. You will need to init the 1st PreviousPeriodLTP to lo or hi, doesn't matter which, add another rule with same trigger before this one with if count=1 set PreviousPeriodLTP=hi.
In your Ending TRs rule calculate both Hi-PreviousPeriodLTP & PreviousPeriodLTP-Hi, ditto Lo, then in your following 4 SumTr rules make the Condition on these values which are >0, to achieve your Abs() function, note your range function needs to be "Max of values".
You also need a different "trigger" condition as they are going to trigger every refresh as they are. You just want "timer_trigger" set and SV "count"<15, same as previous rule.

You also need both a 14th and a <14th period rules both triggered by: "timer_trigger" set and SV "count"=14/<14 as well. BOTH these in their Signal tab need to Clear "timer_trigger", so you can see this signal has triggered all the above rules on this event, but you must now clear it otherwise it will just run this rule again on next refresh.

I suggest trying to get the above bit working first, ie.you get your 1st ATR calculated after 14 10second periods correctly. Utilise logging to check the results.

I hope that helps to start with, this is a tricky one to do, but progress stage at a time is the best way to get there. Once you have the above working progress to the >14period ATR calculation... I can help you there if you need.
Cheers
Goat
gaz36f
Posts: 16
Joined: Mon Nov 30, 2020 7:52 am

Hi Goat, thanks very much indeed for all your help and guidance with this. I thought I would hold off responding until I had 'finished' it. Anyway, it is taking much longer than I thought as I trying to read up more about signals and the implementation of timer rules. I want to ensure I fully understand each step so any subsequent bots will be easier to write. Hopefully I'll post the working bot in this thread in the next couple of days. Cheers, Gary
Post Reply

Return to “Bet Angel - Automation”