c bot stops running
c bot stops running
16 Feb 2021, 01:18
Hi everyone,
I have a question,
I just have added a command to my bot to close a position when the RSI indicator is below 30.
the command works, only every time the RSI reaches 30 the bot is stopped.
I would like to hear from someone how I can make sure that the bot does not stop running.
thank you in advance.
Robin
bot code:
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Periods", DefaultValue = 14)]
public int Periods { get; set; }
private RelativeStrengthIndex rsi;
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
}
protected override void OnBar()
{
if (rsi.Result.LastValue > 70)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000, "Short", 100, 100);
}
if (rsi.Result.LastValue < 30)
{
ClosePosition(Positions.Find("Short", SymbolName, TradeType.Sell));
}
}
}
}
Replies
r.stipriaan
16 Feb 2021, 13:15
HI Panagiots,
Thank you so much for answering my question.
Robin
@r.stipriaan
r.stipriaan
16 Feb 2021, 23:02
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi r.stipriaan,
Check your log and you will see the exception below
The problem is that you are trying to close a position even if there is no position open.
Best Regards,
Panagiotis
Hi Panagiotis,
Thanks for the quick reply!
The solution was more difficult than it seemed, after a few hours of trying still no results. Can you tell what is going wrong from the code?
The last 2 lines have been added but the bot does not return.
Thanks in advance,
Robin
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Periods", DefaultValue = 14)]
public int Periods { get; set; }
private RelativeStrengthIndex rsi;
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
}
protected override void OnBar()
{
if (rsi.Result.LastValue > 70)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000, "Short", 100, 100);
}
if (rsi.Result.LastValue < 30)
{
ClosePosition(Positions.Find("Short", SymbolName, TradeType.Sell));
}
if ((Positions.Find("Short", SymbolName, TradeType.Sell)) == null)
return;
}
}
}
@r.stipriaan
PanagiotisCharalampous
17 Feb 2021, 08:17
Hi r.stipriaan,
Here you go
if ((Positions.Find("Short", SymbolName, TradeType.Sell)) != null && rsi.Result.LastValue < 30)
{
ClosePosition(Positions.Find("Short", SymbolName, TradeType.Sell));
}
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
16 Feb 2021, 08:26
Hi r.stipriaan,
Check your log and you will see the exception below
The problem is that you are trying to close a position even if there is no position open.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous