Topics
15 Sep 2017, 11:56
 2579
 2
08 Jul 2017, 17:05
 0
 4814
 4
09 Apr 2013, 23:46
 3673
 4
11 Feb 2013, 08:50
 3216
 2
28 Nov 2012, 20:57
 3701
 6
23 Nov 2012, 12:23
 3228
 4
09 Nov 2012, 14:50
 3085
 6
08 Nov 2012, 13:19
 3157
 2
06 Nov 2012, 14:56
 6205
 19
25 Oct 2012, 20:17
 12414
 29
Replies

Uche
07 Nov 2012, 11:47

Testing Symbol and currency.

AUDUSD,GBP Account.


@Uche

Uche
02 Nov 2012, 16:24

NEWS TRADING ROBOT

http://www.youtube.com/watch?v=8KflrszGj24&feature=plcp.


@Uche

Uche
01 Nov 2012, 10:14

close position

Goodday Admin

I noticed that this function does not work in the tester;

if (position.GrossProfit < MaxLoss)

            {

                Trade.Close(position);

            }

What do you think is the cause.

 

 


@Uche

Uche
31 Oct 2012, 18:51

RE:
admin said:

Hello,

 

We are testing your code but have been unsuccessful thus far in regenerating neither a technical error nor multiple positions.

In any case we are sending you a modified version of your code which might be a little more clear to read which includes reseting the pendingorder field to ensure that this robot creates exactly one position/pending order each time. 

 

        private Position position;

        private PendingOrder pendingOrder;

        private HistoricalVolatility _Hvol;

        private WilliamsPctR _Wp;

 

        // Initialize Indicators

        protected override void OnStart()

        {

            _Hvol = Indicators.HistoricalVolatility(Source, HVPeriod, HVBarhistory, HVD);

            _Wp = Indicators.WilliamsPctR(WprPeriod);

 

        }

        protected override void OnTick()

        {

            if (Trade.IsExecuting) return;

            ResetPendingOrder();

            double Hvol = _Hvol.Result.LastValue;

            double Wp = _Wp.Result.LastValue;

            double Time = Server.Time.Hour;

            bool isTradingTime = Time >= 2 && Time <= 23;

            bool isHighVolatility = Hvol >= 0.003 && Hvol <= 0.020;

         

            if (isTradingTime && isHighVolatility && position == null && pendingOrder == null)

            {

                double? stopLoss;

                DateTime? expiration = Server.Time.AddSeconds(20);

 

                if (Wp < -80)

                {

                    stopLoss = Symbol.Bid - Symbol.PipSize*StopLoss;

                    Trade.CreateBuyStopOrder(Symbol, Volume, Symbol.Bid, stopLoss, null, expiration);

                }

                else if (Wp > -20)

                {

                    stopLoss = Symbol.Ask + Symbol.PipSize*StopLoss;

                    Trade.CreateSellStopOrder(Symbol, Volume, Symbol.Ask, stopLoss, null, expiration);

                }

            }

            if (position == null) return;

            if (position.TradeType == TradeType.Sell)

            {

                double distance = position.EntryPrice - Symbol.Ask;

                if (distance > Trigger*Symbol.PipSize)

                {

                    double newStopLossPrice = Symbol.Ask + TrailingStop*Symbol.PipSize;

                    if (position.StopLoss == null || newStopLossPrice < position.StopLoss)

                    {

                        Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                    }

                }

            }

            else

            {

                double distance = Symbol.Bid - position.EntryPrice;

                if (distance > Trigger*Symbol.PipSize)

                {

                    double newStopLossPrice = Symbol.Bid - TrailingStop*Symbol.PipSize;

                    if (position.StopLoss == null || newStopLossPrice > position.StopLoss)

                    {

                        Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                    }

                }

            }

        }

 

        private void ResetPendingOrder()

        {

            bool exists = false;

            foreach (var accountPendingOrder in Account.PendingOrders)

            {

                if (pendingOrder.Equals(accountPendingOrder))

                {

                    exists = true;

                    break;

                }

            }

            if (!exists)

                pendingOrder = null;

        }

        protected override void OnPendingOrderCreated(PendingOrder newOrder)

        {

            pendingOrder = newOrder;

            bool isLargeSpread = Symbol.Spread > 2;

            bool isLargeBidDiff = (pendingOrder.TargetPrice - Symbol.Bid)*Symbol.PipSize > 2;

            bool isLargeAskDiff = (Symbol.Ask - pendingOrder.TargetPrice)*Symbol.PipSize > 2;

 

            if (position != null || isLargeSpread || isLargeBidDiff || isLargeAskDiff)

            {

                Trade.DeletePendingOrder(pendingOrder);

                pendingOrder = null;

            }

        }

        protected override void OnPositionOpened(Position openedPosition)

        {

            position = openedPosition;

            // Pending Order Filled

            pendingOrder = null;

 

            if (position.GrossProfit < MaxLoss)

            {

                Trade.Close(position);

            }

        }

        protected override void OnPositionClosed(Position closedPosition)

        {            

            position = null;

        }                

    }

}

 

 

