Hello! I need help building an indicator.
I have an algo that can take three positions at different levels. The indicator should show how many positions it has taken.
I am sending a picture of what the indicator should look like.
Also sends a simple code we can start from.
The idea is to be able to use this indicator for another algo that will hedge this position. I can make the indicator so that it shows the positions but not that it goes down to zero when algo closes the position
// Definition of code parameters
DEFPARAM CumulateOrders = True // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = RSI[14](close)
c1 = (indicator1 < 12)
IF not longonmarket and c1 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
IF longonmarket AND COUNTOFPOSITION=1 and close - tradeprice < -100*pointsize then
Buy 1 CONTRACT AT MARKET
endif
IF longonmarket AND COUNTOFPOSITION=2 and close - tradeprice < 150*pointsize then Buy 1 CONTRACT AT MARKET endif //Take profit floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains If LONGONMARKET and floatingprofit>20 then
sell at market
endif
Sorry. That was not the intention. I messed up.
Now I see the insert code button 🙂
// Definition of code parameters
DEFPARAM CumulateOrders = True // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = RSI[14](close)
c1 = (indicator1 < 30)
IF not longonmarket and c1 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
IF longonmarket AND COUNTOFPOSITION=1 and close - tradeprice < -100*pointsize then
Buy 1 CONTRACT AT MARKET
endif
IF longonmarket AND COUNTOFPOSITION=2 and close - tradeprice < 150*pointsize then
Buy 1 CONTRACT AT MARKET
endif
set stop loss 130
//Take profit
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
If LONGONMARKET and floatingprofit>20 then
sell at market
endif
Crillezz – Do not try to bump your own topic by quoting yourself. Can you imagine what a mess the forums would be if everyone did this?
If you have not received a reply it is most likely because either other forum members don’t know the answer or other forum members don’t understand what you want.
The forums are quieter at the weekends when markets are closed and people are away from their desks so some patience helps.
I have deleted your post.
What a mess. I have tried to delete it but I can´t.
The more I try to arrange, the more wrong it becomes.
The first code was incorrect. So I tried to change it. I don´t find the edit button.
I’ve never had that problem before
You have five minutes to edit your posts – after that time has passed the edit button is no longer available and only moderators can edit or delete your posts for you. If it is the first post in a topic then only Nicolas can delete it and that action deletes the entire topic.
Can someone help me with that indicator please.
I assume that your line 17 should read -150.
There you go:
IF BarIndex = 0 THEN
MyPositionCount = 0
MyPositionPrice = 0
ENDIF
indicator1 = RSI[14](close)
c1 = (indicator1 < 12)
IF MyPositionCount = 0 AND c1 THEN
MyPositionCount = 1
MyPositionPrice = close
ENDIF
IF MyPositionCount = 1 and ((close - MyPositionPrice) < -100*pointsize) then
MyPositionCount = MyPositionCount + 1
MyPositionPrice = (MyPositionPrice + close) / 2
endif
IF MyPositionCount = 2 and ((close - MyPositionPrice) < -150*pointsize) then
MyPositionCount = MyPositionCount + 1
MyPositionPrice = ((MyPositionPrice * (MyPositionCount - 1)) + close) / MyPositionCount
endif
IF (MyPositionPrice - close) >= 130 THEN //Stop Loss
MyPositionCount = 0
MyPositionPrice = 0
ENDIF
IF MyPositionCount THEN
floatingprofit = (((close-MyPositionPrice)*pointvalue)*MyPositionCount)/pipsize
If MyPositionCount > 0 and floatingprofit > 20 then
MyPositionCount = 0
MyPositionPrice = 0
endif
ENDIF
RETURN MyPositionCount AS "MyPositionCount"
Thanks Roberto. Just what I was looking for. But I have a problem. When I have to do the same thing but with a short position, it increases the position when it is profitable. It should only increase the position in case of loss. And it never reaches floating profit. I do not manage to change the code in the opposite direction
IF BarIndex = 0 THEN
MyPositionCount = 0
MyPositionPrice = 0
ENDIF
indicator1 = RSI[14](close)
c1 = (indicator1 < 12)
IF MyPositionCount = 0 AND c1 THEN
MyPositionCount = 1
MyPositionPrice = close
ENDIF
IF MyPositionCount = 1 and ((close - MyPositionPrice) < -60*pointsize) then
MyPositionCount = MyPositionCount + 1
MyPositionPrice = (MyPositionPrice + close) / 2
endif
IF MyPositionCount = 2 and ((close - MyPositionPrice) < -90*pointsize) then
MyPositionCount = MyPositionCount + 1
MyPositionPrice = ((MyPositionPrice * (MyPositionCount - 1)) + close) / MyPositionCount
endif
//////////Short
indicator1 = RSI[14](close)
c2 = (indicator1 > 88)
IF MyPositionCount = 0 AND c2 THEN
MyPositionCount = -1
MyPositionPrice1 = close
ENDIF
IF MyPositionCount = -1 and ((close + MyPositionPrice1) < -60*pointsize) then
MyPositionCount = MyPositionCount - 1
MyPositionPrice1 = (MyPositionPrice1 + close) / 2
endif
IF MyPositionCount = -2 and ((close + MyPositionPrice1) < -90*pointsize) then
MyPositionCount = MyPositionCount - 1
MyPositionPrice1 = ((MyPositionPrice1 * (MyPositionCount - 1)) + close) / MyPositionCount
endif
IF (MyPositionPrice1 - close) >= 130 THEN //Stop Loss
MyPositionCount = 0
MyPositionPrice1 = 0
ENDIF
IF MyPositionCount THEN
floatingprofit = (((close-MyPositionPrice)*pointvalue)*MyPositionCount)/pipsize
If MyPositionCount > 0 and floatingprofit > 20 then
MyPositionCount = 0
MyPositionPrice = 0
endif
If MyPositionCount < 0 and floatingprofit > 20 then
MyPositionCount = 0
MyPositionPrice = 0
endif
ENDIF
RETURN MyPositionCount AS "MyPositionCount"
Lines 40 and 45 should read:
MyPositionPrice1 - close
Just after line 58 (before line 59), insert:
floatingprofit1 = (((MyPositionPrice1-close)*pointvalue)*abs(MyPositionCount))/pipsize
and at line 63 you should reference floatingprofit1.