Basket-Stablize Help Me Please

 

Ok guys, I need another set of eyes to help me with this one.

//~~~~~~~~~~Basket-Stablize:
if(Order_Manage(iMagic,"Abs_Last_Order_xSymbol_wMagic")==true){
    if(StringSubstr(iSym_,0,3)=="USD" && iType== 1){
        if(Base_=="USD" && Signal== 1){Signal=0;}
        if(Base_!="USD" && Signal==-1){Signal=0;}
    }
    if(StringSubstr(iSym_,0,3)=="USD" && iType==-1){
        if(Base_=="USD" && Signal==-1){Signal=0;}
        if(Base_!="USD" && Signal== 1){Signal=0;}
    }
    //~~~~~~~~~~
    if(StringSubstr(iSym_,3,3)=="USD" && iType==-1){
        if(Quot_=="USD" && Signal==-1){Signal=0;}
        if(Quot_!="USD" && Signal== 1){Signal=0;}
    }
    if(StringSubstr(iSym_,3,3)=="USD" && iType== 1){
        if(Quot_=="USD" && Signal== 1){Signal=0;}
        if(Quot_!="USD" && Signal==-1){Signal=0;}
    }
}

I have an algorithm right above this code which generates the signals. The only pairs I'm working with are pairs which have USD. If the Last order Bought USD then I don't want the Next order to purchase USD. If the Last order Sold USD, then I don't want the next order to sell more USD. The Base_ represents the first 3 characters of the Symbol it's currently working on. And Quot_ the last 3 characters.

The OrderManage code select the last order which matches the Magic# and disregards the OrderSymbol.

I really wanna get this logic in before I take a Nap here. Thanks.

 
And your question is?
 
Whats wrong with my code? Its not working. I ran it on demo for the last hour and it fails. Example. it Buys:EURUSD and then Buys:GBPUSD. It's almost as if it does not see the above code. If you have a better way of doing this I'm all ears :)
 
I looked at your code . . . I can't follow it, I don't know what iType is or Signal . . . I understand what you are trying to do but to be able to see a problem in your code I think I need a little more info.
 
ubzen:
Whats wrong with my code? Its not working.
Add Print statements before every if, after every if, and dump your variable to find out WHY!
 

I was afraid it need more information :). My Magic# is Negative for Sells and Positive for Buys. I got into Symmetric Logics at one point to help reduce my code duplication for buys and sells and now I'm stuck with this bad habit within most of my codes which I'll need to correct at a later time. I hope the below helps.

Local Variable: int Signal (Decleared Above the Basket-Stablize)

Global Variables:
int iType;          //Value of Currently Selected Direction
Pretty much all the iXXXXX variable are Global Variables.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~Order_Manage-Function
double Order_Manage(int Magic, string Command){
//~~~~~~~~~~
int Switch, Cnt=0;
Ans=0; iProfit=0; iOrSize=0;
//~~~~~~~~~~
for(i=1;i>0;i--){
    if(Command=="Abs_Last_Order_xSymbol_wMagic"){Switch=13; break;}
}
//~~~~~~~~~~
for(i=OrdersTotal()-1;i>=0;i--){
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
        //~~~~~~~~~~
        switch(Switch){
            case 13: if(Switch==13){//Abs_Last_Order_xSymbol_wMagic
                        if(OrderMagicNumber()==MathAbs(Magic)){
                            iTicket=OrderTicket();
                            iType=Sign(OrderMagicNumber());
                            iOpTime=OrderOpenTime();
                            iOpPrice=OrderOpenPrice();
                            iProfit=OrderProfit();
                            iOrSize=OrderLots();
                            iOrSym_=OrderSymbol();
                            Ans=true; return(Ans);
                        }
                    }
        //~~~~~~~~~~~~~~~~~~
        }
    }
}
//~~~~~~~~~~
return(Ans);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



