Help to Create a simple RSI Strategy
2 replies
LEONARDO RECSKI
8 months ago #287647
Greetings. I come from a Wealthlab background with building blocks, but I am trying to raise the bar and learn and use what StrategyQuant has to offer. I really have read and studied a lot before asking for help. Perhaps a simple tutorial to create a single strategy from scratch, for only one asset, could help newbies with not a lot of experience such as myself. That said, I think by biggest struggle is to build a strategy with only one entry condition e 3 possible exit conditions.
Timeframe: H1
Instruments: a set of US Stocks I have downloaded M1 data
Direction: Long only
Lot size: 1
Only one trade at a time per equity.
ENTRY CONDITION (buy): Price of candle close is below oversold level (range: 5 to 50) of RSI period (range 2 to 10)
EXIT CONDITION 1: Price of candle close is above a SMA period (range: 2 to 30)
EXIT CONDITION 2: Profit target % (range 0.2 to 8) – here no profit target often times generates better results. Can be excluded from the exits to facilitate.
EXIT CONDITION 3: Close position after a number of bars (range: 1 to 30)
Whatever occurs first on exit conditions 1 and 2 closes the trade. Exit condition 3 occurs when 1 and 2 are not met.
I do not want to generate multiple strategies with these rules. I am interested in optimizing each parameter for each stock.
Doubt: in case the last candle of the day closes above the customized SMA the exit will occur on the first candle closed on the following day? This can be good or bad depending on gaps and other vairables. The way to avoid this would be restricting the trading hour to end an hour before the end of the session?
I am including the buiding block I came up with, which is obviously wrong.
Any kind of help is more than welcome.
Regards,
Leonardo

Theo Gottwald
8 months ago #287720
Sure, I can help you get started with StrategyQuant to create a single strategy from scratch. Let’s break down your requirements and create a simple strategy step-by-step.
### Step-by-Step Guide to Create a Strategy in StrategyQuant
#### 1. Setting Up the Environment
– **Timeframe:** H1
– **Instruments:** US Stocks (M1 data)
– **Direction:** Long only
– **Lot size:** 1
– **Maximum open trades:** 1 per equity
#### 2. Entry Condition
– **Buy:** Price of candle close is below oversold level (range: 5 to 50) of RSI period (range 2 to 10)
#### 3. Exit Conditions
1. **Exit Condition 1:** Price of candle close is above a SMA period (range: 2 to 30)
2. **Exit Condition 2:** Profit target % (range 0.2 to 8)
3. **Exit Condition 3:** Close position after a number of bars (range: 1 to 30)
#### 4. Optimization
– Optimize each parameter for each stock.
### Building the Strategy in StrategyQuant
#### Step 1: Create a New Strategy
1. Open StrategyQuant.
2. Go to File
> New Strategy
.
3. Name your strategy and select the appropriate settings (e.g., timeframe, instrument).
#### Step 2: Define the Entry Condition
1. In the Strategy
tab, click on Add Block
> Indicator
.
2. Select RSI
and set the period range (2 to 10).
3. Click on Add Block
> Condition
.
4. Set the condition to Close < RSI(Period)
and set the oversold level range (5 to 50).
#### Step 3: Define the Exit Conditions
1. **Exit Condition 1:**
– Click on Add Block
> Indicator
.
– Select SMA
and set the period range (2 to 30).
– Click on Add Block
> Condition
.
– Set the condition to Close > SMA(Period)
.
2. **Exit Condition 2:**
– Click on Add Block
> Condition
.
– Set the condition to Profit > Target %
and set the range (0.2 to 8).
3. **Exit Condition 3:**
– Click on Add Block
> Condition
.
– Set the condition to Bars Since Entry > Number of Bars
and set the range (1 to 30).
#### Step 4: Combine Exit Conditions
1. Use the Or
block to combine Exit Condition 1 and Exit Condition 2.
2. Use the And
block to combine the result of the Or
block with Exit Condition 3.
#### Step 5: Optimize Parameters
1. Go to the Optimizer
tab.
2. Select the parameters you want to optimize (RSI period, oversold level, SMA period, profit target, number of bars).
3. Set the optimization range for each parameter.
4. Run the optimization.
### Addressing Your Doubt
Regarding the exit condition occurring on the first candle of the following day, you can indeed restrict the trading hours to end an hour before the session ends. This can be done by adding a time filter to your strategy.
#### Adding a Time Filter
1. Click on Add Block
> Condition
.
2. Set the condition to Time > Start Time
and Time < End Time
.
3. Adjust the start and end times to restrict trading to the desired hours.
### Example Building Block
Here’s a simplified example of how your building block might look:
`plaintext
Entry Condition:
– RSI(Period) < Oversold Level
Exit Conditions:
– Close > SMA(Period) OR Profit > Target %
– AND Bars Since Entry > Number of Bars
`
### Final Thoughts
– Make sure to test your strategy thoroughly with backtesting and forward testing.
– Consider adding risk management rules to protect your trades.
– Optimize parameters for each stock individually to find the best settings.
If you have any specific questions or run into issues, feel free to ask! Good luck with your strategy development in StrategyQuant.
[Used Mistral Large]
**Mit besten Grüßen | With best regards | Cordiali saluti**
**Theo Gottwald**
*Leading Expert in SPR & Visual Automation*
???? **What's New in the Q4 Update?**
1️⃣ Open AI Vision: Empower your SPR with cutting-edge visual recognition. See the unseen. ????????
2️⃣ Open AI TTS: Seamlessly transform text into natural, lifelike speech. Hear the difference. ????️????
3️⃣ GPT-4 with 128k Token Context Window by OpenAI: Dive into unparalleled depth in AI conversations. Think deeper. ????????
4️⃣ DALLE-3 by OpenAI: Command revolutionary AI to generate stunning images. Imagine more. ????️????
5️⃣ ElevenLabs Text to Speech: Experience text-to-speech so lifelike, it speaks to you. ????️
6️⃣ Stable Diffusion Local & Online via Automatic1111: Unlock creativity with both local and cloud-based image generation. Create anywhere. ☁️????
7️⃣ GPT4All & LM Studio: Harness powerful offline AI capabilities. Your studio, smarter. ????️????
8️⃣ DeepL Translator with SPR Integration: Achieve real-time, accurate translations. Speak the world's language. ????????
9️⃣ ChatGPT by OpenAI: Engage with your SPR through natural, dynamic conversations. Connect genuinely. ????️
???? WHISPER by OpenAI: Convert voice to text with effortless precision. Listen, transcribe, act. ????️????
???? Mistral AI: Scale your AI integration with seamless efficiency. Elevate your AI journey. ????????
???? Claude 3 by Anthropic: Experience next-level AI understanding and interactivity. Discover AI with a human touch. ????????
???? **Address:**
Herrenstr. 11, 76706 Dettenheim, Germany
???? **Phone:**
Office: +49 (7247) 9851112
Mobile: +49 160 6688 222
Fax: +49 (7247) 9851113
???? **Email:**
[[email protected]](mailto:[email protected])
???? **Websites:**
[IT-Berater](http://www.it-berater.org) | [Smart Package](http://www.smart-package.com)

Theo Gottwald
8 months ago #287721
Outsided of SQX it would look a bit like this ….
[code]
//+——————————————————————+
//| Leonardo.mq5 |
//| Copyright 2024, Theo Gottwald |
//| https://www.fa2.de |
//+——————————————————————+
#property copyright “Copyright 2024, Theo Gottwald”
#property link “https://www.fa2.de”
#property version “1.00”
//+——————————————————————+
//| MyStrategy.mq5 |
//| Generated by Leonardo |
//+——————————————————————+
#property strict
#include <Trade/Trade.mqh>
CTrade trade;
// Input parameters
input int RSI_Period = 5; // RSI Period
input int RSI_Oversold = 30; // RSI Oversold Level
input int SMA_Period = 10; // SMA Period
input double Profit_Target = 0.02; // Profit Target in %
input int Bars_To_Exit = 10; // Number of bars to exit
input double Lot_Size = 0.1; // Lot size for the trade
// Global variables
datetime entryTime;
//+——————————————————————+
//| Expert initialization function |
//+——————————————————————+
int OnInit()
{
// Initialization code
return(INIT_SUCCEEDED);
}
//+——————————————————————+
//| Expert deinitialization function |
//+——————————————————————+
void OnDeinit(const int reason)
{
// Deinitialization code
}
//+——————————————————————+
//| Expert tick function |
//+——————————————————————+
void OnTick()
{
// Check if there is an open position on the current symbol
if (PositionSelect(_Symbol) == false)
{
// Entry condition: RSI below oversold level
double rsi = iRSI(_Symbol, PERIOD_CURRENT, RSI_Period, PRICE_CLOSE);
if (rsi < RSI_Oversold)
{
// Open a long position
if (trade.Buy(Lot_Size, NULL, 0, 0, 0, “MyStrategy”))
{
entryTime = TimeCurrent();
Print(“Opened BUY position at price: “, SymbolInfoDouble(_Symbol, SYMBOL_ASK));
}
else
{
Print(“Error opening position: “, GetLastError());
}
}
}
else
{
// Exit conditions
// Retrieve position details
double entryPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double profit = (currentPrice – entryPrice) / entryPrice * 100;
// Calculate the number of bars since entry
int entryBar = iBarShift(_Symbol, PERIOD_CURRENT, entryTime, true);
int barsSinceEntry = Bars(_Symbol, PERIOD_CURRENT) – entryBar;
// Calculate SMA
double sma = iMA(_Symbol, PERIOD_CURRENT, SMA_Period, 0, MODE_SMA, PRICE_CLOSE);
// Get the latest close price
double closePrice = iClose(_Symbol, PERIOD_CURRENT, 0);
if (closePrice > sma || profit >= Profit_Target || barsSinceEntry >= Bars_To_Exit)
{
// Close the position
if (trade.PositionClose(_Symbol))
{
Print(“Closed position on symbol: “, _Symbol);
}
else
{
Print(“Error closing position: “, GetLastError());
}
}
}
}
//+——————————————————————+
[Untested]
**Mit besten Grüßen | With best regards | Cordiali saluti**
**Theo Gottwald**
*Leading Expert in SPR & Visual Automation*
???? **What's New in the Q4 Update?**
1️⃣ Open AI Vision: Empower your SPR with cutting-edge visual recognition. See the unseen. ????????
2️⃣ Open AI TTS: Seamlessly transform text into natural, lifelike speech. Hear the difference. ????️????
3️⃣ GPT-4 with 128k Token Context Window by OpenAI: Dive into unparalleled depth in AI conversations. Think deeper. ????????
4️⃣ DALLE-3 by OpenAI: Command revolutionary AI to generate stunning images. Imagine more. ????️????
5️⃣ ElevenLabs Text to Speech: Experience text-to-speech so lifelike, it speaks to you. ????️
6️⃣ Stable Diffusion Local & Online via Automatic1111: Unlock creativity with both local and cloud-based image generation. Create anywhere. ☁️????
7️⃣ GPT4All & LM Studio: Harness powerful offline AI capabilities. Your studio, smarter. ????️????
8️⃣ DeepL Translator with SPR Integration: Achieve real-time, accurate translations. Speak the world's language. ????????
9️⃣ ChatGPT by OpenAI: Engage with your SPR through natural, dynamic conversations. Connect genuinely. ????️
???? WHISPER by OpenAI: Convert voice to text with effortless precision. Listen, transcribe, act. ????️????
???? Mistral AI: Scale your AI integration with seamless efficiency. Elevate your AI journey. ????????
???? Claude 3 by Anthropic: Experience next-level AI understanding and interactivity. Discover AI with a human touch. ????????
???? **Address:**
Herrenstr. 11, 76706 Dettenheim, Germany
???? **Phone:**
Office: +49 (7247) 9851112
Mobile: +49 160 6688 222
Fax: +49 (7247) 9851113
???? **Email:**
[[email protected]](mailto:[email protected])
???? **Websites:**
[IT-Berater](http://www.it-berater.org) | [Smart Package](http://www.smart-package.com)
Viewing 2 replies - 1 through 2 (of 2 total)