Search code examples
pine-scriptpine-script-v5trading

Pinescript Trading Strategy not following the long conditions


I am trying to implement an ichimoku cloud strategy in conjunction with MACD.

What I am trying to do is to follow long conditions only when the ichimoku cloud turns green, i.e., when leading span A is above leading span B.

But the long entries are made irrespective of the above condition. Check screenshot.

enter image description here


Solution

  • Ichimoku is a lagging indicator. Those plots are displayed with an offset offset = displacement - 1. Therefore you should use the history reference operator [] in your conditions.

    The below code will tell oyu if the cloud is green or not.

    is_green_cloud = (leadLine1 > leadLine2)[displacement - 1]