Trading inside channel

Hi all,

I'm trying to create an algorithm:

  • I'd like to open short if H cross upperline (point 1)
    pozition_size is 1 contract
    target price is when L cross lowerline
    stoploss is some point higher than upperline

  • I'd like to open long (2) if short from point 1 finished with success
    (but if short form point 1 reach stoploss this long will not occur)
    pozition_size is 2 contract
    target price is when H cross lowerline
    stoploss is some point lower than lowerline

  • I'd like to open short (3) if long from point 2 finished with success
    (but if long form point 2 reach stoploss this short will not occur)
    pozition_size is 4 contract
    target price is when L corss lowerline
    stoploss is some point higher than upperline

-....

I don't expect a code, but maybe someone has some idea what I should use
for this algorithm (...some lopping, some code with barssince, ...).
or maybe someone knows a similar algorithm.
Thanks in advance for any help.

Chart

If you provide the logic of how the upperline and lowerline can be plotted (or computed) in advance, then anyone will be able to write the whole code for you for free.

A chart in hindsight is easier to explain than in foresight is what I meant.
If you look at the two pivots formed before point 1, it has failed to hold resistance and broke out in a smaller range which then turned into a larger range.

thaks for replay.

...so for example
upperline and lowerline are flat
Upperline = 111.46
Lowerline = 111.34

Can anyone help?

Give the lines a study ID (Properties of the line) and use this:
upperline = Study("RE", GetChartID() );
lowerline = Study("SU", GetChartID() );
Title = Title + "\n" + upperline[barcount-1];
if (H[barcount-1] > upperline[barcount-1]) ......Sell
AND so on

1 Like

thanks for replay.

I very week when looping,I put only Short_1 (point 1);

Could somebody help with others ?:
Buy_2 =0; (point2)
Short_3 =0; (point3)
Buy_4 = 0; (point4)
Short_5 =0; (point5)

_SECTION_BEGIN("Price");
SetBarsRequired( 100000, 0 );
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();


Upperline = 111.46;
Plot(Upperline,"Upperline",colorRed,styleLine);
Lowerline = 111.34;
Plot(Lowerline,"Lowerline",colorRed,styleLine);



L_point = L<=Lowerline AND Ref(L,-1) > ref(Lowerline,-1)
 AND BarsSince(H>Upperline)<   Ref(BarsSince(L<=Lowerline),-1); 
PlotShapes((L_point) *shapeCircle,colorGreen,0,L,-50,0);

H_point = H>=Upperline AND Ref(H,-1) < ref(Upperline,-1)
AND BarsSince(L<Lowerline)<   Ref(BarsSince(H>=Upperline),-1); 
PlotShapes((H_point) *shapeCircle,colorviolet,0,h,50,0);
	

trend =0;

Short_1 =0;   
Buy_2 =0; 
Short_3 =0;
Buy_4 = 0;
Short_5 =0;

for(i=0; i<BarCount; i++)
{
	if(H_point[i] AND trend[i-1]==0)
	 {
	  Short_1[i] = 1;	  	  
	  }  
}

printf("Short_1 "  + Short_1);


I was still waiting for a proper definition or description of how he arrived at those lines.
Atleast could've referred to them as trendlines or drawn them on a layer.

Earlier implication was that they were drawn programmatically.

travick, please see my exponation:

in my code:

  • horizontal line (red) are some modifacion of Bollinger Band, but it is not necessery to put it here. In my code above I used Upperline = 111.46; and Lowerline = 111.34;
  • blue (trend) line - it's only drawing, not code - shows only how it should work
    Chart
    please see below drawing how it should work;

Theoretically, if you just wrote this bit of RAW code without further logic, it would emulate what you are thinking.

Probably you aren't able to figure out how to generate signals themselves.
You can start like this:

Upperline = 111.46;
Lowerline = 111.34;
Plot(Upperline,"Upperline",colorRed,styleLine);
Plot(Lowerline,"Lowerline",colorRed,styleLine);

Short = Cross(Upperline,C);

Cover = Cross(Lowerline,C);

Buy = Cross(C,Lowerline);

Sell =  Cross(C, Upperline);

Short=ExRem(Short,Cover); Cover=ExRem(Cover,Short);
Buy= ExRem(Buy,Sell); Sell=ExRem(Sell,Buy); 

///The ABOVE marks a pair of complete Short and Buy trade, you can put logic to reset static variable and restart the cycle

//Throw in some Arrows for visual appeal
shape1 = Buy * shapeUpArrow + Sell * shapeDownArrow;	shape2 = Short * shapeDownArrow + Cover * shapeUpArrow;
PlotShapes( shape1, IIf( Buy, colorBrightGreen, colorLightOrange ), 0, IIf( Buy, Low, High ), -15 );
PlotShapes( shape2, IIf( Cover, colorBrightGreen, colorLightOrange ), 0, IIf( Cover, Low, High ), -15 );

Thank you for your time and a code. Unfortunately, this is not what I'm looking for.
Maybe there is a problem with my exploration.

The idea is:

  • there will be 5 entry points (points on the chart - 1,2,3,4,5)
  • in point 1,3,5 strategy will open short, and in point 2,4 will open long.
  • point 1 (open short) will happen when H cross Upperline. There is a point where strategy is starting. StopLoss is some point higher than upperline (it's not important now) and TargetProfit is where point 2 is (L cross Loweline).
  • point 2 (open long) will happen only when L cross Lowerline and Short from point 1 is closed (target price). If short from point 1 was closed earlier with StopLose point 2 will not occur.
    StopLoss is some point lower than lowerline (it's not important now) and TargetProfit is where point 3 is.
  • points 3,4,5 are similar (adequate) to point 2
  • it is important for me to have point 1,2,3,4,5,... because for each point (entry) I assume different Stoploss and different position size.

Then you can use Static Variables, maybe even where Persist=True to store all state information and use it like this:
This is just pseudo code of what you may need:

// https://forum.amibroker.com/t/trading-inside-channel/8034/7
// Buy/Sell conditions to Trade boundaries

/// Enclosed in Some fool proof condition that starts a trade cycle
StaticVarSetText( "short1", "start", persist = True ) ;
// Dont blindly copy-paste code

Upperline = 111.46;
Lowerline = 111.34;
Plot(Upperline,"Upperline",colorRed,styleLine);
Plot(Lowerline,"Lowerline",colorRed,styleLine);

Short = Cross(Upperline,C) AND StaticVarGetText("short1") == "start";
IIf(Short, StaticVarSetText( "short1", "1", persist = True ), Null );

Cover = Cross(Lowerline,C) AND StaticVarGetText("short1") == "1";
IIf(Cover, StaticVarSetText( "short1", "2", persist = True ), Null );

Buy = Cross(C,Lowerline) AND StaticVarGetText("short1") == "2";
IIf(Buy, StaticVarSetText( "short1", "21", persist = True ), Null );

Sell =  Cross(C, Upperline) AND StaticVarGetText("short1") == "21";
// AND so on for all the 5 trades

You may need more than one static variables, I leave that to you. You can even store your SL info as well.

Even Array static variables are supported but read carefully before using them.(Abuse can occur)

many thanks.

I'll chek it and give you know.
but I'm thinkning that I rather need to use for looping .

You have to think in AFL terms, not the other way around :smiley:

Many codes have used persistent variables with Symbol name, that way you can store state info for each Symbol separately and trade as many as you want.

AFL is not like a single flow start/stop program.
AFL is continuously running, so Logic needs to be incorporated in that way because every event that makes chart update will execute the entire relevant code.