Topics
28 Aug 2012, 13:02
 5230
 2
Replies

sktrader
19 Sep 2012, 12:26

Hi,

I looked at your code and there are a few mistakes.

First of all this is not possible: position = 1. What are you trying to do here?

Here are some corrections:

 

//
//    The "Sample CCI Robot" will create a buy order when the Commodity Channel Index indicator crosses the  level 1, 
//    and a Sell order when the CCI indicator crosses the level -1. The order is closed be either a Stop Loss, defined in 
//    the "Stop Loss" parameter, or by the opposite CCI crossing signal (buy orders close when CCI crosses the -1 level 
//    and sell orders are closed when CCI crosses the 1 level). 
//
//    The robot can generate only one Buy or Sell order at any given time.
//
// -------------------------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class CCIRobot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }
        
        [Parameter("Periods", DefaultValue = 21)]
        public int Periods { get; set; }

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

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

        private Position position;        
		private CommodityChannelIndex cci;

        protected override void OnStart()
        {
            cci = Indicators.CommodityChannelIndex(Periods);
        }

        protected override void OnTick()
        {
            if (Trade.IsExecuting) return;
            
            // position == 1 ???
            // (position is not int or enum, you cannot compare with 1.)

            if (cci.Result.LastValue < 0 && position.TradeType == TradeType.Sell) 
        	{
        		OpenPosition(TradeType.Buy);
        	}
            //position == -1 same as above
            if (cci.Result.LastValue > 0 &&  position.TradeType == TradeType.Buy)
            {
                OpenPosition(TradeType.Sell);
            }
        }

        private void OpenPosition(TradeType command)
        {
            // THIS IS NOT CORRECT:
//            if (position != 1)
//            {
//                Trade.Close(position);
//                position = 1;
//            }

            // is this what you need?
            if(position != null)
                Trade.Close(position);

//        {     <- this is extra

            // THIS IS NOT CORRECT:
//            if (position != -1)
//            {
//                Trade.Close(position);
//                position = -1;
//            }

            Trade.CreateMarketOrder(command, Symbol, Volume);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;
            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), 1);
        } 

        // DUPLICATE...
//		  {
//            position = openedPosition;
//            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), -1);
//        }

        private double GetAbsoluteStopLoss(Position pos, int stopLossInPips)
        {
            return pos.TradeType == TradeType.Buy
                ? pos.EntryPrice - Symbol.PipSize * stopLossInPips
                : pos.EntryPrice + Symbol.PipSize * stopLossInPips;
        }
    }
}

 

 

 


@sktrader

sktrader
18 Sep 2012, 11:28

I'm talking about the example Robot that comes with the program(cAlgo).

The name is SampleRobotReferenceSMA. You can find it in the Robots section of cAlgo.

It's doing the same thing you want to do...


@sktrader

sktrader
18 Sep 2012, 10:23

Check to see if SampleRobotReferenceSMA builds. If it does then that means it's probably your code. 

 


@sktrader

sktrader
17 Sep 2012, 10:06

Hi,

 

You need to click Add Reference on the top next to build, locate your algo file (indicator) and then in the code:

        TrendFinder trendFinder; // Declaration

        protected override void OnStart()
        {
            trendFinder = Indicators.GetIndicator<TrendFinder>(StdDevMultiplier, StdDevPeriod, MAType);
        }

        protected override void OnTick()
        {
            Print("{0}", trendFinder.UpTrend.LastValue);
            Print("{0}", trendFinder.DownTrend) .LastValue);
        }

@sktrader

sktrader
12 Sep 2012, 09:34

Hi Tradematrix,

 

        protected override void OnTick()
        {
            if (Account.Equity - Account.Balance > 10 || Account.Equity - Account.Balance < -10)
            {
                foreach (var position in Account.Positions)
                {
                    Trade.Close(position);
                }
            }
        }

 



 


@sktrader