Market Condition

My strategy is to trade in different chart as per market conditions. So If value comes between > 26.05 to 26.45 the BUY SIGNAL will be generated in chart 1. But if market changes the value shift between 26.45 - 26.55 and sell singal is generated Chart 2.

I want if BUY SIGNAL has GENERATED THEN THE CHART, IT DOESNT CHANGE UNTIL SELL SIGNAL OCCUR IN CHART 1. After selling the stock, we again buy the stock according to market conditions. How to code for that Please guide me.

case1 = IIf( ( Value > 26.05 AND Value < 26.45 ), 1 , 0 );
case2 = IIf( ( Value >= 26.45 AND Value < 26.55 ), 1 , 0 );
..........
..........

for ( i = 0; i < BarCount; i++ )
{
    if ( case1[i] == 1 )
    {
            Trade triggered in Chart 1
    }
    else
        if ( case2[i] == 1 )
        {
        Trade triggered in Chart 2
        }

I assume Chart 1 and 2 both are open and running their respective AFLs

In that case you can use a Flag, that is a simple scalar static variable and check it instead of using a for loop and looping over every bar. I don't think its required.

2 Likes

travick Thanks for reply. It will be very helpful if you you provide BASIC CODE how to do that or some link which increase understanding on the concept. So that I can built my code on that.

Are you referring to this ?? https://www.amibroker.com/guide/afl/setchartoptions.html

https://www.amibroker.com/guide/afl/staticvarset.html

This is a static variable, you will have to read the manual to understand how they can be used.

1 Like

@travick This post is in continuation with previous one in which you guided me. Trade from Watchlist

I had read all the staticvarset related material present in the kb/forum and got a basic understanding of function.

Basically, we have to fix the chart/symbol when buy signal is generated using staticvarset. Since I am new to coding, can you please provide initial few lines of code so that I can take it further. I am trying it from past few days but not able to get the desired result in backtesting.

can you post your code.

I mentioned this few times, people posting on the forum have their specific problem and for me or others, we are answering multiple people in one day.
It is unreasonable for OP to expect me to remember everything or go through all the threads to pick up the flow.

You have to assist others by being as precise & detailed as possible so that we can help you.
Remember, this is community service.

4 Likes

@travick From my previous post :

I added option chain in the watchlist. I want to trade one of the options on predefined conditions. I am not able to get the result (buy the put/call option) in either exploration/backtesting.

listname = "NIFTY18DEC";
category = categoryWatchlist;
listnum = CategoryFind( listname, category );
list = CategoryGetSymbols( category, listnum);
Call = "NIFTY18DEC" + (round(C /100)+ 5 )*100 + "CE";
Put =  "NIFTY18DEC" + (round(C /100)- 5 )*100 + "PE";

for( n = 0; ( symbol = StrExtract( list, n ) ) != ""; n++ )
{
  	if ( buycall[i-1] == 1 && symbol == Call)
	{
		AddTextColumn(Symbol, "Symbol");
		SetForeign(symbol);
		Buy = timeok;
	    RestorePriceArrays();
	}	
	else if ( Buyput[i-1] == 1 && symbol == Put)
	{
		AddTextColumn(Symbol, "Symbol");
		SetForeign(symbol);
		Buy = timeok;
        RestorePriceArrays();
		
	}
}
Sell = something
SellPrice = Open;
Short = 0;
Cover = 0;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

You Suggested
You can't trade a foreign symbol, like the way you have written. All trades are for current symbol.

If you are trading live and just want to fire a signal for example, then you can call your Brokers API from that IF() and fire a trade right away but these are two very different things.

There are more code examples in the Forum, but you need to write a CBT (Mid-level atleast) and that is fairly advanced.

Now::

Instead of using watchlist, I decided to run code only on few options. Based on Moving average calculation I arrived at Value

Value = tot60 + tot15;

case1 = IIf( ( Value >= 26.05 AND Value < 26.45 ), 1 , 0 );
case2 = IIf( ( Value >= 26.45 AND Value < 26.55 ), 1 , 0 );

for ( i = 0; i < BarCount; i++ )
{
    if ( case1[i] == 1 )
    {
        Call = "BANKNIFTY19103" + "26500CE" ;
        Put =  "BANKNIFTY19103" + "26000PE" ;
    }
    else
        if ( case2[i] == 1 )
        {
            Call = "BANKNIFTY19103" + "27000CE";
            Put =  "BANKNIFTY19103" + "26000PE";
        }
}

if ( Name() == Call )
	{
	     Buy = ..........
         Sell = ..........
	}	
	else if ( Name() == Put )
	{
           Buy = ..........
           Sell = ..........
}

So If value comes between > 26.05 to 26.45 the BUY SIGNAL will be generated in chart 1. But if market changes the value shift between 26.45 - 26.55 and sell singal is generated Chart 2.

So, Please help me in coding to fix the chart/symbol using staticvarset.

in this exploration AFL, the Flag can be set like this

if ( Name() == Call )
{
	StaticVarSet( "s" + Call, 1);
	
	Buy = ..........
	Sell = ..........
}
else if ( Name() == Put )
{
	StaticVarSet( "s" + Put, 1);
	
	Buy = ..........
	Sell = ..........
}

and in the Chart AFL, you can check the Flag like this

if( Nz( StaticVarGet( "s" + Name()) ) )
{
	// You know Buy occurred for 
	// this symbol in Exploration
	
	// Code to plot your Signal
	// You can plot using PlotShapes()
	// or Flash Text using PlotText()
        // or Assign Buy/Sell variables etc
	
	// Reset Flag OR Delete it
	StaticVarSet( "s" + Name(), 0);
	StaticVarRemove( "s" + Name() );
	// Either one of the two
}

You are not limited to passing a Flag value, like 0 or 1 but any Value or Array you want to pass data between the two AFLs
Be careful not to abuse Arrays, they consume memory.

You can even convert a lot of values to comma-separated string and use a staticVar Text to pass more info in a single call and less memory.

3 Likes