enterLong (int Lots, var Entry, var Stop, var TakeProfit, var Trail, var TrailSlope, var TrailLock, var TrailStep): TRADE*

enterShort (int Lots, var Entry, var Stop, var TakeProfit, var Trail, var TrailSlope, var TrailLock, var TrailStep): TRADE*

Enters a long or short trade position with static entry and exit parameters. Entry and exit conditions are handled by Zorro.

enterLong (function, var v0, ... var v7): TRADE*

enterShort (function, var v0, ... var v7): TRADE*

Enters a long or short trade position with dynamic entry and exit parameters. Entry and exit conditions are handled by user-provided algorithms in a trade management function (TMF) dependent on the optional variables v0 .. v7.  

Parameters:

Lots Optional number of lots when nonzero; overrides the global Lots variable. Margin and Risk are ignored. A negative number reverses the trade direction: enterLong then opens a short trade and enterShort opens a long trade.
Entry Optional entry stop when > 0, entry limit when < 0 (overrides the global Entry). A
Stop Optional stop loss when nonzero (overrides the global Stop).
TakeProfit Optional profit target when nonzero (overrides the global TakeProfit).
Trail Optional trail limit when nonzero (overrides the global Trail).
TrailSlope Optional trailing speed when nonzero (overrides the global TrailSlope).
TrailLock Optional profit lock percentage when nonzero (overrides the global TrailLock).
TrailStep Optional autotrailing step width when nonzero (overrides the global TrailStep).
function Optional pointer of an int function for micro-managing the trade (see TMF).
v0 ... v7 Up to 8 optional variables that are passed as further arguments to the TMF.

Returns:

TRADE* - a pointer to the created trade struct (see include\trading.h for the definition of the TRADE struct), or 0 when no trade could be entered because the trade volume was zero, trading was disabled (f.i. weekend, time frame, or lookback period), or the trade was rejected by the broker. For pending trades a nonzero pointer is always returned.

Remarks:

Example 1: Simple SMA crossing

function run()
{
  vars Price = series(priceClose());
  vars SMA100 = series(SMA(Price,100));
  vars SMA30 = series(SMA(Price,30));
 
  if(crossOver(SMA30,SMA100))
    enterLong();
  else if(crossUnder(SMA30,SMA100))
    enterShort();
}

Example 2: Grid trading script

// helper function for finding trades at a grid line
bool noTradeAt(var Price,var Grid,bool IsShort) 
{
  for(open_trades)
    if((TradeIsShort == IsShort)
      and between(TradeEntryLimit,Price-Grid/2,Price+Grid/2))
        return false; // trade found
  return true; // no open/pending trade at that price
}
  
function run() 
{
  BarPeriod = 1440;
  Hedge = 2; 
  EntryTime = ExitTime = 500;  
  var Price;
  var Grid = 100*PIP; // grid spacing
  var Current = priceClose();
// place pending trades at 5 grid lines above and below the current price
  for(Price = 0; Price < Current+5*Grid; Price += Grid) {
    if(Price < Current-5*Grid)
      continue;
    if(Price < Current and noTradeAt(Price,Grid,true))
      enterShort(0,Price,0,Grid);      
    else if(Price > Current and noTradeAt(Price,Grid,false))
      enterLong(0,Price,0,Grid);
  }
}

Example 3: Using a trade management function

// TMF that adjusts the stop in a special way 
int TrailingStop()
{
// adjust the stop only when the trade is in profit
if(TradeProfit > 0)
// place the stop at the lowest bottom of the previous 3 candles TradeStopLimit = max(TradeStopLimit,LL(3));
// plot a line to make the stop limit visible in the chart
plot("Stop",TradeStopLimit,MINV,BLACK); // return 0 for checking the limits
return 0;
} // using the trade function function run() { ... Lots = 1; Stop = 10*PIP; Algo = "SIG"; if(crossOver(MySignal,MyThreshold)) enterLong(TrailingStop); ... }

See also:

exitLong/Short, Lots, Risk, Entry, Stop, EntryTime, ExitTime, BarMode, TMF, Fill, Hedge

► latest version online