Topics
Replies
firemyst
13 Aug 2020, 09:20
RE: my code worked perfectly until ...
samuel.jus.cornelio said:
My code was working perfectly until I added a trailing stop and now nothing else works. Could someone tell me how I can solve this problem????
Thankss
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 bot : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } [Parameter("Source")] public DataSeries Source { get; set; } [Parameter("Periods", DefaultValue = 14)] public int Periods { get; set; } [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)] public int StopLoss { get; set; } [Parameter("Volume", DefaultValue = 10000, MinValue = 1000)] public int Volume { get; set; } [Parameter("Take_Profit", DefaultValue = 45, MinValue = 10)] public int TakeProfit { get; set; } [Parameter("Begin Trading Hour", DefaultValue = 5.0)] public double Begin { get; set; } [Parameter("Ending Trading Hour", DefaultValue = 19.0)] public double Ending { get; set; } [Parameter("Trigger When Gaining", DefaultValue = 50)] public double TriggerWhenGaining { get; set; } [Parameter("Trailing Stop Loss Distance", DefaultValue = 1)] public double TrailingStopLossDistance { get; set; } [Parameter("Step (pips)", DefaultValue = 10)] public double Step { get; set; } [Parameter("Buy")] public bool Buy { get; set; } private double _highestGain; private bool _isTrailing; private DateTime startTime; private DateTime endTime; public RelativeStrengthIndex _rsi; private ExponentialMovingAverage _Ema1; protected override void OnStart() { } protected override void OnTick() { { startTime = Server.Time.Date.AddHours(Begin); endTime = Server.Time.Date.AddHours(Ending); if (Trade.IsExecuting) return; bool tradeTime = false; if (Begin < Ending) tradeTime = Server.Time.Hour >= Begin && Server.Time.Hour < Ending; if (Ending < Begin) tradeTime = Server.Time.Hour >= Begin || Server.Time.Hour <= Ending; if (!tradeTime) return; } _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 48); _Ema1 = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 6); { if (_rsi.Result.LastValue < 45) if (_Ema1.Result.LastValue == Symbol.Ask) ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "bot", StopLoss, TakeProfit); } { if (_rsi.Result.LastValue > 55) if (_Ema1.Result.LastValue == Symbol.Bid) ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "bot", StopLoss, TakeProfit); var position = Positions.Find("SampleTrailing"); if (position == null) { Stop(); return; } //If the trigger is reached, the robot starts trailing if (position.Pips >= TriggerWhenGaining) { //Based on the position's direction, we calculate the new stop loss price and we modify the position if (position.TradeType == TradeType.Buy) { var newSLprice = Symbol.Ask - (Symbol.PipSize * TrailingStopLossDistance); if (newSLprice > position.StopLoss) { ModifyPosition(position, newSLprice, null); } } else { var newSLprice = Symbol.Bid + (Symbol.PipSize * TrailingStopLossDistance); if (newSLprice < position.StopLoss) { ModifyPosition(position, newSLprice, null); } } TriggerWhenGaining += Step; } } } protected override void OnStop() { // Put your deinitialization logic here } } }
Whether or not this is the issue, your code will fail and not move the SL if the Symbol.Spread is greater than the Trailing stop distance specified.
For example, with your parameters I see you have a default value of "1" for trailing stop loss distance.
If the spread is 1.1 pips of more, how can you trail a stop loss by 1 pip from the Ask price? You'd be inside the spread, and thus be knocked out of your position (if it even lets you set that).
So in your code you also need to check that
Symbol.Ask - (Symbol.PipSize * TrailingStopLossDistance)
is also > Symbol.Spread distance between the Ask and Bid prices.
Basically, put a check in there that it's also lower than the bid price.
Example:
if (newSLprice > position.StopLoss && newSLprice < Symbol.Bid)
Similarly:
if (newSLprice < position.StopLoss && newSLprice > Symbol.Ask)
@firemyst
firemyst
13 Aug 2020, 07:11
RE: Stop Loss at previous days low or previous days high
samuelbreezey said:
Hi Guys
I am trying to get my stop loss on a execute at market to be @ the previous days low. My code does'nt seem to work :( may be something simple...
Help needed please.
Try posting some code so people can see what you're doing wrong..
@firemyst
firemyst
13 Aug 2020, 07:10
RE:
samuel.jus.cornelio said:
Hello everyone, I'm on my next project now and I need a little help. I'm wondering how exactly I would encode an action that recognizes whether the bar has touched an indicator or not. I'm trying to code an EMA and I need to know if the current price is touching the EMA Thanks for an answer,
This may involve rounding depending on what you consider "touching".
For example, using the PSAR. It's current value could be 1.4587357697 but the current "bid" or "ask" price could be 1.45873.
Is that touching or not?
You also have to determine if you consider if it should be the "bid" or "ask" (or perhaps both) values that touch the price.
@firemyst
firemyst
13 Aug 2020, 07:05
( Updated at: 21 Dec 2023, 09:22 )
RE:
Nabhanyusaf said:
It would be great if the indicator could be visualized on a separate timeframe like one indicator on one customized timeframe instead of being on every timeframe. like in MT4
They can be in a sense, but you have to code it yourself.
For example, on this M2 chart, I have 2 sets of 55 EMAs (the big colorful lines), 2 SuperTrends, and 2 sets of PSARs
The blue PSARs, dashed ST line, and dark blue 55 line is on the M5 timeframe; the white PSARs, whiteish road, and solid ST line is the M2 timeframe.
@firemyst
firemyst
03 Aug 2020, 06:35
RE:
samuel.jus.cornelio said:
Please, how can I use an indicator array for a bot. For example, an array with several RSI contained. I am trying and not succeeding, if anyone can help I will be grateful
Here's a very simple example using MovingAverages instead:
//class declaration
MovingAverage[] _maArray;
//In OnStart (cBot) or Initialize (indicator)
if (_maArray != null)
{
//Clear out old data everytime we start so GC can collect
Array.Clear(_maArray, 0, _maArray.Length);
_maArray = null;
}
_maArray = new MovingAverage[theNumberOfMAObjectsYouWantToHave];
for (int x=0; x < _maArray.Length; x++)
{
_maArray[x] = Indicators.MovingAverage(Bars.ClosePrices, thePeriodToUse, theMATypeToUse);
}
@firemyst
firemyst
03 Aug 2020, 06:21
RE:
samuel.jus.cornelio said:
Can someone tell me how to put two RSI in the same code. The idea is to place two RSI with different configurations,
Declare two RSI objects and then create them with the specific parameters.
Example concept below using MovingAverages:
MovingAverage _ma1;
MovingAverage _ma2;
_ma1 = Indicators.MovingAverage(Bars.ClosePrices, MA1Period, MA1Type);
_ma2 = Indicators.MovingAverage(Bars.ClosePrices, MA2Period, MA2Type);
@firemyst
firemyst
21 Jul 2020, 15:16
RE:
yuval.ein said:
I've installed ctrader 3.7 freom 2 different brokers and visual studio 2019 on a new pc.
When I try to edit a cBot on visual studio i get the following error:
Can not download visual studio extention
I checked the extetions on the VS and cTrader extention is not installed
Pleas help
You have to ask Spotware or @Panagiotis if cTrader is compatible with VS 2019.
@firemyst
firemyst
02 Jul 2020, 03:22
RE: RE:
yuval.ein said:
Thank you
Is there a way to get the full indicator code?
You have to ask Spotware for that.
But in all seriousness, just Google it.
You take the latest value of the sma. That's the middle line.
The top and bottom lines are the standard deviations.
So you can easily code it yourself :-)
@firemyst
firemyst
12 Jun 2020, 13:07
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi firemyst,
Seems to be a common issue on Windows Server. Check here in case it helps.
Best Regards,
Panagiotis
@Panagiotis, I found the root cause and managed to fix it.
For future reference, in my case, this was disabled for some reason!
Once I enabled this, the sounds started playing again as expected.
Thank you.
@firemyst
firemyst
11 Jun 2020, 05:44
RE: RE:
giuseppealessiof said:
PanagiotisCharalampous said:
Hi giuseppealessiof,
You need to provide more details like the cBot code and baclktesting parameters and dates so that we can reproduce this behavior and advise accordingly.
Best Regards,
Panagiotis
ok thank you very much for the answer but what I would like to know most of all is if I can have the opportunity to meet a code that writes me on the screen why the bot stops
thanks a lot
@Panagiotis can confirm, but I don't believe there are any built in reasons or codes as to why a bot was stopped.
there is when a position is closed though.
When you assign a method to run when a positions closed event has happened, you can read the "args" parameter to get a reason.
Example:
private void Positions_Closed(PositionClosedEventArgs args)
{
Print("Position closed for reason {1}", args.Reason);
}
Otherwise, if you want to know a reason why a bot stopped, you'll have to put in all your own codes whereever you call the Stop() method. And then within the Stop method itself you'll have to check your codes to see which one occurred, or if one occurred that you either didn't account for or was beyond your control.
@firemyst
firemyst
09 Jun 2020, 14:05
RE:
michael.g.heiss said:
Hello,
my cTrader freezes on 2 monitors and also a new start doesnt help. So I want to do a new installation of cTrader.
How can I save my imported indicators and bots, so I dont have to import them all again after my new installation?
They are store in the following folder location:
C:\Users\[your login id]\Documents\cAlgo\Sources
Just copy them out somewhere safe.
@firemyst
firemyst
02 Jun 2020, 13:57
RE:
Sharpie said:
can this feature on ctrader be updated so that it has this function of sorting the watch list by change percentage or price change or ascending/descending order. This will greatly help us monitoring quickly which pair moves a lot. Thanks.
If you have your own watch list, you can already sort by symbol name asc/desc (although it is a manual process).
Just left-click-and-hold on the symbol you want, and then drag it up/down to where you want it in the list.
Hope that helps?
@firemyst
firemyst
30 May 2020, 12:39
I'm not sure what you're expecting to see?
When I load up your indicator on an M5 chart, and select the timeframe to be M15, it shows. When I change it from M15 to M5, they overlap.
When I change the chart to M15 chart, and put the timeframe on M5, they both show as they should. When I set them both to M15 on the M15 chart, they both overlap.
??
@firemyst
firemyst
28 May 2020, 15:53
( Updated at: 21 Dec 2023, 09:22 )
RE:
tonylupino01 said:
Could you add a pip counter for single and multiple pares in the top right page on a live trade, which we can if wanted be enlarged, it would help at a glance to see how the trade s going! On google its called a dig pip count! Thanks
WHy don't you just use the "Positions" tab at the bottom of the chart to see all your open positions and pips in profit you are?
@firemyst
firemyst
28 May 2020, 15:49
RE:
bienve.pf said:
Hello, I have tried to make a stopwatch independent of the ticks using the "Task" or "System.Threading.Timer" instruction in the Initialize function of an indicator. I have classified the variable that refers to the Task or thread as "static".
Both methods do not last. They stop after a few minutes.
How can I make a reliable counter inside an indicator without relying on ticks?
Regards.
public static System.Threading.Timer aTimer; public static Task task = null; bool Finished = false; protected override void Initialize() { aTimer = new System.Threading.Timer(TimerCallback2, null, 1000, 50); task = new Task(() => { while (!this.Finished) { this.TimerCallback(null); System.Threading.Thread.Sleep(10); } }); task.Start(); }
Why not just use a StopWatch object? The only timer I think you can reliably use is the built in timer within the cAlgo API.
@firemyst
firemyst
28 May 2020, 15:47
( Updated at: 21 Dec 2023, 09:22 )
RE:
systemtradingvn02 said:
======================================
I need 3 vertical lines on the chart (help me)Line 1: line 26 back
Line 2: line, 55 back
Line 3: line 89 back?can give me this program
Nobody here understands what you're asking for.
@firemyst
firemyst
14 Aug 2020, 08:44
RE:
PanagiotisCharalampous said:
Yes, but it still involves more clicks than it use to from the side menu. For example, to delete an indicator, I have to right click on each one, and click delete. Then move mouse, right click on another one, clicking delete, etc.
From the menu, I used to be able to just go right down the list and click "x". Half the time that wouldn't even involve moving the mouse to click the next tiny "x".
I appreciate what the team tried to do, but I think it's simpler and easier for the user (with less clicks) to separate the "indicators" and "drawings" objects into their own menus instead of being combined.
Especially for those of us (like myself) who typically don't have any drawings on a chart, it's just more unnecessary clicks users have to go through.
@firemyst