Problem Understaning the EL Code

dima777

Member
Messages
85
Likes
1
Hi there!
I am having a difficulty understanding the following easylanguage code so I hope you can help me. The code instructs TS to buy when the moving average slopes up or down at an upper/lower band but in fact the trades are initiated even if the price doesn’t reach these bands – as if shown at the picture that I attached. I wonder what is the purpose of this upBand and dnBand functions. Looking forward to your help!

The code:
Code:
{King Keltner by George Pruitt—based on trading system presented by Chester
Keltner}
Inputs: avgLength(40), atrLength(40);
Vars: upBand(0),dnBand(0),liquidPoint(0),movAvgVal(0);
movAvgVal = Average(Close,avgLength);
upBand = movAvgVal + AvgTrueRange(atrLength);
dnBand = movAvgVal - AvgTrueRange(atrLength);
if(movAvgVal > movAvgVal[1]) then Buy ("KKBuy") tomorrow at upBand stop;
if(movAvgVal < movAvgVal[1]) then Sell Short("KKSell") tomorrow at dnBand
stop;
liquidPoint = movAvgVal;
If(MarketPosition = 1) then Sell tomorrow at liquidPoint stop;
If(MarketPosition = -1) then Buy To Cover tomorrow at liquidPoint stop;

thanks!
 

Attachments

  • pic.jpg
    pic.jpg
    75.4 KB · Views: 336
I would say to check to make sure that you are doing several things correctly. Make sure that you are actually using the proper indicator to accompany the strategy. Make sure that the inputs for each are the same. Make sure that your scaling is set correctly for the indicator. Keep in mind that a stop means this price or worse, meaning the price or higher for a long entry or this price or lower for a short entry and the opposite for an exit. Also keep in mind that it will be using the values on the bar prior to the bar of entry.

here is an example of a confusing trade.

The condition to try for a long is that the average is higher than the average before it. Well this is true and then if the price is at or higher than the upperband on the next bar you will get a trade. You will see a long appear on the screen the next day but the price is way above the upper band. Well that is exactly correct but if you expected it to be on the band always then this is improper thought. The code that is used in this strategy is not a normal Keltner so you can't plop a regular Keltner Channel from TS onto the screen and have it work.
 
Last edited:
The purpose of the bands is to provide 1) the entry price to enter long or short and, 2) the stop price to use. The condition to enter long or short is determined by the moving average, not the price on the upper and lower bands.

goodluck
 
I would say to check to make sure that you are doing several things correctly. Make sure that you are actually using the proper indicator to accompany the strategy. Make sure that the inputs for each are the same. Make sure that your scaling is set correctly for the indicator. Keep in mind that a stop means this price or worse, meaning the price or higher for a long entry or this price or lower for a short entry and the opposite for an exit. Also keep in mind that it will be using the values on the bar prior to the bar of entry.

here is an example of a confusing trade.

The condition to try for a long is that the average is higher than the average before it. Well this is true and then if the price is at or higher than the upperband on the next bar you will get a trade. You will see a long appear on the screen the next day but the price is way above the upper band. Well that is exactly correct but if you expected it to be on the band always then this is improper thought. The code that is used in this strategy is not a normal Keltner so you can't plop a regular Keltner Channel from TS onto the screen and have it work.

hi..thanks for the detailed reply...i made sure that I use all the proper indicators but still i cannot understand what is the purpose of writing that the entry should be AT the upper or lower band and still allow it to be worse than that by using the STOP expression...:(
 
The purpose of the bands is to provide 1) the entry price to enter long or short and, 2) the stop price to use. The condition to enter long or short is determined by the moving average, not the price on the upper and lower bands.

goodluck

Yes. that is right but why would you see such things as you see in teh attached picture - the price of entry is NOT touching the bands there!!:eek:
 
Just having a quick look at your chart....you've got the strategy ATR set at 20 instead of 40, as per your indicator on the chart.

Hope this helps

Blu-Ray
 
In TradeStation you can make this setting automatic by the proper application of global variables which will open a portal between the indicator and strategy and allow them to communicate with one another. In this fashion anytime that you change the settings in your strategy the indicator will then automatically change the settings that it uses to match those of the strategy.

The condition for entry is only that the moving average be sloping up or down. It does not require that anything be touching a band to enter.
 
