Topics
Replies
dykesn92
02 Feb 2023, 11:22
RE:
PanagiotisChar said:
Hi there,
No you don't, you just need to check the current bar time and count only the bars that fall within the certain timeframe.
Thanks for your ideas.
Okay so reflecting on what you said I built a timer to reset to just before the candle I need so that I can keep a counter at all times
I haven't worked out how to make it work if I was to say turn it on after this for the day though since doing current time minus target candle time / 5 to give you candles is what keeps changing for me.
What do you suggest here?
@dykesn92
dykesn92
20 Dec 2022, 14:08
( Updated at: 21 Dec 2023, 09:23 )
Current code with comments on observations and bugs
Thanks so code is below, it is working in parts but not fully like it should. It only opened 4 trades when it's meant to open 50/60
Few comments,
I use supertrend.Uptrend.last(1) > 0 as it returns NA when the opposite direction. When I make logic on this though it locks the last(x) part of the function so I found setting this to a variable and starting the loop on the variable bypasses this issue.
I built this code out to contain a martingdale system in the close trade function however removed it as sells stopped opening.
On tick and on candle are the same thing.
I think the issue might be wiith my if functions, I have tried using if and else if as well as nested, functions etc but this code is the only time I have been able to get it to trade in both directions, despite other attempts being more accurate at entering and exiting trades, they all only opened buys. (Don't have these attempts sadly as I removed lots of code to clean it up for debugging and forgot to save.)
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
// This sample cBot shows how to use the Supertrend indicator
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SupertrendSample : Robot
{
private double _volumeInUnits;
private Supertrend _supertrend;
[Parameter("Volume (Lots)", DefaultValue = 0.01)]
public double VolumeInLots { get; set; }
[Parameter("Stop Loss (Pips)", DefaultValue = 10000)]
public double StopLossInPips { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 10000)]
public double TakeProfitInPips { get; set; }
[Parameter("Label", DefaultValue = "SurferBot")]
public string Label { get; set; }
[Parameter("INNER_UPPER", DefaultValue = 55,Group ="RSI",Step = 0.5)]
public double RSI_INNER_UPPER { get; set; }
[Parameter("INNER_LOWER", DefaultValue = 45,Group ="RSI",Step = 0.5)]
public double RSI_INNER_LOWER { get; set; }
[Parameter("OUTER_UPPER", DefaultValue = 70,Group ="RSI",Step = 0.5)]
public double RSI_OUTER_UPPER { get; set; }
[Parameter("OUTER_LOWER", DefaultValue = 30,Group ="RSI",Step = 0.5)]
public double RSI_OUTER_LOWER { get; set; }
[Parameter("Period", DefaultValue = 14,Group ="RSI")]
public int RSI_PERIOD { get; set; }
[Parameter("SL to BE", DefaultValue = 0,Group ="Trade Management")]
public int BE_PIPS { get; set; }
[Parameter("Trailing SL", DefaultValue = 0,Group ="Trade Management")]
public int Trail_SL { get; set; }
[Parameter("UPTREND", DefaultValue = 0)]
private int UP_TREND { get; set; }
[Parameter("DOWNTREND", DefaultValue = 0)]
private int DOWN_TREND { get; set; }
[Parameter("Max Trades", DefaultValue = 1)]
public int MAX_TRADES { get; set; }
[Parameter("TRADE_COUNT", DefaultValue = 0)]
private int TRADE_COUNT { get; set; }
public Position[] BotPositions
{
get
{
return Positions.FindAll(Label);
}
}
protected override void OnStart()
{
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
_supertrend = Indicators.Supertrend(10, 3);
}
protected override void OnBar()
{
if( _supertrend.UpTrend.Last(0)>0)
{
UP_TREND =1;
DOWN_TREND =0;
}
if (UP_TREND ==1) {
if (_supertrend.DownTrend.Last(1)>0)
{
Print("downtrend moved to uptrend",_supertrend.DownTrend.Last(1));
//ClosePositionBoth();
ClosePositions(TradeType.Sell);// close position
if (TRADE_COUNT < MAX_TRADES )
{ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
TRADE_COUNT ++ ;} //inc trade count
}
}
if( _supertrend.DownTrend.Last(0)>0)
{
UP_TREND =0;
DOWN_TREND =1;
}
if (DOWN_TREND ==1) {
if (_supertrend.UpTrend.Last(1)>0)
{
Print("uptrend moved to downtrend",_supertrend.DownTrend.Last(0));
ClosePositions(TradeType.Buy);// close position
if (TRADE_COUNT < MAX_TRADES )
{ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
TRADE_COUNT ++ ;} //inc trade count
}
}
}
/*private void ClosePositionBoth()
{
foreach (var position in BotPositions)
{
if (position.Label != "SurferBot") continue;
ClosePosition(position);
}
}*/
private void ClosePositions(TradeType tradeType)
{
foreach (var position in BotPositions)
{
if (position.TradeType != tradeType) continue;
if (position.Label != "SurferBot") continue;
ClosePosition(position);
TRADE_COUNT=0;
}
}
}
}
@dykesn92
dykesn92
02 Feb 2023, 11:45
RE:
PanagiotisChar said:
Thanks
@dykesn92