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 with optional 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 script-managed trade with optional user-defined parameters. Entry and exit conditions are handled by a user-provided algorithm in the given trade management function (TMF) dependent on the variables v0 .. v7.

enterTrade (TRADE*): TRADE*

Enters an open, pending, or closed trade from a TRADE struct. Inserts the trade only in the internal trade list; does not send any order to the broker. The asset name can be given through TradeStr[0] resp. the Skill element of the TRADE struct; if it is not set, the current asset is used. The position size (nLots) and the TR_SHORT and TR_OPEN flags must be set in the TRADE struct; other elements are optional. The algo identifier of the trade is set to the current algo. This function can be used for running backtests from a list of trades (see Simulate script) or for reading positions from the broker API and converting them to trades. 


Lots Optional number of lots when nonzero; overrides the global Lots, Amount, Margin, and Risk variables. 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).
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 numerical variables as further arguments to the TMF.


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.


Example 1: Simple SMA crossing

function run()
  vars Price = series(priceClose());
  vars SMA100 = series(SMA(Price,100));
  vars SMA30 = series(SMA(Price,30));
  else if(crossUnder(SMA30,SMA100))

Example 2: Grid trading script

// helper function for finding trades at a grid line
bool noTradeAt(var Price,var Grid,bool IsShort) 
    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)
    if(Price < Current and noTradeAt(Price,Grid,true))
    else if(Price > Current and noTradeAt(Price,Grid,false))

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, tradeUpdate, Lots, Risk, Entry, Stop, EntryTime, ExitTime, BarMode, TMF, Fill, Hedge

► latest version online