Hi All
I am pretty new in coding. is it possible to automate trading with rules below? Also how much ProREaltime will charge for Auto Order?
Buy 80 Contract at market
Set Trailing Stop loss = Distance 25, Step = 20
If Market Price – Entry Price =20 then Close 40
If Market Price – Entry = 30 then Close 20
If Market Price – Entry = 40 then Close 10
If time = 2:00 pm or Entry time + 4 Hrs and nothing triggered
Then
if Market Price > Entry Price then close 40 and Stop = Entry
If Entry Price – Market Price > 15 then Close 40 and limit = Entry +10
If Entry Price -Market Price <15 then Stop = 16 and limit = Entry +5
Notes: As IG will withhold margin for any order all the Close orders should be in limit format not a new order. Also Is it possible to set the order type as Force Open instead of Net postion?
Thanks
A
Fees and spread are not known automatically.
What do you mean by “and limit = Entry +10“ (or 5)?
What do you mean by “As IG will withhold margin for any order all the Close orders should be in limit format not a new order”?
Hi Roberto
PLease see below:
What do you mean by “and limit = Entry +10“ (or 5)?
I know limit is confusing here in other platform limit is the price that you want to enter the market but in IG limit is somehow equal to target/take profit.
In IG platform when you create an order there are three fields, Price level = price you want to open postion, Stop= Stop, Limit = take profit/Target .
What do you mean by “As IG will withhold margin for any order all the Close orders should be in limit format not a new order”?
This is what I am doing manually now: I buy at market 80 contract, and at same time I create 3 sell orders of 40 contract at price level of Entry +20 , 20 contract at Entry +30 and 10 Contract at Entry +40. So when I do this in the platform IG will withhold Margin for all of my orders that means with this strategy I would need margin for at least 160 contract.So if I dont want to use my margin on the sell orders, I have to monitor the charts 24/7 and wait until it meets my criteria and go to the position and partially close the contracts that I want. Other workaround is instead of buying 80 contracts I open 4 different positons at the same time with different limits( targets)
What I am looking for in PRT is to be able to automate this . In the rules when I say “If Market Price – Entry = 40 then Close 10” I dont want the PRT create a new order that use my margin but just partially close 10 contracts (like I am doing it manually) when it reaches to +40.
Hope it is clear now.
Thanks
There’s no need for a FORCE OPEN instruction, as opening opposite positions is not allowed in a single strategy (multipole different strategies can, provided you have set Force Open with IG).
There you go (I tested it on DAX, 1h TF):
DEFPARAM CumulateOrders = FALSE
//initialisation
If Not OnMarket THEN
EntryPrice = 0
Ohour = 0 //Hour when a trade opens
Omin = 0 //Minute when a trade opens
Flag = 0
LotSize = 80
ENDIF
//update EntryPrice after entering at market
IF OnMarket AND Not OnMarket[1] THEN
EntryPrice = TradePrice
ENDIF
IF OnMarket AND IntraDayBarIndex = 0 THEN
Flag = 1
ENDIF
//set entry conditions
Sma100 = average[100,0](close)
Cond = (close CROSSES OVER Sma100) AND Not LongOnMarket
//enter at Market
IF Cond THEN
BUY LotSize Contracts AT Market
EntryPrice = close
Ohour = OpenHour
Omin = OpenMinute
Hlimit = Ohour + 4 //limit of 4 hours after entry
Hlimit = Hlimit - (24 * (Hlimit > 23))
TimeLimit = ((Hlimit * 100) + Omin) * 100 //let's make it 6 digits
ENDIF
//
IF OnMarket THEN
Pips = PositionPerf * PositionPrice / PipSize
Npos = abs(CountOfPosition)
IF ((OpenTime >= Hlimit) OR ((OpenTime <= Hlimit) AND (Flag = 1))) OR (OpenTime >= 140000) THEN
IF (Pips > 15) AND (Npos = LotSize) THEN
SELL 40 Contracts AT Market
NewSL = EntryPrice + 10 * PipSize
ELSIF (Pips > 0) AND (Npos = LotSize) THEN
SELL 40 Contracts AT Market
NewSL = EntryPrice
ELSIF (PipS > 0) AND (Pips < 15) THEN
SELL 16 Contracts AT Market
NewSL = EntryPrice + 5 * PipSize
ENDIF
ELSE
IF (Pips >= 20) AND (Npos = LotSize) THEN
SELL 40 Contracts AT Market
ENDIF
IF Pips >= 30 THEN
SELL 20 Contracts AT Market
ENDIF
IF Pips >= 40 THEN
SELL 10 Contracts AT Market
ENDIF
ENDIF
ENDIF
//
// https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
// (righe 17- 56)
//
//trailing stop function
trailingstart = 20 //25 trailing will start @trailinstart points profit
trailingstep = 5 //20 trailing step to move the "stoploss"
distance = 10 //10 pips distance from caurrent price (if required by the broker)
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
IF LongOnMarket THEN
IF (close + distance) > newSL THEN
SELL AT newSL STOP
ELSIF (close - distance) < newSL THEN
SELL AT newSL LIMIT
ELSE
SELL AT Market
ENDIF
ELSIF ShortOnmarket THEN
IF (close + distance) < newSL THEN
EXITSHORT AT newSL STOP
ELSIF (close - distance) > newSL THEN
EXITSHORT AT newSL LIMIT
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
ENDIF
//*********************************************************************************
//graph Pips
//graph Hlimit
//graph Ohour
//graph Omin
//graph TimeLimit
//graph Npos
//graphonprice NewSL
Thanks for reply , I can see that there is an entry condition. I would like to manually enter the market with no condition when I press start the autotrade. So basically I am more interested in closing the positions with Autotrade not openning the positons.
Remove line 18 and replace line 19 with this one:
Cond = Not OnMarket
It will then enter again as soon as the trade is closed.