Looks good on tester.Lets see how it goes on real time.

 

Thanks for prompt response.

 


@Uche

Uche
31 Oct 2012, 10:41

Hello Admin,

Please look closely at the code and performance to accertain why multiple orders are sent to the server.

Five orders were placed on one currency pair at same time while trading the 1min/2min charts;this is the highest number of orders I have seen so far.

I am trying to code my Mt4 strategy in Calgo,I love the speed and easy coding language but its starting to disappoint.

Kind regards


@Uche

Uche
30 Oct 2012, 17:20

Yes I mean a simultaneous openning of positions.The robot only trails one position,hence returns a technical error on the other.

 


@Uche

Uche
30 Oct 2012, 11:30

Use the 1period SMA, set the source to High.


@Uche

Uche
30 Oct 2012, 10:17

Hello admin,

I am still getting multiple orders during fast movements(high volatility).

This I suspect also creates a technical error when moving the stoploss.

 

Regards


@Uche

Uche
29 Oct 2012, 11:17

Ok Thanks.

 


@Uche

Uche
26 Oct 2012, 17:27

Hello,

I just got a double order demo trading the algo above.Is there something I am missing in the code?


@Uche

Uche
26 Oct 2012, 13:34

Hello,

I modified the code to:

using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class Uchechukwu : Robot
    {                
        
        [Parameter]
        public DataSeries Source { get; set; }
        
        [Parameter(DefaultValue = 100)]
        public int WprPeriod { get; set; }
        
        [Parameter("HV Period", DefaultValue = 14)]
        public int HVPeriod { get; set; }
        
        [Parameter("HV Barhistory", DefaultValue = 400)]
        public int HVBarhistory { get; set; }
        
        [Parameter(DefaultValue = 2)]
        public int HVD { get; set; }

        [Parameter(DefaultValue = 100000)]
        public int Volume { get; set; }

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

        [Parameter("Trigger (pips)", DefaultValue = 30)]
        public int Trigger { get; set; }

        [Parameter("Trailing Stop (pips)", DefaultValue = 30)]
        public int TrailingStop { get; set; }

        [Parameter("MaxLoss", DefaultValue = -500.0)]
        public double MaxLoss { get; set; }
        

        private Position position; 
        private PendingOrder pendingorder;
        private HistoricalVolatility _Hvol;
        private WilliamsPctR _Wp;

        ///
        /// Initialize Indicators
        ///
        protected override void OnStart()
        {
           _Hvol = Indicators.HistoricalVolatility(Source,HVPeriod,HVBarhistory,HVD);
           _Wp = Indicators.WilliamsPctR(WprPeriod);
           
        }
         protected override void OnTick()
        {
            if (Trade.IsExecuting) return;
            
            double Hvol = _Hvol.Result.LastValue;
            double Wp = _Wp.Result.LastValue;
            double Time = Server.Time.Hour;

            
            if (Time>=2 && Time<=23 && Hvol>=0.003 && Hvol<=0.020 && Wp<-80 && position == null) 
            {
            if (pendingorder == null)
            {
                Trade.CreateBuyStopOrder(Symbol,Volume,Symbol.Bid,Symbol.Bid - Symbol.PipSize * StopLoss,null,Server.Time.AddSeconds(20));
            }
            
            } 
            if (Time>=2 && Time<=23 && Hvol>=0.003 && Hvol<=0.020 && Wp>-20 && position == null)
            {
            if (pendingorder == null)
            {
               Trade.CreateSellStopOrder(Symbol,Volume,Symbol.Ask,Symbol.Ask + Symbol.PipSize * StopLoss,null,Server.Time.AddSeconds(20));
            }
            
            }


if (position == null) return;
              {
                if (position !=  null && position.TradeType == TradeType.Sell)
                {
                    double distance = position.EntryPrice - Symbol.Ask;

                    if (distance > Trigger * Symbol.PipSize)
                    {
                        double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;

                        if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                        {
                            Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                        }
                    }
                }
                else
                {
                    double distance = Symbol.Bid - position.EntryPrice;

                    if (distance > Trigger * Symbol.PipSize)
                    {
                        double newStopLossPrice = Symbol.Bid - TrailingStop * Symbol.PipSize;
                        if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                        {
                            Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                        }
                    }
                }
            }
        }
      
        protected override void OnPendingOrderCreated(PendingOrder newOrder)
        {
            pendingorder = newOrder;
        
            if (pendingorder != null && pendingorder.SymbolCode == Symbol.Code) 
            {
            if (position != null && position.SymbolCode == Symbol.Code || Symbol.Spread>2 || (pendingorder.TargetPrice-Symbol.Bid)*Symbol.PipSize>2 || (Symbol.Ask-pendingorder.TargetPrice)*Symbol.PipSize>2)
            {
                Trade.DeletePendingOrder(pendingorder);
            }
            
            }
        }    

        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;
        
            if (position !=  null && position.GrossProfit < MaxLoss)
            {
              Trade.Close(position);
            } 
        }   
    }
}


@Uche