if(Signal== 1){OrderSend(Long_Position); OrderMagic=Signal*iMagic;}
if(Signal==-1){OrderSend(Short_Position); OrderMagic=Signal*iMagic;}


 
WHRoeder:
Add Print statements before every if, after every if, and dump your variable to find out WHY!
Yeah thats pretty much what I'll have to do. As you can see i cannot test this within the back-tester otherwise i'd have the answer by now. Of course I didn't want all the dumps within my actual EA and that's why the codes looks the way it does. But now I don't have a choce :(. If you guys cannot see anything obviously wrong then the problem must be somewhere else. Thanks anyways.
 

Ah Ha ... I got something

iOrSym_ and iSym_, those are different lol.

 
ubzen:

Ah Ha ... I got something

iOrSym_ and iSym_, those are different lol.

Ah I see, half the code in the OP was for the Symbol to be traded and half was for the Symbol last traded ?
 

Yep. the Base_ in the OP is the Base Currency upon Chart_Symbol()___So to speak. I used the same StringSubstr(iSym_,0,3) to get that base value. When I do OrderSelect, because it's Multi-Currency, the Order_Symbol could be any one of the 6-Pairs it trades. So I have to ask it what's the Base Currency again.

StringSubstr(Symbol(),0,3) on EURUSD for Example: Returns EUR. EUR is the Base Currency. String Base_= StringSubstr(Symbol(),0,3);

StringSubstr(Symbol(),3,3) on EURUSD for Example: Returns USD. USD is the Quote Currency. String Quot_= StringSubstr(Symbol(),3,3);

If the current Symbol the system is working on is USDJPY. I have to reverse the logic.

Example: if it Bought EURUSD last: Bought EUR/Sold USD. I cannot allow it to Sell USD again. So if the system gives a Signal of -1 on USDJPY I want to ignore that signal. Because that'll cause me to Sell USD/By buying JPY.

Reason I'm doing this is because usually. EU, GU, AU walks the same path usually. Therefore the system would tend to generate 3 orders in the same direction on those pairs. Thats pretty much like me going from 1-Lot to 3-Lots on Euro, or USD for that matter. I'm hoping this approach will offer some diversification.

One of the reasons I posted this code is because I believed there's gotta be a better way of doing this. If you can come up with something, let me know ;)

 
ubzen:

Yep. the Base_ in the OP is the Base Currency upon Chart_Symbol()___So to speak. I used the same StringSubstr(iSym_,0,3) to get that base value. When I do OrderSelect, because it's Multi-Currency, the Order_Symbol could be any one of the 6-Pairs it trades. So I have to ask it what's the Base Currency again.

StringSubstr(Symbol(),0,3) on EURUSD for Example: Returns EUR. EUR is the Base Currency. String Base_= StringSubstr(Symbol(),0,3);

StringSubstr(Symbol(),3,3) on EURUSD for Example: Returns USD. USD is the Quote Currency. String Quot_= StringSubstr(Symbol(),3,3);

If the current Symbol the system is working on is USDJPY. I have to reverse the logic.

Example: if it Bought EURUSD last: Bought EUR/Sold USD. I cannot allow it to Sell USD again. So if the system gives a Signal of -1 on USDJPY I want to ignore that signal. Because that'll cause me to Sell USD/By buying JPY.

Reason I'm doing this is because usually. EU, GU, AU walks the same path usually. Therefore the system would tend to generate 3 orders in the same direction on those pairs. Thats pretty much like me going from 1-Lot to 3-Lots on Euro, or USD for that matter. I'm hoping this approach will offer some diversification.

One of the reasons I posted this code is because I believed there's gotta be a better way of doing this. If you can come up with something, let me know ;)

BASKET-STABILIZE: I understand stabilize, but what really is your definition of basket, and what you want out of this "basket"? Are you referring to the Currency (& not pairs) index? pos+, neg-, between any two currencies (NOT pairs). Then, the best i can share is, trading with the above approach, chances are, cant be tested on Strategy tester. Have you thought of creating your "basket" into an indicator form? Like the picture below. because this approach works quite well for me, instead of having every logic hard-coded wherever in the EA

Reason: