> For the complete documentation index, see [llms.txt](https://bondingblock.gitbook.io/quant/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bondingblock.gitbook.io/quant/signal-generator.md).

# Signal Generator

#### Summary

This script segment outlines a basic trading strategy that generates buy and sell signals based on the relative positions of the opening and closing prices over a specified period. The strategy enters a long position when the closing price crosses above the opening price and enters a short position when the closing price crosses below the opening price. This approach can be effective in markets where the opening and closing prices exhibit clear trends.

#### Period Input

```javascript
period = input('720')
```

* `period` is an input variable that specifies the time frame for the security's data to be fetched. The default value is set to '720', which typically represents a 4-hour timeframe in many trading platforms.

#### Fetching Security Data

```javascript
ch1 = request.security(syminfo.tickerid, period, open)
ch2 = request.security(syminfo.tickerid, period, close)
```

* `ch1` fetches the opening prices of the security identified by `syminfo.tickerid` over the specified `period`.
* `ch2` fetches the closing prices of the same security over the same `period`.

#### Long Entry Condition

```javascript
longCondition = ta.crossover(ch2, ch1)
if longCondition
    strategy.entry('BUY', strategy.long)
```

* `longCondition` checks if the closing price (`ch2`) crosses over the opening price (`ch1`), indicating a potential buy signal.
* If `longCondition` is true, a long entry order is placed with the label 'BUY'.

#### Short Entry Condition

```javascript
shortCondition = ta.crossunder(ch2, ch1)
if shortCondition
    strategy.entry('SELL', strategy.short)
```

* `shortCondition` checks if the closing price (`ch2`) crosses under the opening price (`ch1`), indicating a potential sell signal.
* If `shortCondition` is true, a short entry order is placed with the label 'SELL'.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://bondingblock.gitbook.io/quant/signal-generator.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
