Topics
Replies
luciancastro89
01 Sep 2020, 15:16
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi Lucian,
What do you mean when you say it is getting "double orders"?
Best Regards,
Panagiotis
Hi Panagiotis,
Instead open only the first trade with double volume after a loss, it is opening the first and the second trade... check image please
@luciancastro89
luciancastro89
01 Sep 2020, 12:07
Hi Panagiotis, just spent more time with my code, I think Im almost there, the issue now is that MG is getting double orders.. here is the code
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 rsimg : Robot
{
[Parameter("Instance Name", DefaultValue = 4, MinValue = 1, Step = 1)]
public string InstanceName { get; set; }
[Parameter("Stop Loss", DefaultValue = 50)]
public double SL { get; set; }
[Parameter("Take Profit", DefaultValue = 150)]
public double TP { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double ParamVol { get; set; }
[Parameter("Long Trades", Group = "Positions", DefaultValue = 1, MinValue = 0)]
public int MaxLongTrades { get; set; }
[Parameter("Short Trades", Group = "Positions", DefaultValue = 1, MinValue = 0)]
public int MaxShortTrades { get; set; }
[Parameter("Source", Group = "RSI")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "RSI", DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("RSI Overbought Level", Group = "RSI", DefaultValue = 80, MinValue = 1, Step = 1)]
public int RSIOverB { get; set; }
[Parameter("RSI Oversold Level", Group = "RSI", DefaultValue = 40, MinValue = 1, Step = 1)]
public int RSIOverS { get; set; }
private RelativeStrengthIndex rsi;
private int LongPositions = 0, ShortPositions = 0, MaxLong = 0, MaxShort = 0;
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
MaxLong = MaxLongTrades;
MaxShort = MaxShortTrades;
}
protected override void OnBar()
{
var LongPositions = Positions.FindAll(InstanceName, Symbol, TradeType.Buy);
var ShortPositions = Positions.FindAll(InstanceName, Symbol, TradeType.Sell);
double v = Symbol.QuantityToVolumeInUnits(ParamVol);
HistoricalTrade lastClosed = History.FindLast(InstanceName, Symbol);
if (LongPositions.Length < MaxLong && rsi.Result.LastValue < RSIOverS)
{
}
ExecuteMarketOrder(TradeType.Buy, SymbolName, v, InstanceName, SL, TP);
Close(TradeType.Sell);
}
if (ShortPositions.Length < MaxShort && rsi.Result.LastValue > RSIOverB)
{
}
ExecuteMarketOrder(TradeType.Sell, SymbolName, v, InstanceName, SL, TP);
Close(TradeType.Buy);
}
}
private void Close(TradeType tradeType)
{
foreach (var position in Positions.FindAll(InstanceName, SymbolName, tradeType))
ClosePosition(position);
}
}
}
@luciancastro89
luciancastro89
13 Jul 2020, 00:51
Many thanks for your help, I'm going to backtest it right now!!
@luciancastro89
luciancastro89
08 Jul 2020, 18:08
Ok thanks for your reply!
Im trying to modify this bot, istead of buy/sell when period K cross D on the stochastic, the bot should buy when are oversold and sell when overbought on stochastic.
Also if it is possible to add a moving average to help, only buying when price closes above MA and sell when closes below.
This is the code:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class StochasticBot : Robot
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Take Profit", Group = "Risk", DefaultValue = 150, MinValue = 4, Step = 1, MaxValue = 1000)]
public int takeProfit { get; set; }
[Parameter("Stop Loss", Group = "Risk", DefaultValue = 150, MinValue = 4, Step = 1, MaxValue = 1000)]
public int stopLoss { get; set; }
[Parameter("Percent K", Group = "Stochastic", DefaultValue = 150, MinValue = 4, Step = 1, MaxValue = 1000)]
public int percentK { get; set; }
[Parameter("Percent K Slow", Group = "Stochastic", DefaultValue = 150, MinValue = 4, Step = 1, MaxValue = 1000)]
public int percentKSlow { get; set; }
[Parameter("Percent D", Group = "Stochastic", DefaultValue = 150, MinValue = 4, Step = 1, MaxValue = 1000)]
public int percentD { get; set; }
private StochasticOscillator stochasticOscillator;
private double volumeInUnits;
protected override void OnStart()
{
volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
stochasticOscillator = Indicators.StochasticOscillator(percentK, percentKSlow, percentD, MovingAverageType.Exponential);
}
protected override void OnBar()
{
if (stochasticOscillator.PercentK.Last(2) < stochasticOscillator.PercentD.Last(2))
{
if (stochasticOscillator.PercentK.LastValue > stochasticOscillator.PercentD.LastValue)
{
Print(stochasticOscillator.PercentK.LastValue);
ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "Stochastic", stopLoss, takeProfit);
}
}
if (stochasticOscillator.PercentK.Last(2) > stochasticOscillator.PercentD.Last(2))
{
if (stochasticOscillator.PercentK.LastValue < stochasticOscillator.PercentD.LastValue)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, "Stochastic", stopLoss, takeProfit);
}
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
@luciancastro89
luciancastro89
01 Sep 2020, 16:06
RE:
Thanks
@luciancastro89