Uche
26 Oct 2012, 13:05

Thanks a lot.Making modifications now.


@Uche

Uche
26 Oct 2012, 10:43

This is my code:

using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class Uchechukwu : Robot
    {                
        
        [Parameter]
        public DataSeries Source { get; set; }
        
        [Parameter(DefaultValue = 100)]
        public int WprPeriod { get; set; }
        
        [Parameter("HV Period", DefaultValue = 14)]
        public int HVPeriod { get; set; }
        
        [Parameter("HV Barhistory", DefaultValue = 400)]
        public int HVBarhistory { get; set; }
        
        [Parameter(DefaultValue = 2)]
        public int HVD { get; set; }

        [Parameter(DefaultValue = 100000)]
        public int Volume { get; set; }

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

        [Parameter("Trigger (pips)", DefaultValue = 30)]
        public int Trigger { get; set; }

        [Parameter("Trailing Stop (pips)", DefaultValue = 30)]
        public int TrailingStop { get; set; }

        [Parameter("MaxLoss", DefaultValue = -500.0)]
        public double MaxLoss { get; set; }
        

        private Position position; 
        private PendingOrder pendingorder;
        private HistoricalVolatility _Hvol;
        private WilliamsPctR _Wp;

        ///
        /// Initialize Indicators
        ///
        protected override void OnStart()
        {
           _Hvol = Indicators.HistoricalVolatility(Source,HVPeriod,HVBarhistory,HVD);
           _Wp = Indicators.WilliamsPctR(WprPeriod);
           
        }
         protected override void OnTick()
        {
            if (Trade.IsExecuting) return;
            
            double Hvol = _Hvol.Result.LastValue;
            double Wp = _Wp.Result.LastValue;
            double Time = Server.Time.Hour;

            
            if (Time>=2 && Time<=23 && Hvol>=0.003 && Hvol<=0.020 && Wp<-80 && position == null) 
            {
            if (pendingorder == null)
            {
                Trade.CreateBuyStopOrder(Symbol,Volume,Symbol.Bid,Symbol.Bid - Symbol.PipSize * StopLoss,null,Server.Time.AddSeconds(20));
            }
            
            } 
            if (Time>=2 && Time<=23 && Hvol>=0.003 && Hvol<=0.020 && Wp>-20 && position == null)
            {
            if (pendingorder == null)
            {
               Trade.CreateSellStopOrder(Symbol,Volume,Symbol.Ask,Symbol.Ask + Symbol.PipSize * StopLoss,null,Server.Time.AddSeconds(20));
            }
            
            }
            
            if (pendingorder != null && pendingorder.SymbolCode == Symbol.Code) 
            {
            if (position != null && position.SymbolCode == Symbol.Code || Symbol.Spread>2 || (pendingorder.TargetPrice-Symbol.Bid)*Symbol.PipSize>2 || (Symbol.Ask-pendingorder.TargetPrice)*Symbol.PipSize>2)
            {
                Trade.DeletePendingOrder(pendingorder);
            }
            
            }
            
            if (position == null) return;
              {
              if (position !=  null && position.GrossProfit < MaxLoss)
              {
              Trade.Close(position);
              } 
                if (position !=  null && position.TradeType == TradeType.Sell)
                {
                    double distance = position.EntryPrice - Symbol.Ask;

                    if (distance > Trigger * Symbol.PipSize)
                    {
                        double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;

                        if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                        {
                            Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                        }
                    }
                }
                else
                {
                    double distance = Symbol.Bid - position.EntryPrice;

                    if (distance > Trigger * Symbol.PipSize)
                    {
                        double newStopLossPrice = Symbol.Bid - TrailingStop * Symbol.PipSize;
                        if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                        {
                            Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                        }
                    }
                }
            }
        }
      
        private void OpenPosition(TradeType command)
        {
            if (position != null)
            {
                Trade.Close(position);
                position = null;
            }

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

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

        private double GetAbsoluteStopLoss(Position position, int stopLossInPips)
        {
            return position.TradeType == TradeType.Buy
                ? position.EntryPrice - Symbol.PipSize * stopLossInPips
                : position.EntryPrice + Symbol.PipSize * stopLossInPips;
        }
        private void ClosePosition(Position position)
        {
            if (position == null) return;
            Trade.Close(position);

        }
        
    }
}


@Uche

Uche
26 Oct 2012, 10:31

I mean to modify entry prices when I say 'modify orders'.

 


@Uche

Uche
26 Oct 2012, 10:28

Be advised that unlike the MT4 ea version Calgo is currently unable to modify orders until SecBMO.

The best it can do for now is to place an order say 5-3 Secs before the news release.Works for me.


@Uche

Uche
26 Oct 2012, 09:50

I did try this line:

if(Trade.IsExecuting)return;
else
{
                 Trade.CreateBuyStopOrder(Symbol,Volume,Symbol.Bid,
                                           Symbol.Bid - Symbol.PipSize * StopLoss,null,
                                           Server.Time.AddSeconds(20));
}  

 It continued to send multiple orders on some occasions.


@Uche