RSI + Bar Close Over/Under MA - Help to add Entry and Exit criteria
RSI + Bar Close Over/Under MA - Help to add Entry and Exit criteria
17 Apr 2021, 05:01
Hi,
I am trying to convert this to change the entry and exit criteria slightly.
I am definitely not an expert at coding so I'm not sure if this is even possible but what I am trying to do is...
Long Position
1. If the RSI enters into Oversold, it will then wait for a bar close Above the Moving Average before Executing a long position.
2. Long position will execute on the Bar Close whether or not the RSI is still Oversold.
Short Position
1. Is the RSI enters into Overbought, it will then wait for a bar close Below the Moving Average before Executing a short position.
2. Short position will execute on the Bar Close whether or not the RSI is still Overbought.
At this point this is what I have and it only executes a entry and exit if both are occurring at the same time.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class OversoldOverboughtMACross : Robot
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Source", Group = "RSI")]
public DataSeries Source { get; set; }
[Parameter("Label", DefaultValue = "MAType")]
public string Label { get; set; }
[Parameter("MA Type")]
public MovingAverageType MAType { get; set; }
[Parameter("Enter And Exit Period", DefaultValue = 55)]
public int EnterAndExitPeriod { get; set; }
[Parameter("Periods", Group = "RSI", DefaultValue = 55)]
public int Periods { get; set; }
[Parameter("Lower RSI Level", Group = "RSI", DefaultValue = 35, MinValue = 0, MaxValue = 50)]
public int LRL { get; set; }
[Parameter("Upper RSI Level", Group = "RSI", DefaultValue = 65, MinValue = 50, MaxValue = 100)]
public int URL { get; set; }
private RelativeStrengthIndex rsi;
private MovingAverage EnterAndExitMA;
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
EnterAndExitMA = Indicators.MovingAverage(Source, EnterAndExitPeriod, MAType);
}
protected override void OnBar()
{
int index = Bars.Count - 2;
Exit(index);
{
if (rsi.Result.LastValue < LRL && Bars.ClosePrices[index] > EnterAndExitMA.Result[index])
{
Open(TradeType.Buy);
}
else if (rsi.Result.LastValue > URL && Bars.ClosePrices[index] < EnterAndExitMA.Result[index])
{
Open(TradeType.Sell);
}
}
}
private void Open(TradeType tradeType)
{
var position = Positions.Find(Label, SymbolName, tradeType);
var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
if (position == null)
ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, Label, null, null);
}
private void Exit(int index)
{
var positions = Positions.FindAll(Label);
foreach (var position in positions)
if ((position.TradeType == TradeType.Buy && rsi.Result.LastValue > URL && Bars.ClosePrices[index] < EnterAndExitMA.Result[index]) || (position.TradeType == TradeType.Sell && rsi.Result.LastValue < LRL && Bars.ClosePrices[index] > EnterAndExitMA.Result[index]))
ClosePosition(position);
}
}
}
Replies
thecaffeinatedtrader
17 Apr 2021, 17:20
RE:
amusleh said:
Hi,
This sample might help you:
using cAlgo.API; using cAlgo.API.Indicators; namespace cAlgo.Robots { /// <summary> /// This sample cBot shows how to use the Relative Strength Index indicator /// </summary> [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class RelativeStrengthIndexSample : Robot { private double _volumeInUnits; private RelativeStrengthIndex _relativeStrengthIndex; [Parameter("Volume (Lots)", DefaultValue = 0.01)] public double VolumeInLots { get; set; } [Parameter("Stop Loss (Pips)", DefaultValue = 10)] public double StopLossInPips { get; set; } [Parameter("Take Profit (Pips)", DefaultValue = 10)] public double TakeProfitInPips { get; set; } [Parameter("Label", DefaultValue = "Sample")] public string Label { get; set; } public Position[] BotPositions { get { return Positions.FindAll(Label); } } protected override void OnStart() { _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots); _relativeStrengthIndex = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 20); } protected override void OnBar() { if (_relativeStrengthIndex.Result.Last(1) > 70 && _relativeStrengthIndex.Result.Last(2) < 70) { ClosePositions(TradeType.Buy); ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips); } else if (_relativeStrengthIndex.Result.Last(1) < 20 && _relativeStrengthIndex.Result.Last(2) > 20) { ClosePositions(TradeType.Sell); ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips); } } private void ClosePositions(TradeType tradeType) { foreach (var position in BotPositions) { if (position.TradeType != tradeType) continue; ClosePosition(position); } } } }
Please start learning C# basics and read cTrader automate API references, if you don't have time then post a job request or ask one of our consultants to develop your cBot/indicator for you.
Hi,
Thanks for replying, but this is the sample that helped me get to the point I am at on this code. I am currently still learning but some steps like what I am trying to accomplish are still a little advanced for me. The sample definitely does not render the result I am looking for as I need to figure out how to create a wait function within the code somehow. I'm sure I'll figure it out eventually with some trial and error but figured I'd see if someone knew how.
@thecaffeinatedtrader
amusleh
17 Apr 2021, 10:12
Hi,
This sample might help you:
Please start learning C# basics and read cTrader automate API references, if you don't have time then post a job request or ask one of our consultants to develop your cBot/indicator for you.
@amusleh