Sample strategy with charts

Video: A more in-depth look at Seetu, including an Exponential Moving Average (EMA) trading strategy that is not meant to make money, but which - surprisingly - does.

Example 9. Strategy: Sample strategy with charts

       This is an example of a basic Seetu program. The trading system is purely an example meant to teach the language,
       and NOT meant to make money. This goal of this program is to demonstrate common components of Seetu code.
       By the way, this area that you are reading (starting with slash-star and ending with star-slash)
       is a multi-line comment. Comments like these are ignored by Seetu.
      // Any line that starts with two slashes, like this one, is a single-line comment
      // which is also ignored by Seetu.
      // Let's start with some common ideas you will often find in Seetu programs:
      // Delay entry/exit by one bar
      SetTradeDelays( 1, 1, 1, 1 );
      // When we examine back-tested results of our trading strategy,
      // results are based on the fact that we start our backtest with $100,000 dollars.
      // We specify this as follows:
      SetOption( "initialequity", 100000 );
      // Round a number of shares traded to 100. 
      // We can also specify that each trade we place will be 25% of available equity.
      SetPositionSize( 25, spsPercentOfEquity ); 
      // Now for the trading system itself. It is based on an Exponential Moving Average, or EMA.
      // Here are the rules.
      - Buy when EMA averages crosses, AND the current HIGH is highest for 50 bars
      - Sell when EMA averages crosses back
      // Set EMA periods to varibles.
      // We can change them easily on this single place when testing different periods.
      LongEMAPeriods = 50; 
      ShortEMAPeriods = 5; 
      // Calculate and save average and recent highest high value 
      // ---------------------------------------------------------------------
      // This section calculates useful values that we will use told decide when to buy and sell.
      // First, the longer moving average
      // Set the variable name LongMA to be the Exponential Moving Average of the last several CLOSEs. 
      // How many closes?
      // A number of periods (variable name LongEMAPeriods) is set to 50. 
      // So we are setting LongMA to the exponential moving average of the last 50 closes.
      LongMA = CalcEMA( Close, LongEMAPeriods );
      // Shorter MA is calculated in a similar fashion, but only uses the last 5 CLOSEs. 
      // (The number 5 is stored in the ShortEMAPeriods variable.)
      ShortMA = CalcEMA( Close, ShortEMAPeriods );
      // What is the highest HIGH in the last 50 bars?
      LastHigh = GetRecentHighest( High, LongEMAPeriods );
      // ---------------------------------------------------------------------
      // Buy logic:
      // Remember the strategy: "Buy when EMA averages crosses, AND the current HIGH is highest for 50 bars."
      // Here's how we say that in Seetu language:
      Buy = Cross( ShortMA, LongMA ) AND High > Ref( LastHigh, -1 );
      // This part of the statement above:
      // 	High > Ref( LastHigh, -1 ) 
      // means: this bar's HIGH must be greater than the greatest HIGH over the last LongEMAPeriods bars
      // If we had instead used H==LastHigh . . . that would have required the current high is equal to the previous high 
      // Sell logic:
      Sell = Cross( LongMA, ShortMA );
      // Remove surplus trade signals to have less rows in Scan and Exploration
      Buy = ExRem(Buy,Sell);
      Sell = ExRem(Sell,Buy);
      // Exploration: 
         Exploration is based on the Filter variable. 
         Exploration includes just those rows where Filter is TRUE. 
      // In this particular case we restrict the Exploration window to display only those rows where Buy or Sell signals occur. 
      Filter = Buy OR Sell;
      // Add some columns to Exploration grid
      AppendColumn( Buy, "Buy", 1 );
      AppendColumn( Sell, "Sell", 1 );
      AppendColumn( High, "High" );
      AppendColumn( LastHigh, "RecentHighest" );
      AppendColumn( LongMA, "Long MA", 1.3 );
      AppendColumn( ShortMA, "Short MA", 1.3 );
      // Charts:
      doPlot( C, "Close Price", colorLightGrey, styleBar);
      doPlot( LongMA, "EMA(C," + PrintVal(LongEMAPeriods,1) + ")", colorRed, styleLine); 
      doPlot( ShortMA, "EMA(C," + PrintVal(ShortEMAPeriods,1) + ")", colorGreen, styleLine);
      doPlot( Ref(Lasthigh,-1), "RecentHighest(H," + PrintVal(LongEMAPeriods,1) + ")", colorBlue, styleDots);