The condition for entry is only that the moving average be sloping up or down. It does not require that anything be touching a band to enter.

The code is using stop orders, so they will only execute if price has touched either band.
 
After the condition is met which is a sloping up moving average for a long entry then the price does not have to be touching a band. A stop is this price or worse and if you send it out based on what the band price was on the previous bar then if the price is well above the band it will execute.
 
When using a stop order, it acts as a stop market order, so price has to touch it, then it will turn into a market order, so you might not get filled at that price ( possibly worse ).
 
Price does not have to touch it based upon a conditional entry as that which is described in the code above. It only has to be at or worse than the price listed for the stop price. If the price is worse than the listed stop price when the conditional is met then the order will be sent as a market order and you will get whatever the next price is regardless of if it is touching the band or not.

Lets say that price is above the bands by a few points but the MA is flat or sloping down for a bit and then it has the one bar up slope required for an entry. It will take this entry by sending out a market order because price is at or worse than the band[ 1 ] for the long entry and you will get whatever price is shown on the next tick.
 
Last edited:
Price does not have to touch it based upon a conditional entry as that which is described in the code above. It only has to be at or worse than the price listed for the stop price. If the price is worse than the listed stop price when the conditional is met then the order will be sent as a market order and you will get whatever the next price is regardless of if it is touching the band or not.

Lets say that price is above the bands by a few points but the MA is flat or sloping down for a bit and then it has the one bar up slope required for an entry. It will take this entry by sending out a market order because price is at or worse than the band[ 1 ] for the long entry and you will get whatever price is shown on the next tick.

Ah, yes I see where you coming from now, my apologies, in that case he would need to use the following for example:

if(movAvgVal > movAvgVal[1]) and high < upband then Buy ("KKBuy") tomorrow at upBand stop;

and vice versa for sellshorts.

Cheers

Blu-Ray
 
Here is an actual trade example from this strategy which illustrates this effect that I am talking about very well.

The green arrow show the condition for entry. Notice that price is actually at the BOTTOM band. It would enter on the gap up shown by the yellow arrow even though price is well above the upper band now. Note this strategy was already in a long position right before and it got taken out with the exit and then re-entered since the conditions were valid once again.
 

Attachments

  • StratOpt_Dima2.png
    StratOpt_Dima2.png
    6 KB · Views: 235
Here is an actual trade example from this strategy which illustrates this effect that I am talking about very well.

The green arrow show the condition for entry. Notice that price is actually at the BOTTOM band. It would enter on the gap up shown by the yellow arrow even though price is well above the upper band now. Note this strategy was already in a long position right before and it got taken out with the exit and then re-entered since the conditions were valid once again.

wow...such a great discussion!!)) i reattached the strategy and the indicators and now can see that the strategy only enters if the price actually exceeds one of the bands - in one or two rare exceptions..i am ok with price exceeding the bands and the trades being placed at a worse price BUT i cannot understand how the trades can be entered when bands have NOT yet been touched....
 

Attachments

  • problematic bars.jpg
    problematic bars.jpg
    73 KB · Views: 253
You have to look at what the price of the bands WAS on the bar before where it takes the entry. Do not look at where the bands ARE at the entry or you will get confused. In the examples that you show here you will see that the price is the correct price for the previous band location. The location of the signal generation is on the bar before the actual trade is taken. The conditions are all met and then the price of the bands is used as the entry criteria for the next bar.
 
well..that is confusing in itself...does it not logically flow that the code which reads "buy tomorrow at" should be referring to the tomorrow value ofthe indicator to which it is referring....thanks anyway for your attention!:clap:
 
Last edited:
Basically you have to project out a price for the next bar to be used for the stop. You know the price of the bands at the point where you are going to send out the order, but you do not know what the price for the bands will be on the next bar. So you send it out using the price that you have. The same would be true if you said buy next bar close stop. This would send out an order for the next bar using the close price of the bar where you sent the order from.

buy tomorrow is antiquated coding construct anyways and not very many programmers are going to be using it for their strategies these days.
 
Thank you StratOpt. You have precisely and clearly noted a factor that I had failed to effectively note - that is, the bands have a one day lag to the current day's prices.:)
 
You are most certainly welcome and thanks for letting me know that something I do might help somebody a bit with learning something new.
 
Top