NexusFi: Find Your Edge


Home Menu

 





Intrabar Order Generation ain't working for moving average crossover


Discussion in MultiCharts

Updated
    1. trending_up 1,544 views
    2. thumb_up 1 thanks given
    3. group 2 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

Intrabar Order Generation ain't working for moving average crossover

  #1 (permalink)
peachfuzz
San Francisco CA USA
 
Posts: 5 since Sep 2015
Thanks Given: 2
Thanks Received: 0

Just started using MC .Net and have since begun learning C#. I've managed to write a simple script based on moving average crossover utilizing the Intrabar order generation, with which I'm already having an issue. I would appreciate any advice.

1) IOG works perfectly for prices. For example, when price either crosses above or below another price (eg. high[0] > high[-1]) or an indicator (high > SMA(50)).

2) IOG does not work when it comes to two moving average crossover. Consider the following example.

Two conditions for BUY and SELL orders are coded to be triggered:
-When SMA(10) crosses above SMA(20), send a BUY order IOG
-When SMA(10) crosses below SMA(20), send a SELL order IOG

Suppose then the first condition is met and the BUY order was filled. However, before the end of the bar is reached, price reversed hard and SMA(10) turned down. Now SMA(10) is, again, below SMA(20). Therefore, a SELL order should be sent out. But it doesn't!!!

In the IOG Property box, I even elected to send as many as 100 entries and exits per bar. But to no avail.

Is this a bug, or am I just not getting it? Please enlighten this noob.



PS. Is it also possible to send the SELL order after a delay of, say, 10 ticks?

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
MC PL editor upgrade
MultiCharts
Cheap historycal L1 data for stocks
Stocks and ETFs
Trade idea based off three indicators.
Traders Hideout
Quantum physics & Trading dynamics
The Elite Circle
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
  #2 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

peachfuzz,

as you noticed it won't work that way i.e. comparing this bar's indicator value to the previous bar to detect multiple intrabar crossings. This is not a but, but expected behavior.
For your goal you need to store the value the indicator has at the previous and the current tick and compare that to the indicator you want to check (In your example the SMA 20). If the current tick's value (of the SMA 10) is lower than the SMA 20 and the value from the previous tick is higher, you have a down cross and vice versa.

Adding a delay to your order entry can be done by setting a counter (a simple variable that you increment within your code) at the moment the entry condition is met. Than increment the counter on each subsequent code cycles, until it's above your threshold (10 in your example). At that moment you send the order.

Regards,
ABCTG

Follow me on Twitter Reply With Quote
  #3 (permalink)
peachfuzz
San Francisco CA USA
 
Posts: 5 since Sep 2015
Thanks Given: 2
Thanks Received: 0



ABCTG View Post
peachfuzz,
For your goal you need to store the value the indicator has at the previous and the current tick and compare that to the indicator you want to check (In your example the SMA 20). If the current tick's value (of the SMA 10) is lower than the SMA 20 and the value from the previous tick is higher, you have a down cross and vice versa.

Thanks for your reply. That makes perfect sense. But honestly, being a real noob, I don't even know where to start. Can you give me a pointer or two on how best to handle the issue? Here's what I have currently. As you can see the crossover is there. How would I store the actual price value at the point of the crossover? I look forward to your reply.

 
Code
 protected override void Create()
	{
            x_FastAverage = new XAverage(this);
	    x_SlowAverage = new XAverage(this);
	    x_FastAvg = new VariableSeries<Double>(this);
            x_SlowAvg = new VariableSeries<Double>(this);
	    x_MA2CrossSE = OrderCreator.MarketNextBar(new 
			SOrderParameters(Contracts.Default, "Short", EOrderAction.SellShort));
            x_MA2CrossLE = OrderCreator.MarketNextBar(new 
			SOrderParameters(Contracts.Default, "Long", EOrderAction.SellShort));
        }

		
        protected override void StartCalc()
	{
            price = Bars.Close;
	    x_FastAverage.Price = price;
	    x_FastAverage.Length = FastLength;
	    x_SlowAverage.Price = price;
	    x_SlowAverage.Length = SlowLength;
        }


        protected override void CalcBar()
	{
            x_FastAvg.Value = x_FastAverage[0];
	    x_SlowAvg.Value = x_SlowAverage[0];
	    if (Displace >= 0 || Bars.CurrentBar > Math.Abs(Displace))
		{
                if (Displace <= 0)
			{
            		if (this.CrossesOver(x_FastAvg, x_SlowAvg))
				{
		        	x_MA2CrossLE.Send();
        			}
                        else if (this.CrossesUnder(x_FastAvg, x_SlowAvg))
				{
		        	x_MA2CrossSE.Send();
        			}
			}
        	 }
          }
    }
}


ABCTG View Post
Adding a delay to your order entry can be done by setting a counter (a simple variable that you increment within your code) at the moment the entry condition is met. Than increment the counter on each subsequent code cycles, until it's above your threshold (10 in your example). At that moment you send the order.

Would something like this work?

 
Code
int numOfTicks = 10;

Reply With Quote
  #4 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

peachfuzz,

as you wrote you just started learning C#, you might want to lay a solid foundation here first and not dive into the actual programming of programming signals with intrabar order generation. It might appear to slow you down at first, but in the end you'll be so much faster with coming from a better understanding of the language.

You wouldn't need to store the price at the cross over in my opinion, but "the value the indicator has at the previous and the current tick and compare that to the indicator you want to check".

I am not sure what you intend with "int numOfTicks = 10;", but it would rather be something like (if numOfTicks is of type private int):
 
Code
if (your Long entry condition is triggered)
numOfTicks++;

if (numOfTicks >= 10)
{
 numOfTicks = 0;
 send the order here...
}
Regards,
ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #5 (permalink)
peachfuzz
San Francisco CA USA
 
Posts: 5 since Sep 2015
Thanks Given: 2
Thanks Received: 0

@ABCTG
Thanks for your input as well as your sage advice. I'm trying, I'm trying, but it ain't easy. Plus trying to tweak the trading strategy to make it more profitable is eating up all my time. I'll get there though.

Reply With Quote




Last Updated on October 1, 2015


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts