Topics
Replies
firemyst
31 Oct 2022, 13:51
( Updated at: 21 Dec 2023, 09:23 )
RE:
Alwin123 said:
How to use 2 macd different timeframe
I can't get past this.
anyone who can improve the script? Both macd must indicate the same sentiment before opening a trade. Preferably above or below the zero line
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class PullbackStrategy : Robot
{[Parameter("Sentiment: Buy", Group = "Sentiment", DefaultValue = true)]
public bool Buy { get; set; }[Parameter("Sentiment: Sell", Group = "Sentiment", DefaultValue = true)]
public bool Sell { get; set; }[Parameter("Another Time Frame MACD ", Group = "Strategy", DefaultValue = "Hour")]
public TimeFrame Multitimeframe { get; set; }[Parameter("Source", Group = "RSI")]
public DataSeries SourceRSI { get; set; }[Parameter("Periods", Group = "RSI", DefaultValue = 19)]
public int PeriodsRSI { get; set; }[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }[Parameter("Stop Loss ", Group = "Risk Managment", DefaultValue = 100)]
public int StopLoss { get; set; }[Parameter("Take Profit", Group = "Risk Managment", DefaultValue = 100)]
public int TakeProfit { get; set; }
public ExponentialMovingAverage i_MA_slow;
public ExponentialMovingAverage i_MA_standart;
public ExponentialMovingAverage i_MA_fast;
private RelativeStrengthIndex rsi;
private MacdCrossOver macd;
private MacdCrossOver MultitimeframeMACD;
private double volumeInUnits;
private Position position;
protected override void OnStart(){
i_MA_slow = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 50);
i_MA_standart = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 20);
i_MA_fast = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 8);
rsi = Indicators.RelativeStrengthIndex(SourceRSI, PeriodsRSI);
macd = Indicators.MacdCrossOver(26, 12, 9);
MultitimeframeMACD = Indicators.MacdCrossOver(26, 12, 9);
volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
Positions.Opened += OnPositionsOpened;
Positions.Closed += OnPositionsClosed;
}void OnPositionsClosed(PositionClosedEventArgs obj)
{
position = null;
}void OnPositionsOpened(PositionOpenedEventArgs obj)
{
position = obj.Position;
}
[Obsolete]
protected override void OnBar()
{
var MACDLine = macd.MACD.Last(1);
var PrevMACDLine = macd.MACD.Last(2);
var Signal = macd.Signal.Last(1);
var PrevSignal= macd.Signal.Last(2);// both macd must be above the zero line for an entry buy or sell
var MultitimeframeMACD = MarketData.GetSeries(Multitimeframe);
var Multitimeframe = MultitimeframeMACD.MACD.Last(1);
var PrevMACDLineMultiTF = MultitimeframeMACD.MACD.Last(2);
var SignalMultiTF = MultitimeframeMACD.Signal.Last(1);
var PrevSignalMultiTF = MultitimeframeMACD.Signal.Last(2);if (position != null)
return;
{
if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
{
int index = MarketSeries.Close.Count;if (MACDLine > Signal & PrevMACDLine <PrevSignal & default==Sell
& Multitimeframe > SignalMultiTF & PrevMACDLineMultiTF < PrevSignalMultiTF & default == Sell
& i_MA_fast.Result[index - 2] > MarketSeries.Close[index - 2]
& i_MA_fast.Result[index - 1] < MarketSeries.Close[index - 1]
& i_MA_fast.Result.LastValue < i_MA_standart.Result.LastValue
& i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue
& i_MA_standart.Result.LastValue < i_MA_slow.Result.LastValue)
{
ExecuteMarketOrder( TradeType.Buy, SymbolName, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
}
else if (MACDLine < Signal & PrevMACDLine >PrevSignal & default== Buy
& Multitimeframe > SignalMultiTF & PrevMACDLineMultiTF < PrevSignalMultiTF & default == Buy
& i_MA_fast.Result[index - 2] < MarketSeries.Close[index - 2]
& i_MA_fast.Result[index - 1] > MarketSeries.Close[index - 1]
& i_MA_fast.Result.LastValue > i_MA_standart.Result.LastValue
& i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue
& i_MA_standart.Result.LastValue > i_MA_slow.Result.LastValue){
ExecuteMarketOrder( TradeType.Sell, SymbolName, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
}
}
}
}
protected override void OnStop()
{
}
}
}
HI @Alwin123:
Your code isn't going to work, because you have to pass in a different time frame series when you create the MACD indicator.
It'll be something like this:
private Bars _marketSeriesH1;
MacdCrossOver macd;
//In the OnStart method, do something like this. This will get the data for the H1 timeframe:
_marketSeriesH1 = MarketData.GetBars(TimeFrame.Hour, Symbol.Name);
//This gets the MACD with the above time frame set (eg, H1 in this case):
macd = Indicators.MacdCrossOver(_marketSeriesH1.ClosePrices, LongPeriod, ShortPeriod, SignalPeriod);
@firemyst
firemyst
31 Oct 2022, 13:50
RE:
PanagiotisChar said:
Hi Waxy,
Did you try
[Parameter("Color", DefaultValue = "Red")] public MyColors Color { get; set; }
Need help? Join us on Telegram
Need premium support? Trade with us
HI @PanagiotisChar:
I believe it's broken in the latest cTrader release, because the code below doesn't work for me any more either. It always comes up with a default value of "Yesterday" instead of "Three_Weeks":
public enum LoadFromData
{
Yesterday,
Today,
Two_Days,
Three_Days,
One_Week,
Two_Weeks,
Three_Weeks,
Monthly,
Custom
}
[Parameter("Amount of Historic Data to Load:", DefaultValue = LoadFromData.Three_Weeks)]
public LoadFromData LoadFromInput { get; set; }
@firemyst
firemyst
31 Oct 2022, 13:45
HI @Alwin123:
Your code isn't going to work, because you have to pass in a different time frame series when you create the MACD indicator.
It'll be something like this:
private Bars _marketSeriesH1;
MacdCrossOver macd;
//In the OnStart method, do something like this. This will get the data for the H1 timeframe:
_marketSeriesH1 = MarketData.GetBars(TimeFrame.Hour, Symbol.Name);
//This gets the MACD with the above time frame set (eg, H1 in this case):
macd = Indicators.MacdCrossOver(_marketSeriesH1.ClosePrices, LongPeriod, ShortPeriod, SignalPeriod);
@firemyst
firemyst
27 Oct 2022, 12:11
( Updated at: 21 Dec 2023, 09:23 )
RE:
mindfulness said:
Hello,
even with using the memory manager, the used memory is increasing overtime and i have to restart the bot after maybe two days. Is there another way to handle it?
For example on one VPS there are running two ctrade instances. One with 37 pairs/bots and one with 15. The VPS has 4 cores and 8 GB RAM. Here the usage after one day.
How much writing to the log does the bot do? Eg, PRINT statements? You can't clear the log programmatically, and constantly writing to it will suck up memory.
@firemyst
firemyst
20 Oct 2022, 09:09
RE:
PanagiotisCharalampous said:
As far as developer support is concerned, I am 100% sure that we have the best community team out there!
Best Regards,
Panagiotis
There's definitely room for improvement. I don't think you've ever used the platform "ProRealTime" and/or visited their "ProRealCode" forums or code libraries?:-)
They have dedicated employees not only writing code/custom indicators, but also responding to queries in the forums on a daily basis, in multiple languages, for both their "ProRealTime" product and their custom "ProRealCode" coding.
@Spotware is nowhere near as interactive in its cTrader forums.
@firemyst
firemyst
13 Oct 2022, 15:50
RE:
PanagiotisChar said:
Hi firemyst,
I cannot think of an accurate way to do so without tick data.
Need help? Join us on Telegram
Need premium support? Trade with us
Thank you @PanagiotisChar
And congratulations on your new positions. :-)
@firemyst
firemyst
04 Oct 2022, 03:25
You could just try reading online documentation provided by Spotware, such as:
https://help.ctrader.com/ctrader-automate/creating-and-running-a-cbot/
or here:
https://help.ctrader.com/ctrader-automate/guides/
or even the higher level link:
https://help.ctrader.com/ctrader-automate/
@firemyst
firemyst
04 Oct 2022, 03:18
RE:
r.stipriaan said:
Hi hamirady60,
I think its not posible to close positon on bars, i use this method, it works with time.
var SellPosition = Positions.Find("Short", SymbolName, TradeType.Sell); if (SellPosition != null && SellPosition.EntryTime.AddHours(5) < TimeInUtc) { ClosePosition(Positions.Find("Short", SymbolName, TradeType.Sell)); }
Positions can be closed on bars.
Here's one way to do it:
1) set a global variable for keeping track:
int _indexOrderPlaced = 0;
int _currentIndex = 0;
2) in OnStart, reset the value to zero:
_indexOrderPlaced = 0;
_currentIndex = Bars.OpenTimes.GetIndexByTime(Bars.OpenTimes.LastValue);
3) When an order is placed, get the bar's current index similar to:
_indexOrderPlaced = Bars.OpenTimes.GetIndexByTime(Bars.OpenTimes.LastValue);
Every time OnBar is called:
//Get the current index
_currentIndex = Bars.OpenTimes.GetIndexByTime(Bars.OpenTimes.LastValue);
//Check if it's within 5 of the order being placed
if (_currentIndex - _indexOrderPlaced > 5)
{
//close the position here
//reset this
_indexOrderPlaced = 0;
}
Just a schematic, but should help to get you started.
@firemyst
firemyst
23 Sep 2022, 06:50
RE:
jeremy.ayres said:
When comparing moving average values on open and close how can I compare this against Price Action values? E,g,. if one is greater than the other, surely I would need to compare similar values, or there a built-in way to call this is in the code?
What price action values do you want to compare against? Open prices? Close prices?
You would just need to do something similar to:
MovingAverage1.Result.LastValue >= Bars.ClosePrices.LastValue
Not that in the current bar, the current price is always the "close price" even if the bar hasn't closed yet.
@firemyst
firemyst
23 Sep 2022, 06:48
I think you can do what you want in the Calculate method:
1) create a class variable that's an int that keeps track of previous indexes:
private int _previousIndex;
2) in the initialize method, set _previousIndex = 0
3) In Calculate:
if (index > _previousIndex)
{
//do all your logic
_previousIndex = index;
}
The above code will only run then when it moves to the next bar, thereby not calculating it on the current bar.
@firemyst
firemyst
18 Sep 2022, 09:58
RE:
jaydcrowe1989 said:
Hi,
I am not sure if this is possible but is there a way to get the last drawn fractal arrow or is there a way to match the arrows up with the correct bar they were drawn at?
Cheers,
Jay
Yes. I wrote my own fractal indicator that does just that.
If you need help, please post your code.
@firemyst
firemyst
18 Sep 2022, 09:57
Google and searching the cTrader.com website is your friend:
https://help.ctrader.com/ctrader-automate/debugging/#debugging-a-cbotindicator
@firemyst
firemyst
03 Sep 2022, 18:37
RE:
douglascvas said:
Could cTrader go back to 1 single process, please? It is so hard to debug algos when the process keeps changing on each run.
Yes, it is a little bit more of a hassle.
Perhaps this will help:
https://help.ctrader.com/ctrader-automate/debugging/#debugging-a-cbotindicator
@firemyst
firemyst
01 Sep 2022, 04:38
( Updated at: 01 Sep 2022, 04:39 )
RE: Hi Firemyst,
Omega said:
firemyst said:
Omega said:
Hi fellas'
I have two algo's I use to trade...
First is an algo that trades a basket of currencies the second will close all positions when the account reaches it's profit target.
The issue I have is that the first algo that trades doesn't stop once the positions have been closed by the second algo if I'm not there to manually stop ctrader.
Is there a way to stop the trading algo after it has reached my profit target without my being there to do it manually?
Look forward to your ideas...
O
ALl you have to do is put this line of code in your cBot when you want it to stop:
Stop();
So when the position is closed, in the running bot instance check to make sure the current bot instance is the one that had its position closed. If it was, tell it to "Stop()".
Thanks for that...
I get the 'Stop(); line but I have an algo on each chart, it's the collective or basket of currencies that provide the profit overall so I'm thinking that it will need a separate algo for this?
Since you can't control one bot from another bot, there are at least 2 options I can think of off the top of my head that you can try:
1) (the easiest one) - create a public static variable in your bot code. That'll be visible across all bots. Call it something like:
public static bool StopAllBotInstances = false;
Make it a boolean. Have each bot check it during the onTick method. If it's ever set to true by one bot, call Stop(); Every bot should then stop with the next tick.
2) (not as easy) Create your own event handler called something like "OnProfitTargetReached" that does the same thing -- Stop() when the event is fired by any bot instance.
@firemyst
firemyst
01 Nov 2022, 00:59
Your code isn't going to work, because you're not creating the marketseries variables correctly:
_marketSeries2 = MarketData.GetBars(macd2, Symbol.Name);
_marketSeries1 = MarketData.GetBars(macd1, Symbol.Name);
The method is "GetBars", so why are you passing in the macd indicators? The first parameter of the GetBars method is the TIMEFRAME, not an indicator.
You are also not creating/initializing the MACD indicator as I showed you in my example -- the first parameter should be the data series, not the long period.
Please go back and reread my example making sure you understand it.
Spotware also has an example on their website which should help:
https://help.ctrader.com/ctrader-automate/indicator-code-samples/#multiple-timeframes
@firemyst