Stochastic Oscillator Crossover
Stochastic Oscillator Crossover
04 Mar 2020, 10:10
Dear All,
I would like to create a Cbot based on Stochastic Oscillator which will create buy Order when the %K line cross the %D from below and close the order when % K line cross %D from above and create the new Sell order. I tried to create the code based on the sample codes available in this forum but it seems bot execution was not correct.
I am using the bot in 1 Hour timeframe, but when I backtest the positions are opened and closed at every 4 hours. How to modify the code to work on all timeframes.
using System.Linq;
using cAlgo.API;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
public class STO : Robot
{
[Parameter("Volume", DefaultValue = 1000)]
public int volume { get; set; }
[Parameter(DefaultValue = 500, MinValue = 1)]
public int StopLoss { get; set; }
[Parameter(DefaultValue = 500, MinValue = 1)]
public int TakeProfit { get; set; }
[Parameter(DefaultValue = 6)]
public MovingAverageType MaType { get; set; }
[Parameter("K Periods", DefaultValue = 9)]
public int KPeriods { get; set; }
[Parameter("D Periods", DefaultValue = 9)]
public int DPeriods { get; set; }
[Parameter("K Slowing", DefaultValue = 3)]
public int K_Slowing { get; set; }
private StochasticOscillator _SOC;
protected override void OnStart()
{
var dailySeries = MarketData.GetSeries(TimeFrame.Hour);
_SOC = Indicators.StochasticOscillator(KPeriods, K_Slowing, DPeriods, MaType);
}
protected override void OnTick()
{
if (Positions.Count(x => x.TradeType == TradeType.Buy) == 0 && _SOC.PercentK.Last(1) > _SOC.PercentD.Last(1) && _SOC.PercentK.Last(0) > _SOC.PercentD.Last(0))
{
foreach (var position in Positions.Where(x => x.TradeType == TradeType.Sell))
position.Close();
ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "STOBOT", StopLoss, TakeProfit, 5);
}
if (Positions.Count(x => x.TradeType == TradeType.Sell) == 0 && _SOC.PercentK.Last(1) < _SOC.PercentD.Last(1) && _SOC.PercentK.Last(0) < _SOC.PercentD.Last(0))
{
foreach (var position in Positions.Where(x => x.TradeType == TradeType.Buy))
position.Close();
ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "STOBOT", StopLoss, TakeProfit, 5);
}
}
}
}
So could you please help us to fix this bot.
Thanks in Advance
R. Vadivelan
Replies
velu130486
04 Mar 2020, 12:05
( Updated at: 21 Dec 2023, 09:21 )
RE:
Hi Panagiotis,
Thanks for your quick reply.
Please help me to modify the Cbot to work in multiple timeframes (i.e.) Cbot to run based on Chart Timeframe. I hope remaining part of my coding is fine, but if I want to reference to the indicator (say hull moving average) which is not inbuilt in Ctrader how to include the same. I have the indicator with source code in the indicator folder.
PanagiotisCharalampous said:
Hi Vadivelan,
I backtested your cBot but I cannot see such a behavior. See below a backtesting on m1 chart. Seems fine to me, I do not see positions opening and closing every 4 hours.
Best Regards,
Panagiotis
Thanks in Advance
R. Vadivelan
PanagiotisCharalampous
04 Mar 2020, 12:09
Hi Vadivelan,
I cannot undertake custom development requests. If you need somebody to develop this cBot for you, please post a Job or contact a Consultant. If you still have specific questions about the API or cTrader, please let me know.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
04 Mar 2020, 10:40
Hi Vadivelan,
I backtested your cBot but I cannot see such a behavior. See below a backtesting on m1 chart. Seems fine to me, I do not see positions opening and closing every 4 hours.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous