Topics
25 Apr 2014, 22:15
 3325
 5
01 Apr 2014, 17:19
 2946
 6
30 Mar 2014, 01:35
 4208
 6
29 Mar 2014, 21:02
 4764
 6
Replies

Tempestshade
03 Apr 2014, 17:18

Still haven't been able to figure this out. Have tried tweaking it every which way but with no luck... :/


@Tempestshade

Tempestshade
02 Apr 2014, 15:38

Thanks for replying! 

 

I will try to be more specific.

 

My system opens a new trade every bar that a signal is valid. I want each trade to have an individual trailing stop. Essentially, I want every trade to be managed separately instead of as a basket like it is now.

 

Cheers,

David


@Tempestshade

Tempestshade
31 Mar 2014, 02:41

Stupid mistake changed Positions.FindAll.... to Positions.Find


@Tempestshade

Tempestshade
31 Mar 2014, 02:29

Am now having another problem with my code:

 

 

Throwing errors:

Error CS1502: The best overloaded method match for 'cAlgo.API.Robot.ClosePosition(cAlgo.API.Position)' has some invalid arguments

Error CS1503: Argument 1: cannot convert from 'cAlgo.API.Position[]' to 'cAlgo.API.Position'

            var longposition = Positions.FindAll(label, Symbol, TradeType.Buy);
            var shortposition = Positions.FindAll(label, Symbol, TradeType.Sell);

            if (fSignal.Result.HasCrossedAbove(sSignal.Result.LastValue, checkbars) == true && currentrsi > 50)
            {
                if (shortposition != null)
                    ClosePosition(shortposition);

                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
            }
            else if (fSignal.Result.HasCrossedBelow(sSignal.Result.LastValue, checkbars) == true && currentrsi < 50)
            {
                if (longposition != null)
                    ClosePosition(longposition);

                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }

 


@Tempestshade

Tempestshade
30 Mar 2014, 21:23

Hello,

 

Although I am new to cAlgo I would love to give it a shot. But would like a few more details. Can you explain exactly when a trade will be taken? When the MACD itself has a crossover? When the Stoch has a crossover? 

 

Cheers,

David


@Tempestshade

Tempestshade
30 Mar 2014, 20:10

Hello again,

 

I attempted to recode the above but am now receiving an error: = Backtesting stopped: Error #16309803 occurred

 

As always I have no idea where I am going wrong. I feel like I streamlined the code quite a bit.

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class fourHScalping : Robot
    {
        [Parameter("Trade Comment", DefaultValue = "4H Scalper")]
        public string label { get; set; }

        [Parameter("Take Profit", DefaultValue = 20)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLoss { get; set; }

        [Parameter("Volume", DefaultValue = 10000)]
        public int Volume { get; set; }

        [Parameter("Datasource")]
        public DataSeries Datasource { get; set; }

        [Parameter("MA Type")]
        public MovingAverageType typeMA { get; set; }

        [Parameter("MA Period", DefaultValue = 21)]
        public int periodMA { get; set; }

        private MovingAverage mavg;

        protected override void OnStart()
        {
            // Put your initialization logic here
            mavg = Indicators.MovingAverage(Datasource, periodMA, typeMA);
        }

        protected override void OnBar()
        {
            // Put your core logic here
            var cavg = mavg.Result.Last(0);
            int index = MarketSeries.Median.Count - 1;
            var avgprice = MarketSeries.Median[index - 1];

            if (Symbol.Bid > cavg && Symbol.Bid > avgprice)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);
            }
            else if (Symbol.Ask < cavg && Symbol.Ask < avgprice)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit);
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }


    }
}

Thanks,

David


@Tempestshade

Tempestshade
30 Mar 2014, 15:26 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Hello again all,

I still can't figure out the problem with my above code so instead I went ahead and tried to code another strategy I found. Very simple

Indicators - 21 EMA

TF - H4

BUY conditions - Place buy orders on bar open when price is above the EMA and when the opening price of bar is greater than the average price (Median?) of the previous bar.

SELL conditions - Place sell orders on bar open when price is below the EMA and when the opening price of bar is less than the average price (Median?) of the previous bar.

 

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("TP", DefaultValue = 40)]
        public int TakeProfit { get; set; }

        [Parameter("SL", DefaultValue = 40)]
        public int StopLoss { get; set; }

        [Parameter("Volume", DefaultValue = 10000)]
        public int Volume { get; set; }

        [Parameter("MA Period", DefaultValue = 21)]
        public int MAPeriod { get; set; }

        [Parameter("MA Source")]
        public DataSeries MAsource { get; set; }

        private MovingAverage EMA;
        private const string label = "4H Scalping";
        private MedianPrice _Price;
        private int index;
        private double avgprice;

        protected override void OnStart()
        {
            // Put your initialization logic here
            EMA = Indicators.ExponentialMovingAverage(MAsource, MAPeriod);
            _Price = Indicators.MedianPrice();
            avgprice = _Price.Result[index];
        }

        protected override void OnBar()
        {
            // Put your core logic here
            var currentema = EMA.Result.Last(0);
            _Price = Indicators.MedianPrice();

            if (Symbol.Bid > currentema && Symbol.Bid > avgprice)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);
                avgprice = _Price.Result[index];
            }
            else if (Symbol.Ask < currentema && Symbol.Ask < avgprice)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit);
                avgprice = _Price.Result[index];
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

The problem I am having right now is that even when the median price isn't greater/lower it is still taking buy/sells.

 

In a buy example it should only be taking a trade every time there is a previous bull bar. As you can see by the example I have made a green dot where a trade should have been taken. You will see that when the previous bar is a bearish bar a trade is not taken. As you can see on the right, this was not the case.

 

 

 

 

 

 

 

 

 

 

 

 

 

As always any help pointing me to where I went wrong would be appreciated.

 

Cheers,

David


@Tempestshade

Tempestshade
30 Mar 2014, 04:17

I should add it is doing this repeatedly so every bar it is closing the previous trade, and opening a new trade. Next bar is closes the trade it just opened and opens a new trade....

 

Thanks,

David


@Tempestshade

Tempestshade
29 Mar 2014, 23:30

RE:

Tempestshade said:

 

if (currentSlowMa <= currentFastMa)
{
 ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, Profit);
}
else if 
*repeat for sells
}

That ended up working! On to my next project.

 

I am looking to add a close on bar open as well. However, my code doesn't seem to want to compile for whatever reason.

            var positions = Positions.FindAll(label);

            foreach (var position in positions)
            {
                if (position.Pips >= Profit)
                {
                    ClosePosition(position.Volume);
                }
                if (position.Pips <= StopLoss)
                {
                    ClosePosition(position.Volume);
                }
            }

I get the errors : Error CS1502: The best overloaded method match for 'cAlgo.API.Robot.ClosePosition(cAlgo.API.Position)' has some invalid arguments

 

What exactly is causing this?

 

Thank you,

David


@Tempestshade

Tempestshade
29 Mar 2014, 23:23

Another question for you all,

 

Again as above, I have been modifying the same Trend sample EA.

 

I have modified so it works on new bars only and was wondering why my EA isn't taking a new trade every time the signal is still valid? For code I have:

 


            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, Profit);
            }
            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, Profit);
            }

 

I am probably missing something when it comes to the logic of the trades but for the life of me can't figure it out. I don't want to get rid of the functionality that when the MAs cross a trade is opened, but I also want it to keep 'stacking' trades in the direction of the trend.

 

I imagine if I added it would work:

if (currentSlowMa <= currentFastMa)
{
 ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, Profit);
}
else if 
*repeat for sells
}

I am going to try it, but would still like some input.

 

Thanks!


@Tempestshade

Tempestshade
29 Mar 2014, 22:48

Thank you very much!

 


@Tempestshade