Login Page - Create Account

Support Board


Date/Time: Wed, 15 Jan 2025 12:53:37 +0000



Simple auto trading buy/sell crossover SMA

View Count: 3843

[2017-08-08 14:46:15]
Turbo-Jammer - Posts: 14
Hi,

Am trying to test a simple auto trading method using 2 simple moving averages using the spreadsheet. I want to buy when it crosses the above lower SMA and exit when it crosses below the higher SMA during uptrends and opposite in downtrends. I have following formulas in the respective cells for uptrend:

Buy Entry K3:
=CROSSFROMBELOW( ID0.SG4@3:ID0.SG4@4, ID2.SG1@3:ID2.SG1@4)

Buy Exit L3:
=CROSSFROMABOVE( ID0.SG4@3:ID0.SG4@4, ID3.SG1@3:ID3.SG1@4)

Based on the above when a buy order is triggered a corresponding buy exit is suppose to close out the long position only. But am not seeing a one to one relationship on the attached chart. I only want 1 trade to trigger at a time, once a buy entry is triggered no new buy until the open trade is closed by the buy exit. As you can see from on the attached chart that's not happening, what am I doing wrong?
imageEUR-201709-GLOBEX [CV] 240 Min #1 2017-08-08 10_42_09.73.png / V - Attached On 2017-08-08 14:44:02 UTC - Size: 55.31 KB - 675 views
imageScreenShot292 _2017-08-08-10-06.png / V - Attached On 2017-08-08 14:44:28 UTC - Size: 27.92 KB - 427 views
[2017-08-08 18:17:06]
Sawtooth - Posts: 4151
It looks to me like you have swapped ID2 and ID3.
[2017-08-08 18:53:42]
Turbo-Jammer - Posts: 14
Silly me, it looks better after I swapped ID2 and ID3, but am still seeing trades without corresponding Buy Entry/Buy Exit. How do I fix that?

http://www.sierrachart.com/image.php?Image=1502218303605.png
[2017-08-08 20:23:21]
Sawtooth - Posts: 4151
If you want alternating arrows, you'll need to create a persistent variable at an entry, then cancel it at an exit.
Then look for the first occurrence of each for an entry and exit, respectively.

Try this:
O3:
=IF(CROSSFROMBELOW(ID0.SG4@3:ID0.SG4@4,ID3.SG1@3:ID3.SG1@4),1,IF(CROSSFROMABOVE(ID0.SG4@3:ID0.SG4@4,ID2.SG1@3:ID2.SG1@4),0,O4))
K3:
=AND(O4=0,O3)
L3:
=AND(O4,O3=0)

Your entries and exits are intrabar (Signal Only On Bar Close is set to No), so this will not always work correctly.
Arrows will only show what happened at close of bar, so you will have some entries and exits in bars without arrows.
Using intrabar signals will always complicate things.
Date Time Of Last Edit: 2017-08-08 20:24:50
[2017-08-09 03:00:00]
Turbo-Jammer - Posts: 14
Not sure why the intrabar matters, I just want when a buy order is open no other buy order is allowed until the 1st buy order is closed with a buy exit. And the opposite when a sell order is open no other sell order until the 1st sell order is closed by a sell exit.
[2017-08-09 04:01:22]
Sawtooth - Posts: 4151
With intrabar signals, you could get an exit before exit arrow. Then the entry formula could place another order at an arrow that didn't show before. As I said, using intrabar signals will complicate things.
Date Time Of Last Edit: 2017-08-09 05:09:16
[2017-08-09 13:12:19]
Sawtooth - Posts: 4151
What you're not seeing with close-of-bar arrows is the intrabar crossovers that didn't also occur at close of bar.
With alternating close-of-bar arrows, some close-of-bar entry crossovers will not show because a close-of-bar exit hadn't occurred yet.
With alternating close-of-bar arrows, some close-of-bar exit crossovers will not show because a close-of-bar entry hadn't occurred yet.
So if an unmarked intrabar crossover causes an entry or exit, a new previously hidden close-of-bar arrow might appear.
[2017-08-09 15:37:56]
Turbo-Jammer - Posts: 14
Changing the "(Signal Only On Bar Close is set to Yes)" and your logic in comment#4 seems to work. That means after the signal is generated it'll send a market order after the close of the bar?

http://www.sierrachart.com/image.php?Image=1502293045806.png
[2017-08-09 15:55:20]
Sawtooth - Posts: 4151
That means after the signal is generated it'll send a market order after the close of the bar?
Yes, at the open of the following bar.

The multiple arrows in your pic likely correspond to the earliest bars/rows of the spreadsheet study's Number of Rows setting, IOW 1000 bars back from the current bar. There are likely no arrows on the chart earlier than that. This is the result of the formula catching up to itself.
[2017-08-09 16:19:06]
Turbo-Jammer - Posts: 14
ok, one more question. Instead of buying after it closed above the lower line, what's the code to buy after the first or second close below the lower line? We'll keep the buy exit the same.
[2017-08-09 17:29:16]
Sawtooth - Posts: 4151
1) Use column P to count the bars since a crossover of ID3 where the close is less than ID3:
P3:
=IF(O3=0,0,IF(AND(O3,E3<ID3.SG1@3),P4+1,P4))

