Rejoignez-nous
Instructions/probuilder · probacktest · proorder · proscreener

THEN

THEN links an IF condition to the block of ProBuilder code that runs when the condition is true. Mandatory in every IF statement, before ELSE or ELSIF.

Syntaxe

probuilder
IF condition THEN
  // code executed when the condition is true
ENDIF

Comment ça marche

THEN is the bridge between a condition and its consequences. The compiler reads everything between IF and THEN as the boolean expression to evaluate, and everything between THEN and the next branch keyword (ELSIF, ELSE, or ENDIF) as the block to execute when that expression is true.

Le mot-clé n'est pas optionnel. Omettre THEN après une condition IF est une erreur de syntaxe, et il en va de même après chaque condition ELSIF dans une instruction chaînée. ELSE, en revanche, ne prend pas de condition et donc pas de THEN.

THEN carries no logic of its own, it is a structural marker. Complex conditions built with AND, OR, NOT, and parentheses all sit on the left side of THEN, and the block on the right side can contain any statements valid in the sub-language, including nested IF...THEN blocks. Every IF opened with THEN must eventually be closed with ENDIF.

Le mot-clé apparaît aussi dans la lecture compacte en un seul concept des conditionnelles ProBuilder : condition, THEN, action, ENDIF. Garder cet ordre à l'esprit résout la plupart des erreurs de syntaxe autour des blocs conditionnels.

THEN behaves identically across all four sub-languages. Indicator code, strategy code, and screener code all use the same IF...THEN...ENDIF structure, so conditional logic written for one context ports directly to the others as long as the statements inside the block are valid there.

Exemples

Exemple 1, Marquer les bars haussiers (Indicateur)

probuilder
// Draw a green up arrow under every bar that closed above its open
IF close > open THEN
  DRAWARROWUP(barindex, low) COLOURED(0, 255, 0)
ENDIF
RETURN

Lorsque la condition à gauche de THEN est vraie, l'instruction de dessin du bloc s'exécute et une flèche apparaît sous le bar.

Exemple 2, Entrée conditionnelle avec une condition composée (ProOrder)

probuilder
// Enter long when the fast average crosses over the slow one
// and the market is not already long
fastMA = average[20](close)
slowMA = average[100](close)

IF fastMA crosses over slowMA AND NOT longonmarket THEN
  BUY 1 CONTRACT AT MARKET
ENDIF

L'expression composée entière se place avant THEN. L'instruction d'ordre après THEN ne s'exécute que sur les bars où les deux parties sont vérifiées.

Exemple 3, Branchement avec ELSIF et THEN (ProScreener)

probuilder
// Classify momentum, THEN is required after IF and after ELSIF
mom = close - close[10]

IF mom > 0 THEN
  state = 1
ELSIF mom < 0 THEN
  state = -1
ELSE
  state = 0
ENDIF

SCREENER[state = 1] (mom AS "10-bar momentum")

Chaque branche conditionnelle portant une condition se termine par THEN. La branche ELSE n'a pas de condition et donc pas de THEN.

Erreurs et pièges courants

  • THEN manquant. IF close > open DRAWARROWUP(barindex, low) ENDIF ne compile pas. Chaque condition IF et chaque condition ELSIF doit être suivie de THEN avant le début du bloc.
  • THEN after ELSE. ELSE introduces the fallback block directly and takes neither a condition nor a THEN. Writing ELSE THEN est une erreur de syntaxe.
  • Bloc non fermé. Un bloc THEN s'étend jusqu'à ELSIF, ELSE ou ENDIF. Oublier ENDIF amène le compilateur à absorber les instructions suivantes dans le bloc et à signaler une erreur loin de sa cause.
  • Instructions du côté de la condition. Tout ce qui se trouve entre IF et THEN doit se réduire à vrai ou faux. Les affectations ou les simples expressions arithmétiques à cette position ne compilent pas.
  • IF, ouvre l'instruction conditionnelle à laquelle THEN appartient.
  • ELSE, branche de repli exécutée lorsqu'aucune condition n'a été satisfaite.
  • ELSIF, condition supplémentaire dans le même bloc, également suivie de THEN.
  • ENDIF, ferme l'instruction conditionnelle.
  • AND, combine des conditions à gauche de THEN.
  • OR, conditions alternatives à gauche de THEN.
  • NOT, nie une condition avant THEN.