Comparison of Open and Close Price

Created at 23 Jan 2013, 17:38
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
supafly's avatar

supafly

Joined 26.12.2012

Comparison of Open and Close Price
23 Jan 2013, 17:38



I am trying to create a condition that will compare the open price with the close price as displayed on candles, but it doesnt open any positions when infact that simple condition is met many times. Here is the sample code. Im not sure if i am using MarketSeries correctly.

double firstclose = MarketSeries.Close[MarketSeries.Close.Count-1];            double firstopen = MarketSeries.Open[MarketSeries.Close.Count-1];

if (firstopen > firstclose)

{

 //Sell

}



 


@supafly
Replies

admin
23 Jan 2013, 17:55

Yes, it is correct. A simple print statement will prove that this condition is met within the code. It is probably something else that is not correct.

You can test this code and let us know:

    [Robot]
    public class NewRobot : Robot
    {
    	Position _position;
    	
        protected override void OnTick()
        {
        	if(Trade.IsExecuting)
        		return;
        		
            double firstclose = MarketSeries.Close[MarketSeries.Close.Count-1];            
            double firstopen = MarketSeries.Open[MarketSeries.Close.Count-1];
            if (firstopen > firstclose)
            {
            	//Sell
            	if(_position == null)
	            	Trade.CreateSellMarketOrder(Symbol, 1000);
            }
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
        	_position = openedPosition;           
        }
    }




@admin

supafly
23 Jan 2013, 19:42

Admin,

Thanks for taking the time to write the above algo. I have just compared it to my algo and they are very similar in logic and syntax. I am still unable to locate the problem so i compiled your algo to see the outcome and it is giving me the same results which are 0 trades executed, so im not sure what else to do.

If it helps, the only differences are:

private Position position; 
protected override void OnBar()

Thanks

@supafly

supafly
23 Jan 2013, 19:56

Also, I have modified the command below to 10000 which is the minimum allowed volume but again there is no result.

Trade.CreateSellMarketOrder(Symbol, 1000);

@supafly

admin
24 Jan 2013, 10:59

Have you tried backtesting it? If you are using OnBar that means that the code will execute on each new trendbar which will depend on the timeframe that you are using. If you are using the default hourly timeframe you would have to wait up to an hour before the code executes. Try a small timeframe like a minute, as well as backtesting.


@admin

supafly
24 Jan 2013, 13:38

Hi Admin,

I have backtested and dropped down to the 1 minute but again no result. If its possible, can you run the above algo and tell me if it works on your system. Im not sure if there is an issue when doing a comparison and both conditions have the same MarketSeries count. 

Thanks


@supafly

admin
24 Jan 2013, 14:40

The code we provided does work. Can you send us your exact code to engage@ctrader.com for further investigation. It may also help if you include in the email the version of cAlgo you are using. Thanks in advance.


@admin

admin
24 Jan 2013, 15:43

I apologize for not indicating this sooner. If you use the OnBar method you should access the previous to the current bar prices for accuracy. Therefore, you need to use count - 2. 


@admin

supafly
24 Jan 2013, 18:20

So what you are saying is that count -1 represents the last candlestick which is not completed?


@supafly

supafly
24 Jan 2013, 18:46

Also, I have another live working algorithm that uses count -1 for a MarketSeries command and  moving averages with count -1 and count -2. If my above statement is true, when I tried to modify the algorithm to say -2 instead of -1, the bactesting results were poor compared to the previous backtest results. Again this algorithm uses OnBar(). 

Thanks

 


@supafly

admin
25 Jan 2013, 09:48

You can use print statements to confirm the values like so:

        protected override void OnBar()
        {
            int index = MarketSeries.Close.Count - 1;

            Print("Open Time {0}", MarketSeries.OpenTime[index]);
            Print("Open {0}", MarketSeries.Open[index]);
            Print("Close {0}", MarketSeries.Close[index]);
            Print("High {0}", MarketSeries.High[index]);
            Print("Low {0}", MarketSeries.Low[index]);

            Print("Open Time {0}", MarketSeries.OpenTime.LastValue);
            Print("Open {0}", MarketSeries.Open.LastValue);
            Print("Close {0}", MarketSeries.Close.LastValue);
            Print("High {0}", MarketSeries.High.LastValue);
            Print("Low {0}", MarketSeries.Low.LastValue);

            Print("Open Time {0}", MarketSeries.OpenTime[index - 1]);
            Print("Open {0}", MarketSeries.Open[index - 1]);
            Print("Close {0}", MarketSeries.Close[index - 1]);
            Print("High {0}", MarketSeries.High[index - 1]);
            Print("Low {0}", MarketSeries.Low[index - 1]);
            
        }




@admin

supafly
25 Jan 2013, 13:33

RE:

Admin,

I will use the print statement and review the results. Thanks for your help. Much apppreciated.

 


@supafly

cprcrack
18 Feb 2015, 14:05

OnBar() seems to be fired immediately after the first tick of a new bar (open price), and thus the last bar is useless in general, you will have to use the previous to the last one.


@cprcrack