I wrote some code (First time!) and it doesn't seem to be triggering my sell signals correctly.
This is a long system.
I would like a sell signal to be generated when the Top Bollinger Band (20 MA w/ 1 Std Dev) crosses down over the 50 SMA. The code I wrote is:
TopBB1 = BBandTop (Close,20,1);
Sell = Cross (Close, MA(Close, 200)) OR
Does this look correct?
Thank you in advance!
You said bbtop crosses down below 50 day sma.
Right now your cross() has “close” crossing above bbtop or 50 day MA.
The link gives example of handling cross above and below. As per your definition of bbtop crossing 50 day sma you don’t need “close” involved at all.
In the Sell rules, it’s the second line of code I’m asking about:
I'm starting to wonder if the issue is the OR operator. I have 2 lines of code for my sell condition. One is for profit, the other is a stop. The line of code after the OR does not seem to be triggering.
Am I missing something?
I think I failed to use IF/Else First.
You need to use Code Tags! See How to use this site.
Cross (TopBB1, FiftyMA);
See this post for some debugging tips: How do I debug my formula?
Thank you. I did read that, I think I'm still in a stage where I am still learning how certain functions work. I think I'm trying to get to the point where I can be self sufficient but I don't think I'm there quite yet.
I tried flipping the arrays as mentioned twice above but the signal is still not being generated when it should.
I think I should have asked my initial question differently (I did search for this answer)
What is the best way to write a sell order where you have mutually exclusive conditions? I used OR but I'm wondering if that is the entire issue. Should I try to nest them using if/else and then make else=false?
At my stage simpler is better. I was thinking I could just string conditions together using OR but I doesn't seem to work that way.
How would we know without seeing your full code?
@tgiann3 I don't think you want to sell when your Close rises ABOVE your long term moving average.
And we did not see if you followed @mradtke advice to debug your code. You would have found that mistake. There is nothing wrong with using "OR" if you code your conditions properly and use the correct logic.
TopBB1 = BBandTop( Close, 20, 1 ); FiftyMA = MA( Close, 50 ); Sell1 = Cross( MA( Close, 200 ), Close ); Sell2 = Cross( FiftyMA, TopBB1 ); Sell = Sell1 OR Sell2; // debug your code using an Exploration // Filter = 1; AddColumn( C, "Close" ); AddColumn( MA( Close, 200 ), "MA200" ); AddColumn( TopBB1, "TopBB1" ); AddColumn( FiftyMA, "FiftyMA" ); AddColumn( Sell1, "Sell1", 1.0, colorDefault, IIf( Sell1, colorRed, colorDefault ) ); AddColumn( Sell2, "Sell2", 1.0, colorDefault, IIf( Sell2, colorRed, colorDefault ) );
Thank you all for taking the time to respond to my post.
I probably should have stated in my initial post that this is a mean reversion system. It's literally titled "My First Algo," so I wasn't expecting miracles.
In plane english, the starting concept of the strategy was:
What would happen if you had a mean reversion system for microcaps where the buy signal was price crossing a Bollinger Band with a very high standard deviation?
This was as much about seeing if I could get a small program to work properly as it was about answering the original question.
After I wrote the code, I did a few things to check my code for bugs before I posted:
I used the check syntax button along the process
I used the show current trade arrows feature after I completed the backtest
I used the code check & profile feature when I determined the last line of code was not working properly.
I went back through the code line by line multiple times to see if I could determine the cause of the last line issue.
I used Help to review the syntax of the Cross function
At that point I was stumped. I had not been formally taught in either of my Amibroker classes(yet) how to use the OR function (One of my classes taught the use of the AND function and I extrapolated the use of OR from that), so I started to think that could be the issue.
That's when I posted my question. I had read the use of the forum post several months ago but, yes, I failed to used code tags, and I apologize. I did, however, search the forum in advance. I will admit that sometimes, I am not entirely certain what to search for. I will start making a habit of just generally reading others posts to gain more knowledge of common terms.
Yesterday, prior to @portfoliobuilder's response, I decided to go through and try to use more variables in working towards the use of optimization. That's when I found the issue. As @portfoliobuilder wrote in his code above, I defined my variables for moving averages as constants, not as moving averages. I will go back and look at it this morning but I think if I want to use variables in the moving average it should be a 2 step process (define the variable for the MA range, use that variable in the MA range array).
I am posting my full code (after multiple revisions since the first post) below. I fixed the issue I stated above (where I defined moving averages as constants instead of defining them as moving averages). I will work on adding that additional step of creating variables for the 2 MA range arrays and further optimizing the system from a trading perspective today.
Finally, I would like to thank @portfoliobuilder for the code you wrote above. I will add this to my strategy today and learn how this step works.
Again, Thank you everyone.
This is my code as it stands this morning before today's work:
////////// My First Algo ///////////// /*Rules Buy = Price 0.5-1.5 (optimize price) Volume > 1 million/day for last 100 trading days (optimize range of days) Close outside 3 std dev Lower Bollinger Band (Optimize how many std deviations) Sell = close cross 200 SMA (optimize range, type of MA) */ setOption("MaxOpenPositions", 1000); SetOption("InitialEquity",1000000); //SetOption("CommissionMode",3); // 0= use portfolio manager commission table, 1= % of trade, 2= $ per trade, 3= $ per share/contract //SetOption("CommissionAmount", .01); //SetOption("InterestRate",0); SetOption("AllowPositionShrinking" , True); roundlotsize = 1; SetTradeDelays(1, 1, 1, 1); SetPositionSize(3,spsPercentOfEquity); Short = False; Cover = False; BuyPrice = Open; SellPrice = Open; LowerPrice = 0.5; //Buy Price Low UpperPrice = 1.5; //Buy Price High VolumeRequirement = 1000000; //Liquidity Check- Minimum Daily Volume Required DaysVolumeRequired = 100; // Liquidity Check - Number of Days Volume is Required BuyBottomBBMA = 20; //Buy Signal Bollinger MA (Is a bottom BB) BuyBottomBBSTDV = 3; //Buy signal Bollinger Std Dev (Is a bottom BB) ProfitMA = MA(Close,200); StopBottomBBMA = 20; // Bottom Bollinger Band MA for STOP StopBottomBBSTDV = 6; //Bottom Bollinger Band Standard Deviation for STOP StopTopBBMA = 20; // Top Bollinger Band MA - STOP StopTopBBSTDV = 1; // Top Bollinger Band Standard Deviation - STOP STOPMA = MA(Close,200); BuyBottomBB = BBandBot(Close,BuyBottomBBMA,BuyBottomBBSTDV); StopBottomBB = BBandBot(Close,StopBottomBBMA,StopBottomBBSTDV); StopTopBB = BBandTop (Close,StopTopBBMA,StopTopBBSTDV); Buy = Close > LowerPrice AND Close < UpperPrice AND HHV (Volume,DaysVolumeRequired) > VolumeRequirement AND Cross (BuyBottomBB,Close); Sell = Cross (Close, ProfitMA) OR Cross (STOPMA, StopTopBB); //Cross (StopBottomBB,Close); type or paste code here
This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.