2) Enter a numeric value in cell K1 as the triggering bar count value.
If you change this, you'll need to go to Chart >> Recalculate to update the arrows.

3) Change the formula in K3 to this:
=AND(O3,P4<$K$1,P3=$K$1)
where the value of K1 is the triggering bar count value. Edit as needed.

4) Use column Q to create a persistent variable of the new entry signal:
Q3:
=IF(K3,1,IF(O3=0,0,Q4))

5) Change the formula in M3 to this:
=AND(Q4,Q3=0)
Date Time Of Last Edit: 2017-08-09 17:33:11
[2017-08-09 20:53:22]
Turbo-Jammer - Posts: 14
For downtrend I made the following changes, but doesn't seem work.

O3:
=IF(CROSSFROMABOVE(ID0.SG4@3:ID0.SG4@4,ID2.SG1@3:ID2.SG1@4),1,IF(CROSSFROMBELOW(ID0.SG4@3:ID0.SG4@4,ID3.SG1@3:ID3.SG1@4),0,O4))

P3:
=IF(O3=0,0,IF(AND(O3,E3>ID2.SG1@3),P4+1,P4))

M3:
=AND(O3,P4<$K$1,P3=$K$1)

N3:
=AND(Q4,Q3=0)

Q3:
=IF(M3,1,IF(O3=0,0,Q4))

With the changes to O3,Q3 and P3, isn't it suppose to sell Entry when it close above High SMA and Sell Exit when it close above the low SMA?
[2017-08-09 23:05:15]
Sawtooth - Posts: 4151
Columns O, P and Q are for longs only.
For short trades, you need to replicate columns O, P and Q. Use R, S, and T.
You can still reference K1 in long and short formulas.

isn't it suppose to sell Entry when it close above High SMA and Sell Exit when it close above the low SMA?
No, but your formula is correct, except you can't use column O if it is also used for longs.
If you are using the inverse of the long formula, you want the sell entry to cross from above the higher SMA (ID2), and the sell exit to cross from below the lower SMA (ID3).

Longs:
O3:
=IF(CROSSFROMBELOW(E3:E4,ID3.SG1@3:ID3.SG1@4),1,IF(CROSSFROMABOVE(E3:E4,ID2.SG1@3:ID2.SG1@4),0,O4))
P3:
=IF(O3=0,0,IF(AND(O3,E3<ID3.SG1@3),P4+1,P4))
Q3:
=IF(K3,1,IF(O3=0,0,Q4))
K3:
=IF($K$1=0,AND(O4=0,O3),AND(O3>0,P4<$K$1,P3=$K$1))
L3:
=AND(Q4,Q3=0)

Shorts:
R3:
=IF(CROSSFROMABOVE(E3:E4,ID2.SG1@3:ID2.SG1@4),1,IF(CROSSFROMBELOW(E3:E4,ID3.SG1@3:ID3.SG1@4),0,R4))
S3:
=IF(R3=0,0,IF(AND(R3,E3>ID2.SG1@3),S4+1,S4))
T3:
=IF(M3,1,IF(R3=0,0,T4))
M3:
=IF($K$1=0,AND(R4=0,R3),AND(R3>0,S4<$K$1,S3=$K$1))
N3:
=AND(T4,T3=0)

I changed K3 and M3 so you can use 0 in K1 for no offset. However, this will require setting Support Reversals to Yes.
When K1 is >0, these formulas will sometimes give you consecutive longs or consecutive shorts because of the offset entries.
[2017-08-10 20:19:42]
Turbo-Jammer - Posts: 14
That sort of works, but not exactly what am looking for. Here's an example of how it should trigger a short.

http://www.sierrachart.com/image.php?Image=1502396256907.png
[2017-08-10 20:43:44]
Sierra Chart Engineering - Posts: 104368
only want 1 trade to trigger at a time, once a buy entry is triggered no new buy until the open trade is closed by the buy exit. As you can see from on the attached chart that's not happening, what am I doing wrong?
The study Input settings look fine to prevent this.

Regarding the question in the first attached image, you cannot look at the arrows to know if there is an actual trade. This is explained here:
Spreadsheet Systems, Alerts and Automated Trading: Buy/Sell Arrows Do Not Mean Trade Signals

Also, refer to this section and all of the subsections:
Spreadsheet Systems, Alerts and Automated Trading: Ignored Signals With Spreadsheet Systems or Alerts
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2017-08-10 20:44:17
[2017-08-15 03:48:43]
Turbo-Jammer - Posts: 14
Well, why display an arrow if no trade actually triggered? Is there a way to only display the arrows when the actual trade was triggered?
[2017-08-15 06:15:39]
Sierra Chart Engineering - Posts: 104368
The arrows represent the TRUE/1 values in the formula column cells.


You instead need to display the order fills:
Chart Trading and the Chart DOM: Displaying Order Fills on the Chart
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2017-08-16 18:38:25]
Turbo-Jammer - Posts: 14
Where I can learn more on how to write the formulas that go into the spreadsheet? Specifically fields K through T?
[2017-08-16 20:52:57]
Sierra Chart Engineering - Posts: 104368
Refer to:
Working with Spreadsheets
Spreadsheet Functions
Using the Spreadsheet Study: Step By Step Instructions to use the Spreadsheet Study
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account