= Video tutorial available

Strategy: Simple MACD

MACD, short for moving average convergence/divergence, is a trading indicator used in technical analysis of stock prices, created by Gerald Appel in the late 1970s. It is supposed to reveal changes in the strength, direction, momentum, and duration of a trend in a stock's price.

/*
This is a multiline comment.

------ Simple MACD Strategy Example ------

*/

// Set initial capital 
SetOption("InitialEquity",50000);

// Positions sizes: use 25% of the current portfolio equity. (It means max. 4 open positions.)
SetPositionSize( 25, spsPercentOfEquity );

// Round a number of shares to 100. 
SetOption("RoundLotSize",100);

// Buy when MACD crosses the Signal line up. 
Buy = Cross(CalcMACD(), CalcMACDSignal());

// Sell when Signal crosses the MACD line. 
Sell = Cross(CalcMACDSignal(), CalcMACD());

// Delay trades to the next bar open.
SetTradeDelays( 1, 1, 0, 0 );

// Set buy and sell prices to the opening price.
BuyPrice = Open;
SellPrice = Open; 



Exploration: Stocks volumes

 
  // Find stocks having a 100 days volume moving average greater than 30,000,000

  // Calculate the 100 days moving avarage. 
  // Save a result to some variable - we will use it twice.
  VolumeAverage = CalcMA(Volume, 100);
    
  // Seetu Exploration is based on the Filter variable.
  // Fill the Filter variable by our desired condition.
  Filter = VolumeAverage > 30000000; 

  // Define columns we want to see.
  AppendColumn(VolumeAverage,"Volume Avg",1.0);
  AppendColumn(Volume,"This date volume",1.0);
  AppendTextColumn(FullName(),"Name");


Exploration: Technical indicators

How to use technical indicators and arbitrary custom defined values in Exploration.

 
    // -----------
    // Exploration
    // -----------

    // Show me some indicators values when Close is above its 20-days moving average.
    Filter = close > CalcMa( close, 20 ); 

    // ... and show me just most recent values.
    // (We are using a daily data so "more recent values" are from yesterday.)

    // Use this idiom if you want to use just the last bar.
    ThisIsLastBar = BarIndex() == LastValue( BarIndex() ); 

    // Add our "just most recent" condition to the Filter variable.
    Filter = Filter AND ThisIsLastBar;


    // Define Exploration columns

    // Show me *how much* is Close above its average
    AppendColumn( Close - CalcMa( Close, 20 ), "Close Up");

    // Show me following technical indicators values:

    AppendColumn( CalcMacd(), "MACD", 1.4 );
    AppendColumn( CalcMACDSignal(), "Signal", 1.4 );
    AppendColumn( CalcADX(), "ADX", 1.4 );
    AppendColumn( CalcRSI(), "RSI", 1.4 );
    AppendColumn( CalcROC( Close, 15 ), "ROC(15)", 1.4 );

/*
    Comments
    --------

    1) Recommended, more "ecological" code which does not calculate "CalcMa( Close, 20 )" twice: 

       // Calculate and save moving average to the "myMA20" variable 
       myMA20 = CalcMa( close, 20 );

       // Use myMA20
       Filter = close > myMA20; 

       // Use myMA20
       AppendColumn( Close - myMA20, "Close Up");
    
    
    2) Barindex() is an array of ordinal bars numbers (starting from zero).
       Say we have 5 bars (days) in the database.
       Barindex() is the following array of values:  
       0 1 2 3 4
       
       LastValue( BarIndex() ) is 4
       
       When we compare an array and a number in Seetu, the number is converted to the array too.
       So the expression "BarIndex() == LastValue( BarIndex() )" compares following arrays:

       0 1 2 3 4  ... this is the BarIndex() array
       4 4 4 4 4  ... this is the number 4 converted to array
       
       The result is an array:
       0 0 0 0 1  ... just the last item is the same in both arrays 

       This array is stored to the ThisIsLastBar variable in our example.
       
       We use the ThisIsLastBar variable to convert all but last values in Filter to FALSE.
       
       Say the Filter variable content is: 
        1 0 1 1 1 
       after the "Filter = close > CalcMa( close, 20 );" command.
       
       The command "Filter = Filter AND ThisIsLastBar" means a logical AND of two arrays then:
       1 0 1 1 1    ... Filter before the command 
       0 0 0 0 1    ... ThisIsLastBar variable content
       0 0 0 0 1    ... the result. Assigned to the Filter variable.
       
       It means the Filter variable holds just the last value of the "close > CalcMa( close, 20 )" condition.
    
*/



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. 
SetOption("RoundLotSize",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); 


Introduction to generating Collective2 Signals (C2TradingSignal command)

How to use Seetu to generate Collective2 trading signals which get sent to your Collective2 trading strategy.

/* 
   Example: Simple Relative Strength Index strategy + Collective2 trading signals
*/

  // Collective2 trading system ID
  SetOption("C2TradingSystemId",92923646);   

  // If you want it, you can get "Buying Power" of the above system
  C2BuyPower = GetOption("C2BuyPower");   

  // Trading system logic

  // Calculate and save Relative Strength Index
  Rsi_Value = CalcRSI();  

  // Buy if RSI_Value crosses above 30
  Buy = Cross( Rsi_Value, 30 ); 

  // Sell if RSI_Value falls under 70
  Sell = Cross( 70, Rsi_Value );
  
  // Remove surplus trade signals to have less rows in Scan and Exploration
  Buy = ExRem(Buy,Sell);
  Sell = ExRem(Sell,Buy);

  // Delay trades to the next bar.
  SetTradeDelays( 1, 1, 0, 0 );

  // Round a number of shares to 100. 
  SetOption("RoundLotSize",100);

  // Use Open as buy and sell prices 
  BuyPrice = SellPrice = Open; 

  // trade size: 25% of current portfolio equity
  SetPositionSize( 25, spsPercentOfEquity );

  // Prepare signals for Collective2. Signal are generated in "Exploration".

  // We show you several variants in this code:

  // 1. This variant shows just last trading signals (if any).
  // It also demonstrates automatic stop loss and profit target signals.
  C2TradingSignal(sigBTO, priceType = priceLimit, stopLoss = BuyPrice * 0.9, profitTarget = BuyPrice * 1.1); 


  // 2. This variant shows all generated BTO signals.
  // C2TradingSignal(sigBTO, sigfilter = Buy); 


/*
  // 3. This variant shows a general pattern how to show several last trading signals. 
  // Say we want to see last two signals.
  // Prepare cumulative sum of Buy signals
  cumulativeBuySignals = cum(buy);
  // Get bars where cumulative sum of Buy signals is greater than the latest value minus 2 
  signalFilter = cumulativeBuySignals > lastvalue(cumulativeBuySignals) - 2;
  // Use this filter 
  C2TradingSignal(sigBTO, sigfilter = signalFilter ); 
*/

/*
  // 4. This variant generates conditional signals.
  // Say we want to buy 100 stocks and add another 50 to position if the BuyPrice price falls 10% 
  C2TradingSignal(sigBTO, Amount = 100, Price = BuyPrice * 0.9, priceType = priceLimit, profitTarget = BuyPrice * 1.1); 
  // Conditional signals for BTO signals
  C2TradingSignalCond(sigBTO, sigBTO, Amount = 50, Price = BuyPrice * 0.8, priceType = priceLimit);
*/

  // Exploration columns
  // Show just Buy signals in Exploration
  Filter = Buy;
  AddColumn(Rsi_Value, "RSI");
  AddColumn(Open, "Open");


More examples to come.