Need some help with this code

Hello World! please help

I am very much unaware of coding just trying to learn.
I have a high and low, I want to place buy order 2,3,5 points above the High and 2,3,5 points below the low for sell, want to change the desired numbers from parameters as when required.

Now if the BUY is triggered want to place a stop loss order 2,3,5 points as desired below the buy price. And if SELL is triggered want to place a stop loss 2,3,5 points above the sell order.

Also want to do the the same with Targets if BUY or SELL is triggered.

Please help me code

("COMMODITY TRADING");
SetChartOptions(0,chartShowArrows|chartShowDates);
dec = (Param("Decimals",2,0,7,1)/10)+1;
SetChartBkColor(ParamColor("Outer panel",colorBlack));
SetChartBkGradientFill(
ParamColor("Inner panel upper",colorBlack),
ParamColor("Inner panel lower",colorBlack));
//ToolTip=StrFormat("Open: %g\nHigh: %g\nLow: %g\nClose: %g (%.1f%%)\nVolume: "+NumToStr( V, 1 ), O, H, L, C) + "\n" + "\n" + ("***** HAPPY TRADING ***** " );

breakoutime = 110000;
afterbreakout0 = Cross(TimeNum(),110000);
afterbreakout1 = TimeNum()>=110000;
NewDay = Day()!= Ref(Day(), -1);
highestoftheday = HighestSince(newday,H,1);
Lowestoftheday =LowestSince(newday,L,1);
ORBHigh = ValueWhen(afterbreakout0,highestoftheday,1);
ORBLow = ValueWhen(afterbreakout0,lowestoftheday,1);
Buy= Cross(C,orbhigh) AND afterbreakout1;
Sell = Cross(orblow,C) AND afterbreakout1;
Short = Sell;
Cover = Buy;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Tar=Param("Target",1,1,100);
BuyFirst = ORBHigh + Tar ;
Plot(BuyFirst,"",colorGreen,styleLine);
SellFirst = ORBLow - Tar;
Plot(SellFirst,"",colorRed,styleLine ) ;

Plot(C,"",colorWhite,styleCandle);
PlotShapes( shapeUpArrow * Buy, colorGreen,0,L,-12);
PlotShapes( shapeDownArrow * Sell, colorRed,0,H,-12);
Plot(NewDay,"",colorWhite,styleHistogram|styleOwnScale);
Plot(afterbreakout0,"",colorBlue,styleHistogram|styleOwnScale);
Plot(ORBHigh,"",colorGreen,styleDots);
Plot(ORBLow,"",colorRed,styleDots);

Title = EncodeColor(55)+ Title = Name() + " " + EncodeColor(55)
+ Date() + " " + EncodeColor(55) + "{{INTERVAL}} " +
EncodeColor(55)+ " Open = "+ EncodeColor(55)+ NumToStr
(O,dec) +
EncodeColor(55)+ " High = "+ EncodeColor(55) + NumToStr
(H,dec) +
EncodeColor(55)+ " Low = "+ EncodeColor(55)+ NumToStr
(L,dec) +
EncodeColor(55)+ " Close = "+ EncodeColor(55)+ NumToStr
(C,dec) +
//    EncodeColor(55)+ " BI = "+ EncodeColor(42)+ NumToStr
//(bi,1.0) +
EncodeColor(55)+ " Volume = "+ EncodeColor(55)+ NumToStr
(V,1.0) +"\n"+"\n"+
EncodeColor(55)+ " BUY ABOVE : "+ EncodeColor(55)+ NumToStr
(ORBHigh,dec) +"\n"+
EncodeColor(55)+ " TAR : "+ EncodeColor(55)+ NumToStr
(BuyFirst,dec) +"\n"+

EncodeColor(55)+ " SELL BELOW : " + EncodeColor(55)+ NumToStr
(ORBLow,dec) +"\n"+
EncodeColor(55)+ " TAR : " + EncodeColor(55)+ NumToStr
(SellFirst,dec) +"\n" ;
GraphXSpace=12;
_

When you say "place buy order" do you mean send order to broker ? Or do you want to backtest it ?

1 Like

As of now back test
:slight_smile: really happy you replied

@awilson would also like the trades to end at 3:30 pm

Just add x points to ORBHigh and ORBLow

ORBHigh = ValueWhen(afterbreakout0,highestoftheday,1) + points;
ORBLow = ValueWhen(afterbreakout0,lowestoftheday,1)  - points;

points can be a Param or a constant

1 Like

@awilson Yesterday I watched 3 hrs video on variables, list, loop, for & while hope you understand what I mean to say ... LOL
Any ways I tried inserting your code with numbers ... dint give me the required results when back testing, looks same.

Any ways Lets say I have a line plotted in the chart

BuyHere=Param("BuyAT",1,1,100);
BuyFirst = ORBHigh + BuyHere ;
Plot(BuyFirst,"",colorBlue,styleLine);

Please help to write a code to entry at the "value" and not the O H L C
I want to entry at the plotted line value

Hey Twin
find the edited code,

("COMMODITY TRADING");
SetChartOptions(0,chartShowArrows|chartShowDates);
dec = (Param("Decimals",2,0,7,1)/10)+1;
SetChartBkColor(ParamColor("Outer panel",colorBlack));
SetChartBkGradientFill(
ParamColor("Inner panel upper",colorBlack),
ParamColor("Inner panel lower",colorBlack));
//ToolTip=StrFormat("Open: %g\nHigh: %g\nLow: %g\nClose: %g (%.1f%%)\nVolume: "+NumToStr( V, 1 ), O, H, L, C) + "\n" + "\n" + ("***** HAPPY TRADING ***** " );

breakoutime = 110000;
afterbreakout0 = Cross(TimeNum(),110000);
afterbreakout1 = TimeNum()>=110000;
NewDay = Day()!= Ref(Day(), -1);
highestoftheday = HighestSince(newday,H,1);
Lowestoftheday =LowestSince(newday,L,1);
ORBHigh = ValueWhen(afterbreakout0,highestoftheday,1);
ORBLow = ValueWhen(afterbreakout0,lowestoftheday,1);

Tar=Param("Target",1,1,100);

BuyFirst = ORBHigh + Tar ;
Plot(BuyFirst,"",colorGreen,styleLine);
SellFirst = ORBLow - Tar;
Plot(SellFirst,"",colorRed,styleLine ) ;

Buy= Cross(C,buyfirst) AND afterbreakout1;
Sell = (Cross(sellfirst,C) AND afterbreakout1) OR TimeNum()>152000;
Short = Sell;
Cover = Buy;
BuyPrice = buyfirst;
SellPrice = sellfirst;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
position = Flip(Buy,Sell);

Plot(C,"",colorWhite,styleCandle);
PlotShapes( shapeUpArrow * Buy, colorGreen,0,L,-12);
PlotShapes( shapeDownArrow * Sell, colorRed,0,H,-12);
Plot(NewDay,"",colorWhite,styleHistogram|styleOwnScale);
Plot(afterbreakout0,"",colorBlue,styleHistogram|styleOwnScale);
Plot(ORBHigh,"",colorGreen,styleDots);
Plot(ORBLow,"",colorRed,styleDots);

Title = EncodeColor(55)+ Title = Name() + " " + EncodeColor(55)

  • Date() + " " + EncodeColor(55) + "{{INTERVAL}} " +
    EncodeColor(55)+ " Open = "+ EncodeColor(55)+ NumToStr
    (position,dec) +
    EncodeColor(55)+ " High = "+ EncodeColor(55) + NumToStr
    (H,dec) +
    EncodeColor(55)+ " Low = "+ EncodeColor(55)+ NumToStr
    (L,dec) +
    EncodeColor(55)+ " Close = "+ EncodeColor(55)+ NumToStr
    (C,dec) +
    // EncodeColor(55)+ " BI = "+ EncodeColor(42)+ NumToStr
    //(bi,1.0) +
    EncodeColor(55)+ " Volume = "+ EncodeColor(55)+ NumToStr
    (V,1.0) +"\n"+"\n"+
    EncodeColor(55)+ " BUY ABOVE : "+ EncodeColor(55)+ NumToStr
    (ORBHigh,dec) +"\n"+
    EncodeColor(55)+ " TAR : "+ EncodeColor(55)+ NumToStr
    (BuyFirst,dec) +"\n"+

EncodeColor(55)+ " SELL BELOW : " + EncodeColor(55)+ NumToStr
(ORBLow,dec) +"\n"+
EncodeColor(55)+ " TAR : " + EncodeColor(55)+ NumToStr
(SellFirst,dec) +"\n" ;
GraphXSpace=12;

1 Like

Please paste the code properly ---use tags!!!

1 Like

Hi thanks for the code, however not what i want when back testing.
What I want

  1. Plot the high low at 11:30
  2. If the high low is triggered that is if LTP crosses the high or low .... want to place if Buy 2,3 points above the High and if sell 2,3 points below the Low (NOTE not at the OHLC price of the candle but at the points defined.
  3. If BUY or SELL is initiated want to place SL and Target order 2,3 points ...that is 1:1 trade
  4. if the target or SL is hit wait for another signal
  5. If any order is generated and if SL and target is not hit by 3:30 exit the price at the LTP or the close of the candle.

Want to back test

You want all these points to be dispayed on the charts???

Are you looking for a manual trading solution, so that you can see on charts and place orders

Warm Regards,

Prashant Golyan

1 Like

I can display all the points on the chart ,
I want to back test the strategy, I want to Backtest with the given criteria on amibroker analysis, not show on the chat. Thankyou

the edited code which i have send you , will do the backtesting job, and price also you will get at Tar plus high or low as the case may be

1 Like

I did the back testing it does not exit if the SL price is triggered , even does not exit when the target is hit, and also have an open position even after 3:30

could you please paste the code here for all?

1 Like

ok, so you are looking for complete trading system, i thought you are looking for buy and sell price handling, in that case whole code has to be written , which is going to take time. i can assist you in handling any case scenarios, but for complete trading system code it takes time

1 Like

closing time exit is coded already but your amibroker should be set to look at start time of candle ese you can replace this line

Sell = (Cross(sellfirst,C) AND afterbreakout1) OR TimeNum()>152000;

with

Sell = (Cross(sellfirst,C) AND afterbreakout1) OR TimeNum()>=151959;
1 Like

I think i will give up on this for some time ... LOL